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