Search in sources :

Example 1 with ClassLoaderResourceAccessor

use of liquibase.resource.ClassLoaderResourceAccessor in project liquibase by liquibase.

the class ConvertCommand method run.

@Override
protected CommandResult run() throws Exception {
    List<ResourceAccessor> openers = new ArrayList<ResourceAccessor>();
    openers.add(new FileSystemResourceAccessor());
    openers.add(new ClassLoaderResourceAccessor());
    if (classpath != null) {
        openers.add(new FileSystemResourceAccessor(classpath));
    }
    ResourceAccessor resourceAccessor = new CompositeResourceAccessor(openers);
    ChangeLogParser sourceParser = ChangeLogParserFactory.getInstance().getParser(src, resourceAccessor);
    ChangeLogSerializer outSerializer = ChangeLogSerializerFactory.getInstance().getSerializer(out);
    DatabaseChangeLog changeLog = sourceParser.parse(src, new ChangeLogParameters(), resourceAccessor);
    File outFile = new File(out);
    if (!outFile.exists()) {
        outFile.getParentFile().mkdirs();
    }
    FileOutputStream outputStream = new FileOutputStream(outFile);
    try {
        outSerializer.write(changeLog.getChangeSets(), outputStream);
    } finally {
        outputStream.flush();
        outputStream.close();
    }
    return new CommandResult("Converted successfully");
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ArrayList(java.util.ArrayList) ChangeLogSerializer(liquibase.serializer.ChangeLogSerializer) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) CommandResult(liquibase.command.CommandResult) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) ChangeLogParser(liquibase.parser.ChangeLogParser) FileOutputStream(java.io.FileOutputStream) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) File(java.io.File)

Example 2 with ClassLoaderResourceAccessor

use of liquibase.resource.ClassLoaderResourceAccessor in project liquibase by liquibase.

the class DatabaseType method createDatabase.

public Database createDatabase(ClassLoader classLoader) {
    logParameters();
    validateParameters();
    try {
        DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
        if (databaseClass != null) {
            Database databaseInstance = (Database) ClasspathUtils.newInstance(databaseClass, classLoader, Database.class);
            databaseFactory.register(databaseInstance);
        }
        DatabaseConnection jdbcConnection;
        if (getUrl().startsWith("offline:")) {
            jdbcConnection = new OfflineConnection(getUrl(), new ClassLoaderResourceAccessor(classLoader));
        } else {
            Driver driver = (Driver) ClasspathUtils.newInstance(getDriver(), classLoader, Driver.class);
            if (driver == null) {
                throw new BuildException("Unable to create Liquibase Database instance. Could not instantiate the JDBC driver.");
            }
            Properties connectionProps = new Properties();
            String user = getUser();
            if (user != null && !user.isEmpty()) {
                connectionProps.setProperty(USER, user);
            }
            String password = getPassword();
            if (password != null && !password.isEmpty()) {
                connectionProps.setProperty(PASSWORD, password);
            }
            ConnectionProperties connectionProperties = getConnectionProperties();
            if (connectionProperties != null) {
                connectionProps.putAll(connectionProperties.buildProperties());
            }
            Connection connection = driver.connect(getUrl(), connectionProps);
            if (connection == null) {
                throw new BuildException("Unable to create Liquibase Database instance. Could not connect to the database.");
            }
            jdbcConnection = new JdbcConnection(connection);
        }
        Database database = databaseFactory.findCorrectDatabaseImplementation(jdbcConnection);
        String schemaName = getDefaultSchemaName();
        if (schemaName != null) {
            database.setDefaultSchemaName(schemaName);
        }
        String catalogName = getDefaultCatalogName();
        if (catalogName != null) {
            database.setDefaultCatalogName(catalogName);
        }
        String currentDateTimeFunction = getCurrentDateTimeFunction();
        if (currentDateTimeFunction != null) {
            database.setCurrentDateTimeFunction(currentDateTimeFunction);
        }
        database.setOutputDefaultSchema(isOutputDefaultSchema());
        database.setOutputDefaultCatalog(isOutputDefaultCatalog());
        String liquibaseSchemaName = getLiquibaseSchemaName();
        if (liquibaseSchemaName != null) {
            database.setLiquibaseSchemaName(liquibaseSchemaName);
        }
        String liquibaseCatalogName = getLiquibaseCatalogName();
        if (liquibaseCatalogName != null) {
            database.setLiquibaseCatalogName(liquibaseCatalogName);
        }
        String databaseChangeLogTableName = getDatabaseChangeLogTableName();
        if (databaseChangeLogTableName != null) {
            database.setDatabaseChangeLogTableName(databaseChangeLogTableName);
        }
        String databaseChangeLogLockTableName = getDatabaseChangeLogLockTableName();
        if (databaseChangeLogLockTableName != null) {
            database.setDatabaseChangeLogLockTableName(databaseChangeLogLockTableName);
        }
        String liquibaseTablespaceName = getLiquibaseTablespaceName();
        if (liquibaseTablespaceName != null) {
            database.setLiquibaseTablespaceName(liquibaseTablespaceName);
        }
        return database;
    } catch (SQLException e) {
        throw new BuildException("Unable to create Liquibase database instance. A JDBC error occurred. " + e.toString(), e);
    } catch (DatabaseException e) {
        throw new BuildException("Unable to create Liquibase database instance. " + e.toString(), e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Driver(java.sql.Driver) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) Properties(java.util.Properties) DatabaseFactory(liquibase.database.DatabaseFactory) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) BuildException(org.apache.tools.ant.BuildException) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) DatabaseException(liquibase.exception.DatabaseException)

