@@ -50,73 +50,69 @@ struct ReminderTag: Codable, FetchableRecord, MutablePersistableRecord {
5050 var tagID : Int64 ?
5151}
5252
53- extension DatabaseReader where Self == DatabaseQueue {
54- static var appDatabase : Self {
55- let databaseQueue : DatabaseQueue
56- var configuration = Configuration ( )
57- configuration. foreignKeysEnabled = true
58- configuration. prepareDatabase { db in
59- #if DEBUG
60- db. trace ( options: . profile) {
61- print ( $0. expandedDescription)
62- }
63- #endif
64- }
65- if ProcessInfo . processInfo. environment [ " XCODE_RUNNING_FOR_PREVIEWS " ] == nil && !isTesting {
66- let path = URL . documentsDirectory. appending ( component: " db.sqlite " ) . path ( )
67- print ( " open " , path)
68- databaseQueue = try ! DatabaseQueue ( path: path, configuration: configuration)
69- } else {
70- databaseQueue = try ! DatabaseQueue ( configuration: configuration)
71- }
72- var migrator = DatabaseMigrator ( )
53+ func appDatabase( inMemory: Bool = false ) throws -> any DatabaseWriter {
54+ let database : any DatabaseWriter
55+ var configuration = Configuration ( )
56+ configuration. foreignKeysEnabled = true
57+ configuration. prepareDatabase { db in
7358 #if DEBUG
74- migrator. eraseDatabaseOnSchemaChange = true
75- #endif
76- defer {
77- #if DEBUG
78- migrator. registerMigration ( " Add mock data " ) { db in
79- try db. createMockData ( )
59+ db. trace ( options: . profile) {
60+ print ( $0. expandedDescription)
8061 }
81- #endif
82- try ! migrator. migrate ( databaseQueue)
62+ #endif
63+ }
64+ if inMemory {
65+ database = try DatabaseQueue ( configuration: configuration)
66+ } else {
67+ let path = URL . documentsDirectory. appending ( component: " db.sqlite " ) . path ( )
68+ print ( " open " , path)
69+ database = try DatabasePool ( path: path, configuration: configuration)
70+ }
71+ var migrator = DatabaseMigrator ( )
72+ #if DEBUG
73+ migrator. eraseDatabaseOnSchemaChange = true
74+ #endif
75+ migrator. registerMigration ( " Add reminders lists table " ) { db in
76+ try db. create ( table: RemindersList . databaseTableName) { table in
77+ table. autoIncrementedPrimaryKey ( " id " )
78+ table. column ( " color " , . integer) . defaults ( to: 0x4a99ef ) . notNull ( )
79+ table. column ( " name " , . text) . notNull ( )
8380 }
84- migrator. registerMigration ( " Add reminders lists table " ) { db in
85- try db. create ( table: RemindersList . databaseTableName) { table in
86- table. autoIncrementedPrimaryKey ( " id " )
87- table. column ( " color " , . integer) . defaults ( to: 0x4a99ef ) . notNull ( )
88- table. column ( " name " , . text) . notNull ( )
89- }
81+ }
82+ migrator. registerMigration ( " Add reminders table " ) { db in
83+ try db. create ( table: Reminder . databaseTableName) { table in
84+ table. autoIncrementedPrimaryKey ( " id " )
85+ table. column ( " date " , . date)
86+ table. column ( " isCompleted " , . boolean) . defaults ( to: false ) . notNull ( )
87+ table. column ( " isFlagged " , . boolean) . defaults ( to: false ) . notNull ( )
88+ table. column ( " listID " , . integer)
89+ . references ( RemindersList . databaseTableName, column: " id " , onDelete: . cascade)
90+ . notNull ( )
91+ table. column ( " notes " , . text) . notNull ( )
92+ table. column ( " priority " , . integer)
93+ table. column ( " title " , . text) . notNull ( )
9094 }
91- migrator. registerMigration ( " Add reminders table " ) { db in
92- try db. create ( table: Reminder . databaseTableName) { table in
93- table. autoIncrementedPrimaryKey ( " id " )
94- table. column ( " date " , . date)
95- table. column ( " isCompleted " , . boolean) . defaults ( to: false ) . notNull ( )
96- table. column ( " isFlagged " , . boolean) . defaults ( to: false ) . notNull ( )
97- table. column ( " listID " , . integer)
98- . references ( RemindersList . databaseTableName, column: " id " , onDelete: . cascade)
99- . notNull ( )
100- table. column ( " notes " , . text) . notNull ( )
101- table. column ( " priority " , . integer)
102- table. column ( " title " , . text) . notNull ( )
103- }
95+ }
96+ migrator. registerMigration ( " Add tags table " ) { db in
97+ try db. create ( table: Tag . databaseTableName) { table in
98+ table. autoIncrementedPrimaryKey ( " id " )
99+ table. column ( " name " , . text) . notNull ( ) . collate ( . nocase) . unique ( )
104100 }
105- migrator. registerMigration ( " Add tags table " ) { db in
106- try db. create ( table: Tag . databaseTableName) { table in
107- table. autoIncrementedPrimaryKey ( " id " )
108- table. column ( " name " , . text) . notNull ( ) . collate ( . nocase) . unique ( )
109- }
110- try db. create ( table: ReminderTag . databaseTableName) { table in
111- table. column ( " reminderID " , . integer) . notNull ( )
112- . references ( Reminder . databaseTableName, column: " id " , onDelete: . cascade)
113- table. column ( " tagID " , . integer) . notNull ( )
114- . references ( Tag . databaseTableName, column: " id " , onDelete: . cascade)
115- }
101+ try db. create ( table: ReminderTag . databaseTableName) { table in
102+ table. column ( " reminderID " , . integer) . notNull ( )
103+ . references ( Reminder . databaseTableName, column: " id " , onDelete: . cascade)
104+ table. column ( " tagID " , . integer) . notNull ( )
105+ . references ( Tag . databaseTableName, column: " id " , onDelete: . cascade)
116106 }
117-
118- return databaseQueue
119107 }
108+ #if DEBUG
109+ migrator. registerMigration ( " Add mock data " ) { db in
110+ try db. createMockData ( )
111+ }
112+ #endif
113+ try migrator. migrate ( database)
114+
115+ return database
120116}
121117
122118#if DEBUG
0 commit comments