CloseableLiquibaseWithFileSystemMigrationsFile( ManagedDataSource dataSource, Database database, String file ) throws LiquibaseException, SQLException { super(file, new FileSystemResourceAccessor(), database, dataSource); }
java.sql.Connection c = YOUR_CONNECTION; Liquibase liquibase = null; try { Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(c)) liquibase = new Liquibase(YOUR_CHANGELOG, new FileSystemResourceAccessor(), database); liquibase.update(); } catch (SQLException e) { throw new DatabaseException(e); } finally { if (c != null) { try { c.rollback(); c.close(); } catch (SQLException e) { //nothing to do } } }
private void initDatabaseSchema() throws SQLException, LiquibaseException { if (config.hasKey("database.changelog")) { ResourceAccessor resourceAccessor = new FileSystemResourceAccessor(); Database database = DatabaseFactory.getInstance().openDatabase( config.getString("database.url"), config.getString("database.user"), config.getString("database.password"), config.getString("database.driver"), null, null, null, resourceAccessor); Liquibase liquibase = new Liquibase( config.getString("database.changelog"), resourceAccessor, database); liquibase.clearCheckSums(); liquibase.update(new Contexts()); } }
ResourceAccessor fsFO = new FileSystemResourceAccessor();
/** * API method. */ public ResourceAccessor createResourceAccessor() { return new FileSystemResourceAccessor(ROOT_PATH); }
CloseableLiquibaseWithFileSystemMigrationsFile( ManagedDataSource dataSource, Database database, String file ) throws LiquibaseException, SQLException { super(file, new FileSystemResourceAccessor(), database, dataSource); }
@Before public void createTestData() throws Exception { Handle handle = dbiProvider.get().open(); Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(handle.getConnection())); Liquibase liquibase = new Liquibase("singularity_test.sql", new FileSystemResourceAccessor(), database); liquibase.update((String) null); try { database.close(); } catch (Throwable t) { } handle.close(); }
/** * Interface used for callbacks when updating the database. Implement this interface and pass it * to {@link DatabaseUpdater#executeChangelog(String, Map, ChangeSetExecutorCallback)} */ public interface ChangeSetExecutorCallback { /** * This method is called after each changeset is executed. * * @param changeSet the liquibase changeset that was just run * @param numChangeSetsToRun the total number of changesets in the current file */ public void executing(ChangeSet changeSet, int numChangeSetsToRun); }
Liquibase liquibase = new liquibase.Liquibase(log, new FileSystemResourceAccessor(), database);
ResourceAccessor resourceAccessor = new FileSystemResourceAccessor(); Database db = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(yourJdbcConnection)); Liquibase liquibase = new Liquibase("data/filename", resourceAccessor, db); try (Writer writer = new OutputStreamWriter(System.out)) { liquibase.update((Contexts) null, writer); }
@Override protected ResourceAccessor getFileOpener(ClassLoader cl) { List<ResourceAccessor> resourceAccessors = new ArrayList<ResourceAccessor>(); resourceAccessors.add(new MavenResourceAccessor(cl)); resourceAccessors.add(new FileSystemResourceAccessor(project.getBasedir().getAbsolutePath())); if (changeLogDirectory != null) { calculateChangeLogDirectoryAbsolutePath(); resourceAccessors.add(new FileSystemResourceAccessor(changeLogDirectory)); } return new CompositeResourceAccessor(resourceAccessors); }
protected ResourceAccessor getFileOpener(ClassLoader cl) { ResourceAccessor mFO = new MavenResourceAccessor(cl); ResourceAccessor fsFO = new FileSystemResourceAccessor(project.getBasedir().getAbsolutePath()); return new CompositeResourceAccessor(mFO, fsFO); }
private static void executeMasters(Connection connection, Optional<String> schema, File changelogTempDirectory, String preMaster) throws LiquibaseException { List<File> masterChangelogs = Arrays.asList(changelogTempDirectory.listFiles((FilenameFilter) (dir, name) -> name.endsWith(preMaster))); LOG.info("\tMaster Liquibase files found: {}", masterChangelogs.size()); LOG.trace("\tSorting master Liquibase files found."); masterChangelogs.sort((f1, f2) -> f1.getAbsolutePath().compareTo(f2.getAbsolutePath())); for (File masterChangelog : masterChangelogs) { LOG.info("\t\tExcuting liquibase script: {}", masterChangelog.getAbsolutePath()); Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection)); if (schema.isPresent()) { database.setDefaultSchemaName(schema.get()); } Liquibase liquibase = new Liquibase(masterChangelog.getAbsolutePath(), new FileSystemResourceAccessor(), database); liquibase.update(null); LOG.debug("\t\tExcuted liquibase script: {}", masterChangelog.getAbsolutePath()); } }
private static void executeMasters(Connection connection, Optional<String> schema, File changelogTempDirectory, String preMaster) throws LiquibaseException { List<File> masterChangelogs = Arrays.asList(changelogTempDirectory.listFiles((FilenameFilter) (dir, name) -> name.endsWith(preMaster))); LOG.info("\tMaster Liquibase files found: {}", masterChangelogs.size()); LOG.trace("\tSorting master Liquibase files found."); masterChangelogs.sort((f1, f2) -> f1.getAbsolutePath().compareTo(f2.getAbsolutePath())); for (File masterChangelog : masterChangelogs) { LOG.info("\t\tExcuting liquibase script: {}", masterChangelog.getAbsolutePath()); Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection)); if (schema.isPresent()) { database.setDefaultSchemaName(schema.get()); } Liquibase liquibase = new Liquibase(masterChangelog.getAbsolutePath(), new FileSystemResourceAccessor(), database); liquibase.update(null); LOG.debug("\t\tExcuted liquibase script: {}", masterChangelog.getAbsolutePath()); } }
public void migrate(String dbName) { try { Configuration configuration = yamlConfiguration.subset(dbName + ".Hibernate"); Properties properties = new Properties(); properties.put("user", configuration.getProperty("hibernate.connection.username")); properties.put("password", configuration.getProperty("hibernate.connection.password")); Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = (String) configuration.getProperty("hibernate.connection.url"); java.sql.Connection connection = DriverManager.getConnection(url, properties); Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection)); ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource(dbName + "/migrations.xml").getFile()); Liquibase liquibase = new Liquibase(file.getCanonicalPath(), new FileSystemResourceAccessor(), database); liquibase.update(new Contexts()); } catch (Exception e) { System.err.println("Unable to perform database migration."); e.printStackTrace(); } } }
public static void runChangeLog(DataSource dataSource, String mainChangelogFile) { try (Connection connection = dataSource.getConnection()) { JdbcConnection jdbcConnection = new JdbcConnection(connection); Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection); ClassLoader classLoader = LiquibaseChangelogRunnerUtils.class.getClassLoader(); if (classLoader == null) { throw new RuntimeException("Could not get a classloader"); } ResourceAccessor classLoaderResourceAccessor = new ClassLoaderResourceAccessor(classLoader); ResourceAccessor fileSystemResourceAccessor = new FileSystemResourceAccessor(); CompositeResourceAccessor resourceAccessor = new CompositeResourceAccessor(classLoaderResourceAccessor, fileSystemResourceAccessor); Liquibase liquibase = new Liquibase(mainChangelogFile, resourceAccessor, database); liquibase.update(""); } catch (SQLException e) { throw new RuntimeException("SQL error", e); } catch (DatabaseException e) { throw new RuntimeException("Database error", e); } catch (LiquibaseException e) { throw new RuntimeException("Liquibase error", e); } } }
void diff(Connection referenceConnection, Connection targetConnection) throws LiquibaseException, IOException, ParserConfigurationException { Liquibase liquibase = null; try { Database referenceDatabase = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(referenceConnection)); Database targetDatabase = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(targetConnection)); liquibase = new Liquibase("", new FileSystemResourceAccessor(), referenceDatabase); DiffResult diffResult = liquibase.diff(referenceDatabase, targetDatabase, new CompareControl()); new DiffToChangeLog(diffResult, new DiffOutputControl()).print(System.out); } finally { if (liquibase != null) { liquibase.forceReleaseLocks(); } } }
protected void runLiquibase(Database database) { logger.info("Updating database definitions"); ResourceAccessor resourceAccessor = new FileSystemResourceAccessor(appDir.getAbsolutePath()); ConnectionProvider connectionProvider = database.getConnectionProvider();
@Before public void createTestData() throws Exception { Handle handle = dbiProvider.get().open(); Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(handle.getConnection())); Liquibase liquibase = new Liquibase("singularity_test.sql", new FileSystemResourceAccessor(), database); liquibase.update((String) null); try { database.close(); } catch (Throwable t) { } handle.close(); }
/** * Setup hsql db. * * @param dbName the db name * @param path the path * @param changeLogPath the change log path * @throws SQLException the SQL exception * @throws LiquibaseException the liquibase exception */ private void setupHsqlDb(String dbName, String path, String changeLogPath) throws SQLException, LiquibaseException { Server server = new Server(); server.setLogWriter(new PrintWriter(System.out)); server.setErrWriter(new PrintWriter(System.out)); server.setSilent(true); server.setDatabaseName(0, dbName); server.setDatabasePath(0, "file:" + path); server.start(); BasicDataSource ds = UtilityMethods.getDataSourceFromConf(conf); Liquibase liquibase = new Liquibase(UserConfigLoader.class.getResource(changeLogPath).getFile(), new FileSystemResourceAccessor(), new HsqlConnection(ds.getConnection())); liquibase.update(""); }