90 lines
3.9 KiB
Swift
90 lines
3.9 KiB
Swift
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)
|
|
table.column("type", .text).notNull()
|
|
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)
|
|
table.column("date", .datetime).notNull()
|
|
table.column("pending", .boolean).notNull()
|
|
table.column("sentError", .boolean).notNull()
|
|
}
|
|
|
|
// attachments
|
|
try db.create(table: "attachments", options: [.ifNotExists]) { table in
|
|
table.column("id", .text).notNull().primaryKey().unique(onConflict: .replace)
|
|
table.column("type", .integer).notNull()
|
|
table.column("localPath", .text)
|
|
table.column("remotePath", .text)
|
|
table.column("localThumbnailPath", .text)
|
|
}
|
|
}
|
|
|
|
// 2nd migration - add foreign key constraints
|
|
migrator.registerMigration("Add foreign keys") { db in
|
|
// messages to attachments
|
|
try db.alter(table: "messages") { table in
|
|
table.add(column: "attachmentId", .text).references("attachments", onDelete: .cascade)
|
|
}
|
|
|
|
// attachments to messsages
|
|
try db.alter(table: "attachments") { table in
|
|
table.add(column: "messageId", .text).references("messages", onDelete: .cascade)
|
|
}
|
|
}
|
|
|
|
// return migrator
|
|
return migrator
|
|
}()
|
|
}
|