aboutsummaryrefslogtreecommitdiff
path: root/src/scripts
diff options
context:
space:
mode:
authorPeerawas Archavanuntakun <[email protected]>2022-01-06 03:30:32 +0700
committerGitHub <[email protected]>2022-01-05 14:30:32 -0600
commit51d378e4cb4584196b14132b4d330b8d370f8fb3 (patch)
tree176959c304bcb28ef33df0a3546bb4b88d691b09 /src/scripts
parent129d121364c7e976c7bf5e2da3976da230058d77 (diff)
downloadmuse-51d378e4cb4584196b14132b4d330b8d370f8fb3.tar.xz
muse-51d378e4cb4584196b14132b4d330b8d370f8fb3.zip
Setup and migrate to Prisma (#456)
Diffstat (limited to 'src/scripts')
-rw-r--r--src/scripts/migrate-and-start.ts83
-rw-r--r--src/scripts/run-with-database-url.ts13
-rw-r--r--src/scripts/start.ts9
3 files changed, 105 insertions, 0 deletions
diff --git a/src/scripts/migrate-and-start.ts b/src/scripts/migrate-and-start.ts
new file mode 100644
index 0000000..d971745
--- /dev/null
+++ b/src/scripts/migrate-and-start.ts
@@ -0,0 +1,83 @@
+// This script applies Prisma migrations
+// and then starts Muse.
+import dotenv from 'dotenv';
+dotenv.config();
+
+import {execa, ExecaError} from 'execa';
+import {promises as fs} from 'fs';
+import Prisma from '@prisma/client';
+import ora from 'ora';
+import {startBot} from '../index.js';
+import logBanner from '../utils/log-banner.js';
+import {createDatabasePath} from '../utils/create-database-url.js';
+import {DATA_DIR} from '../services/config.js';
+
+const client = new Prisma.PrismaClient();
+
+const migrateFromSequelizeToPrisma = async () => {
+ await execa('prisma', ['migrate', 'resolve', '--applied', '20220101155430_migrate_from_sequelize'], {preferLocal: true});
+};
+
+const doesUserHaveExistingDatabase = async () => {
+ try {
+ await fs.access(createDatabasePath(DATA_DIR));
+
+ return true;
+ } catch {
+ return false;
+ }
+};
+
+const hasDatabaseBeenMigratedToPrisma = async () => {
+ try {
+ await client.$queryRaw`SELECT COUNT(id) FROM _prisma_migrations`;
+ } catch (error: unknown) {
+ if (error instanceof Prisma.Prisma.PrismaClientKnownRequestError && error.code === 'P2010') {
+ // Table doesn't exist
+ return false;
+ }
+
+ throw error;
+ }
+
+ return true;
+};
+
+(async () => {
+ // Banner
+ logBanner();
+
+ const spinner = ora('Applying database migrations...').start();
+
+ if (await doesUserHaveExistingDatabase()) {
+ if (!(await hasDatabaseBeenMigratedToPrisma())) {
+ try {
+ await migrateFromSequelizeToPrisma();
+ } catch (error) {
+ if ((error as ExecaError).stderr) {
+ spinner.fail('Failed to apply database migrations (going from Sequelize to Prisma):');
+ console.error((error as ExecaError).stderr);
+ process.exit(1);
+ } else {
+ throw error;
+ }
+ }
+ }
+ }
+
+ try {
+ await execa('prisma', ['migrate', 'deploy'], {preferLocal: true});
+ } catch (error: unknown) {
+ if ((error as ExecaError).stderr) {
+ spinner.fail('Failed to apply database migrations:');
+ console.error((error as ExecaError).stderr);
+ process.exit(1);
+ } else {
+ throw error;
+ }
+ }
+
+ spinner.succeed('Database migrations applied.');
+
+ await startBot();
+})();
diff --git a/src/scripts/run-with-database-url.ts b/src/scripts/run-with-database-url.ts
new file mode 100644
index 0000000..b7ae3a7
--- /dev/null
+++ b/src/scripts/run-with-database-url.ts
@@ -0,0 +1,13 @@
+import {DATA_DIR} from '../services/config.js';
+import createDatabaseUrl from '../utils/create-database-url.js';
+import {execa} from 'execa';
+
+process.env.DATABASE_URL = createDatabaseUrl(DATA_DIR);
+
+(async () => {
+ await execa(process.argv[2], process.argv.slice(3), {
+ preferLocal: true,
+ stderr: process.stderr,
+ stdout: process.stdout,
+ });
+})();
diff --git a/src/scripts/start.ts b/src/scripts/start.ts
new file mode 100644
index 0000000..a9e294e
--- /dev/null
+++ b/src/scripts/start.ts
@@ -0,0 +1,9 @@
+// This script is mainly used during development.
+// Starts Muse without applying database migrations.
+import {startBot} from '../index.js';
+import logBanner from '../utils/log-banner.js';
+
+(async () => {
+ logBanner();
+ await startBot();
+})();