Search in sources :

Example 1 with AddUniqueConstraintStatement

use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.

the class AddUniqueConstraintChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    //todo    	if (database instanceof SQLiteDatabase) {
    //    		// return special statements for SQLite databases
    //    		return generateStatementsForSQLiteDatabase(database);
    //        }
    boolean deferrable = false;
    if (getDeferrable() != null) {
        deferrable = getDeferrable();
    }
    boolean initiallyDeferred = false;
    if (getInitiallyDeferred() != null) {
        initiallyDeferred = getInitiallyDeferred();
    }
    boolean disabled = false;
    if (getDisabled() != null) {
        disabled = getDisabled();
    }
    boolean clustered = false;
    if (getClustered() != null) {
        clustered = getClustered();
    }
    AddUniqueConstraintStatement statement = new AddUniqueConstraintStatement(getCatalogName(), getSchemaName(), getTableName(), ColumnConfig.arrayFromNames(getColumnNames()), getConstraintName());
    statement.setTablespace(getTablespace()).setDeferrable(deferrable).setInitiallyDeferred(initiallyDeferred).setDisabled(disabled).setClustered(clustered);
    statement.setForIndexName(getForIndexName());
    statement.setForIndexSchemaName(getForIndexSchemaName());
    statement.setForIndexCatalogName(getForIndexCatalogName());
    return new SqlStatement[] { statement };
}
Also used : SqlStatement(liquibase.statement.SqlStatement) AddUniqueConstraintStatement(liquibase.statement.core.AddUniqueConstraintStatement)

Example 2 with AddUniqueConstraintStatement

use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.

the class AddUniqueConstraintExecutorTest method execute_withSchema.

@SuppressWarnings("unchecked")
@Test
public void execute_withSchema() throws Exception {
    statementUnderTest = new AddUniqueConstraintStatement(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA, TABLE_NAME, new ColumnConfig[] { new ColumnConfig().setName(COLUMN_NAME) }, CONSTRAINT_NAME);
    // FIXME Syntax for mysql is correct, but exception "Table 'liquibaseb.adduqtest' doesn't exist" is thrown
    // 		assertCorrect("alter table `liquibaseb`.`adduqtest` add constraint `uq_test` unique (`coltomakeuq`)", MySQLDatabase.class);
    assertCorrect("alter table liquibaseb.adduqtest add constraint unique (coltomakeuq) constraint uq_test", InformixDatabase.class);
    assertCorrect("alter table liquibasec.adduqtest add constraint uq_test unique (coltomakeuq)", OracleDatabase.class);
    assertCorrect("alter table liquibaseb.\"adduqtest\" add constraint uq_test unique (\"coltomakeuq\")", PostgresDatabase.class);
    assertCorrect("alter table liquibasec.adduqtest add constraint uq_test unique (coltomakeuq)", DerbyDatabase.class);
    assertCorrect("alter table [liquibaseb].[adduqtest] add constraint [uq_test] unique ([coltomakeuq])", SybaseASADatabase.class, SybaseDatabase.class, MSSQLDatabase.class);
    assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])", FirebirdDatabase.class);
    assertCorrect("alter table [liquibaseb].[adduqtest] add constraint [uq_test] unique ([coltomakeuq])", HsqlDatabase.class);
    assertCorrectOnRest("alter table [liquibasec].[adduqtest] add constraint [uq_test] unique ([coltomakeuq])");
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) AddUniqueConstraintStatement(liquibase.statement.core.AddUniqueConstraintStatement) Test(org.junit.Test)

Example 3 with AddUniqueConstraintStatement

use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.

the class AddUniqueConstraintExecutorTest method execute_noConstraintName.

@SuppressWarnings("unchecked")
@Test
public void execute_noConstraintName() throws Exception {
    this.statementUnderTest = new AddUniqueConstraintStatement(null, null, TABLE_NAME, new ColumnConfig[] { new ColumnConfig().setName(COLUMN_NAME) }, null);
    assertCorrect("alter table adduqtest add unique (coltomakeuq)", MySQLDatabase.class);
    assertCorrect("alter table adduqtest add constraint unique (coltomakeuq)", InformixDatabase.class);
    assertCorrect("alter table adduqtest add unique (coltomakeuq)", OracleDatabase.class);
    assertCorrect("alter table \"adduqtest\" add unique (\"coltomakeuq\")", PostgresDatabase.class);
    assertCorrect("alter table adduqtest add unique (coltomakeuq)", DerbyDatabase.class);
    assertCorrect("alter table [adduqtest] add unique ([coltomakeuq])", SybaseASADatabase.class, SybaseDatabase.class);
    assertCorrect("alter table [adduqtest] add unique ([coltomakeuq])", MSSQLDatabase.class);
    assertCorrect("alter table [adduqtest] add unique ([coltomakeuq])");
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) AddUniqueConstraintStatement(liquibase.statement.core.AddUniqueConstraintStatement) Test(org.junit.Test)

