use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class CreateTableGeneratorTest method testAutoIncrementStartWithIncrementByMSSQLDatabase.
@Test
public void testAutoIncrementStartWithIncrementByMSSQLDatabase() throws Exception {
for (Database database : TestContext.getInstance().getAllDatabases()) {
if (database instanceof MSSQLDatabase) {
CreateTableStatement statement = new CreateTableStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
statement.addColumn(COLUMN_NAME1, DataTypeFactory.getInstance().fromDescription("BIGINT{autoIncrement:true}", database), new ColumnConstraint[] { new AutoIncrementConstraint(COLUMN_NAME1, BigInteger.ZERO, BigInteger.TEN), new NotNullConstraint(COLUMN_NAME1) });
Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
assertEquals("CREATE TABLE [CATALOG_NAME].[SCHEMA_NAME].[TABLE_NAME] ([COLUMN1_NAME] [bigint] IDENTITY (0, 10) NOT NULL)", generatedSql[0].toSql());
}
}
}
use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class MssqlIntegrationTest method dataTypesTest.
@Test
public void dataTypesTest() throws Exception {
if (this.getDatabase() == null) {
return;
}
Liquibase liquibase = createLiquibase("changelogs/mssql/issues/data.types.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];
if (expectedType.equalsIgnoreCase("text")) {
expectedType = "nvarchar";
}
String foundTypeDefinition = DataTypeFactory.getInstance().from(column.getType(), new MSSQLDatabase()).toDatabaseDataType(getDatabase()).toString();
String foundType = foundTypeDefinition.replaceFirst("\\(.*", "");
assertEquals("Wrong data type for " + table.getName() + "." + column.getName(), expectedType.toLowerCase(), foundType.toLowerCase());
if (expectedType.equalsIgnoreCase("varbinary")) {
if (column.getName().endsWith("_MAX")) {
assertEquals("VARBINARY(MAX)", foundTypeDefinition);
} else {
assertEquals("VARBINARY(1)", foundTypeDefinition);
}
}
}
}
}
use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class CharType method toDatabaseDataType.
@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof MSSQLDatabase) {
Object[] parameters = getParameters();
if (parameters.length > 0) {
String param1 = parameters[0].toString();
if (!param1.matches("\\d+") || new BigInteger(param1).compareTo(BigInteger.valueOf(8000)) > 0) {
DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("char"), 8000);
type.addAdditionalInformation(getAdditionalInformation());
return type;
}
}
if (parameters.length == 0) {
parameters = new Object[] { 1 };
} else if (parameters.length > 1) {
parameters = Arrays.copyOfRange(parameters, 0, 1);
}
DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("char"), parameters);
type.addAdditionalInformation(getAdditionalInformation());
return type;
} else if (database instanceof PostgresDatabase) {
if (getParameters() != null && getParameters().length == 1 && getParameters()[0].toString().equals("2147483647")) {
DatabaseDataType type = new DatabaseDataType("CHARACTER");
type.addAdditionalInformation("VARYING");
return type;
}
return super.toDatabaseDataType(database);
}
return super.toDatabaseDataType(database);
}
use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class LoggingExecutor method outputStatement.
private void outputStatement(SqlStatement sql, List<SqlVisitor> sqlVisitors) throws DatabaseException {
try {
if (SqlGeneratorFactory.getInstance().generateStatementsVolatile(sql, database)) {
throw new DatabaseException(sql.getClass().getSimpleName() + " requires access to up to date database metadata which is not available in SQL output mode");
}
if (sql instanceof ExecutablePreparedStatement) {
output.write("WARNING: This statement uses a prepared statement which cannot be execute directly by this script. Only works in 'update' mode\n\n");
}
for (String statement : applyVisitors(sql, sqlVisitors)) {
if (statement == null) {
continue;
}
if (database instanceof OracleDatabase) {
//remove trailing /
while (statement.matches("(?s).*[\\s\\r\\n]*/[\\s\\r\\n]*$")) {
//all trailing /'s
statement = statement.replaceFirst("[\\s\\r\\n]*/[\\s\\r\\n]*$", "");
}
}
output.write(statement);
if (database instanceof MSSQLDatabase || database instanceof SybaseDatabase || database instanceof SybaseASADatabase) {
output.write(StreamUtil.getLineSeparator());
output.write("GO");
// } else if (database instanceof OracleDatabase) {
// output.write(StreamUtil.getLineSeparator());
// output.write("/");
} else {
String endDelimiter = ";";
String potentialDelimiter = null;
if (sql instanceof RawSqlStatement) {
potentialDelimiter = ((RawSqlStatement) sql).getEndDelimiter();
} else if (sql instanceof CreateProcedureStatement) {
potentialDelimiter = ((CreateProcedureStatement) sql).getEndDelimiter();
}
if (potentialDelimiter != null) {
//ignore trailing $ as a regexp to determine if it should be output
potentialDelimiter = potentialDelimiter.replaceFirst("\\$$", "");
if (potentialDelimiter.replaceAll("\\n", "\n").replace("\\r", "\r").matches("[;/\r\n\\w@\\-]+")) {
endDelimiter = potentialDelimiter;
}
}
endDelimiter = endDelimiter.replace("\\n", "\n");
endDelimiter = endDelimiter.replace("\\r", "\r");
if (!statement.endsWith(endDelimiter)) {
output.write(endDelimiter);
}
}
output.write(StreamUtil.getLineSeparator());
output.write(StreamUtil.getLineSeparator());
}
} catch (IOException e) {
throw new DatabaseException(e);
}
}
use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.
the class MissingIndexChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
Index index = (Index) missingObject;
if (comparisonDatabase instanceof MSSQLDatabase) {
PrimaryKey primaryKey = index.getTable().getPrimaryKey();
if (primaryKey != null && DatabaseObjectComparatorFactory.getInstance().isSameObject(missingObject, primaryKey.getBackingIndex(), control.getSchemaComparisons(), referenceDatabase)) {
//will be handled by the PK
return new Change[0];
}
}
CreateIndexChange change = createCreateIndexChange();
change.setTableName(index.getTable().getName());
if (control.getIncludeTablespace()) {
change.setTablespace(index.getTablespace());
}
if (control.getIncludeCatalog()) {
change.setCatalogName(index.getTable().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(index.getTable().getSchema().getName());
}
change.setIndexName(index.getName());
change.setUnique(index.isUnique() != null && index.isUnique() ? Boolean.TRUE : null);
change.setAssociatedWith(index.getAssociatedWithAsString());
change.setClustered(index.getClustered() != null && index.getClustered() ? Boolean.TRUE : null);
for (Column column : index.getColumns()) {
change.addColumn(new AddColumnConfig(column));
}
return new Change[] { change };
}
Aggregations