2024-06-19 15:15:27 +00:00
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
|
|
|
|
extension Database {
|
|
|
|
static var migrator: DatabaseMigrator = {
|
|
|
|
var migrator = DatabaseMigrator()
|
|
|
|
|
|
|
|
// flush db on schema change (only in DEV mode)
|
|
|
|
#if DEBUG
|
|
|
|
migrator.eraseDatabaseOnSchemaChange = true
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 1st migration - basic tables
|
|
|
|
migrator.registerMigration("Add basic tables") { db in
|
|
|
|
// accounts
|
|
|
|
try db.create(table: "accounts", options: [.ifNotExists]) { table in
|
|
|
|
table.column("bareJid", .text).notNull().primaryKey().unique(onConflict: .replace)
|
|
|
|
table.column("pass", .text).notNull()
|
|
|
|
table.column("isActive", .boolean).notNull().defaults(to: true)
|
|
|
|
table.column("isTemp", .boolean).notNull().defaults(to: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rosters
|
|
|
|
try db.create(table: "rosterVersions", options: [.ifNotExists]) { table in
|
|
|
|
table.column("bareJid", .text).notNull().primaryKey().unique(onConflict: .replace)
|
|
|
|
table.column("version", .text).notNull()
|
|
|
|
}
|
|
|
|
try db.create(table: "rosters", options: [.ifNotExists]) { table in
|
|
|
|
table.column("bareJid", .text).notNull()
|
|
|
|
table.column("contactBareJid", .text).notNull()
|
|
|
|
table.column("name", .text)
|
|
|
|
table.column("subscription", .text).notNull()
|
|
|
|
table.column("ask", .boolean).notNull().defaults(to: false)
|
|
|
|
table.column("data", .text).notNull()
|
|
|
|
table.primaryKey(["bareJid", "contactBareJid"], onConflict: .replace)
|
|
|
|
table.column("locallyDeleted", .boolean).notNull().defaults(to: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// chats
|
|
|
|
try db.create(table: "chats", options: [.ifNotExists]) { table in
|
|
|
|
table.column("id", .text).notNull().primaryKey().unique(onConflict: .replace)
|
|
|
|
table.column("account", .text).notNull()
|
|
|
|
table.column("participant", .text).notNull()
|
|
|
|
table.column("type", .integer).notNull()
|
|
|
|
}
|
|
|
|
|
|
|
|
// messages
|
|
|
|
try db.create(table: "messages", options: [.ifNotExists]) { table in
|
|
|
|
table.column("id", .text).notNull().primaryKey().unique(onConflict: .replace)
|
2024-06-24 10:44:55 +00:00
|
|
|
table.column("type", .text).notNull()
|
2024-06-24 13:28:26 +00:00
|
|
|
table.column("contentType", .text).notNull()
|
|
|
|
table.column("from", .text).notNull()
|
|
|
|
table.column("to", .text)
|
|
|
|
table.column("body", .text)
|
|
|
|
table.column("subject", .text)
|
|
|
|
table.column("thread", .text)
|
|
|
|
table.column("oobUrl", .text)
|
2024-06-25 11:13:59 +00:00
|
|
|
table.column("date", .datetime).notNull()
|
2024-06-26 07:27:23 +00:00
|
|
|
table.column("pending", .boolean).notNull()
|
2024-06-26 10:26:04 +00:00
|
|
|
table.column("sentError", .boolean).notNull()
|
2024-07-13 13:38:15 +00:00
|
|
|
table.column("attachmentType", .integer)
|
2024-07-14 10:08:51 +00:00
|
|
|
table.column("attachmentLocalName", .text)
|
2024-07-13 13:42:47 +00:00
|
|
|
table.column("attachmentRemotePath", .text)
|
2024-07-14 10:08:51 +00:00
|
|
|
table.column("attachmentThumbnailName", .text)
|
2024-07-13 13:42:47 +00:00
|
|
|
table.column("attachmentDownloadFailed", .boolean).notNull().defaults(to: false)
|
2024-07-11 13:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-29 16:36:22 +00:00
|
|
|
// 2nd migration - channels/rooms
|
|
|
|
migrator.registerMigration("Add channels/rooms") { db in
|
2024-07-30 09:26:30 +00:00
|
|
|
// rooms
|
|
|
|
try db.create(table: "rooms", options: [.ifNotExists]) { table in
|
2024-07-29 16:36:22 +00:00
|
|
|
table.column("id", .text).notNull().primaryKey().unique(onConflict: .replace)
|
2024-08-06 11:15:13 +00:00
|
|
|
table.column("account", .text).notNull()
|
|
|
|
table.column("nickname", .text).notNull()
|
|
|
|
table.column("password", .text)
|
2024-07-29 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 09:26:30 +00:00
|
|
|
// channels
|
|
|
|
// try db.create(table: "channels", options: [.ifNotExists]) { table in
|
2024-07-29 16:36:22 +00:00
|
|
|
// table.column("id", .text).notNull().primaryKey().unique(onConflict: .replace)
|
2024-07-30 09:26:30 +00:00
|
|
|
// table.column("account", .text).notNull()
|
|
|
|
// table.column("channel", .text).notNull()
|
2024-07-29 16:36:22 +00:00
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2024-06-19 15:15:27 +00:00
|
|
|
// return migrator
|
|
|
|
return migrator
|
|
|
|
}()
|
|
|
|
}
|