Example 3 with ClassLoaderResourceAccessor

use of liquibase.resource.ClassLoaderResourceAccessor in project liquibase by liquibase.

the class ExtServiceLocatorTest method setup.

@Before
public void setup() throws Exception {
    ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor();
    serviceLocator = ServiceLocator.getInstance();
    serviceLocator.setResourceAccessor(resourceAccessor);
}
Also used : ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) Before(org.junit.Before)

Example 4 with ClassLoaderResourceAccessor

use of liquibase.resource.ClassLoaderResourceAccessor in project openmrs-core by openmrs.

the class DatabaseUpgradeTestUtil method upgrade.

public void upgrade(String filename) throws IOException, SQLException {
    try {
        Liquibase liquibase = new Liquibase(filename, new ClassLoaderResourceAccessor(getClass().getClassLoader()), liqubaseConnection);
        liquibase.update(null);
        connection.commit();
    } catch (LiquibaseException e) {
        throw new IOException(e);
    }
}
Also used : Liquibase(liquibase.Liquibase) LiquibaseException(liquibase.exception.LiquibaseException) IOException(java.io.IOException) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor)

Example 5 with ClassLoaderResourceAccessor

use of liquibase.resource.ClassLoaderResourceAccessor in project microservice_framework by CJSCommonPlatform.

the class AbstractJdbcRepositoryIT method initDatabase.

private void initDatabase() throws Exception {
    Liquibase liquibase = new Liquibase(liquibaseLocation, new ClassLoaderResourceAccessor(), new JdbcConnection(dataSource.getConnection()));
    liquibase.dropAll();
    liquibase.update("");
}
Also used : Liquibase(liquibase.Liquibase) JdbcConnection(liquibase.database.jvm.JdbcConnection) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor)

Aggregations

ClassLoaderResourceAccessor (liquibase.resource.ClassLoaderResourceAccessor)38 Liquibase (liquibase.Liquibase)25 JdbcConnection (liquibase.database.jvm.JdbcConnection)20 SQLException (java.sql.SQLException)11 CompositeResourceAccessor (liquibase.resource.CompositeResourceAccessor)9 Database (liquibase.database.Database)8 LiquibaseException (liquibase.exception.LiquibaseException)8 Connection (java.sql.Connection)7 DatabaseException (liquibase.exception.DatabaseException)7 FileSystemResourceAccessor (liquibase.resource.FileSystemResourceAccessor)7 ResourceAccessor (liquibase.resource.ResourceAccessor)7 File (java.io.File)6 ArrayList (java.util.ArrayList)3 Contexts (liquibase.Contexts)3 FileOutputStream (java.io.FileOutputStream)2 DataSource (javax.sql.DataSource)2 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)2 ChangeSet (liquibase.changelog.ChangeSet)2 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)2 DatabaseConnection (liquibase.database.DatabaseConnection)2