diff options
Diffstat (limited to 'scripts/db_migrations.js')
-rw-r--r-- | scripts/db_migrations.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/scripts/db_migrations.js b/scripts/db_migrations.js new file mode 100644 index 00000000..df5196b1 --- /dev/null +++ b/scripts/db_migrations.js @@ -0,0 +1,80 @@ +#!/usr/bin/node +const path = require("path"); +const fs = require("fs"); +const { stdout, exit } = require("process"); +const { execIn } = require("./utils.js"); +const { ask } = require("./utils/ask.js"); + +async function main() { + let filename; + if(process.argv[2]) filename = process.argv[2]; + else filename = await ask("Please enter the name of your migration: "); + let dbconf; + try { + dbconf = JSON.parse(fs.readFileSync("dbconf.json")); + } catch (e) { + console.log("No dbconf.json found!"); + dbconf = {}; + } + + if(!dbconf["sqlite"]) + dbconf.sqlite = { + conn_str: "migrations.db", + migrations_dir: "sqlite", + package: "sqlite3" + } + if(!dbconf["postgres"] && process.env.FC_DB_POSTGRES) { + console.log("Found FC_DB_POSTGRES environment variable. Using it!"); + dbconf.postgres = { + conn_str: process.env.FC_DB_POSTGRES, + migrations_dir: "postgres", + package: "pg" + } + } + if(!dbconf["mariadb"] && process.env.FC_DB_MARIADB){ + console.log("Found FC_DB_MARIADB environment variable. Using it!"); + dbconf.mariadb = { + conn_str: process.env.FC_DB_MARIADB, + migrations_dir: "mariadb", + package: "mysql2" + } + } + fs.writeFileSync("dbconf.json", JSON.stringify(dbconf, null, 4)); + + //build + execIn(`node scripts/build_new.js`, process.cwd(), {stdio: "inherit"}); + + if(fs.existsSync(".env") && !fs.existsSync(".env.bak")) + fs.renameSync(".env", ".env.bak"); + Object.keys(dbconf).forEach((db) => { + console.log(`Applying migrations for ${db}`); + if(!fs.existsSync(path.join("node_modules", dbconf[db].package))) + execIn(`npm i ${dbconf[db].package}`, process.cwd()); + fs.writeFileSync( + `.env`, + `DATABASE=${dbconf[db].conn_str} + THREADS=1 + DB_MIGRATE=true + DB_VERBOSE=true` + ); + execIn(`node dist/start.js`, process.cwd(), {stdio: "inherit"}); + }); + + Object.keys(dbconf).forEach((db) => { + console.log(`Generating new migrations for ${db}`); + fs.writeFileSync( + `.env`, + `DATABASE=${dbconf[db].conn_str} + THREADS=1 + DB_MIGRATE=true + DB_VERBOSE=true` + ); + execIn(`node node_modules/typeorm/cli.js migration:generate "src/util/migrations/${db}/${filename}" -d dist/util/util/Database.js -p`, process.cwd(), {stdio: "inherit"}); + }); + if(fs.existsSync(".env.bak")) { + fs.rmSync(".env"); + fs.renameSync(".env.bak", ".env"); + } + exit(0); +} +main(); \ No newline at end of file |