Search in sources :

Example 11 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore in project xwiki-platform by xwiki.

the class HibernateDataMigrationManager method liquibaseUpdate.

/**
 * Run liquibase for a given set of change logs
 *
 * @param migrations the set of migration to visit
 * @param preHibernate if true, use pre-hibernate schema update changelogs.
 * @throws XWikiException
 * @throws DataMigrationException
 * @since 4.3
 */
private void liquibaseUpdate(Collection<XWikiMigration> migrations, boolean preHibernate) throws XWikiException, DataMigrationException {
    String liquibaseChangeLogs = getLiquibaseChangeLogs(migrations, preHibernate);
    if (liquibaseChangeLogs == null || liquibaseChangeLogs.length() == 0) {
        return;
    }
    final String database = getXWikiContext().getWikiId();
    if (this.logger.isInfoEnabled()) {
        if (preHibernate) {
            this.logger.info("Running early schema updates (using liquibase) for database [{}]", database);
        } else {
            this.logger.info("Running additional schema updates (using liquibase) for database [{}]", database);
        }
    }
    final StringBuilder changeLogs = new StringBuilder(10000);
    changeLogs.append(getLiquibaseChangeLogHeader());
    changeLogs.append(liquibaseChangeLogs);
    changeLogs.append(getLiquibaseChangeLogFooter());
    final XWikiHibernateBaseStore store = getStore();
    store.executeRead(getXWikiContext(), new HibernateCallback<Object>() {

        @Override
        public Object doInHibernate(Session session) throws XWikiException {
            Liquibase lb;
            try {
                Database lbDatabase = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(session.connection()));
                // Precise the schema name to liquibase, since it does not usually determine it
                // properly (See XWIKI-8813).
                lbDatabase.setDefaultSchemaName(store.getSchemaFromWikiName(getXWikiContext()));
                lb = new Liquibase(MigrationResourceAccessor.CHANGELOG_NAME, new MigrationResourceAccessor(changeLogs.toString()), lbDatabase);
            } catch (LiquibaseException e) {
                throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_MIGRATION, String.format("Unable to launch liquibase for database %s, schema update failed.", database), e);
            }
            try {
                lb.update(null);
            } catch (LiquibaseException e) {
                throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_MIGRATION, String.format("Unable to update schema of database %s.", database), e);
            }
            return null;
        }
    });
}
Also used : JdbcConnection(liquibase.database.jvm.JdbcConnection) Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) LiquibaseException(liquibase.exception.LiquibaseException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 12 with XWikiHibernateBaseStore

use of com.xpn.xwiki.store.XWikiHibernateBaseStore in project xwiki-platform by xwiki.

the class HibernateDataMigrationManager method startMigrations.

@Override
protected void startMigrations() throws DataMigrationException {
    XWikiContext context = getXWikiContext();
    XWikiHibernateBaseStore store = getStore();
    Session originalSession = store.getSession(context);
    Transaction originalTransaction = store.getTransaction(context);
    store.setSession(null, context);
    store.setTransaction(null, context);
    try {
        super.startMigrations();
    } finally {
        store.setSession(originalSession, context);
        store.setTransaction(originalTransaction, context);
    }
}
Also used : Transaction(org.hibernate.Transaction) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) Session(org.hibernate.Session)

Aggregations

XWikiHibernateBaseStore (com.xpn.xwiki.store.XWikiHibernateBaseStore)12 XWikiContext (com.xpn.xwiki.XWikiContext)10 Session (org.hibernate.Session)9 XWikiException (com.xpn.xwiki.XWikiException)7 HibernateException (org.hibernate.HibernateException)5 MailStoreException (org.xwiki.mail.MailStoreException)4 List (java.util.List)2 Query (org.hibernate.Query)2 Transaction (org.hibernate.Transaction)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 XWikiCacheStoreInterface (com.xpn.xwiki.store.XWikiCacheStoreInterface)1 HibernateCallback (com.xpn.xwiki.store.XWikiHibernateBaseStore.HibernateCallback)1 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 DataMigrationException (com.xpn.xwiki.store.migration.DataMigrationException)1 XWikiDBVersion (com.xpn.xwiki.store.migration.XWikiDBVersion)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Liquibase (liquibase.Liquibase)1