Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package androidx.room.writer |
| 18 | |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 19 | import androidx.room.compiler.codegen.CodeLanguage |
| 20 | import androidx.room.compiler.codegen.VisibilityModifier |
| 21 | import androidx.room.compiler.codegen.XCodeBlock |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 22 | import androidx.room.compiler.codegen.XFunSpec |
| 23 | import androidx.room.compiler.codegen.XFunSpec.Builder.Companion.addStatement |
elifbilgin | f748a17 | 2023-03-02 10:27:43 -0800 | [diff] [blame^] | 24 | import androidx.room.compiler.codegen.XMemberName.Companion.packageMember |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 25 | import androidx.room.compiler.codegen.XTypeSpec |
Daniel Santiago Rivera | 885c9eb | 2022-09-29 21:49:35 -0400 | [diff] [blame] | 26 | import androidx.room.compiler.codegen.XTypeSpec.Builder.Companion.addOriginatingElement |
Daniel Santiago Rivera | 8b793f8 | 2022-09-28 13:23:51 -0400 | [diff] [blame] | 27 | import androidx.room.compiler.codegen.XTypeSpec.Builder.Companion.addProperty |
Daniel Santiago Rivera | 051c4a5e | 2021-09-02 10:21:25 -0700 | [diff] [blame] | 28 | import androidx.room.compiler.processing.XTypeElement |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 29 | import androidx.room.ext.RoomTypeNames |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 30 | import androidx.room.ext.SupportDbTypeNames |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 31 | import androidx.room.migration.bundle.EntityBundle |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 32 | import androidx.room.migration.bundle.FtsEntityBundle |
Elif Bilgin | 51f4b35 | 2021-04-20 16:02:55 -0700 | [diff] [blame] | 33 | import androidx.room.vo.AutoMigration |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Writes the implementation of migrations that were annotated with @AutoMigration. |
| 37 | */ |
| 38 | class AutoMigrationWriter( |
Daniel Santiago Rivera | 051c4a5e | 2021-09-02 10:21:25 -0700 | [diff] [blame] | 39 | private val dbElement: XTypeElement, |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 40 | val autoMigration: AutoMigration, |
Daniel Santiago Rivera | fe98c70 | 2022-09-22 22:58:50 -0400 | [diff] [blame] | 41 | codeLanguage: CodeLanguage |
| 42 | ) : TypeWriter(codeLanguage) { |
Elif Bilgin | 51f4b35 | 2021-04-20 16:02:55 -0700 | [diff] [blame] | 43 | private val addedColumns = autoMigration.schemaDiff.addedColumns |
| 44 | private val addedTables = autoMigration.schemaDiff.addedTables |
| 45 | private val renamedTables = autoMigration.schemaDiff.renamedTables |
| 46 | private val complexChangedTables = autoMigration.schemaDiff.complexChangedTables |
| 47 | private val deletedTables = autoMigration.schemaDiff.deletedTables |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 48 | |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 49 | override fun createTypeSpecBuilder(): XTypeSpec.Builder { |
| 50 | val builder = XTypeSpec.classBuilder( |
| 51 | codeLanguage, |
Daniel Santiago Rivera | 587c0219 | 2022-09-17 12:55:48 -0700 | [diff] [blame] | 52 | autoMigration.getImplTypeName(dbElement.asClassName()) |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 53 | ) |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 54 | builder.apply { |
| 55 | addOriginatingElement(dbElement) |
Daniel Santiago Rivera | 7e7bb0b | 2022-11-07 10:54:49 -0500 | [diff] [blame] | 56 | superclass(RoomTypeNames.MIGRATION) |
Daniel Santiago Rivera | 63239ab | 2022-12-21 16:36:29 -0500 | [diff] [blame] | 57 | // Class is package-protected in Java (no visibility modifier) and internal in Kotlin |
| 58 | if (language == CodeLanguage.KOTLIN) { |
| 59 | setVisibility(VisibilityModifier.INTERNAL) |
| 60 | } |
Elif Bilgin | 51f4b35 | 2021-04-20 16:02:55 -0700 | [diff] [blame] | 61 | if (autoMigration.specClassName != null) { |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 62 | builder.addProperty( |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 63 | name = "callback", |
Daniel Santiago Rivera | 7e7bb0b | 2022-11-07 10:54:49 -0500 | [diff] [blame] | 64 | typeName = RoomTypeNames.AUTO_MIGRATION_SPEC, |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 65 | visibility = VisibilityModifier.PRIVATE, |
| 66 | initExpr = if (!autoMigration.isSpecProvided) { |
Daniel Santiago Rivera | 9b41215 | 2022-09-23 22:26:42 -0400 | [diff] [blame] | 67 | XCodeBlock.ofNewInstance( |
| 68 | codeLanguage, |
Daniel Santiago Rivera | 7e7bb0b | 2022-11-07 10:54:49 -0500 | [diff] [blame] | 69 | autoMigration.specClassName |
Daniel Santiago Rivera | 9b41215 | 2022-09-23 22:26:42 -0400 | [diff] [blame] | 70 | ) |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 71 | } else { |
| 72 | null |
Elif Bilgin | e6f17b4 | 2021-04-20 15:32:25 -0700 | [diff] [blame] | 73 | } |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 74 | ) |
Elif Bilgin | c6db95e | 2021-04-07 11:57:50 -0700 | [diff] [blame] | 75 | } |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 76 | addFunction(createConstructor()) |
| 77 | addFunction(createMigrateMethod()) |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 78 | } |
| 79 | return builder |
| 80 | } |
| 81 | |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Builds the constructor of the generated AutoMigration. |
| 84 | * |
| 85 | * @return The constructor of the generated AutoMigration |
| 86 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 87 | private fun createConstructor(): XFunSpec { |
| 88 | return XFunSpec.constructorBuilder(codeLanguage, VisibilityModifier.PUBLIC).apply { |
| 89 | callSuperConstructor( |
| 90 | XCodeBlock.of(codeLanguage, "%L", autoMigration.from), |
| 91 | XCodeBlock.of(codeLanguage, "%L", autoMigration.to), |
Elif Bilgin | 51f4b35 | 2021-04-20 16:02:55 -0700 | [diff] [blame] | 92 | ) |
| 93 | if (autoMigration.isSpecProvided) { |
Elif Bilgin | e6f17b4 | 2021-04-20 15:32:25 -0700 | [diff] [blame] | 94 | addParameter( |
Daniel Santiago Rivera | 7e7bb0b | 2022-11-07 10:54:49 -0500 | [diff] [blame] | 95 | typeName = RoomTypeNames.AUTO_MIGRATION_SPEC, |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 96 | name = "callback", |
Elif Bilgin | e6f17b4 | 2021-04-20 15:32:25 -0700 | [diff] [blame] | 97 | ) |
| 98 | addStatement("this.callback = callback") |
| 99 | } |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 100 | }.build() |
| 101 | } |
| 102 | |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 103 | private fun createMigrateMethod(): XFunSpec { |
| 104 | val migrateFunctionBuilder: XFunSpec.Builder = XFunSpec.builder( |
| 105 | language = codeLanguage, |
| 106 | name = "migrate", |
| 107 | visibility = VisibilityModifier.PUBLIC, |
Daniel Santiago Rivera | 76bf256 | 2022-10-16 12:31:34 -0400 | [diff] [blame] | 108 | isOverride = true, |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 109 | ).apply { |
Daniel Santiago Rivera | 7e7bb0b | 2022-11-07 10:54:49 -0500 | [diff] [blame] | 110 | addParameter( |
| 111 | typeName = SupportDbTypeNames.DB, |
elifbilgin | d62dc35 | 2023-02-03 11:22:25 -0800 | [diff] [blame] | 112 | name = "db", |
Daniel Santiago Rivera | 7e7bb0b | 2022-11-07 10:54:49 -0500 | [diff] [blame] | 113 | ) |
| 114 | addMigrationStatements(this) |
| 115 | if (autoMigration.specClassName != null) { |
elifbilgin | d62dc35 | 2023-02-03 11:22:25 -0800 | [diff] [blame] | 116 | addStatement("callback.onPostMigrate(db)") |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 117 | } |
Daniel Santiago Rivera | 7e7bb0b | 2022-11-07 10:54:49 -0500 | [diff] [blame] | 118 | } |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 119 | return migrateFunctionBuilder.build() |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Takes the changes provided in the {@link AutoMigrationResult} which are differences detected |
| 124 | * between the two versions of the same database, and converts them to the appropriate |
| 125 | * sequence of SQL statements that migrate the database from one version to the other. |
| 126 | * |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 127 | * @param migrateBuilder Builder for the migrate() function to be generated |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 128 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 129 | private fun addMigrationStatements(migrateBuilder: XFunSpec.Builder) { |
Elif Bilgin | 51f4b35 | 2021-04-20 16:02:55 -0700 | [diff] [blame] | 130 | addDropViewStatements(migrateBuilder) |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 131 | addSimpleChangeStatements(migrateBuilder) |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 132 | addComplexChangeStatements(migrateBuilder) |
Elif Bilgin | 51f4b35 | 2021-04-20 16:02:55 -0700 | [diff] [blame] | 133 | addRecreateViewStatements(migrateBuilder) |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Adds SQL statements to drop all views of the database in the 'from' version. |
| 138 | * |
| 139 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 140 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 141 | private fun addDropViewStatements(migrateBuilder: XFunSpec.Builder) { |
Elif Bilgin | 51f4b35 | 2021-04-20 16:02:55 -0700 | [diff] [blame] | 142 | autoMigration.schemaDiff.fromViews.forEach { view -> |
| 143 | addDatabaseExecuteSqlStatement(migrateBuilder, "DROP VIEW ${view.viewName}") |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Adds SQL statements to create all views of the database in the 'to' version. |
| 149 | * |
| 150 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 151 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 152 | private fun addRecreateViewStatements(migrateBuilder: XFunSpec.Builder) { |
Elif Bilgin | 51f4b35 | 2021-04-20 16:02:55 -0700 | [diff] [blame] | 153 | autoMigration.schemaDiff.toViews.forEach { view -> |
| 154 | addDatabaseExecuteSqlStatement(migrateBuilder, view.createView()) |
| 155 | } |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Adds SQL statements performing schema altering commands that are not directly supported by |
| 160 | * SQLite (e.g. foreign key changes). These changes are referred to as "complex" changes. |
| 161 | * |
| 162 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 163 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 164 | private fun addComplexChangeStatements(migrateBuilder: XFunSpec.Builder) { |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 165 | // Create a collection that is sorted such that FTS bundles are handled after the normal |
| 166 | // tables have been processed |
| 167 | complexChangedTables.values.sortedBy { |
| 168 | it.newVersionEntityBundle is FtsEntityBundle |
| 169 | }.forEach { |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 170 | ( |
| 171 | _, |
| 172 | tableNameWithNewPrefix, |
| 173 | oldEntityBundle, |
| 174 | newEntityBundle, |
| 175 | renamedColumnsMap |
| 176 | ) -> |
| 177 | |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 178 | if (oldEntityBundle is FtsEntityBundle && |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 179 | oldEntityBundle.ftsOptions.contentTable.isNotBlank() |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 180 | ) { |
| 181 | addStatementsToMigrateFtsTable( |
| 182 | migrateBuilder, |
| 183 | oldEntityBundle, |
| 184 | newEntityBundle, |
| 185 | renamedColumnsMap |
| 186 | ) |
| 187 | } else { |
| 188 | addStatementsToCreateNewTable(newEntityBundle, migrateBuilder) |
| 189 | addStatementsToContentTransfer( |
| 190 | oldEntityBundle.tableName, |
| 191 | tableNameWithNewPrefix, |
| 192 | oldEntityBundle, |
| 193 | newEntityBundle, |
| 194 | renamedColumnsMap, |
| 195 | migrateBuilder |
| 196 | ) |
| 197 | addStatementsToDropTableAndRenameTempTable( |
| 198 | oldEntityBundle.tableName, |
| 199 | newEntityBundle.tableName, |
| 200 | tableNameWithNewPrefix, |
| 201 | migrateBuilder |
| 202 | ) |
| 203 | addStatementsToRecreateIndexes(newEntityBundle, migrateBuilder) |
| 204 | if (newEntityBundle.foreignKeys.isNotEmpty()) { |
| 205 | addStatementsToCheckForeignKeyConstraint( |
| 206 | newEntityBundle.tableName, |
| 207 | migrateBuilder |
| 208 | ) |
| 209 | } |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 210 | } |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 214 | private fun addStatementsToMigrateFtsTable( |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 215 | migrateBuilder: XFunSpec.Builder, |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 216 | oldTable: EntityBundle, |
| 217 | newTable: EntityBundle, |
| 218 | renamedColumnsMap: MutableMap<String, String> |
| 219 | ) { |
| 220 | addDatabaseExecuteSqlStatement(migrateBuilder, "DROP TABLE `${oldTable.tableName}`") |
| 221 | addDatabaseExecuteSqlStatement(migrateBuilder, newTable.createTable()) |
| 222 | |
| 223 | // Transfer contents of the FTS table, using the content table if available. |
| 224 | val newColumnSequence = oldTable.fieldsByColumnName.keys.filter { |
| 225 | oldTable.fieldsByColumnName.keys.contains(it) || |
| 226 | renamedColumnsMap.containsKey(it) |
| 227 | }.toMutableList() |
| 228 | val oldColumnSequence = mutableListOf<String>() |
| 229 | newColumnSequence.forEach { column -> |
| 230 | oldColumnSequence.add(renamedColumnsMap[column] ?: column) |
| 231 | } |
| 232 | if (oldTable is FtsEntityBundle) { |
| 233 | oldColumnSequence.add("rowid") |
| 234 | newColumnSequence.add("docid") |
| 235 | } |
| 236 | val contentTable = (newTable as FtsEntityBundle).ftsOptions.contentTable |
| 237 | val selectFromTable = if (contentTable.isEmpty()) { |
| 238 | oldTable.tableName |
| 239 | } else { |
| 240 | contentTable |
| 241 | } |
| 242 | addDatabaseExecuteSqlStatement( |
| 243 | migrateBuilder, |
| 244 | buildString { |
| 245 | append( |
Elif Bilgin | 1027998 | 2021-10-26 16:59:42 -0700 | [diff] [blame] | 246 | "INSERT INTO `${newTable.tableName}` " + |
| 247 | "(${newColumnSequence.joinToString(",") { "`$it`" }})" + |
| 248 | " SELECT ${oldColumnSequence.joinToString(",") { "`$it`" }} " + |
| 249 | "FROM `$selectFromTable`", |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 250 | ) |
| 251 | } |
| 252 | ) |
| 253 | } |
| 254 | |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 255 | /** |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 256 | * Adds SQL statements performing schema altering commands directly supported by SQLite |
| 257 | * (adding tables/columns, renaming tables/columns, dropping tables/columns). These changes |
| 258 | * are referred to as "simple" changes. |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 259 | * |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 260 | * @param migrateBuilder Builder for the migrate() function to be generated |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 261 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 262 | private fun addSimpleChangeStatements(migrateBuilder: XFunSpec.Builder) { |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 263 | addDeleteTableStatements(migrateBuilder) |
| 264 | addRenameTableStatements(migrateBuilder) |
| 265 | addNewColumnStatements(migrateBuilder) |
| 266 | addNewTableStatements(migrateBuilder) |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Adds the SQL statements for creating a new table in the desired revised format of table. |
| 271 | * |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 272 | * @param newTable Schema of the new table to be created |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 273 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 274 | */ |
| 275 | private fun addStatementsToCreateNewTable( |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 276 | newTable: EntityBundle, |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 277 | migrateBuilder: XFunSpec.Builder |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 278 | ) { |
| 279 | addDatabaseExecuteSqlStatement( |
| 280 | migrateBuilder, |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 281 | newTable.createNewTable() |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 282 | ) |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Adds the SQL statements for transferring the contents of the old table to the new version. |
| 287 | * |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 288 | * @param oldTableName Name of the table in the old version of the database |
| 289 | * @param tableNameWithNewPrefix Name of the table with the '_new_' prefix added |
| 290 | * @param oldEntityBundle Entity bundle of the table in the old version of the database |
| 291 | * @param newEntityBundle Entity bundle of the table in the new version of the database |
| 292 | * @param renamedColumnsMap Map of the renamed columns of the table (new name -> old name) |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 293 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 294 | */ |
| 295 | private fun addStatementsToContentTransfer( |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 296 | oldTableName: String, |
| 297 | tableNameWithNewPrefix: String, |
| 298 | oldEntityBundle: EntityBundle, |
| 299 | newEntityBundle: EntityBundle, |
| 300 | renamedColumnsMap: MutableMap<String, String>, |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 301 | migrateBuilder: XFunSpec.Builder |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 302 | ) { |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 303 | val newColumnSequence = newEntityBundle.fieldsByColumnName.keys.filter { |
| 304 | oldEntityBundle.fieldsByColumnName.keys.contains(it) || |
| 305 | renamedColumnsMap.containsKey(it) |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 306 | }.toMutableList() |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 307 | val oldColumnSequence = mutableListOf<String>() |
| 308 | newColumnSequence.forEach { column -> |
| 309 | oldColumnSequence.add(renamedColumnsMap[column] ?: column) |
| 310 | } |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 311 | |
| 312 | addDatabaseExecuteSqlStatement( |
| 313 | migrateBuilder, |
| 314 | buildString { |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 315 | append( |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 316 | "INSERT INTO `$tableNameWithNewPrefix` " + |
Elif Bilgin | 1027998 | 2021-10-26 16:59:42 -0700 | [diff] [blame] | 317 | "(${newColumnSequence.joinToString(",") { "`$it`" }})" + |
| 318 | " SELECT ${oldColumnSequence.joinToString(",") { "`$it`" }} FROM " + |
Elif Bilgin | 7a55b71 | 2021-04-06 09:24:33 -0700 | [diff] [blame] | 319 | "`$oldTableName`", |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 320 | ) |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 321 | } |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 322 | ) |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /** |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 326 | * Adds the SQL statements for dropping the table at the old version and renaming the |
| 327 | * temporary table to the name of the original table. |
Elif Bilgin | f2f87f5 | 2021-02-24 09:12:24 -0800 | [diff] [blame] | 328 | * |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 329 | * @param oldTableName Name of the table in the old version of the database |
| 330 | * @param newTableName Name of the table in the new version of the database |
| 331 | * @param tableNameWithNewPrefix Name of the table with the '_new_' prefix added |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 332 | * @param migrateBuilder Builder for the migrate() function to be generated |
Elif Bilgin | f2f87f5 | 2021-02-24 09:12:24 -0800 | [diff] [blame] | 333 | */ |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 334 | private fun addStatementsToDropTableAndRenameTempTable( |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 335 | oldTableName: String, |
| 336 | newTableName: String, |
| 337 | tableNameWithNewPrefix: String, |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 338 | migrateBuilder: XFunSpec.Builder |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 339 | ) { |
| 340 | addDatabaseExecuteSqlStatement( |
| 341 | migrateBuilder, |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 342 | "DROP TABLE `$oldTableName`" |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 343 | ) |
| 344 | addDatabaseExecuteSqlStatement( |
| 345 | migrateBuilder, |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 346 | "ALTER TABLE `$tableNameWithNewPrefix` RENAME TO `$newTableName`" |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 347 | ) |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Adds the SQL statements for recreating indexes. |
| 352 | * |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 353 | * @param table The table the indexes of which will be recreated |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 354 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 355 | */ |
| 356 | private fun addStatementsToRecreateIndexes( |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 357 | table: EntityBundle, |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 358 | migrateBuilder: XFunSpec.Builder |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 359 | ) { |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 360 | table.indices.forEach { index -> |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 361 | addDatabaseExecuteSqlStatement( |
| 362 | migrateBuilder, |
| 363 | index.getCreateSql(table.tableName) |
Elif Bilgin | f2f87f5 | 2021-02-24 09:12:24 -0800 | [diff] [blame] | 364 | ) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 369 | * Adds the SQL statement for checking the foreign key constraints. |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 370 | * |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 371 | * @param tableName Name of the table |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 372 | * @param migrateBuilder Builder for the migrate() function to be generated |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 373 | */ |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 374 | private fun addStatementsToCheckForeignKeyConstraint( |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 375 | tableName: String, |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 376 | migrateBuilder: XFunSpec.Builder |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 377 | ) { |
Elif Bilgin | 7917c78 | 2021-06-07 13:58:31 -0700 | [diff] [blame] | 378 | migrateBuilder.addStatement( |
elifbilgin | f748a17 | 2023-03-02 10:27:43 -0800 | [diff] [blame^] | 379 | "%M(db, %S)", |
| 380 | RoomTypeNames.DB_UTIL.packageMember("foreignKeyCheck"), |
Elif Bilgin | 7917c78 | 2021-06-07 13:58:31 -0700 | [diff] [blame] | 381 | tableName |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 382 | ) |
| 383 | } |
| 384 | |
| 385 | /** |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 386 | * Adds the SQL statements for removing a table. |
| 387 | * |
| 388 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 389 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 390 | private fun addDeleteTableStatements(migrateBuilder: XFunSpec.Builder) { |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 391 | deletedTables.forEach { tableName -> |
| 392 | val deleteTableSql = buildString { |
| 393 | append( |
| 394 | "DROP TABLE `$tableName`" |
| 395 | ) |
| 396 | } |
| 397 | addDatabaseExecuteSqlStatement( |
| 398 | migrateBuilder, |
| 399 | deleteTableSql |
| 400 | ) |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Adds the SQL statements for renaming a table. |
| 406 | * |
| 407 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 408 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 409 | private fun addRenameTableStatements(migrateBuilder: XFunSpec.Builder) { |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 410 | renamedTables.forEach { (oldName, newName) -> |
| 411 | val renameTableSql = buildString { |
| 412 | append( |
| 413 | "ALTER TABLE `$oldName` RENAME TO `$newName`" |
| 414 | ) |
| 415 | } |
| 416 | addDatabaseExecuteSqlStatement( |
| 417 | migrateBuilder, |
| 418 | renameTableSql |
| 419 | ) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /** |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 424 | * Adds the SQL statements for adding new columns to a table. |
| 425 | * |
| 426 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 427 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 428 | private fun addNewColumnStatements(migrateBuilder: XFunSpec.Builder) { |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 429 | addedColumns.forEach { |
| 430 | val addNewColumnSql = buildString { |
| 431 | append( |
Daniel Santiago Rivera | bfa17ff | 2021-09-23 12:10:59 -0700 | [diff] [blame] | 432 | "ALTER TABLE `${it.tableName}` ADD COLUMN `${it.fieldBundle.columnName}` " + |
Elif Bilgin | 87c1e43 | 2021-07-19 10:05:33 -0700 | [diff] [blame] | 433 | "${it.fieldBundle.affinity}" |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 434 | ) |
Daniel Santiago Rivera | bfa17ff | 2021-09-23 12:10:59 -0700 | [diff] [blame] | 435 | if (it.fieldBundle.isNonNull) { |
Elif Bilgin | 87c1e43 | 2021-07-19 10:05:33 -0700 | [diff] [blame] | 436 | append(" NOT NULL") |
| 437 | } |
| 438 | if (it.fieldBundle.defaultValue?.isNotEmpty() == true) { |
| 439 | append(" DEFAULT ${it.fieldBundle.defaultValue}") |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 440 | } else { |
Elif Bilgin | 87c1e43 | 2021-07-19 10:05:33 -0700 | [diff] [blame] | 441 | check( |
| 442 | !it.fieldBundle.isNonNull |
| 443 | ) { "A Non-Null field should always have a default value." } |
| 444 | append(" DEFAULT NULL") |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | addDatabaseExecuteSqlStatement( |
| 448 | migrateBuilder, |
| 449 | addNewColumnSql |
| 450 | ) |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Adds the SQL statements for adding new tables to a database. |
| 456 | * |
| 457 | * @param migrateBuilder Builder for the migrate() function to be generated |
| 458 | */ |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 459 | private fun addNewTableStatements(migrateBuilder: XFunSpec.Builder) { |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 460 | addedTables.forEach { addedTable -> |
| 461 | addDatabaseExecuteSqlStatement( |
| 462 | migrateBuilder, |
| 463 | addedTable.entityBundle.createTable() |
| 464 | ) |
Elif Bilgin | bc4d0b0 | 2021-06-06 12:02:05 -0700 | [diff] [blame] | 465 | addStatementsToRecreateIndexes(addedTable.entityBundle, migrateBuilder) |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Adds the given SQL statements into the generated migrate() function to be executed by the |
| 471 | * database. |
| 472 | * |
| 473 | * @param migrateBuilder Builder for the migrate() function to be generated |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 474 | * @param sql The SQL statement to be executed by the database |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 475 | */ |
| 476 | private fun addDatabaseExecuteSqlStatement( |
Daniel Santiago Rivera | b604ea3 | 2022-08-10 10:06:19 -0700 | [diff] [blame] | 477 | migrateBuilder: XFunSpec.Builder, |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 478 | sql: String |
| 479 | ) { |
| 480 | migrateBuilder.addStatement( |
elifbilgin | d62dc35 | 2023-02-03 11:22:25 -0800 | [diff] [blame] | 481 | "db.execSQL(%S)", |
Elif Bilgin | 26e3f9d | 2021-03-29 05:57:30 -0700 | [diff] [blame] | 482 | sql |
Elif Bilgin | fba75de | 2021-03-09 04:44:10 -0800 | [diff] [blame] | 483 | ) |
Elif Bilgin | 63ffe60 | 2021-02-19 10:46:51 -0800 | [diff] [blame] | 484 | } |
| 485 | } |