Search in sources :

Example 56 with Database

use of liquibase.database.Database in project liquibase by liquibase.

the class InternalGenerateChangelogCommandStep method run.

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
    CommandScope commandScope = resultsBuilder.getCommandScope();
    outputBestPracticeMessage();
    String changeLogFile = StringUtil.trimToNull(commandScope.getArgumentValue(CHANGELOG_FILE_ARG));
    if (changeLogFile != null && changeLogFile.toLowerCase().endsWith(".sql")) {
        Scope.getCurrentScope().getUI().sendMessage("\n" + INFO_MESSAGE + "\n");
        Scope.getCurrentScope().getLog(getClass()).info("\n" + INFO_MESSAGE + "\n");
    }
    final Database referenceDatabase = commandScope.getArgumentValue(REFERENCE_DATABASE_ARG);
    InternalSnapshotCommandStep.logUnsupportedDatabase(referenceDatabase, this.getClass());
    DiffResult diffResult = createDiffResult(commandScope);
    DiffToChangeLog changeLogWriter = new DiffToChangeLog(diffResult, commandScope.getArgumentValue(DIFF_OUTPUT_CONTROL_ARG));
    changeLogWriter.setChangeSetAuthor(commandScope.getArgumentValue(AUTHOR_ARG));
    changeLogWriter.setChangeSetContext(commandScope.getArgumentValue(CONTEXT_ARG));
    changeLogWriter.setChangeSetPath(changeLogFile);
    ObjectQuotingStrategy originalStrategy = referenceDatabase.getObjectQuotingStrategy();
    try {
        referenceDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
        if (StringUtil.trimToNull(changeLogFile) != null) {
            changeLogWriter.print(changeLogFile);
        } else {
            PrintStream outputStream = new PrintStream(resultsBuilder.getOutputStream());
            try {
                changeLogWriter.print(outputStream);
            } finally {
                outputStream.flush();
            }
        }
        if (StringUtil.trimToNull(changeLogFile) != null) {
            Scope.getCurrentScope().getUI().sendMessage("Generated changelog written to " + new File(changeLogFile).getAbsolutePath());
        }
    } finally {
        referenceDatabase.setObjectQuotingStrategy(originalStrategy);
    }
}
Also used : PrintStream(java.io.PrintStream) Database(liquibase.database.Database) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DiffResult(liquibase.diff.DiffResult) File(java.io.File) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 57 with Database

use of liquibase.database.Database in project liquibase by liquibase.

the class InternalSnapshotCommandStep method run.

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
    CommandScope commandScope = resultsBuilder.getCommandScope();
    Database database = commandScope.getArgumentValue(DATABASE_ARG);
    SnapshotListener snapshotListener = commandScope.getArgumentValue(SNAPSHOT_LISTENER_ARG);
    CatalogAndSchema[] schemas = commandScope.getArgumentValue(SCHEMAS_ARG);
    InternalSnapshotCommandStep.logUnsupportedDatabase(database, this.getClass());
    SnapshotControl snapshotControl = new SnapshotControl(database);
    snapshotControl.setSnapshotListener(snapshotListener);
    if (schemas == null) {
        schemas = new CatalogAndSchema[] { database.getDefaultSchema() };
    }
    ObjectQuotingStrategy originalQuotingStrategy = database.getObjectQuotingStrategy();
    database.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
    DatabaseSnapshot snapshot;
    try {
        snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, database, snapshotControl);
    } finally {
        database.setObjectQuotingStrategy(originalQuotingStrategy);
    }
    snapshot.setMetadata(this.getSnapshotMetadata());
    resultsBuilder.addResult("snapshot", snapshot);
}
Also used : Database(liquibase.database.Database) CatalogAndSchema(liquibase.CatalogAndSchema) SnapshotControl(liquibase.snapshot.SnapshotControl) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotListener(liquibase.snapshot.SnapshotListener) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 58 with Database

use of liquibase.database.Database in project liquibase by liquibase.

the class DiffGeneratorFactory method compare.

