Search in sources :

Example 1 with TriggerReBuildReason

use of org.jumpmind.symmetric.model.TriggerReBuildReason 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

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