Search in sources :

Example 21 with MSSQLDatabase

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());
        }
    }
}
Also used : AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) CreateTableStatement(liquibase.statement.core.CreateTableStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) DB2Database(liquibase.database.core.DB2Database) H2Database(liquibase.database.core.H2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) SybaseDatabase(liquibase.database.core.SybaseDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) Database(liquibase.database.Database) HsqlDatabase(liquibase.database.core.HsqlDatabase) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) NotNullConstraint(liquibase.statement.NotNullConstraint) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 22 with MSSQLDatabase

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);
                }
            }
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test)

Example 23 with MSSQLDatabase

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);
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) DatabaseDataType(liquibase.datatype.DatabaseDataType) BigInteger(java.math.BigInteger) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 24 with MSSQLDatabase

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);
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) RawSqlStatement(liquibase.statement.core.RawSqlStatement) SybaseDatabase(liquibase.database.core.SybaseDatabase) CreateProcedureStatement(liquibase.statement.core.CreateProcedureStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) IOException(java.io.IOException) DatabaseException(liquibase.exception.DatabaseException) ExecutablePreparedStatement(liquibase.statement.ExecutablePreparedStatement)

Example 25 with MSSQLDatabase

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 };
}
Also used : Column(liquibase.structure.core.Column) PrimaryKey(liquibase.structure.core.PrimaryKey) Index(liquibase.structure.core.Index) AddColumnConfig(liquibase.change.AddColumnConfig) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Change(liquibase.change.Change) CreateIndexChange(liquibase.change.core.CreateIndexChange) CreateIndexChange(liquibase.change.core.CreateIndexChange)

Aggregations

MSSQLDatabase (liquibase.database.core.MSSQLDatabase)38 Test (org.junit.Test)17 Sql (liquibase.sql.Sql)12 OracleDatabase (liquibase.database.core.OracleDatabase)11 Database (liquibase.database.Database)7 DB2Database (liquibase.database.core.DB2Database)7 MySQLDatabase (liquibase.database.core.MySQLDatabase)7 PostgresDatabase (liquibase.database.core.PostgresDatabase)7 RawSqlStatement (liquibase.statement.core.RawSqlStatement)6 Column (liquibase.structure.core.Column)6 ArrayList (java.util.ArrayList)5 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)5 DatabaseException (liquibase.exception.DatabaseException)5 Change (liquibase.change.Change)4 HsqlDatabase (liquibase.database.core.HsqlDatabase)4 SybaseDatabase (liquibase.database.core.SybaseDatabase)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 Executor (liquibase.executor.Executor)4 InsertOrUpdateStatement (liquibase.statement.core.InsertOrUpdateStatement)4 BigInteger (java.math.BigInteger)3