Search in sources :

Example 1 with ITriggerCreationListener

use of org.jumpmind.symmetric.config.ITriggerCreationListener in project symmetric-ds by JumpMind.

the class TriggerRouterService method dropTriggers.

protected void dropTriggers(TriggerHistory history, StringBuilder sqlBuffer) {
    if (StringUtils.isNotBlank(history.getNameForInsertTrigger())) {
        symmetricDialect.removeTrigger(sqlBuffer, history.getSourceCatalogName(), history.getSourceSchemaName(), history.getNameForInsertTrigger(), history.getSourceTableName());
    }
    if (StringUtils.isNotBlank(history.getNameForDeleteTrigger())) {
        symmetricDialect.removeTrigger(sqlBuffer, history.getSourceCatalogName(), history.getSourceSchemaName(), history.getNameForDeleteTrigger(), history.getSourceTableName());
    }
    if (StringUtils.isNotBlank(history.getNameForUpdateTrigger())) {
        symmetricDialect.removeTrigger(sqlBuffer, history.getSourceCatalogName(), history.getSourceSchemaName(), history.getNameForUpdateTrigger(), history.getSourceTableName());
    }
    if (parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
        for (ITriggerCreationListener l : extensionService.getExtensionPointList(ITriggerCreationListener.class)) {
            l.triggerInactivated(null, history);
        }
    }
    boolean triggerExists = symmetricDialect.doesTriggerExist(history.getSourceCatalogName(), history.getSourceSchemaName(), history.getSourceTableName(), history.getNameForInsertTrigger());
    triggerExists |= symmetricDialect.doesTriggerExist(history.getSourceCatalogName(), history.getSourceSchemaName(), history.getSourceTableName(), history.getNameForUpdateTrigger());
    triggerExists |= symmetricDialect.doesTriggerExist(history.getSourceCatalogName(), history.getSourceSchemaName(), history.getSourceTableName(), history.getNameForDeleteTrigger());
    if (triggerExists) {
        log.warn("There are triggers that have been marked as inactive.  Please remove triggers represented by trigger_id={} and trigger_hist_id={}", history.getTriggerId(), history.getTriggerHistoryId());
    } else {
        inactivateTriggerHistory(history);
    }
}
Also used : ITriggerCreationListener(org.jumpmind.symmetric.config.ITriggerCreationListener)

Example 2 with ITriggerCreationListener

use of org.jumpmind.symmetric.config.ITriggerCreationListener in project symmetric-ds by JumpMind.

the class TriggerRouterService method updateOrCreateDatabaseTriggers.

