|
| 1 | +package code.api.util.migration |
| 2 | + |
| 3 | +import code.api.util.APIUtil |
| 4 | +import code.api.util.migration.Migration.{DbFunction, saveLog} |
| 5 | +import code.chat.ChatRoom |
| 6 | +import net.liftweb.common.Full |
| 7 | +import net.liftweb.db.DB |
| 8 | +import net.liftweb.mapper.Schemifier |
| 9 | +import net.liftweb.util.DefaultConnectionIdentifier |
| 10 | + |
| 11 | +object MigrationOfChatRoomIsOpenRoom { |
| 12 | + |
| 13 | + /** |
| 14 | + * Migrate the old `allusersareparticipants` column to the new `isopenroom` column. |
| 15 | + * |
| 16 | + * Schemifier will have already created the new `isopenroom` column (defaulting to false). |
| 17 | + * This migration copies data from the old column and then drops it. |
| 18 | + * |
| 19 | + * If the old column does not exist (fresh install), this is a no-op. |
| 20 | + */ |
| 21 | + def migrateColumn(name: String): Boolean = { |
| 22 | + DbFunction.tableExists(ChatRoom) match { |
| 23 | + case true => |
| 24 | + val startDate = System.currentTimeMillis() |
| 25 | + val commitId: String = APIUtil.gitCommit |
| 26 | + |
| 27 | + // Check if the old column exists before attempting migration |
| 28 | + val oldColumnExists = try { |
| 29 | + DB.use(DefaultConnectionIdentifier) { conn => |
| 30 | + val rs = conn.getMetaData.getColumns(null, null, "chatroom", "allusersareparticipants") |
| 31 | + val exists = rs.next() |
| 32 | + rs.close() |
| 33 | + exists |
| 34 | + } |
| 35 | + } catch { |
| 36 | + case _: Throwable => false |
| 37 | + } |
| 38 | + |
| 39 | + if (!oldColumnExists) { |
| 40 | + val endDate = System.currentTimeMillis() |
| 41 | + val comment = "Old column allusersareparticipants does not exist (fresh install). No migration needed." |
| 42 | + saveLog(name, commitId, true, startDate, endDate, comment) |
| 43 | + return true |
| 44 | + } |
| 45 | + |
| 46 | + var isSuccessful = false |
| 47 | + |
| 48 | + val executedSql = |
| 49 | + DbFunction.maybeWrite(true, Schemifier.infoF _) { |
| 50 | + APIUtil.getPropsValue("db.driver") match { |
| 51 | + case Full(dbDriver) if dbDriver.contains("com.microsoft.sqlserver.jdbc.SQLServerDriver") => |
| 52 | + () => |
| 53 | + """ |
| 54 | + |UPDATE chatroom SET isopenroom = allusersareparticipants WHERE allusersareparticipants IS NOT NULL; |
| 55 | + |ALTER TABLE chatroom DROP COLUMN allusersareparticipants; |
| 56 | + |""".stripMargin |
| 57 | + case _ => |
| 58 | + // PostgreSQL and MySQL |
| 59 | + () => |
| 60 | + """ |
| 61 | + |UPDATE chatroom SET isopenroom = allusersareparticipants WHERE allusersareparticipants IS NOT NULL; |
| 62 | + |ALTER TABLE chatroom DROP COLUMN allusersareparticipants; |
| 63 | + |""".stripMargin |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + val endDate = System.currentTimeMillis() |
| 68 | + val comment: String = |
| 69 | + s"""Executed SQL: |
| 70 | + |$executedSql |
| 71 | + |""".stripMargin |
| 72 | + isSuccessful = true |
| 73 | + saveLog(name, commitId, isSuccessful, startDate, endDate, comment) |
| 74 | + isSuccessful |
| 75 | + |
| 76 | + case false => |
| 77 | + val startDate = System.currentTimeMillis() |
| 78 | + val commitId: String = APIUtil.gitCommit |
| 79 | + val isSuccessful = false |
| 80 | + val endDate = System.currentTimeMillis() |
| 81 | + val comment: String = |
| 82 | + s"""${ChatRoom._dbTableNameLC} table does not exist""" |
| 83 | + saveLog(name, commitId, isSuccessful, startDate, endDate, comment) |
| 84 | + isSuccessful |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments