diff options
author | 2021-08-01 11:07:57 +0000 | |
---|---|---|
committer | 2021-08-01 16:37:57 +0530 | |
commit | 419933f6505caf4c5e685f8436b1ff735185e55a (patch) | |
tree | 152dcb9d2b35d29f862cc57a605b9ae2a0f7c300 /src/internal-server/start/migrate.js | |
parent | Removed duplicated contributors badge. (diff) | |
download | ferdium-app-419933f6505caf4c5e685f8436b1ff735185e55a.tar.gz ferdium-app-419933f6505caf4c5e685f8436b1ff735185e55a.tar.zst ferdium-app-419933f6505caf4c5e685f8436b1ff735185e55a.zip |
Moved 'internal-server' into a sub-folder as opposed to a git submodule. (#1715)
* Ignored tests in 'internal-server' folder since there are none.
* Linter fixes
Diffstat (limited to 'src/internal-server/start/migrate.js')
-rw-r--r-- | src/internal-server/start/migrate.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/internal-server/start/migrate.js b/src/internal-server/start/migrate.js new file mode 100644 index 000000000..6846beef6 --- /dev/null +++ b/src/internal-server/start/migrate.js | |||
@@ -0,0 +1,44 @@ | |||
1 | /** | ||
2 | * Migrate server database to work with current Ferdi version | ||
3 | */ | ||
4 | const Database = use('Database'); | ||
5 | const User = use('App/Models/User'); | ||
6 | |||
7 | const migrateLog = (text) => { | ||
8 | console.log('\x1b[36m%s\x1b[0m', 'Ferdi Migration:', '\x1b[0m', text); | ||
9 | }; | ||
10 | |||
11 | module.exports = async () => { | ||
12 | migrateLog('🧙 Running database migration wizard'); | ||
13 | |||
14 | // Make sure user table exists | ||
15 | await Database.raw('CREATE TABLE IF NOT EXISTS `users` (`id` integer not null primary key autoincrement, `settings` text, `created_at` datetime, `updated_at` datetime);'); | ||
16 | |||
17 | const user = await User.find(1); | ||
18 | let settings; | ||
19 | if (!user) { | ||
20 | migrateLog('🎩 Migrating from old Ferdi version as user doesn\'t exist'); | ||
21 | |||
22 | // Create new user | ||
23 | await Database.raw('INSERT INTO "users" ("id") VALUES (\'1\');'); | ||
24 | } else { | ||
25 | settings = typeof user.settings === 'string' ? JSON.parse(user.settings) : user.settings; | ||
26 | } | ||
27 | |||
28 | if (!settings || !settings.db_version || settings.db_version !== process.env.FERDI_VERSION) { | ||
29 | const srcVersion = settings && settings.db_version ? settings.db_version : '5.4.0-beta.2'; | ||
30 | migrateLog(`🔮 Migrating table from ${srcVersion} to ${process.env.FERDI_VERSION}`); | ||
31 | |||
32 | // Migrate database to current Ferdi version | ||
33 | // Currently no migrations | ||
34 | |||
35 | // Update version number in database | ||
36 | if (!settings) settings = {}; | ||
37 | settings.db_version = process.env.FERDI_VERSION; | ||
38 | const newUser = await User.find(1); // Fetch user again as we might have only just created it | ||
39 | newUser.settings = JSON.stringify(settings); | ||
40 | await newUser.save(); | ||
41 | } else { | ||
42 | migrateLog('🔧 Nothing to migrate, already on the newest version'); | ||
43 | } | ||
44 | }; | ||