@@ -42,7 +42,7 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
42
42
this . db = new Database ( this . config . path ) ;
43
43
44
44
// Database upgrade logic
45
- const TARGET_VERSION = 22 ;
45
+ const TARGET_VERSION = 23 ;
46
46
47
47
if ( do_setup ) {
48
48
this . log . noticeme ( `SETUP: creating database at ${ this . config . path } ` ) ;
@@ -71,6 +71,7 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
71
71
'0022_dev-center-max.sql' ,
72
72
'0023_fix-kv.sql' ,
73
73
'0024_default-groups.sql' ,
74
+ '0025_system-user.dbmig.js'
74
75
] . map ( p => path_ . join ( __dirname , 'sqlite_setup' , p ) ) ;
75
76
const fs = require ( 'fs' ) ;
76
77
for ( const filename of sql_files ) {
@@ -175,6 +176,10 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
175
176
upgrade_files . push ( '0024_default-groups.sql' ) ;
176
177
}
177
178
179
+ if ( user_version <= 22 ) {
180
+ upgrade_files . push ( '0025_system-user.dbmig.js' ) ;
181
+ }
182
+
178
183
if ( upgrade_files . length > 0 ) {
179
184
this . log . noticeme ( `Database out of date: ${ this . config . path } ` ) ;
180
185
this . log . noticeme ( `UPGRADING DATABASE: ${ user_version } -> ${ TARGET_VERSION } ` ) ;
@@ -188,7 +193,20 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
188
193
const basename = path_ . basename ( filename ) ;
189
194
this . log . noticeme ( `applying ${ basename } ` ) ;
190
195
const contents = fs . readFileSync ( filename , 'utf8' ) ;
191
- this . db . exec ( contents ) ;
196
+ switch ( path_ . extname ( filename ) ) {
197
+ case '.sql' :
198
+ this . db . exec ( contents ) ;
199
+ break ;
200
+ case '.js' :
201
+ await this . run_js_migration_ ( {
202
+ filename, contents,
203
+ } ) ;
204
+ break ;
205
+ default :
206
+ throw new Error (
207
+ `unrecognized migration type: ${ filename } `
208
+ ) ;
209
+ }
192
210
}
193
211
194
212
// Update version number
@@ -273,6 +291,17 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
273
291
return p ;
274
292
} ) ;
275
293
}
294
+
295
+ async run_js_migration_ ( { filename, contents } ) {
296
+ contents = `(async () => {${ contents } })()` ;
297
+ const vm = require ( 'vm' ) ;
298
+ const context = vm . createContext ( {
299
+ read : this . read . bind ( this ) ,
300
+ write : this . write . bind ( this ) ,
301
+ log : this . log ,
302
+ } ) ;
303
+ await vm . runInContext ( contents , context ) ;
304
+ }
276
305
277
306
_register_commands ( commands ) {
278
307
commands . registerCommands ( 'sqlite' , [
0 commit comments