Search in sources :

Example 16 with ClassLoaderResourceAccessor

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

the class XMLChangeLogSerializerTest method createNode_SQLFileChange.

@Test
public void createNode_SQLFileChange() throws Exception {
    String fileName = "liquibase/change/core/SQLFileTestData.sql";
    SQLFileChange change = new SQLFileChange();
    ClassLoaderResourceAccessor opener = new ClassLoaderResourceAccessor();
    change.setPath(fileName);
    Element element = new XMLChangeLogSerializer(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()).createNode(change);
    assertEquals("sqlFile", element.getTagName());
    assertEquals(fileName, element.getAttribute("path"));
}
Also used : Element(org.w3c.dom.Element) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) Test(org.junit.Test)

Example 17 with ClassLoaderResourceAccessor

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

the class ConvertCommandStep method run.

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
    CommandScope commandScope = resultsBuilder.getCommandScope();
    String src = commandScope.getArgumentValue(SRC_ARG);
    String out = commandScope.getArgumentValue(OUT_ARG);
    String classpath = commandScope.getArgumentValue(CLASSPATH_ARG);
    List<ResourceAccessor> openers = new ArrayList<>();
    openers.add(new FileSystemResourceAccessor());
    openers.add(new ClassLoaderResourceAccessor());
    if (classpath != null) {
        openers.add(new FileSystemResourceAccessor(new File(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();
    }
    try (FileOutputStream outputStream = new FileOutputStream(outFile)) {
        outSerializer.write(changeLog.getChangeSets(), outputStream);
    }
    Scope.getCurrentScope().getUI().sendMessage("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) 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 18 with ClassLoaderResourceAccessor

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

the class OfflineChangeLogHistoryServiceTest method createService.

/**
 * Create OfflineChangeLogHistoryService and register LoggingExecutor
 */
private OfflineChangeLogHistoryService createService(Writer writer, String outputLiquibaseSql) {
    HsqlDatabase database = new HsqlDatabase();
    File changeLogCsvFile = new File(temporaryFolder.getRoot(), CHANGE_LOG_CSV);
    OfflineConnection connection = new OfflineConnection("offline:hsqldb?changeLogFile=" + changeLogCsvFile.getAbsolutePath() + "&outputLiquibaseSql=" + outputLiquibaseSql, new ClassLoaderResourceAccessor());
    database.setConnection(connection);
    connection.attached(database);
    OfflineChangeLogHistoryService changeLogHistoryService = (OfflineChangeLogHistoryService) ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
    // 
    // Create the new LoggingExecutor and give it the original Executor as a delegator
    // We also set the LoggingExecutor as the JDBC Executor
    // 
    LoggingExecutor loggingExecutor = new LoggingExecutor(Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database), writer, database);
    Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("logging", database, loggingExecutor);
    Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("jdbc", database, loggingExecutor);
    return changeLogHistoryService;
}
Also used : HsqlDatabase(liquibase.database.core.HsqlDatabase) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService) OfflineConnection(liquibase.database.OfflineConnection) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) File(java.io.File)

Example 19 with ClassLoaderResourceAccessor

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

the class DatabaseMigrator method migrate.

public void migrate(Connection connection) throws SQLException {
    try {
        log.info("Upgrading database, this might take a while depending on the size of the database.");
        List<String> messages = Arrays.asList(repeat("*", "", 72), "WARNING: Shutting down your server at this point will lead to a database corruption. Please wait until the database upgrade completes.", repeat("*", "", 72));
        for (String message : messages) {
            System.err.println(message);
            log.info(message);
        }
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
        Liquibase liquibase = new Liquibase("db-migration-scripts/liquibase.xml", new ClassLoaderResourceAccessor(getClass().getClassLoader()), database);
        liquibase.update(new Contexts());
        System.err.println("INFO: Database upgrade completed successfully.");
        log.info("Database upgrade completed successfully.");
        DataMigrationRunner.run(connection);
    } catch (LiquibaseException e) {
        String message = "Unable to create database upgrade script for database. The problem was: " + e.getMessage();
        if (e.getCause() != null) {
            message += ". The cause was: " + e.getCause().getMessage();
        }
        log.error(message, e);
        System.err.println(message);
        e.printStackTrace(System.err);
        throw new SQLException("Unable to migrate the database", e);
    }
}
Also used : Liquibase(liquibase.Liquibase) SQLException(java.sql.SQLException) Database(liquibase.database.Database) JdbcConnection(liquibase.database.jvm.JdbcConnection) LiquibaseException(liquibase.exception.LiquibaseException) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) Contexts(liquibase.Contexts)

Example 20 with ClassLoaderResourceAccessor

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

the class ServiceLocatorTest method setup.

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

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