Search in sources :

Example 1 with Authorized

use of org.openmrs.annotation.Authorized in project openmrs-core by openmrs.

the class DatabaseUpdater method getDatabaseChanges.

/**
 * Looks at the current liquibase-update-to-latest
 * .xml file and then checks the database to see
 * if they have been run.
 *
 * @return list of changesets that both have and haven't been run
 */
@Authorized(PrivilegeConstants.GET_DATABASE_CHANGES)
public static List<OpenMRSChangeSet> getDatabaseChanges() throws Exception {
    Database database = null;
    try {
        Liquibase liquibase = getLiquibase(CHANGE_LOG_FILE, null);
        database = liquibase.getDatabase();
        DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse(CHANGE_LOG_FILE, new ChangeLogParameters(), liquibase.getFileOpener());
        List<ChangeSet> changeSets = changeLog.getChangeSets();
        List<OpenMRSChangeSet> results = new ArrayList<>();
        for (ChangeSet changeSet : changeSets) {
            OpenMRSChangeSet omrschangeset = new OpenMRSChangeSet(changeSet, database);
            results.add(omrschangeset);
        }
        return results;
    } finally {
        try {
            if (database != null) {
                database.getConnection().close();
            }
        } catch (Exception e) {
        // pass
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) Database(liquibase.database.Database) ArrayList(java.util.ArrayList) XMLChangeLogSAXParser(liquibase.parser.core.xml.XMLChangeLogSAXParser) ChangeSet(liquibase.changelog.ChangeSet) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) LockException(liquibase.exception.LockException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LiquibaseException(liquibase.exception.LiquibaseException) Authorized(org.openmrs.annotation.Authorized)

Example 2 with Authorized

use of org.openmrs.annotation.Authorized in project openmrs-core by openmrs.

the class DatabaseUpdater method getUnrunDatabaseChanges.

/**
 * Looks at the specified liquibase change log files and returns all changesets in the files
 * that have not been run on the database yet. If no argument is specified, then it looks at the
 * current liquibase-update-to-latest.xml file
 *
 * @param changeLogFilenames the filenames of all files to search for unrun changesets
 * @return list of change sets
 */
@Authorized(PrivilegeConstants.GET_DATABASE_CHANGES)
public static List<OpenMRSChangeSet> getUnrunDatabaseChanges(String... changeLogFilenames) {
    log.debug("Getting unrun changesets");
    Database database = null;
    try {
        if (changeLogFilenames == null) {
            throw new IllegalArgumentException("changeLogFilenames cannot be null");
        }
        // if no argument, look ONLY in liquibase-update-to-latest.xml
        if (changeLogFilenames.length == 0) {
            changeLogFilenames = new String[] { CHANGE_LOG_FILE };
        }
        List<OpenMRSChangeSet> results = new ArrayList<>();
        for (String changelogFile : changeLogFilenames) {
            Liquibase liquibase = getLiquibase(changelogFile, null);
            database = liquibase.getDatabase();
            List<ChangeSet> changeSets = liquibase.listUnrunChangeSets(CONTEXT);
            for (ChangeSet changeSet : changeSets) {
                OpenMRSChangeSet omrschangeset = new OpenMRSChangeSet(changeSet, database);
                results.add(omrschangeset);
            }
        }
        return results;
    } catch (Exception e) {
        throw new RuntimeException("Error occurred while trying to get the updates needed for the database. " + e.getMessage(), e);
    } finally {
        try {
            database.getConnection().close();
        } catch (Exception e) {
        // pass
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) ArrayList(java.util.ArrayList) ChangeSet(liquibase.changelog.ChangeSet) LockException(liquibase.exception.LockException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LiquibaseException(liquibase.exception.LiquibaseException) Authorized(org.openmrs.annotation.Authorized)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Liquibase (liquibase.Liquibase)2 ChangeSet (liquibase.changelog.ChangeSet)2 Database (liquibase.database.Database)2 LiquibaseException (liquibase.exception.LiquibaseException)2 LockException (liquibase.exception.LockException)2 Authorized (org.openmrs.annotation.Authorized)2 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)1 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)1 XMLChangeLogSAXParser (liquibase.parser.core.xml.XMLChangeLogSAXParser)1