Example 4 with AddUniqueConstraintStatement

use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.

the class AddUniqueConstraintExecutorTest method execute_noSchema.

//    @Test
//    public void execute_noSchema() throws Exception {
//        new DatabaseTestTemplate().testOnAvailableDatabases(
//                new SqlStatementDatabaseTest(null, new AddUniqueConstraintStatement(null, TABLE_NAME, COLUMN_NAME, "uq_adduqtest")) {
//
//                    protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
//                        assertFalse(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
//                    }
//
//                    protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
//                        //todo: enable snapshot and assertion when snapshot can check for unique constraints
//                        //snapshot = new DatabaseSnapshotGenerator(snapshot);
//                    	assertTrue(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
//                    }
//                });
//    }
//
//    @Test
//    public void execute_withSchema() throws Exception {
//        new DatabaseTestTemplate().testOnAvailableDatabases(
//                new SqlStatementDatabaseTest(TestContext.ALT_SCHEMA, new AddUniqueConstraintStatement(TestContext.ALT_SCHEMA, TABLE_NAME, COLUMN_NAME, "uq_adduqtest")) {
//                    protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
//                        assertFalse(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
//                    }
//
//                    protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
//                        //todo: enable snapshot and assertion when snapshot can check for unique constraints
//                snapshot = new DatabaseSnapshotGenerator(database, TestContext.ALT_SCHEMA);
//                assertTrue(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
//                    }
//
//                });
//    }
//
//    @Test
//    public void execute_withTablespace() throws Exception {
//        new DatabaseTestTemplate().testOnAvailableDatabases(
//                new SqlStatementDatabaseTest(null, new AddUniqueConstraintStatement(null, TABLE_NAME, COLUMN_NAME, "uq_adduqtest").setTablespace(TestContext.ALT_TABLESPACE)) {
//                    protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
//                        assertFalse(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
//                    }
//
//                    protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
//                        //todo: enable snapshot and assertion when snapshot can check for unique constraints
//                        // snapshot = new DatabaseSnapshotGenerator(database);
////                assertTrue(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
//                    }
//                });
//    }
@SuppressWarnings("unchecked")
@Test
public void execute_noSchema() throws Exception {
    this.statementUnderTest = new AddUniqueConstraintStatement(null, null, TABLE_NAME, new ColumnConfig[] { new ColumnConfig().setName(COLUMN_NAME) }, CONSTRAINT_NAME);
    assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])", SybaseDatabase.class);
    assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])", MSSQLDatabase.class);
    assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])", SybaseASADatabase.class);
    assertCorrect("alter table adduqtest add constraint uq_test unique (coltomakeuq)", MySQLDatabase.class);
    assertCorrect("alter table adduqtest add constraint unique (coltomakeuq) constraint uq_test", InformixDatabase.class);
    assertCorrect("alter table adduqtest add constraint uq_test unique (coltomakeuq)", OracleDatabase.class);
    assertCorrect("alter table \"adduqtest\" add constraint uq_test unique (\"coltomakeuq\")", PostgresDatabase.class);
    assertCorrect("alter table adduqtest add constraint uq_test unique (coltomakeuq)", DerbyDatabase.class);
    assertCorrect("alter table [adduqtest] add constraint [uq_test] unique ([coltomakeuq])");
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) AddUniqueConstraintStatement(liquibase.statement.core.AddUniqueConstraintStatement) Test(org.junit.Test)

Example 5 with AddUniqueConstraintStatement

use of liquibase.statement.core.AddUniqueConstraintStatement in project liquibase by liquibase.

the class AddColumnGenerator method addUniqueConstrantStatements.

protected void addUniqueConstrantStatements(AddColumnStatement statement, Database database, List<Sql> returnSql) {
    if (statement.isUnique()) {
        AddUniqueConstraintStatement addConstraintStmt = new AddUniqueConstraintStatement(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), ColumnConfig.arrayFromNames(statement.getColumnName()), statement.getUniqueStatementName());
        returnSql.addAll(Arrays.asList(SqlGeneratorFactory.getInstance().generateSql(addConstraintStmt, database)));
    }
}
Also used : AddUniqueConstraintStatement(liquibase.statement.core.AddUniqueConstraintStatement)

Aggregations

AddUniqueConstraintStatement (liquibase.statement.core.AddUniqueConstraintStatement)5 ColumnConfig (liquibase.change.ColumnConfig)3 Test (org.junit.Test)3 SqlStatement (liquibase.statement.SqlStatement)1