Migrating triggers prematurely during a data synchronization or migration task can cause data inconsistency between the source and destination databases. This topic explains how to configure trigger synchronization or migration to prevent this issue.
Prerequisites
-
The source and destination databases must be of a supported type, such as from an ApsaraDB RDS for MariaDB instance to an ApsaraDB RDS for MySQL instance. For more information, see Supported instances.
-
The instance is configured for both schema and incremental tasks.
-
Migration instance: For Migration Types, you must select Schema Migration and Incremental Data Migration.
-
Synchronization instance: For Synchronization Types, Incremental Data Synchronization is selected by default, and you must also select Schema Synchronization.
-
-
When you configure the instance, you must select objects for Source Objects at the entire database or schema level.
Supported instances
|
Source database |
Destination database |
Description |
|
MySQL, PolarDB for MySQL, MariaDB |
MySQL, PolarDB for MySQL, MariaDB |
If Method to Migrate Triggers in Source Database is set to Automatically Migrate, DTS adds a control statement to the trigger in the destination database. This statement prevents the trigger from firing by default. For more information, see Automatic migration. |
|
SQL Server |
SQL Server |
If Method to Migrate Triggers in Source Database is set to Automatically Migrate, DTS automatically migrates triggers to the destination database. Important
If incremental data changes in the source database cause triggers to update data in both the source and destination databases, this can create data inconsistencies in the destination database. We recommend that you select Manual Migration and perform the required follow-up operations. |
-
If the source database of the synchronization or migration instance contains triggers and the destination database is a PostgreSQL, PolarDB for PostgreSQL, or PolarDB for PostgreSQL (Compatible with Oracle) instance, DTS helps prevent data inconsistency caused by trigger synchronization or migration while the instance is running.
-
If the account used for the destination database has sufficient privileges, such as a privileged account or a super-privileged account, DTS automatically disables the trigger at the session level after it is synchronized or migrated to the destination database.
-
If the destination database account lacks sufficient privileges, ensure that the
session_replication_roleparameter is set toreplicain the destination database while the instance is running.
-
-
For other synchronization or migration instances that contain triggers, see How do I configure a synchronization or migration task when the source database contains triggers?
Instance configuration
In the Configure Objects step, select an option for Method to Migrate Triggers in Source Database.
On the previous version of the configuration page, this option is in the Configure Objects and Advanced Settings step.
-
Select Manual Migration: During schema migration, DTS generates an
object ignored due to incremental migration is enabledalert for any trigger it encounters and does not migrate it to the destination database. You must manually synchronize or migrate the triggers from the source database to the destination database before you end the incremental task. For more information, see Manual migration. -
Select Automatically Migrate: No additional actions are required.
Manual migration
The procedure is similar for synchronization and migration instances. This section uses a synchronization instance as an example.
Go to the data synchronization task list page in the destination region. You can do this in one of two ways.
DTS console
Log on to the DTS console.
In the navigation pane on the left, click Data Synchronization.
In the upper-left corner of the page, select the region where the synchronization instance is located.
DMS console
NoteThe actual steps may vary depending on the mode and layout of the DMS console. For more information, see Simple mode console and Customize the layout and style of the DMS console.
Log on to the DMS console.
In the top menu bar, choose .
To the right of Data Synchronization Tasks, select the region of the synchronization instance.
-
Click the ID of the target instance.
-
Optional: In the left-side navigation pane, click Task Management.
-
In the Progress section, click the Incremental Write module.
-
On the Basic Information tab, click Migrate Triggers.
NoteIf the Migrate Triggers button is not displayed, no triggers were found during the schema migration.
You can view the trigger synchronization results in the Progress section, within the Schema Migration3 module.
Automatic migration
DTS copies the trigger code from the source database, adds a control statement to the trigger's header, and then writes the modified code to the destination database. The following code shows an example:
Source database trigger:
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW
BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
END;
Destination database trigger:
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW
BEGIN
IF (SELECT @`__#aliyun_dts_writer#__) IS NULL THEN
INSERT INTO test2 SET a2 = NEW.a1;
END IF;
END;
FAQ
-
What is meant by a task that includes both schema and incremental operations?
For a synchronization instance, this means both Schema Synchronization and Incremental Data Synchronization are selected under Synchronization Types. For a migration instance, this means both Schema Migration and Incremental Data Migration are selected under Migration Types.
-
How do I end an incremental task?
You can end an incremental task by terminating, resetting, or releasing the instance. For more information, see Terminate a DTS instance, Reset a DTS instance, and Release a DTS instance.