-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: clearCheckSums generated invalid SQL (#354)
Spanner requires that all UPDATE and DELETE statements contain a WHERE clause as safety precaution against accidental updates and deletes. Liquibase however by default generates UPDATE and DELETE statements without a WHERE clause if none has been specified. The Spanner Liquibase provider therefore now adds custom generators for those statements that automatically add a `WHERE true` clause to statements that do not have a WHERE clause.
- Loading branch information
Showing
4 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/liquibase/ext/spanner/sqlgenerator/DeleteGeneratorSpanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package liquibase.ext.spanner.sqlgenerator; | ||
|
||
import liquibase.database.Database; | ||
import liquibase.ext.spanner.ICloudSpanner; | ||
import liquibase.sql.Sql; | ||
import liquibase.sqlgenerator.SqlGeneratorChain; | ||
import liquibase.sqlgenerator.core.DeleteGenerator; | ||
import liquibase.statement.core.DeleteStatement; | ||
|
||
public class DeleteGeneratorSpanner extends DeleteGenerator { | ||
|
||
@Override | ||
public int getPriority() { | ||
return PRIORITY_DATABASE; | ||
} | ||
|
||
@Override | ||
public boolean supports(DeleteStatement statement, Database database) { | ||
return database instanceof ICloudSpanner; | ||
} | ||
|
||
@Override | ||
public Sql[] generateSql( | ||
DeleteStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { | ||
if (statement.getWhere() == null) { | ||
statement.setWhere("true"); | ||
} | ||
return super.generateSql(statement, database, sqlGeneratorChain); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/liquibase/ext/spanner/sqlgenerator/UpdateGeneratorSpanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package liquibase.ext.spanner.sqlgenerator; | ||
|
||
import liquibase.database.Database; | ||
import liquibase.ext.spanner.ICloudSpanner; | ||
import liquibase.sql.Sql; | ||
import liquibase.sqlgenerator.SqlGeneratorChain; | ||
import liquibase.sqlgenerator.core.UpdateGenerator; | ||
import liquibase.statement.core.UpdateStatement; | ||
|
||
public class UpdateGeneratorSpanner extends UpdateGenerator { | ||
|
||
@Override | ||
public int getPriority() { | ||
return PRIORITY_DATABASE; | ||
} | ||
|
||
@Override | ||
public boolean supports(UpdateStatement statement, Database database) { | ||
return database instanceof ICloudSpanner; | ||
} | ||
|
||
@Override | ||
public Sql[] generateSql( | ||
UpdateStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { | ||
if (statement.getWhereClause() == null) { | ||
statement.setWhereClause("true"); | ||
} | ||
return super.generateSql(statement, database, sqlGeneratorChain); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters