Skip to content

Commit d0e461e

Browse files
committed
fix: sqlite migrations error
This issue was caused by an INSERT statement which adds the admin user to the admin group. The admin user is created at initialization, so for new installs it does not exist at the time migration scripts are run. Additionally, new javascript-coded migrations (.mig.js) would not have run for new installs because of redundant (WET; not DRY) code for initial setup. This commit also improves error handling of the migration service, which was necessary to diagnose this issue. In the future, setup always needs to be tested after updating migration scripts, even though migration of existing databases is where most of the issues will happen. Resolves: #639
1 parent 1c5ef01 commit d0e461e

File tree

2 files changed

+27
-57
lines changed

2 files changed

+27
-57
lines changed

src/backend/src/services/database/SqliteDatabaseAccessService.js

+27-45
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
const { es_import_promise } = require("../../fun/dev-console-ui-utils");
2020
const { surrounding_box } = require("../../fun/dev-console-ui-utils");
21+
const { CompositeError } = require("../../util/errorutil");
2122
const structutil = require("../../util/structutil");
2223
const { BaseDatabaseAccessService } = require("./BaseDatabaseAccessService");
2324

@@ -45,51 +46,18 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
4546
// Database upgrade logic
4647
const TARGET_VERSION = 24;
4748

48-
if ( do_setup ) {
49-
this.log.noticeme(`SETUP: creating database at ${this.config.path}`);
50-
const sql_files = [
51-
'0001_create-tables.sql',
52-
'0002_add-default-apps.sql',
53-
'0003_user-permissions.sql',
54-
'0004_sessions.sql',
55-
'0005_background-apps.sql',
56-
'0006_update-apps.sql',
57-
'0007_sessions.sql',
58-
'0008_otp.sql',
59-
'0009_app-prefix-fix.sql',
60-
'0010_add-git-app.sql',
61-
'0011_notification.sql',
62-
'0012_appmetadata.sql',
63-
'0013_protected-apps.sql',
64-
'0014_share.sql',
65-
'0015_group.sql',
66-
'0016_group-permissions.sql',
67-
'0017_publicdirs.sql',
68-
'0018_fix-0003.sql',
69-
'0019_fix-0016.sql',
70-
'0020_dev-center.sql',
71-
'0021_app-owner-id.sql',
72-
'0022_dev-center-max.sql',
73-
'0023_fix-kv.sql',
74-
'0024_default-groups.sql',
75-
'0025_system-user.dbmig.js',
76-
'0026_user-groups.dbmig.js',
77-
].map(p => path_.join(__dirname, 'sqlite_setup', p));
78-
const fs = require('fs');
79-
for ( const filename of sql_files ) {
80-
const basename = path_.basename(filename);
81-
this.log.noticeme(`applying ${basename}`);
82-
const contents = fs.readFileSync(filename, 'utf8');
83-
this.db.exec(contents);
84-
}
85-
await this.db.exec(`PRAGMA user_version = ${TARGET_VERSION};`);
86-
}
87-
88-
const [{ user_version }] = await this._read('PRAGMA user_version');
49+
const [{ user_version }] = do_setup
50+
? [{ user_version: -1 }]
51+
: await this._read('PRAGMA user_version');
8952
this.log.info('database version: ' + user_version);
9053

9154
const upgrade_files = [];
9255

56+
if ( user_version === -1 ) {
57+
upgrade_files.push('0001_create-tables.sql');
58+
upgrade_files.push('0002_add-default-apps.sql');
59+
}
60+
9361
if ( user_version <= 0 ) {
9462
upgrade_files.push('0003_user-permissions.sql');
9563
}
@@ -201,12 +169,26 @@ class SqliteDatabaseAccessService extends BaseDatabaseAccessService {
201169
const contents = fs.readFileSync(filename, 'utf8');
202170
switch ( path_.extname(filename) ) {
203171
case '.sql':
204-
this.db.exec(contents);
172+
const stmts = contents.split(/;\s*\n/);
173+
for ( let i=0 ; i < stmts.length ; i++ ) {
174+
if ( stmts[i].trim() === '' ) continue;
175+
const stmt = stmts[i] + ';';
176+
try {
177+
this.db.exec(stmt);
178+
} catch (e) {
179+
debugger;
180+
throw new CompositeError(`failed to apply: ${basename} at line ${i}`, e);
181+
}
182+
}
205183
break;
206184
case '.js':
207-
await this.run_js_migration_({
208-
filename, contents,
209-
});
185+
try {
186+
await this.run_js_migration_({
187+
filename, contents,
188+
});
189+
} catch (e) {
190+
throw new CompositeError(`failed to apply: ${basename}`, e);
191+
}
210192
break;
211193
default:
212194
throw new Error(

src/backend/src/services/database/sqlite_setup/0024_default-groups.sql

-12
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,3 @@ INSERT INTO `group` (
2020
'{"type": "default", "name": "developer"}',
2121
'{"title": "Developer", "color": "#32a852"}')
2222
;
23-
24-
INSERT INTO `jct_user_group` (
25-
`user_id`,
26-
`group_id`,
27-
`extra`,
28-
`metadata`
29-
) VALUES (
30-
1,
31-
(SELECT `id` FROM `group` WHERE uid='ca342a5e-b13d-4dee-9048-58b11a57cc55'),
32-
'{}',
33-
'{"default": true}'
34-
);

0 commit comments

Comments
 (0)