Search in sources :

Example 1 with LockException

use of liquibase.exception.LockException in project liquibase by liquibase.

the class Liquibase method tag.

/**
     * 'Tags' the database for future rollback
     */
public void tag(String tagString) throws LiquibaseException {
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    try {
        ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).generateDeploymentId();
        checkLiquibaseTables(false, null, new Contexts(), new LabelExpression());
        getDatabase().tag(tagString);
    } finally {
        try {
            lockService.releaseLock();
        } catch (LockException e) {
            log.severe("Could not release lock", e);
        }
    }
}
Also used : LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException)

Example 2 with LockException

use of liquibase.exception.LockException in project liquibase by liquibase.

the class Liquibase method changeLogSync.

public void changeLogSync(Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    try {
        DatabaseChangeLog changeLog = getDatabaseChangeLog();
        checkLiquibaseTables(true, changeLog, contexts, labelExpression);
        ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).generateDeploymentId();
        changeLog.validate(database, contexts, labelExpression);
        ChangeLogIterator logIterator = new ChangeLogIterator(changeLog, new NotRanChangeSetFilter(database.getRanChangeSetList()), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database));
        logIterator.run(new ChangeLogSyncVisitor(database, changeLogSyncListener), new RuntimeEnvironment(database, contexts, labelExpression));
    } finally {
        try {
            lockService.releaseLock();
        } catch (LockException e) {
            log.severe("Could not release lock", e);
        }
        resetServices();
    }
}
Also used : LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException)

Example 3 with LockException

use of liquibase.exception.LockException in project liquibase by liquibase.

the class Liquibase method generateDocumentation.

public void generateDocumentation(String outputDirectory, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    log.info("Generating Database Documentation");
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    try {
        DatabaseChangeLog changeLog = getDatabaseChangeLog();
        checkLiquibaseTables(false, changeLog, new Contexts(), new LabelExpression());
        changeLog.validate(database, contexts, labelExpression);
        ChangeLogIterator logIterator = new ChangeLogIterator(changeLog, new DbmsChangeSetFilter(database));
        DBDocVisitor visitor = new DBDocVisitor(database);
        logIterator.run(visitor, new RuntimeEnvironment(database, contexts, labelExpression));
        visitor.writeHTML(new File(outputDirectory), resourceAccessor);
    } catch (IOException e) {
        throw new LiquibaseException(e);
    } finally {
        try {
            lockService.releaseLock();
        } catch (LockException e) {
            log.severe("Could not release lock", e);
        }
    }
//        try {
//            if (!LockService.getExecutor(database).waitForLock()) {
//                return;
//            }
//
//            DBDocChangeLogHandler changeLogHandler = new DBDocChangeLogHandler(outputDirectory, this, changeLogFile,resourceAccessor);
//            runChangeLogs(changeLogHandler);
//
//            changeLogHandler.writeHTML(this);
//        } finally {
//            releaseLock();
//        }
}
Also used : LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 4 with LockException

use of liquibase.exception.LockException in project openmrs-core by openmrs.

the class DatabaseUpdater method releaseDatabaseLock.

/**
 * This method releases the liquibase db lock after a crashed database update. First, it
 * checks whether "liquibasechangeloglock" table exists in db. If so, it will check
 * whether the database is locked. If that is also true, this means that last attempted db
 * update crashed.<br>
 * <br>
 * This should only be called if the user is sure that no one else is currently running
 * database updates. This method should be used if there was a db crash while updates
 * were being written and the lock table was never cleaned up.
 *
 * @throws LockException
 */
