|
| 1 | +--- |
| 2 | +page_title: "Source versioning: migrating to `materialize_source_table` Resource" |
| 3 | +subcategory: "" |
| 4 | +description: |- |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +# Source versioning: migrating to `materialize_source_table` Resource |
| 9 | + |
| 10 | +In previous versions of the Materialize Terraform provider, source tables were defined within the source resource itself and were considered subsources of the source rather than separate entities. |
| 11 | + |
| 12 | +This guide will walk you through the process of migrating your existing source table definitions to the new `materialize_source_table` resource. |
| 13 | + |
| 14 | +## Old Approach |
| 15 | + |
| 16 | +Previously, source tables were defined directly within the source resource: |
| 17 | + |
| 18 | +### Example: MySQL Source |
| 19 | + |
| 20 | +```hcl |
| 21 | +resource "materialize_source_mysql" "mysql_source" { |
| 22 | + name = "mysql_source" |
| 23 | + cluster_name = "cluster_name" |
| 24 | + |
| 25 | + mysql_connection { |
| 26 | + name = materialize_connection_mysql.mysql_connection.name |
| 27 | + } |
| 28 | + |
| 29 | + table { |
| 30 | + upstream_name = "mysql_table1" |
| 31 | + upstream_schema_name = "shop" |
| 32 | + name = "mysql_table1_local" |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +The same approach was used for other source types such as Postgres and the load generator sources. |
| 38 | + |
| 39 | +## New Approach |
| 40 | + |
| 41 | +The new approach separates source definitions and table definitions. You will now create the source without specifying the tables, and then define each table using the `materialize_source_table` resource. |
| 42 | + |
| 43 | +## Manual Migration Process |
| 44 | + |
| 45 | +This manual migration process requires users to create new source tables using the new `materialize_source_table` resource and then remove the old ones. |
| 46 | + |
| 47 | +### Step 1: Define `materialize_source_table` Resources |
| 48 | + |
| 49 | +Before making any changes to your existing source resources, create new `materialize_source_table` resources for each table that is currently defined within your sources. This ensures that the tables are preserved during the migration: |
| 50 | + |
| 51 | +```hcl |
| 52 | +resource "materialize_source_table" "mysql_table_from_source" { |
| 53 | + name = "mysql_table1_from_source" |
| 54 | + schema_name = "public" |
| 55 | + database_name = "materialize" |
| 56 | + |
| 57 | + source { |
| 58 | + name = materialize_source_mysql.mysql_source.name |
| 59 | + // Define the schema and database for the source if needed |
| 60 | + } |
| 61 | + |
| 62 | + upstream_name = "mysql_table1" |
| 63 | + upstream_schema_name = "shop" |
| 64 | + |
| 65 | + ignore_columns = ["about"] |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Step 2: Apply the Changes |
| 70 | + |
| 71 | +Run `terraform plan` and `terraform apply` to create the new `materialize_source_table` resources. This step ensures that the tables are defined separately from the source and are not removed from Materialize. |
| 72 | + |
| 73 | +> **Note:** This will start an ingestion process for the newly created source tables. |
| 74 | + |
| 75 | +### Step 3: Remove Table Blocks from Source Resources |
| 76 | + |
| 77 | +Once the new `materialize_source_table` resources are successfully created, you can safely remove the `table` blocks from your existing source resources: |
| 78 | + |
| 79 | +```hcl |
| 80 | +resource "materialize_source_mysql" "mysql_source" { |
| 81 | + name = "mysql_source" |
| 82 | + cluster_name = "cluster_name" |
| 83 | + |
| 84 | + mysql_connection { |
| 85 | + name = materialize_connection_mysql.mysql_connection.name |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +This will drop the old tables from the source resources. |
| 91 | + |
| 92 | +### Step 4: Update Terraform State |
| 93 | + |
| 94 | +After removing the `table` blocks from your source resources, run `terraform plan` and `terraform apply` again to update the Terraform state and apply the changes. |
| 95 | + |
| 96 | +### Step 5: Verify the Migration |
| 97 | + |
| 98 | +After applying the changes, verify that your tables are still correctly set up in Materialize by checking the table definitions using Materialize’s SQL commands. |
| 99 | + |
| 100 | +During the migration, you can use both the old `table` blocks and the new `materialize_source_table` resources simultaneously. This allows for a gradual transition until the old method is fully deprecated. |
| 101 | + |
| 102 | +## Automated Migration Process (TBD) |
| 103 | + |
| 104 | +> **Note:** This will still not work as the previous source tables are considered subsources of the source and are missing from the `mz_tables` table in Materialize so we can't import them directly without recreating them. |
| 105 | + |
| 106 | +Once the migration on the Materialize side has been implemented, a more automated migration process will be available. The steps will include: |
| 107 | + |
| 108 | +### Step 1: Define `materialize_source_table` Resources |
| 109 | + |
| 110 | +First, define the new `materialize_source_table` resources for each table: |
| 111 | + |
| 112 | +```hcl |
| 113 | +resource "materialize_source_table" "mysql_table_from_source" { |
| 114 | + name = "mysql_table1_from_source" |
| 115 | + schema_name = "public" |
| 116 | + database_name = "materialize" |
| 117 | + |
| 118 | + source { |
| 119 | + name = materialize_source_mysql.mysql_source.name |
| 120 | + // Define the schema and database for the source if needed |
| 121 | + } |
| 122 | + |
| 123 | + upstream_name = "mysql_table1" |
| 124 | + upstream_schema_name = "shop" |
| 125 | + |
| 126 | + ignore_columns = ["about"] |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### Step 2: Modify the Existing Source Resource |
| 131 | + |
| 132 | +Next, modify the existing source resource by removing the `table` blocks and adding an `ignore_changes` directive for the `table` attribute. This prevents Terraform from trying to delete the tables: |
| 133 | + |
| 134 | +```hcl |
| 135 | +resource "materialize_source_mysql" "mysql_source" { |
| 136 | + name = "mysql_source" |
| 137 | + cluster_name = "cluster_name" |
| 138 | + |
| 139 | + mysql_connection { |
| 140 | + name = materialize_connection_mysql.mysql_connection.name |
| 141 | + } |
| 142 | + |
| 143 | + lifecycle { |
| 144 | + ignore_changes = [table] |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +- **`lifecycle { ignore_changes = [table] }`**: This directive tells Terraform to ignore changes to the `table` attribute, preventing it from trying to delete tables that were previously defined in the source resource. |
| 150 | + |
| 151 | +### Step 3: Import the Existing Tables |
| 152 | + |
| 153 | +You can then import the existing tables into the new `materialize_source_table` resources without disrupting your existing setup: |
| 154 | + |
| 155 | +```bash |
| 156 | +terraform import materialize_source_table.mysql_table_from_source <region>:<table_id> |
| 157 | +``` |
| 158 | + |
| 159 | +Replace `<region>` with the actual region and `<table_id>` with the table ID. You can find the table ID by querying the `mz_tables` table. |
| 160 | + |
| 161 | +### Step 4: Run Terraform Plan and Apply |
| 162 | + |
| 163 | +Finally, run `terraform plan` and `terraform apply` to ensure that everything is correctly set up without triggering any unwanted deletions. |
| 164 | + |
| 165 | +This approach allows you to migrate your tables safely without disrupting your existing setup. |
| 166 | + |
| 167 | +## Importing Existing Tables |
| 168 | + |
| 169 | +To import existing tables into your Terraform state using the manual migration process, use the following command: |
| 170 | + |
| 171 | +```bash |
| 172 | +terraform import materialize_source_table.table_name <region>:<table_id> |
| 173 | +``` |
| 174 | + |
| 175 | +Ensure you replace `<region>` with the region where the table is located and `<table_id>` with the ID of the table. |
| 176 | + |
| 177 | +> **Note:** The `upstream_name` and `upstream_schema_name` attributes are not yet implemented on the Materialize side, so the import process will not work until these changes are made. |
| 178 | + |
| 179 | +## Future Improvements |
| 180 | + |
| 181 | +The Kafka and Webhooks sources are currently being implemented. Once these changes, the migration process will be updated to include them. |
0 commit comments