protected void updateOrCreateDatabaseTriggers(Trigger trigger, Table table, StringBuilder sqlBuffer, boolean force, boolean verifyInDatabase, List<TriggerHistory> activeTriggerHistories) {
    TriggerHistory newestHistory = null;
    TriggerReBuildReason reason = TriggerReBuildReason.NEW_TRIGGERS;
    String errorMessage = null;
    if (verifyInDatabase) {
        Channel channel = configurationService.getChannel(trigger.getChannelId());
        if (channel == null) {
            errorMessage = String.format("Trigger %s had an unrecognized channel_id of '%s'.  Please check to make sure the channel exists.  Creating trigger on the '%s' channel", trigger.getTriggerId(), trigger.getChannelId(), Constants.CHANNEL_DEFAULT);
            log.error(errorMessage);
            trigger.setChannelId(Constants.CHANNEL_DEFAULT);
        }
    }
    try {
        boolean foundPk = false;
        Column[] columns = trigger.filterExcludedAndIncludedColumns(table.getColumns());
        for (Column column : columns) {
            foundPk |= column.isPrimaryKey();
            if (foundPk) {
                break;
            }
        }
        if (!foundPk) {
            table = platform.makeAllColumnsPrimaryKeys(table);
        }
        TriggerHistory latestHistoryBeforeRebuild = getNewestTriggerHistoryForTrigger(trigger.getTriggerId(), trigger.isSourceCatalogNameWildCarded() ? table.getCatalog() : trigger.getSourceCatalogName(), trigger.isSourceSchemaNameWildCarded() ? table.getSchema() : trigger.getSourceSchemaName(), trigger.isSourceTableNameWildCarded() ? table.getName() : trigger.getSourceTableName());
        boolean forceRebuildOfTriggers = false;
        if (latestHistoryBeforeRebuild == null) {
            reason = TriggerReBuildReason.NEW_TRIGGERS;
            forceRebuildOfTriggers = true;
        } else if (table.calculateTableHashcode() != latestHistoryBeforeRebuild.getTableHash()) {
            reason = TriggerReBuildReason.TABLE_SCHEMA_CHANGED;
            forceRebuildOfTriggers = true;
        } else if (trigger.hasChangedSinceLastTriggerBuild(latestHistoryBeforeRebuild.getCreateTime()) || trigger.toHashedValue() != latestHistoryBeforeRebuild.getTriggerRowHash()) {
            reason = TriggerReBuildReason.TABLE_SYNC_CONFIGURATION_CHANGED;
            forceRebuildOfTriggers = true;
        } else if (symmetricDialect.getTriggerTemplate().toHashedValue() != latestHistoryBeforeRebuild.getTriggerTemplateHash()) {
            reason = TriggerReBuildReason.TRIGGER_TEMPLATE_CHANGED;
            forceRebuildOfTriggers = true;
        } else if (force) {
            reason = TriggerReBuildReason.FORCED;
            forceRebuildOfTriggers = true;
        }
        boolean supportsTriggers = symmetricDialect.getPlatform().getDatabaseInfo().isTriggersSupported();
        newestHistory = rebuildTriggerIfNecessary(sqlBuffer, forceRebuildOfTriggers, trigger, DataEventType.INSERT, reason, latestHistoryBeforeRebuild, null, trigger.isSyncOnInsert() && supportsTriggers, table, activeTriggerHistories);
        newestHistory = rebuildTriggerIfNecessary(sqlBuffer, forceRebuildOfTriggers, trigger, DataEventType.UPDATE, reason, latestHistoryBeforeRebuild, newestHistory, trigger.isSyncOnUpdate() && supportsTriggers, table, activeTriggerHistories);
        newestHistory = rebuildTriggerIfNecessary(sqlBuffer, forceRebuildOfTriggers, trigger, DataEventType.DELETE, reason, latestHistoryBeforeRebuild, newestHistory, trigger.isSyncOnDelete() && supportsTriggers, table, activeTriggerHistories);
        if (latestHistoryBeforeRebuild != null && newestHistory != null) {
            inactivateTriggerHistory(latestHistoryBeforeRebuild);
        }
        if (newestHistory != null) {
            synchronized (activeTriggerHistories) {
                activeTriggerHistories.add(newestHistory);
            }
            newestHistory.setErrorMessage(errorMessage);
            if (parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
                for (ITriggerCreationListener l : extensionService.getExtensionPointList(ITriggerCreationListener.class)) {
                    l.triggerCreated(trigger, newestHistory);
                }
            }
        }
    } catch (Exception ex) {
        log.error(String.format("Failed to create triggers for %s", trigger.qualifiedSourceTableName()), ex);
        if (newestHistory != null) {
            // Make sure all the triggers are removed from the
            // table
            symmetricDialect.removeTrigger(null, trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), newestHistory.getNameForInsertTrigger(), trigger.getSourceTableName());
            symmetricDialect.removeTrigger(null, trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), newestHistory.getNameForUpdateTrigger(), trigger.getSourceTableName());
            symmetricDialect.removeTrigger(null, trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), newestHistory.getNameForDeleteTrigger(), trigger.getSourceTableName());
        }
        for (ITriggerCreationListener l : extensionService.getExtensionPointList(ITriggerCreationListener.class)) {
            l.triggerFailed(trigger, ex);
        }
    }
}
Also used : Column(org.jumpmind.db.model.Column) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Channel(org.jumpmind.symmetric.model.Channel) ITriggerCreationListener(org.jumpmind.symmetric.config.ITriggerCreationListener) TriggerReBuildReason(org.jumpmind.symmetric.model.TriggerReBuildReason) SymmetricException(org.jumpmind.symmetric.SymmetricException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ITriggerCreationListener (org.jumpmind.symmetric.config.ITriggerCreationListener)2 ExecutionException (java.util.concurrent.ExecutionException)1 Column (org.jumpmind.db.model.Column)1 SymmetricException (org.jumpmind.symmetric.SymmetricException)1 Channel (org.jumpmind.symmetric.model.Channel)1 TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)1 TriggerReBuildReason (org.jumpmind.symmetric.model.TriggerReBuildReason)1