public static synchronized void releaseDatabaseLock() throws LockException {
    Database database = null;
    try {
        Liquibase liquibase = getLiquibase(null, null);
        database = liquibase.getDatabase();
        if (database.hasDatabaseChangeLogLockTable() && isLocked()) {
            LockService.getInstance(database).forceReleaseLock();
        }
    } catch (Exception e) {
        throw new LockException(e);
    } finally {
        try {
            database.getConnection().close();
        } catch (Exception e) {
        // pass
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) LockException(liquibase.exception.LockException) Database(liquibase.database.Database) LockException(liquibase.exception.LockException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 5 with LockException

use of liquibase.exception.LockException in project liquibase by liquibase.

the class HubUpdater method register.

/**
 * Automatically register the current user with Hub
 *
 * @param changeLogFile ChangeLog path for this operation
 * @throws LiquibaseException        Thrown if registration fails
 * @throws CommandExecutionException Thrown if registerChangeLog fails
 */
public HubRegisterResponse register(String changeLogFile) throws LiquibaseException {
    HubRegisterResponse registerResponse = null;
    if (!hubService.isOnline()) {
        return null;
    }
    // 
    if (!Scope.getCurrentScope().getUI().getAllowPrompt()) {
        return null;
    }
    // 2.  We have a changelog and a changeLogId in it already
    if (!StringUtil.isEmpty(HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValue()) || (changeLog != null && changeLog.getChangeLogId() != null)) {
        return null;
    }
    if (skipAutoRegistration != null && skipAutoRegistration) {
        return null;
    }
    // 
    try {
        LockService lockService = LockServiceFactory.getInstance().getLockService(database);
        lockService.releaseLock();
    } catch (LockException e) {
        Scope.getCurrentScope().getLog(HubUpdater.class).warning(Liquibase.MSG_COULD_NOT_RELEASE_LOCK);
    }
    String promptString = "Do you want to see this operation's report in Liquibase Hub, which improves team collaboration? \n" + "If so, enter your email. If not, enter [N] to no longer be prompted, or [S] to skip for now, but ask again next time";
    String input = Scope.getCurrentScope().getUI().prompt(promptString, "S", (input1, returnType) -> {
        input1 = input1.trim().toLowerCase();
        if (!(input1.equals("s") || input1.equals("n") || input1.contains("@"))) {
            throw new IllegalArgumentException(String.format("Invalid value: '%s'", input1));
        }
        return input1;
    }, String.class);
    // 
    // Re-lock before proceeding
    // 
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.waitForLock();
    String defaultsFilePath = Scope.getCurrentScope().get("defaultsFile", String.class);
    File defaultsFile = null;
    if (defaultsFilePath != null) {
        defaultsFile = new File(defaultsFilePath);
    }
    input = input.toLowerCase();
    if (input.equals("n")) {
        // 
        try {
            String message = "No operations will be reported. Simply add a liquibase.hub.apiKey setting to generate free deployment reports. Learn more at https://hub.liquibase.com";
            Scope.getCurrentScope().getUI().sendMessage(message);
            Scope.getCurrentScope().getLog(getClass()).info(message);
            writeToPropertiesFile(defaultsFile, "\nliquibase.hub.mode=off\n");
            message = "* Updated properties file " + defaultsFile + " to set liquibase.hub.mode=off";
            Scope.getCurrentScope().getUI().sendMessage(message);
            Scope.getCurrentScope().getLog(getClass()).info(message);
            DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_MODE, HubConfiguration.HubMode.OFF);
        } catch (IOException ioe) {
            String message = "Unable to write hubMode to liquibase.properties: " + ioe.getMessage();
            Scope.getCurrentScope().getUI().sendMessage(message);
            Scope.getCurrentScope().getLog(getClass()).warning(message);
        }
    } else if (input.equals("s")) {
        String message = "Skipping auto-registration";
        Scope.getCurrentScope().getUI().sendMessage(message);
        Scope.getCurrentScope().getLog(getClass()).warning(message);
        skipAutoRegistration = true;
    } else {
        // 
        try {
            registerResponse = hubService.register(input);
        } catch (LiquibaseException lhe) {
            String message = "Account creation failed for email address '" + input + "': " + lhe.getMessage() + ".\n" + "No operation report will be generated.";
            Scope.getCurrentScope().getUI().sendMessage(message);
            Scope.getCurrentScope().getLog(HubUpdater.class).warning(message);
            return registerResponse;
        }
        if (registerResponse == null) {
            String message = "Account creation failed for email address '" + input + "'.\n" + "No operation report will be generated.";
            Scope.getCurrentScope().getUI().sendMessage(message);
            Scope.getCurrentScope().getLog(HubUpdater.class).warning(message);
            return registerResponse;
        }
        String message;
        try {
            // 
            // Update the properties file
            // 
            writeToPropertiesFile(defaultsFile, "\nliquibase.hub.apiKey=" + registerResponse.getApiKey() + "\n");
            // 
            // If there is no liquibase.hub.mode setting then add one with value 'all'
            // Do not update liquibase.hub.mode if it is already set
            // 
            ConfiguredValue<HubConfiguration.HubMode> hubModeProperty = HubConfiguration.LIQUIBASE_HUB_MODE.getCurrentConfiguredValue();
            if (hubModeProperty.wasDefaultValueUsed()) {
                writeToPropertiesFile(defaultsFile, "\nliquibase.hub.mode=all\n");
                message = "* Updated properties file " + defaultsFile + " to set liquibase.hub properties";
                Scope.getCurrentScope().getUI().sendMessage(message);
                Scope.getCurrentScope().getLog(getClass()).info(message);
            } else {
                message = "* Updated the liquibase.hub.apiKey property.";
                String message2 = "The liquibase.hub.mode is already set to " + hubModeProperty.getValue() + ". It will not be updated.";
                Scope.getCurrentScope().getUI().sendMessage(message);
                Scope.getCurrentScope().getUI().sendMessage(message2);
                Scope.getCurrentScope().getLog(getClass()).warning(message);
                Scope.getCurrentScope().getLog(getClass()).warning(message2);
            }
            // register the changelog if it exist
            DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_API_KEY, registerResponse.getApiKey());
            if (changeLog != null) {
                message = "* Registering changelog file " + changeLogFile + " with Hub";
                Scope.getCurrentScope().getUI().sendMessage(message);
                Scope.getCurrentScope().getLog(getClass()).info(message);
                // Update the API key in HubConfiguration
                registerChangeLog(registerResponse.getProjectId(), changeLog, changeLogFile);
            }
            message = "Great! Your free operation and deployment reports will be available to you after your local Liquibase commands complete.";
            Scope.getCurrentScope().getUI().sendMessage(message);
            Scope.getCurrentScope().getLog(getClass()).info(message);
        } catch (IOException ioe) {
            message = "Unable to write information to liquibase.properties: " + ioe.getMessage() + "\n" + "Please check your permissions.  No operations will be reported.";
            Scope.getCurrentScope().getUI().sendMessage(message);
            Scope.getCurrentScope().getLog(getClass()).warning(message);
        } catch (CommandExecutionException cee) {
            message = "Unable to register changelog: " + cee.getMessage() + "\n" + "No operations will be reported.";
            Scope.getCurrentScope().getUI().sendMessage(message);
            Scope.getCurrentScope().getLog(getClass()).warning(message);
            // System.setProperty(HubConfiguration.LIQUIBASE_HUB_API_KEY.getKey(), null);
            DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_API_KEY, null);
        }
    }
    return registerResponse;
}
Also used : LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException) ConfiguredValue(liquibase.configuration.ConfiguredValue) CommandExecutionException(liquibase.exception.CommandExecutionException) IOException(java.io.IOException) LiquibaseException(liquibase.exception.LiquibaseException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

LockException (liquibase.exception.LockException)13 LiquibaseException (liquibase.exception.LiquibaseException)6 LockService (liquibase.lockservice.LockService)6 SQLException (java.sql.SQLException)5 DatabaseException (liquibase.exception.DatabaseException)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 Database (liquibase.database.Database)3 ExecutorService (liquibase.executor.ExecutorService)3 InvalidExampleException (liquibase.snapshot.InvalidExampleException)3 IOException (java.io.IOException)2 Liquibase (liquibase.Liquibase)2 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)2 Executor (liquibase.executor.Executor)2 LoggingExecutor (liquibase.executor.LoggingExecutor)2 Sql (liquibase.sql.Sql)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 RandomAccessFile (java.io.RandomAccessFile)1 Statement (java.sql.Statement)1 LocalDateTime (java.time.LocalDateTime)1