Home / Repstance User Guide / CONFIGURE REPLICATION WITH REPSTANCE / Objects and Data Transformation

Objects and Data Transformation

A transformation rule changes what is replicated: the names of schemas, tables and columns, the type or the data of a column. It can also leave a table or a column out. This page shows the rules that are used most. The full syntax is in Objects Mapping and Possible Transforms.

How the rules work

  • A rule of a Capture Process is global: it changes the objects for all the Apply Processes that read from it. A rule of an Apply Process is local: it affects only that process. The rules of an Apply Process see the names that the rules of the Capture Process have set.
  • The rules are tried in the order of their ids. Write the rules for tables before the rules for columns (see the order of the rules in Objects Mapping and Possible Transforms).
  • An Apply Process reads its rules when it starts. After alter, stop the process and run it again.
  • ddlprocessing=map applies the rules to the DDL statements too.
  • A rule is typed as one argument, without a space after a semicolon. A text constant needs special quotes, see “Add a column” below.

The example replicates the tables of the schema dbo of a SQL Server database to the schema public of a PostgreSQL database and changes them on the way. The commands are typed in repcli, as in Basic Replication Configuration.

Rename the schema

Prepare the Source and the Target Databases and create the Capture Process MAPCAP1 and the Apply Process. The rule map=1,rule=(dbo.%:public.%) of the Capture Process changes the schema of every table of dbo to public, and the tables keep their names. All the Apply Processes that read from the Capture Process get the new names. ddlprocessing=map applies the rule to the DDL statements too (see PostgreSQL Apply Parameters):

repcli>prepare database=source dbtype=mssql server=db-src.example.com port=1433 user=repuser password=Passw0rd123 dbname=testDB
[Completed]: Database [db-src.example.com:1433,testDB] has been prepared.
repcli>prepare database=target dbtype=postgresql server=pg-tgt.example.com port=5432 user=repuser password=Passw0rd123 dbname=testDB
[Completed]: Database [pg-tgt.example.com:5432,testDB] has been prepared.
repcli>prepare process=capture id=8 dbtype=mssql name=MAPCAP1 server=db-src.example.com port=1433 user=repuser password=Passw0rd123 dbname=testDB dmlinclude=dbo.% ddlinclude=dbo.% map=1,rule=(dbo.%:public.%) autostart=1
[Completed]: Capture 8 has been added
repcli>prepare process=apply id=8 dbtype=postgresql capturename=MAPCAP1 server=pg-tgt.example.com port=5432 user=repuser password=Passw0rd123 dbname=testDB ddlprocessing=map autostart=1
[Completed]: Apply 8 has been added

Rename a table

The following rules change the Apply Process 8. Stop the process, add a rule that renames the table public.Customer to public.Clients, and run the process again. The numbers of the rules belong to the process: the rule 1 of the Apply Process is not the rule 1 of the Capture Process (see Alter Process):

repcli>stop process=apply id=8
[In Process]: Command [STOP] has been sent. Use [status] command to monitor the Apply 8 process
repcli>alter process=apply id=8 map=1,rule=(public.Customer:public.Clients)
[Completed]: Apply 8 has been altered
repcli>run process=apply id=8
[In Process]: Command [RUN] has been sent. Use [status] command to monitor the Apply 8 process

Rename a column

Rename the column public.Customer.ID to CLIENT_ID. The rule is written after the rule that renames the table, and its right side has the new name of the table, public.Clients. Stop the Apply Process, run the command and run the process again:

repcli>alter process=apply id=8 map=2,rule=(public.Customer.ID:public.Clients.CLIENT_ID)
[Completed]: Apply 8 has been altered

Remove a column

Ignore the column public.Customer.SQL_ID in all the SQL statements. NULL on the right side of a rule leaves the column out:

repcli>alter process=apply id=8 map=3,rule=(public.Customer.SQL_ID:public.Clients.NULL)
[Completed]: Apply 8 has been altered

Change the type of a column

Change the type of all the UNIQUEIDENTIFIER columns of the schema public to VARCHAR(64). The attributes are written without spaces:

repcli>alter process=apply id=8 map=4,rule=(public.%.%.[TYPE=UNIQUEIDENTIFIER]:public.%.%.[TYPE=VARCHAR;LEN=64])
[Completed]: Apply 8 has been altered

Add a column

Add the column SOURCE_SYS VARCHAR(64) to all the tables of the schema public and fill it with the constant text REPLICATED FROM SQL SERVER. A text constant is written between single quotes inside the double quotes of DATA, and the double quotes are doubled, \""'text'\"": then repcli keeps the single quotes and the spaces of the text (see the quotes in Objects Mapping and Possible Transforms):

repcli>alter process=apply id=8 map=5,rule=(public.%.NULL:public.%.SOURCE_SYS.[TYPE=varchar;LEN=64;DATA=\""'REPLICATED FROM SQL SERVER'\""])
[Completed]: Apply 8 has been altered

Check the rules before you save them

Add preview=1 to the alter command to see what the rules do without saving them. The answer lists the tables of the Apply Process and, under Map Tables, what each rule does to them. The example is from a test installation where the Apply Process 1 reads the tables dbo.DimCustomer and dbo.DimCurrency of a sample database: the table is renamed, a column is renamed and the column Title is left out.

repcli>alter process=apply id=1 map=1,rule=(dbo.DimCustomer:dbo.Clients) map=2,rule=(dbo.DimCustomer.CustomerKey:dbo.Clients.CLIENT_ID) map=3,rule=(dbo.DimCustomer.Title:dbo.Clients.NULL) preview=1
[Completed]: Apply 1 has been altered
                                                                      [Replication Tables]
                                                                      --------------------
                                       TABLE_NAME |    [DML]    |    [DDL]    |    [Load]   |
---------------------------------------------------------------------------------------------
                                  dbo.DimCurrency |      +      |      +      |      -      |
                                  dbo.DimCustomer |      +      |      +      |      -      |
---------------------------------------------------------------------------------------------
                                                                              [Map Tables]
                                                                              ------------
 ID |                                Transformation
---------------------------------------------------------------------------------------------
    | dbo.DimCustomer -> dbo.Clients
  3 | - Title -> [REMOVED]
  2 | - CustomerKey[PK=P;TYPE=INT;ID=1] -> CLIENT_ID[PK=P;TYPE=INT;ID=1]
---------------------------------------------------------------------------------------------

The same check works for the rules of a Capture Process (see Alter Process). If a rule is wrong, the answer is Failed with the reason, and nothing is saved.