

# Changing data mappings

In a database cluster group for Aurora, you change data mappings with `INSERT`, `UPDATE`, and `DELETE` on `dbclustergroup_sys.data_mappings` at the routing endpoint. A mapping write moves no data. It changes only where the routing layer sends statements. To put data on another member cluster, see [Moving data between member clusters](databaseclustergroups-data-movement.md).

Mapping changes are incremental, not transactional. A multi-row `INSERT` applies all of its rows or none, but `BEGIN` and `COMMIT` don't group separate mapping statements, and a failed mapping change doesn't roll back the earlier ones. Stage a large edit so that each intermediate state still routes: put each set of mappings that must appear together into one multi-row `INSERT`. For when a change reaches the routing layer, what an open transaction sees, and when to retry, see [Applying data mapping changes](databaseclustergroups-data-mappings.md#databaseclustergroups-when-a-data-mapping-change-takes-effect). For the statement grammar, see [Data mapping statements](databaseclustergroups-sql-mappings.md).

## Before you change mappings
<a name="databaseclustergroups-changing-mappings-before"></a>

Before every data mapping change, check the following:
+ Connect to the routing endpoint in query routing mode, in a session that isn't pinned, as a router user that has `dbclustergroup_metadata_admin`. The same privilege covers scaleout table and routing function definitions. A pinned session sends data mapping statements to the member cluster, where they fail.
+ Run each data mapping write outside a transaction. The routing layer rejects a write to `dbclustergroup_sys.data_mappings` inside an open transaction. For how Aurora MySQL treats an open transaction, see the Aurora MySQL information after this list.
+ Check that no data movement job of the collocation is running. While a move runs, and until its cleanup completes, the routing layer refuses a change to the mappings of the collocation that it moves. The statement fails with error `1205` and the message `Metadata for collocation group '<NAME>' is being changed by another operation` (or `Metadata for data mapping '<NAME>' is being changed by another operation`). Wait until `cleanup_status` reads `Complete` in `move_info`, then run the statement again. Mappings of other collocations aren't affected.
+ Copy each member cluster alias from `dbclustergroup_sys.member_cluster_mappings`. Don't derive an alias from a DB cluster identifier or from a cluster's position in the group. Check that each member cluster alias that the change names appears in that listing. The routing layer rejects at the `INSERT` a mapping that names an alias that the listing doesn't show, with Aurora MySQL error `64004` (SQLSTATE `42000`). For the error text, see [Error codes for database cluster groups for Aurora](databaseclustergroups-error-reference.md).
+ Write each statement in the simple form that the routing layer accepts. A `WHERE` clause takes one column equal to one value, or one tuple of columns equal to one tuple of values. The routing layer rejects `AND`, `OR`, `IN` lists, comparison operators other than `=`, and modifiers such as `IGNORE`, `ON DUPLICATE KEY`, `REPLACE INTO`, `LIMIT`, and `ORDER BY`. Put every column that you filter on into one tuple.
+ Export the data mappings, as described in the following section. The export gives you the member cluster aliases and cluster names from `dbclustergroup_sys.member_cluster_mappings` and `dbclustergroup_sys.data_mappings`.

------
#### [ Aurora MySQL ]

The routing layer rejects a data mapping write inside a transaction with error `1235` (SQLSTATE `42000`). A session with `autocommit = 0` counts as an open transaction. Some drivers, such as PyMySQL, turn autocommit off by default, so run `SET autocommit = 1`, or commit, before a data mapping change.

Filter on `member_cluster_alias`, not `db_cluster_identifier`. The routing layer rejects `db_cluster_identifier` in a `WHERE` clause on `dbclustergroup_sys.data_mappings`.

------

## Exporting the mappings before and after a change
<a name="databaseclustergroups-export-mappings"></a>

No API restores the data mappings on their own. You export them and import them again at the routing endpoint. Export the data mappings before and after every data mapping change, move, schema change, upgrade, and restore, and keep the export as your undo.

**To export the mappings**

1. At the routing endpoint, read the data mappings, the member cluster mappings, and the definition of each scaleout table and routing function, as shown in the following tabs.

1. In your client, save the output to a file with your change record. Keep the database name next to each scaleout table definition. Sort the rows in your client, because the routing layer refuses `ORDER BY` on its system tables. Read each table with one `SELECT` and no `LIMIT`, so that the export is complete.

1. After the change, export again at the routing endpoint, and compare the two files row by row.

```sql
SELECT * FROM dbclustergroup_sys.data_mappings;
SELECT * FROM dbclustergroup_sys.member_cluster_mappings;
SHOW SCALEOUT TABLES;
SHOW SCALEOUT ROUTING FUNCTIONS;
SHOW CREATE SCALEOUT TABLE {{SCALEOUT_TABLE}};          -- once per scaleout table
SHOW CREATE SCALEOUT ROUTING FUNCTION {{ROUTING_FUNCTION}};     -- once per routing function
```

Only the rows that you meant to add, change, or remove should differ. Any other difference means that someone else changed the data mappings at the same time or that a statement matched more rows than you meant. Find the cause before you continue.

To restore from the export, run one statement per row, outside a transaction:
+ **A deleted row:** `INSERT` it back with every writable column, including `router_collocation_group`. Leave out `db_cluster_identifier`, which is read-only.
+ **A collocation on the wrong member cluster:** repoint it back by `router_collocation_group`, with the member cluster alias from the export.
+ **A wrong cluster name:** `UPDATE` the row back.

Writes that the application made while a data mapping was wrong are on the member cluster that the data mapping named at the time. Find them by ID or timestamp, and copy them to where they belong.

## Adding a mapping
<a name="databaseclustergroups-changing-mappings-add"></a>

Add a data mapping to place a database or a table on a member cluster. Give each data mapping a collocation in `router_collocation_group`. A row without one joins the member cluster's default collocation and moves with everything else in it.

**To add a data mapping**

1. On the member cluster, through a session pin or a direct connection, create the database or table. The routing layer doesn't check the data mapping against the member cluster. A data mapping that names a missing object registers without error, and the first routed statement that uses it fails.

1. At the routing endpoint, insert the rows. Put rows that must appear together into one multi-row `INSERT`.

1. If a write fails, stop. Read the data mapping table again, and fix the cause before you continue. A failed data mapping change doesn't roll back the earlier ones.

1. At the routing endpoint, read the rows back with `SELECT * FROM dbclustergroup_sys.data_mappings WHERE (router_db_name, router_table_name) = ('{{DB}}', '{{TABLE}}')`, and run one routed read of each new name. For the read, see [SELECT FROM dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-select-data-mappings.md).

------
#### [ Aurora MySQL ]

In step 2, the first example places a whole database, and the second places one table under a router name.

```sql
INSERT INTO dbclustergroup_sys.data_mappings
  (cluster_db_name, router_db_name, member_cluster_alias, router_collocation_group)
VALUES ('{{CLUSTER_DB}}', '{{ROUTER_DB}}', '{{MEMBER_CLUSTER_ALIAS}}', '{{COLLOCATION_GROUP}}');

INSERT INTO dbclustergroup_sys.data_mappings
  (cluster_db_name, cluster_table_name, router_db_name, router_table_name, member_cluster_alias, router_collocation_group)
VALUES ('{{CLUSTER_DB}}', '{{CLUSTER_TABLE}}', '{{ROUTER_DB}}', '{{ROUTER_TABLE}}', '{{MEMBER_CLUSTER_ALIAS}}', '{{COLLOCATION_GROUP}}');
```

------

While a database has a database-level mapping, the routing layer refuses a table-level mapping in it. To change the level, see [Changing from database-level to table-level mappings](databaseclustergroups-mapping-levels.md#databaseclustergroups-changing-from-database-level-to-table-level-mappings).

## Repointing mappings by collocation
<a name="databaseclustergroups-changing-mappings-repoint"></a>

A repoint sends a collocation's statements to another member cluster but copies no rows. Repoint only when every object in the collocation already has a correct, current copy on the target, for example because you copied the data yourself. Otherwise, move the collocation with data movement.

A member table of a scaleout table moves only with its whole collocation when the table has a routing function and the member is in a collocation that the function targets, either by a repoint of the collocation, as in the following procedure, or by data movement. The routing layer refuses an `UPDATE` that moves one member table to another collocation when that would break the scaleout table's placement. On Aurora MySQL, the statement fails with error `64024` when the target collocation already holds a member table of the same scaleout table. It fails with error `64065` when the `UPDATE` moves two member tables of one scaleout table into one collocation. It fails with error `64066` when the scaleout table would have no member table in a collocation that its routing function targets.

**To repoint a collocation**

1. In the application, stop writes to the collocation's objects. Then, over direct connections, copy what changed since your copy started. For details, see [Moving data yourself](databaseclustergroups-backfill.md#databaseclustergroups-moving-data-yourself).

1. Over direct connections, compare row counts and checksums on the source and the target. In your export, count the collocation's mapping rows.

1. At the routing endpoint, repoint every row of the collocation in one statement. Check that the number of rows affected equals the collocation's mapping row count.

1. At the routing endpoint, from a new connection, run one routed read of each object in the collocation. Then resume writes.

To repoint the collocation in step 3, run the following SQL `UPDATE` statement. It sets the member cluster alias of every mapping in the collocation.

```sql
UPDATE dbclustergroup_sys.data_mappings
   SET member_cluster_alias = '{{TARGET_ALIAS}}'
 WHERE router_collocation_group = '{{COLLOCATION_GROUP}}';
```

The `WHERE` clause follows the simple form described in [Before you change mappings](#databaseclustergroups-changing-mappings-before). For the full grammar and errors, see [UPDATE dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-update-data-mappings.md).

To repoint one object that's alone in its collocation, set `member_cluster_alias` and `router_collocation_group` together in one `UPDATE` that matches exactly that row. The routing layer refuses a change to either column alone on a single mapping. To take one object out of a collocation that other objects share, set both columns together to a new collocation on the target member cluster, and move its data first.

To rename a collocation, set `router_collocation_group` in an `UPDATE` that matches the collocation by its current name, as in the following statement. The rename moves no data and keeps the collocation on its member cluster. The rows affected count is the number of mappings in the collocation. Routing functions that target the collocation follow the rename, so you don't change them. Collocation names are case-sensitive.

```sql
UPDATE dbclustergroup_sys.data_mappings
   SET router_collocation_group = '{{NEW_COLLOCATION_GROUP}}'
 WHERE router_collocation_group = '{{COLLOCATION_GROUP}}';
```

Copy each table with its full definition, not with `CREATE TABLE ... AS SELECT`. Before you repoint, compare the schemas on both member clusters.

## Renaming an object and keeping its mappings
<a name="databaseclustergroups-renaming-an-object-and-keeping-its-mappings"></a>

To point a router name at another database or table on the same member cluster, `UPDATE` its row in place. If you delete the row and insert a corrected one, the router name has no mapping between the two writes.

------
#### [ Aurora MySQL ]

```sql
UPDATE dbclustergroup_sys.data_mappings
   SET cluster_table_name = '{{NEW_TABLE}}'
 WHERE (router_db_name, router_table_name) = ('{{ROUTER_DB}}', '{{ROUTER_TABLE}}');
```

------

Under a table-level mapping, the router name stays the same when you rename the table on the member cluster. If you rename the table first and then update the mapping, routed statements fail with error 1146 until the mapping changes. To rename the table with no such interval, point the mapping at a view under the new name first.

**To rename a table under a table-level mapping**

1. On the member cluster, through a session pin or a direct connection, create a view under the new name that selects every column of the table, for example `CREATE VIEW {{NEW_TABLE}} AS SELECT * FROM {{OLD_TABLE}}`. A view over all the columns of one table accepts reads and DML. Until you swap the view and the table, `TRUNCATE` on the new name fails with error 1146 and `ALTER TABLE` on the new name fails with error 1347, because the new name is a view.

1. On the member cluster, if member cluster users have table-level privileges on the table, grant the same privileges on the new name. Otherwise, statements through the view fail with error 1142, and the grants stay on the old name after the swap.

1. At the routing endpoint, set `cluster_table_name` to the new name with the `UPDATE` shown earlier. Routed statements now reach the table through the view. Before the next step, wait until the change reaches the routing layer, as described in [Applying data mapping changes](databaseclustergroups-data-mappings.md#databaseclustergroups-when-a-data-mapping-change-takes-effect).

1. On the member cluster, swap the view and the table in one statement, for example `RENAME TABLE {{NEW_TABLE}} TO {{BRIDGE_VIEW}}, {{OLD_TABLE}} TO {{NEW_TABLE}}`. The statement renames both at once, so the new name always exists.

1. On the member cluster, drop the view {{BRIDGE\_VIEW}}.

Under a database-level mapping, the cluster table name is the name that the application uses. A rename is then an application change with no step at the routing endpoint. The table answers under its new name after the rename, and the old name returns an unknown-table error. When the table is on several member clusters, rename it on one member cluster at a time, as described in [Rolling out a schema change](databaseclustergroups-schema-changes.md#databaseclustergroups-rolling-out-a-schema-change).

## Removing a mapping
<a name="databaseclustergroups-changing-mappings-remove"></a>

Remove a mapping after the application stops using its router name. Remove the row at the routing endpoint before you drop the object on the member cluster so that no mapping names a missing object. The delete leaves the object on its member cluster.

Give every `DELETE` a `WHERE` clause that matches exactly one row, and check that the statement affects one row. A `WHERE` clause on `router_collocation_group` or `member_cluster_alias` matches every row with that value. The routing layer refuses to remove a row that a scaleout table uses until you drop the scaleout table.

**Warning**  
A `DELETE` on `dbclustergroup_sys.data_mappings` with no `WHERE` clause removes data mappings from across the group, and every object whose mapping it removes becomes unreachable through the routing endpoint. One statement removes up to 1,000 mappings, table-level mappings first. The affected-rows count shows how many it removed, and each repeat removes the next mappings. The statement keeps the mappings that a scaleout table uses, and when only those are left, it fails with error 1235. If a data movement job holds a lock on any mapping, the statement fails with error 1205 and removes nothing.  
To recover, insert the rows again from your export. Leave out the empty table columns of a database-level row, and split a restore of more than 1,000 rows into several `INSERT` statements.

------
#### [ Aurora MySQL ]

```sql
-- A database-level row
DELETE FROM dbclustergroup_sys.data_mappings WHERE (router_db_name, router_table_name) = ('{{ROUTER_DB}}', '');
-- A table-level row
DELETE FROM dbclustergroup_sys.data_mappings
 WHERE (router_db_name, router_table_name) = ('{{ROUTER_DB}}', '{{ROUTER_TABLE}}');
```

------

## Checking a mapping change
<a name="databaseclustergroups-changing-mappings-check"></a>

After every data mapping change, compare a new export with the one from before the change, and check the following:
+ The object exists on the member cluster that each changed row names.
+ At the routing endpoint, from a new connection, a routed read of each changed name succeeds, and so does one read on each member cluster whose data mappings didn't change.

## Related pages
<a name="databaseclustergroups-changing-mappings-related"></a>

For related tasks and concepts, see the following pages:
+ [Data mappings and collocations](databaseclustergroups-data-mappings.md)
+ [Using database-level and table-level mappings](databaseclustergroups-mapping-levels.md)
+ [Moving data between member clusters](databaseclustergroups-data-movement.md)
+ [Error codes for database cluster groups for Aurora](databaseclustergroups-error-reference.md)