public DiffResult compare(DatabaseSnapshot referenceSnapshot, DatabaseSnapshot comparisonSnapshot, CompareControl compareControl) throws DatabaseException {
    Database referenceDatabase = referenceSnapshot.getDatabase();
    if (comparisonSnapshot != null && referenceDatabase != null) {
        if (referenceDatabase.getDefaultCatalogName() == null || referenceDatabase.getDefaultCatalogName().isEmpty()) {
            referenceDatabase.setDefaultCatalogName(comparisonSnapshot.getDatabase().getDefaultCatalogName());
        }
        if (referenceDatabase.getDefaultSchemaName() == null || referenceDatabase.getDefaultSchemaName().isEmpty()) {
            referenceDatabase.setDefaultSchemaName(comparisonSnapshot.getDatabase().getDefaultSchemaName());
        }
    }
    Database comparisonDatabase;
    if (comparisonSnapshot == null) {
        comparisonDatabase = referenceSnapshot.getDatabase();
        try {
            comparisonSnapshot = new EmptyDatabaseSnapshot(referenceDatabase, referenceSnapshot.getSnapshotControl());
        } catch (InvalidExampleException e) {
            throw new UnexpectedLiquibaseException(e);
        }
    } else {
        comparisonDatabase = comparisonSnapshot.getDatabase();
    }
    return getGenerator(referenceDatabase, comparisonDatabase).compare(referenceSnapshot, comparisonSnapshot, compareControl);
}
Also used : Database(liquibase.database.Database) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 59 with Database

use of liquibase.database.Database in project liquibase by liquibase.

the class DiffResult method getChangedObject.

public ObjectDifferences getChangedObject(DatabaseObject example, CompareControl.SchemaComparison[] schemaComparisons) {
    Database accordingTo = this.getComparisonSnapshot().getDatabase();
    DatabaseObjectComparatorFactory comparator = DatabaseObjectComparatorFactory.getInstance();
    for (Map.Entry<? extends DatabaseObject, ObjectDifferences> entry : getChangedObjects(example.getClass()).entrySet()) {
        if (comparator.isSameObject(entry.getKey(), example, schemaComparisons, accordingTo)) {
            return entry.getValue();
        }
    }
    return null;
}
Also used : Database(liquibase.database.Database) DatabaseObjectComparatorFactory(liquibase.diff.compare.DatabaseObjectComparatorFactory)

Example 60 with Database

use of liquibase.database.Database in project liquibase by liquibase.

the class CreateTableGeneratorTest method testAutoIncrementStartWithIncrementByDerbyDatabase.

@Test
public void testAutoIncrementStartWithIncrementByDerbyDatabase() throws Exception {
    for (Database database : TestContext.getInstance().getAllDatabases()) {
        if (database instanceof DerbyDatabase) {
            CreateTableStatement statement = new CreateTableStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
            statement.addColumn(COLUMN_NAME1, DataTypeFactory.getInstance().fromDescription("BIGINT{autoIncrement:true}", database), new AutoIncrementConstraint(COLUMN_NAME1, BigInteger.ZERO, BigInteger.TEN));
            Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
            assertEquals("CREATE TABLE CATALOG_NAME.TABLE_NAME (COLUMN1_NAME BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 0, INCREMENT BY 10))", generatedSql[0].toSql());
        }
    }
}
Also used : DerbyDatabase(liquibase.database.core.DerbyDatabase) CreateTableStatement(liquibase.statement.core.CreateTableStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) 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) AbstractDb2Database(liquibase.database.core.AbstractDb2Database) HsqlDatabase(liquibase.database.core.HsqlDatabase) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Aggregations

Database (liquibase.database.Database)220 Test (org.junit.Test)99 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)60 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)57 Sql (liquibase.sql.Sql)57 OracleDatabase (liquibase.database.core.OracleDatabase)55 MySQLDatabase (liquibase.database.core.MySQLDatabase)53 PostgresDatabase (liquibase.database.core.PostgresDatabase)51 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)48 H2Database (liquibase.database.core.H2Database)47 DerbyDatabase (liquibase.database.core.DerbyDatabase)45 AbstractDb2Database (liquibase.database.core.AbstractDb2Database)44 HsqlDatabase (liquibase.database.core.HsqlDatabase)44 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)44 SybaseDatabase (liquibase.database.core.SybaseDatabase)44 CreateTableStatement (liquibase.statement.core.CreateTableStatement)44 DatabaseException (liquibase.exception.DatabaseException)28 SQLException (java.sql.SQLException)25 AbstractJdbcDatabaseTest (liquibase.database.AbstractJdbcDatabaseTest)23 Liquibase (liquibase.Liquibase)22