use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class MissingTableChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
Table missingTable = (Table) missingObject;
PrimaryKey primaryKey = missingTable.getPrimaryKey();
// if (control.diffResult.getReferenceSnapshot().getDatabase().isLiquibaseTable(missingTable.getSchema().toCatalogAndSchema(), missingTable.getName())) {
// continue;
// }
CreateTableChange change = createCreateTableChange();
change.setTableName(missingTable.getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(missingTable.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(missingTable.getSchema().getName());
}
if (missingTable.getRemarks() != null) {
change.setRemarks(missingTable.getRemarks());
}
for (Column column : missingTable.getColumns()) {
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setName(column.getName());
LiquibaseDataType ldt = DataTypeFactory.getInstance().from(column.getType(), referenceDatabase);
DatabaseDataType ddt = ldt.toDatabaseDataType(comparisonDatabase);
String typeString = ddt.toString();
if (comparisonDatabase instanceof MSSQLDatabase) {
typeString = comparisonDatabase.unescapeDataTypeString(typeString);
}
columnConfig.setType(typeString);
if (column.isAutoIncrement()) {
columnConfig.setAutoIncrement(true);
}
ConstraintsConfig constraintsConfig = null;
// In MySQL, the primary key must be specified at creation for an autoincrement column
if (column.isAutoIncrement() && primaryKey != null && primaryKey.getColumns().size() == 1 && primaryKey.getColumnNamesAsList().contains(column.getName())) {
if (referenceDatabase instanceof MSSQLDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && !primaryKey.getBackingIndex().getClustered()) {
// have to handle PK as a separate statement
} else if (referenceDatabase instanceof PostgresDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && primaryKey.getBackingIndex().getClustered()) {
// have to handle PK as a separate statement
} else {
constraintsConfig = new ConstraintsConfig();
if (shouldAddPrimarykeyToConstraints(missingObject, control, referenceDatabase, comparisonDatabase)) {
constraintsConfig.setPrimaryKey(true);
constraintsConfig.setPrimaryKeyTablespace(primaryKey.getTablespace());
// MySQL sets some primary key names as PRIMARY which is invalid
if (comparisonDatabase instanceof MySQLDatabase && "PRIMARY".equals(primaryKey.getName())) {
constraintsConfig.setPrimaryKeyName(null);
} else {
constraintsConfig.setPrimaryKeyName(primaryKey.getName());
}
control.setAlreadyHandledMissing(primaryKey);
control.setAlreadyHandledMissing(primaryKey.getBackingIndex());
} else {
constraintsConfig.setNullable(false);
}
}
} else if (column.isNullable() != null && !column.isNullable()) {
constraintsConfig = new ConstraintsConfig();
constraintsConfig.setNullable(false);
}
if (constraintsConfig != null) {
columnConfig.setConstraints(constraintsConfig);
}
setDefaultValue(columnConfig, column, referenceDatabase);
if (column.getRemarks() != null) {
columnConfig.setRemarks(column.getRemarks());
}
if (column.getAutoIncrementInformation() != null) {
BigInteger startWith = column.getAutoIncrementInformation().getStartWith();
BigInteger incrementBy = column.getAutoIncrementInformation().getIncrementBy();
if (startWith != null && !startWith.equals(BigInteger.ONE)) {
columnConfig.setStartWith(startWith);
}
if (incrementBy != null && !incrementBy.equals(BigInteger.ONE)) {
columnConfig.setIncrementBy(incrementBy);
}
}
change.addColumn(columnConfig);
control.setAlreadyHandledMissing(column);
}
return new Change[] { change };
}
use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class UnknownType method toDatabaseDataType.
@Override
public DatabaseDataType toDatabaseDataType(Database database) {
int dataTypeMaxParameters;
if (getName().equalsIgnoreCase("enum") || getName().equalsIgnoreCase("set")) {
dataTypeMaxParameters = Integer.MAX_VALUE;
} else {
dataTypeMaxParameters = database.getDataTypeMaxParameters(getName());
}
Object[] parameters = getParameters();
if (database instanceof OracleDatabase) {
if (getName().equalsIgnoreCase("LONG") || getName().equalsIgnoreCase("BFILE") || getName().equalsIgnoreCase("ROWID") || getName().equalsIgnoreCase("ANYDATA") || getName().equalsIgnoreCase("SDO_GEOMETRY")) {
parameters = new Object[0];
} else if (getName().equalsIgnoreCase("RAW")) {
return new DatabaseDataType(getName(), parameters);
} else if (getName().toUpperCase().startsWith("INTERVAL ")) {
return new DatabaseDataType(getName().replaceAll("\\(\\d+\\)", ""));
} else {
//probably a user defined type. Can't call getUserDefinedTypes() to know for sure, since that returns all types including system types.
return new DatabaseDataType(getName().toUpperCase());
}
}
if (dataTypeMaxParameters < parameters.length) {
parameters = Arrays.copyOfRange(parameters, 0, dataTypeMaxParameters);
}
DatabaseDataType type;
if (database instanceof MSSQLDatabase) {
type = new DatabaseDataType(database.escapeDataTypeName(getName()), parameters);
} else {
type = new DatabaseDataType(getName().toUpperCase(), parameters);
}
type.addAdditionalInformation(getAdditionalInformation());
return type;
}
use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class DeleteGeneratorTest method testGenerateSql.
@Test
public void testGenerateSql() {
// given
DeleteStatement statement = new DeleteStatement(null, null, "DATABASECHANGELOG");
statement.setWhere(":name = :value AND :name = :value AND :name = :value");
statement.addWhereColumnName("ID");
statement.addWhereColumnName("AUTHOR");
statement.addWhereColumnName("FILENAME");
statement.addWhereParameter("1");
statement.addWhereParameter("a");
statement.addWhereParameter("server_principals/BUILTIN$Administrators.xml");
Database database = new MSSQLDatabase();
DeleteGenerator generator = new DeleteGenerator();
// when
Sql[] sqls = generator.generateSql(statement, database, null);
// then
assertEquals("DELETE FROM [DATABASECHANGELOG] " + "WHERE [ID] = '1' " + "AND [AUTHOR] = 'a' " + "AND [FILENAME] = 'server_principals/BUILTIN$Administrators.xml'", sqls[0].toSql());
}
use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class InsertOrUpdateGeneratorMSSQLTest method getElse.
@Test
public void getElse() {
InsertOrUpdateGeneratorMSSQL generator = new InsertOrUpdateGeneratorMSSQL();
MSSQLDatabase database = new MSSQLDatabase();
InsertOrUpdateStatement statement = new InsertOrUpdateStatement("mycatalog", "myschema", "mytable", "pk_col1");
statement.addColumnValue("pk_col1", "value1");
statement.addColumnValue("col2", "value2");
String where = "1 = 1";
Class c = InsertOrUpdateGenerator.class.getClass();
//InsertOrUpdateStatement insertOrUpdateStatement, Database database, SqlGeneratorChain sqlGeneratorChain
String insertStatement = (String) invokePrivateMethod(generator, "getElse", new Object[] { database });
Integer lineNumber = 0;
String[] lines = insertStatement.split("\n");
assertEquals("ELSE", lines[lineNumber]);
}
use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class MssqlIntegrationTest method dataTypeParamsTest.
@Test
public void dataTypeParamsTest() throws Exception {
if (this.getDatabase() == null) {
return;
}
Liquibase liquibase = createLiquibase("changelogs/mssql/issues/data.type.params.xml");
liquibase.update((String) null);
DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(CatalogAndSchema.DEFAULT, this.getDatabase(), new SnapshotControl(getDatabase()));
for (Table table : snapshot.get(Table.class)) {
if (getDatabase().isLiquibaseObject(table)) {
continue;
}
for (Column column : table.getColumns()) {
String expectedType = column.getName().split("_")[0];
String foundTypeDefinition = DataTypeFactory.getInstance().from(column.getType(), new MSSQLDatabase()).toDatabaseDataType(getDatabase()).toString();
assertFalse("Parameter found in " + table.getName() + "." + column.getName(), foundTypeDefinition.contains("("));
}
}
}
Aggregations