private void runOnAllDataSources() throws LiquibaseException { Iterator i$ = this.dataSources.iterator(); while(i$.hasNext()) { DataSource aDataSource = (DataSource)i$.next(); this.log.info("Initializing Liquibase for data source " + aDataSource); CustomSpringLiquibase liquibase = this.getSpringLiquibase(aDataSource); liquibase.afterPropertiesSet(); this.log.info("Liquibase ran for data source " + aDataSource); } }
private void runOnAllSchemas() throws LiquibaseException { Iterator i$ = this.schemas.iterator(); while(i$.hasNext()) { String schema = (String)i$.next(); if (StringUtils.isEmpty(schema)) continue; if (schema.equals("default")) { schema = null; } this.log.info("Initializing Liquibase for schema " + schema); CustomSpringLiquibase liquibase = this.getSpringLiquibase(this.dataSource); liquibase.setDefaultSchema(schema); liquibase.afterPropertiesSet(); this.log.info("Liquibase ran for schema " + schema); } }
log.info(LogType.LOG, String.format("[id = %s] JVM lock acquired, acquiring file lock", id)); String lockPath = String.format("%s/schema.liquibase.lock", ROOT_PATH); log.info(LogType.LOG, String.format("[id = %s] Created lock file [path='%s'].", id, lockPath)); log.info(LogType.LOG, String.format("[id = %s] Trying to acquire the file lock [file='%s']...", id, lockPath)); log.info(LogType.LOG, String.format("[id = %s] File lock acquired, running liquibase...", id)); actionResult = action.call(); lock.release();
private String copyToFile(final String id, final String liquibase, final String schema) { log.info(LogType.LOG, String.format("[id = %s] copyToFile(%s, %s)", id, liquibase, schema)); is = this.getClass().getClassLoader().getResourceAsStream(schema); log.info(LogType.LOG, String.format("[id = %s] Transferring schema [resource=%s] to directory [path=%s]...", id, schema, liquibase)); String path = schema.startsWith("/") ? schema.substring(1) : schema; log.debug(LogType.LOG, String.format("[id = %s] LiquibaseSchema path is [path='%s'].", id, path)); log.info(LogType.LOG, String.format("[id = %s] Directories for [path='%s'] file created.", id, file.getAbsolutePath())); log.info(LogType.LOG, String.format("[id = %s] LiquibaseSchema file [path='%s'] already exists, deleting...", id, file.getAbsolutePath())); if (file.delete()) { log.info(LogType.LOG, String.format("[id = %s] File [path='%s'] deleted.", id, file.getAbsolutePath())); log.info(LogType.LOG, String.format("[id = %s] File [path='%s'] created.", id, file.getAbsolutePath())); log.info(LogType.LOG, String.format("[id = %s] Data copied, schema path is [path='%s'].", id, schemaPath)); return schemaPath; } catch (IOException e) {
@Override public void executeStatements(Change change, DatabaseChangeLog changeLog, List<SqlVisitor> sqlVisitors) throws LiquibaseException { if (getConnection() != null) { // don't log if running offline Logger log = LogFactory.getInstance().getLog(); log.info("Executing " + change.getClass().getSimpleName()); } super.executeStatements(change, changeLog, sqlVisitors); } }
logger.info("Can not use " + clazz + " as a Liquibase service because it does not have a no-argument constructor"); } catch (NoClassDefFoundError e) { + e.getMessage().replace("/", ".") + " is not in the classpath"; if (e.getMessage().startsWith("org/yaml/snakeyaml")) { logger.info(message); } else { logger.warning(message);
public void afterPropertiesSet() throws Exception { if (this.dataSource == null && this.schemas == null) { this.log.info("DataSources based multitenancy enabled"); this.resolveDataSources(); this.runOnAllDataSources(); } else { if (this.dataSource == null && this.schemas != null) { throw new LiquibaseException("When schemas are defined you should also define a base dataSource"); } if (this.dataSource != null) { this.log.info("Schema based multitenancy enabled"); if (this.schemas == null || this.schemas.isEmpty()) { this.log.warning("Schemas not defined, using defaultSchema only"); this.schemas = new ArrayList(); this.schemas.add(this.defaultSchema); } this.runOnAllSchemas(); } } }
private void resolveDataSources() throws NamingException { Context context = new InitialContext(); int lastIndexOf = this.jndiBase.lastIndexOf("/"); String jndiRoot = this.jndiBase.substring(0, lastIndexOf); String jndiParent = this.jndiBase.substring(lastIndexOf + 1); Context base = (Context)context.lookup(jndiRoot); NamingEnumeration list = base.list(jndiParent); while(list.hasMoreElements()) { NameClassPair entry = (NameClassPair)list.nextElement(); String name = entry.getName(); String jndiUrl; if (entry.isRelative()) { jndiUrl = this.jndiBase + "/" + name; } else { jndiUrl = name; } Object lookup = context.lookup(jndiUrl); if (lookup instanceof DataSource) { this.dataSources.add((DataSource)lookup); this.log.debug("Added a data source at " + jndiUrl); } else { this.log.info("Skipping a resource " + jndiUrl + " not compatible with DataSource."); } } }
protected EntityManagerFactory createEntityManagerFactory() { DefaultPersistenceUnitManager internalPersistenceUnitManager = new DefaultPersistenceUnitManager(); String[] packagesToScan = getHibernateConnection().getPath().split(","); for (String packageName : packagesToScan) { LOG.info("Found package " + packageName); } internalPersistenceUnitManager.setPackagesToScan(packagesToScan); internalPersistenceUnitManager.preparePersistenceUnitInfos(); PersistenceUnitInfo persistenceUnitInfo = internalPersistenceUnitManager.obtainDefaultPersistenceUnitInfo(); HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); if (persistenceUnitInfo instanceof SmartPersistenceUnitInfo) { ((SmartPersistenceUnitInfo) persistenceUnitInfo).setPersistenceProviderPackageName(jpaVendorAdapter.getPersistenceProviderRootPackage()); } Map<String, String> map = new HashMap<>(); map.put(AvailableSettings.DIALECT, getProperty(AvailableSettings.DIALECT)); map.put(AvailableSettings.USE_SECOND_LEVEL_CACHE, Boolean.FALSE.toString()); map.put(AvailableSettings.PHYSICAL_NAMING_STRATEGY, getHibernateConnection().getProperties().getProperty(AvailableSettings.PHYSICAL_NAMING_STRATEGY)); map.put(AvailableSettings.IMPLICIT_NAMING_STRATEGY, getHibernateConnection().getProperties().getProperty(AvailableSettings.IMPLICIT_NAMING_STRATEGY)); EntityManagerFactoryBuilderImpl builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(persistenceUnitInfo, map); return builder.build(); }
@PostConstruct public void onStartup() { log.info(LogType.LOG, "Booting Liquibase " + LiquibaseUtil.getBuildVersion()); String hostName; try { hostName = NetUtil.getLocalHostName(); } catch (Exception e) { log.warning(LogType.LOG, "Cannot find hostname: " + e.getMessage()); log.debug(LogType.LOG, "", e); return; } LiquibaseConfiguration liquibaseConfiguration = LiquibaseConfiguration.getInstance(); if (!liquibaseConfiguration.getConfiguration(GlobalConfiguration.class).getShouldRun()) { log.info(LogType.LOG, String.format("Liquibase did not run on %s because %s was set to false.", hostName, liquibaseConfiguration.describeValueLookupLogic( GlobalConfiguration.class, GlobalConfiguration.SHOULD_RUN) )); return; } initialized = true; try { performUpdate(); } catch (LiquibaseException e) { throw new UnexpectedLiquibaseException(e); } }
if (annotatedClasses != null) { for (TypedStringValue className : annotatedClasses) { LOG.info("Found annotated class " + className.getValue()); sources.addAnnotatedClass(findClass(className.getValue())); ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); for (TypedStringValue mappingLocation : mappingLocations) { LOG.info("Found mappingLocation " + mappingLocation.getValue()); Resource[] resources = resourcePatternResolver.getResources(mappingLocation.getValue()); for (int i = 0; i < resources.length; i++) { URL url = resources[i].getURL(); LOG.info("Adding resource " + url); sources.addURL(url);
/** * Creates the base {@link MetadataSources} to use for this database. * Normally, the result of this method is passed through {@link #configureSources(MetadataSources)}. */ protected MetadataSources createMetadataSources() throws DatabaseException { String dialectString = findDialectName(); if (dialectString != null) { try { dialect = (Dialect) Class.forName(dialectString).newInstance(); LOG.info("Using dialect " + dialectString); } catch (Exception e) { throw new DatabaseException(e); } } else { LOG.info("Could not determine hibernate dialect, using HibernateGenericDialect"); dialect = new HibernateGenericDialect(); } ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder() .applySetting(AvailableSettings.DIALECT, dialect) .addService(ConnectionProvider.class, new NoOpConnectionProvider()) .addService(MultiTenantConnectionProvider.class, new NoOpConnectionProvider()) .build(); return new MetadataSources(standardRegistry); }
@Override public void setConnection(DatabaseConnection conn) { super.setConnection(conn); try { LOG.info("Reading hibernate configuration " + getConnection().getURL()); this.metadata = buildMetadata(); afterSetup(); } catch (DatabaseException e) { throw new UnexpectedLiquibaseException(e); } }
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(Index.class)) { return; } if (foundObject instanceof Table) { Table table = (Table) foundObject; org.hibernate.mapping.Table hibernateTable = findHibernateTable(table, snapshot); if (hibernateTable == null) { return; } Iterator indexIterator = hibernateTable.getIndexIterator(); while (indexIterator.hasNext()) { org.hibernate.mapping.Index hibernateIndex = (org.hibernate.mapping.Index) indexIterator.next(); Index index = new Index(); index.setTable(table); index.setName(hibernateIndex.getName()); Iterator columnIterator = hibernateIndex.getColumnIterator(); while (columnIterator.hasNext()) { org.hibernate.mapping.Column hibernateColumn = (org.hibernate.mapping.Column) columnIterator.next(); index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table)); } LOG.info("Found index " + index.getName()); table.getIndexes().add(index); } } }
@Override protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (example.getSnapshotId() != null) { return example; } Table table = ((Index) example).getTable(); org.hibernate.mapping.Table hibernateTable = findHibernateTable(table, snapshot); if (hibernateTable == null) { return example; } Iterator indexIterator = hibernateTable.getIndexIterator(); while (indexIterator.hasNext()) { org.hibernate.mapping.Index hibernateIndex = (org.hibernate.mapping.Index) indexIterator.next(); Index index = new Index(); index.setTable(table); index.setName(hibernateIndex.getName()); Iterator columnIterator = hibernateIndex.getColumnIterator(); while (columnIterator.hasNext()) { org.hibernate.mapping.Column hibernateColumn = (org.hibernate.mapping.Column) columnIterator.next(); index.getColumns().add(new Column(hibernateColumn.getName()).setRelation(table)); } if (index.getColumnNames().equalsIgnoreCase(((Index) example).getColumnNames())) { LOG.info("Found index " + index.getName()); table.getIndexes().add(index); return index; } } return example; }
@Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) { return; } if (foundObject instanceof Schema) { Schema schema = (Schema) foundObject; HibernateDatabase database = (HibernateDatabase) snapshot.getDatabase(); Configuration cfg = database.getConfiguration(); Iterator<org.hibernate.mapping.Table> tableMappings = cfg.getTableMappings(); while (tableMappings.hasNext()) { org.hibernate.mapping.Table hibernateTable = tableMappings.next(); if (hibernateTable.isPhysicalTable()) { Table table = new Table().setName(hibernateTable.getName()); table.setSchema(schema); LOG.info("Found table " + table.getName()); schema.addDatabaseObject(table); } } } } }
@Override protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (example.getSnapshotId() != null) { return example; } org.hibernate.mapping.Table hibernateTable = findHibernateTable(example, snapshot); if (hibernateTable == null) { return example; } Table table = new Table().setName(hibernateTable.getName()); LOG.info("Found table " + table.getName()); // table.setSnapshotId(SnapshotIdService.getInstance().generateId()); table.setSchema(example.getSchema()); return table; }
public void afterPropertiesSet() throws LiquibaseException { ConfigurationProperty shouldRunProperty = LiquibaseConfiguration.getInstance().getProperty(GlobalConfiguration.class, "shouldRun"); if (!((Boolean)shouldRunProperty.getValue(Boolean.class)).booleanValue()) { LogFactory.getLogger().info("Liquibase did not run because " + LiquibaseConfiguration.getInstance().describeValueLookupLogic(shouldRunProperty) + " was set to false"); } else if (!this.shouldRun) { LogFactory.getLogger().info("Liquibase did not run because 'shouldRun' property was set to false on " + this.getBeanName() + " Liquibase Spring bean."); } else { Connection c = null;
LOG.info("Found primary key " + pk.getName()); table.setPrimaryKey(pk); Index index = new Index();