Search in sources :

Example 16 with SQLiteDatabase

use of liquibase.database.core.SQLiteDatabase in project liquibase by liquibase.

the class CopyRowsGenerator method generateSql.

@Override
public Sql[] generateSql(CopyRowsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    StringBuilder sql = new StringBuilder();
    if (statement.getCopyColumns().isEmpty()) {
        return new Sql[] {};
    }
    if (database instanceof SQLiteDatabase) {
        sql.append("INSERT INTO ").append(database.escapeTableName(null, null, statement.getTargetTable())).append(" (");
        for (int i = 0; i < statement.getCopyColumns().size(); i++) {
            ColumnConfig column = statement.getCopyColumns().get(i);
            if (i > 0) {
                sql.append(",");
            }
            sql.append(database.escapeColumnName(null, null, statement.getTargetTable(), column.getName()));
        }
        sql.append(") SELECT ");
        for (int i = 0; i < statement.getCopyColumns().size(); i++) {
            ColumnConfig column = statement.getCopyColumns().get(i);
            if (i > 0) {
                sql.append(",");
            }
            sql.append(database.escapeColumnName(null, null, statement.getSourceTable(), column.getName()));
        }
        sql.append(" FROM ").append(database.escapeTableName(null, null, statement.getSourceTable()));
    }
    return new Sql[] { new UnparsedSql(sql.toString(), getAffectedTable(statement)) };
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) UnparsedSql(liquibase.sql.UnparsedSql) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 17 with SQLiteDatabase

use of liquibase.database.core.SQLiteDatabase in project collect by openforis.

the class ExternalCodeListIntegrationTest method before.

@SuppressWarnings("deprecation")
@Before
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void before() throws IdmlParseException, IOException, SurveyImportException, SQLException, LiquibaseException, SurveyValidationException {
    SQLiteDatabase database = new SQLiteDatabase();
    Connection connection = DataSourceUtils.getConnection(transactionAwareDataSource);
    database.setConnection(new JdbcConnection(connection));
    Liquibase liquibase = new Liquibase(LIQUIBASE_CHANGELOG, new ClassLoaderResourceAccessor(), database);
    liquibase.update((String) null);
    survey = loadSurvey();
    createHierarchicalTestList();
    createFlatTestList();
    surveyManager.importModel(survey);
}
Also used : Liquibase(liquibase.Liquibase) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) Before(org.junit.Before) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with SQLiteDatabase

use of liquibase.database.core.SQLiteDatabase in project collect by openforis.

the class LiquibaseRelationalSchemaCreator method createRelationalSchema.

@Override
public void createRelationalSchema(RelationalSchema schema, Connection targetConn) throws CollectRdbException {
    PrintStream ps = null;
    try {
        LiquidbaseDatabaseSnapshotBuilder snapshotGen = new LiquidbaseDatabaseSnapshotBuilder();
        Database rdb = getDatabaseImplementation(targetConn);
        boolean dbSupportsFKs = rdb instanceof SQLiteDatabase ? false : true;
        DatabaseSnapshot generatedSnapshot = snapshotGen.createSnapshot(schema, dbSupportsFKs);
        String targetSchema = schema.getName();
        rdb.setDefaultSchemaName(targetSchema);
        DatabaseSnapshot emptyDbSnapshot = new DatabaseSnapshot(rdb, targetSchema);
        // Generate change set
        Diff diff = new Diff(generatedSnapshot, emptyDbSnapshot);
        DiffResult diffResult = diff.compare();
        File tmpFile = File.createTempFile("collect-schemagen", ".xml");
        ps = new PrintStream(new FileOutputStream(tmpFile));
        diffResult.setChangeSetAuthor("collect3");
        diffResult.setChangeSetContext("schemagen");
        System.out.println("Writing change log to " + tmpFile.getAbsolutePath());
        diffResult.printChangeLog(ps, rdb);
        ps.flush();
        // Execute change set
        Liquibase liq = new Liquibase(tmpFile.getName(), new FileSystemResourceAccessor(tmpFile.getParent()), rdb);
        liq.update("schemagen");
    } catch (LiquibaseException e) {
        throw new CollectRdbException("Failed to update schema", e);
    } catch (IOException e) {
        throw new CollectRdbException("Failed to create temp db changelog file", e);
    } catch (ParserConfigurationException e) {
        throw new CollectRdbException("Failed to write temp db changelog file", e);
    } finally {
        if (ps != null) {
            ps.close();
        }
    }
}
Also used : PrintStream(java.io.PrintStream) Diff(liquibase.diff.Diff) IOException(java.io.IOException) Liquibase(liquibase.Liquibase) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) CollectRdbException(org.openforis.collect.relational.CollectRdbException) FileOutputStream(java.io.FileOutputStream) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) Database(liquibase.database.Database) DiffResult(liquibase.diff.DiffResult) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) LiquibaseException(liquibase.exception.LiquibaseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) File(java.io.File)

Aggregations

SQLiteDatabase (liquibase.database.core.SQLiteDatabase)18 Database (liquibase.database.Database)8 PostgresDatabase (liquibase.database.core.PostgresDatabase)7 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)6 Sql (liquibase.sql.Sql)6 AbstractDb2Database (liquibase.database.core.AbstractDb2Database)5 DB2Database (liquibase.database.core.DB2Database)5 MySQLDatabase (liquibase.database.core.MySQLDatabase)5 OracleDatabase (liquibase.database.core.OracleDatabase)5 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)5 SybaseDatabase (liquibase.database.core.SybaseDatabase)5 SqlStatement (liquibase.statement.SqlStatement)5 ArrayList (java.util.ArrayList)4 DerbyDatabase (liquibase.database.core.DerbyDatabase)4 H2Database (liquibase.database.core.H2Database)4 HsqlDatabase (liquibase.database.core.HsqlDatabase)4 ColumnConfig (liquibase.change.ColumnConfig)3 DatabaseException (liquibase.exception.DatabaseException)3 LiquibaseException (liquibase.exception.LiquibaseException)3 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)3