diff options
Diffstat (limited to 'scripts/db_migrations.js')
-rw-r--r-- | scripts/db_migrations.js | 124 |
1 files changed, 63 insertions, 61 deletions
diff --git a/scripts/db_migrations.js b/scripts/db_migrations.js index df5196b1..fe92a532 100644 --- a/scripts/db_migrations.js +++ b/scripts/db_migrations.js @@ -6,75 +6,77 @@ 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 = {}; - } + 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)); + 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"}); + //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} + 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"}); - }); + ); + 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} + 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); + ); + 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 +main(); |