Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3121 from liquibase/DAT-11147
Changed what sequence attributes are included in diff/generate changelog for Snowflake
- Loading branch information
Showing
5 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...in/java/liquibase/diff/output/changelog/core/ChangedSequenceChangeGeneratorSnowflake.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 @@ | ||
package liquibase.diff.output.changelog.core; | ||
|
||
import liquibase.change.Change; | ||
import liquibase.change.core.AlterSequenceChange; | ||
import liquibase.database.Database; | ||
import liquibase.database.core.SnowflakeDatabase; | ||
import liquibase.diff.ObjectDifferences; | ||
import liquibase.diff.output.DiffOutputControl; | ||
import liquibase.diff.output.changelog.ChangeGeneratorChain; | ||
import liquibase.structure.DatabaseObject; | ||
import liquibase.structure.core.Sequence; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ChangedSequenceChangeGeneratorSnowflake extends ChangedSequenceChangeGenerator{ | ||
|
||
@Override | ||
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) { | ||
int priority = super.getPriority(objectType, database); | ||
if ((Sequence.class.isAssignableFrom(objectType)) && (database instanceof SnowflakeDatabase)) { | ||
priority += PRIORITY_DATABASE; | ||
} | ||
return priority; | ||
} | ||
|
||
@Override | ||
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) { | ||
Sequence sequence = (Sequence) changedObject; | ||
|
||
List<Change> changes = new ArrayList<>(); | ||
AlterSequenceChange accumulatedChange = createAlterSequenceChange(sequence, control); | ||
|
||
if (differences.isDifferent("incrementBy")) { | ||
AlterSequenceChange change = createAlterSequenceChange(sequence, control); | ||
change.setIncrementBy(sequence.getIncrementBy()); | ||
accumulatedChange.setIncrementBy(sequence.getIncrementBy()); | ||
changes.add(change); | ||
} | ||
|
||
if (changes.isEmpty()) { | ||
return null; | ||
} else { | ||
return changes.toArray(new Change[changes.size()]); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...in/java/liquibase/diff/output/changelog/core/MissingSequenceChangeGeneratorSnowflake.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,41 @@ | ||
package liquibase.diff.output.changelog.core; | ||
|
||
import liquibase.change.Change; | ||
import liquibase.change.core.CreateSequenceChange; | ||
import liquibase.database.Database; | ||
import liquibase.database.core.SnowflakeDatabase; | ||
import liquibase.diff.output.DiffOutputControl; | ||
import liquibase.diff.output.changelog.ChangeGeneratorChain; | ||
import liquibase.structure.DatabaseObject; | ||
import liquibase.structure.core.Sequence; | ||
|
||
public class MissingSequenceChangeGeneratorSnowflake extends MissingSequenceChangeGenerator{ | ||
|
||
@Override | ||
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) { | ||
int priority = super.getPriority(objectType, database); | ||
if ((Sequence.class.isAssignableFrom(objectType)) && (database instanceof SnowflakeDatabase)) { | ||
priority += PRIORITY_DATABASE; | ||
} | ||
return priority; | ||
} | ||
|
||
@Override | ||
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) { | ||
Sequence sequence = (Sequence) missingObject; | ||
|
||
CreateSequenceChange change = new CreateSequenceChange(); | ||
change.setSequenceName(sequence.getName()); | ||
if (control.getIncludeCatalog()) { | ||
change.setCatalogName(sequence.getSchema().getCatalogName()); | ||
} | ||
if (control.getIncludeSchema()) { | ||
change.setSchemaName(sequence.getSchema().getName()); | ||
} | ||
change.setStartValue(sequence.getStartValue()); | ||
change.setIncrementBy(sequence.getIncrementBy()); | ||
|
||
return new Change[] { change }; | ||
|
||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...snowflake/src/main/java/liquibase/sqlgenerator/core/CreateSequenceGeneratorSnowflake.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,54 @@ | ||
package liquibase.sqlgenerator.core; | ||
|
||
import liquibase.database.Database; | ||
import liquibase.database.core.*; | ||
import liquibase.exception.ValidationErrors; | ||
import liquibase.sql.Sql; | ||
import liquibase.sql.UnparsedSql; | ||
import liquibase.sqlgenerator.SqlGeneratorChain; | ||
import liquibase.statement.core.CreateSequenceStatement; | ||
|
||
public class CreateSequenceGeneratorSnowflake extends CreateSequenceGenerator{ | ||
|
||
@Override | ||
public int getPriority() { | ||
return PRIORITY_DATABASE; | ||
} | ||
|
||
@Override | ||
public boolean supports(CreateSequenceStatement statement, Database database) { | ||
return database instanceof SnowflakeDatabase; | ||
} | ||
|
||
@Override | ||
public ValidationErrors validate(CreateSequenceStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { | ||
ValidationErrors validationErrors = new ValidationErrors(); | ||
|
||
validationErrors.checkRequiredField("sequenceName", statement.getSequenceName()); | ||
|
||
validationErrors.checkDisallowedField("minValue", statement.getMinValue(), database, SnowflakeDatabase.class); | ||
validationErrors.checkDisallowedField("maxValue", statement.getMaxValue(), database, SnowflakeDatabase.class); | ||
validationErrors.checkDisallowedField("cacheSize", statement.getCacheSize(), database, SnowflakeDatabase.class); | ||
validationErrors.checkDisallowedField("cycle", statement.getCycle(), database, SnowflakeDatabase.class); | ||
validationErrors.checkDisallowedField("datatype", statement.getDataType(), database, SnowflakeDatabase.class); | ||
validationErrors.checkDisallowedField("ordered", statement.getOrdered(), database, SnowflakeDatabase.class); | ||
|
||
return validationErrors; | ||
} | ||
|
||
@Override | ||
public Sql[] generateSql(CreateSequenceStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { | ||
StringBuilder queryStringBuilder = new StringBuilder(); | ||
queryStringBuilder.append("CREATE SEQUENCE "); | ||
queryStringBuilder.append(database.escapeSequenceName(statement.getCatalogName(), statement.getSchemaName(), statement.getSequenceName())); | ||
if (database instanceof SnowflakeDatabase) { | ||
if (statement.getStartValue() != null) { | ||
queryStringBuilder.append(" START WITH ").append(statement.getStartValue()); | ||
} | ||
if (statement.getIncrementBy() != null) { | ||
queryStringBuilder.append(" INCREMENT BY ").append(statement.getIncrementBy()); | ||
} | ||
} | ||
return new Sql[]{new UnparsedSql(queryStringBuilder.toString(), getAffectedSequence(statement))}; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...lake/src/main/resources/META-INF/services/liquibase.diff.output.changelog.ChangeGenerator
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,2 @@ | ||
liquibase.diff.output.changelog.core.ChangedSequenceChangeGeneratorSnowflake | ||
liquibase.diff.output.changelog.core.MissingSequenceChangeGeneratorSnowflake |
1 change: 1 addition & 0 deletions
1
liquibase-snowflake/src/main/resources/META-INF/services/liquibase.sqlgenerator.SqlGenerator
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