From 6173678b918db4f9855714e5dc987c65f8705a6d Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Thu, 2 May 2024 20:27:24 -0300 Subject: [PATCH] chore: clean database before deploy --- deployment/Dockerfile | 2 +- ...nt_moon_knight.sql => 0000_steep_grandmaster.sql} | 0 packages/graphql/drizzle/meta/0000_snapshot.json | 2 +- packages/graphql/drizzle/meta/_journal.json | 4 ++-- packages/graphql/package.json | 1 + packages/graphql/scripts/run.sh | 3 +++ packages/graphql/src/app.ts | 6 ------ packages/graphql/src/infra/database/Db.ts | 12 ++++++++++++ packages/graphql/src/infra/server/Program.ts | 10 ++++++++++ 9 files changed, 30 insertions(+), 10 deletions(-) rename packages/graphql/drizzle/{0000_hesitant_moon_knight.sql => 0000_steep_grandmaster.sql} (100%) create mode 100755 packages/graphql/scripts/run.sh diff --git a/deployment/Dockerfile b/deployment/Dockerfile index 50e69286c..26f6f3757 100644 --- a/deployment/Dockerfile +++ b/deployment/Dockerfile @@ -34,4 +34,4 @@ RUN pnpm db:setup # Expose the specified port EXPOSE ${SERVER_PORT} WORKDIR /app-explorer -CMD ["pnpm", "server:start"] +CMD ["/app-explorer/scripts/run.sh"] diff --git a/packages/graphql/drizzle/0000_hesitant_moon_knight.sql b/packages/graphql/drizzle/0000_steep_grandmaster.sql similarity index 100% rename from packages/graphql/drizzle/0000_hesitant_moon_knight.sql rename to packages/graphql/drizzle/0000_steep_grandmaster.sql diff --git a/packages/graphql/drizzle/meta/0000_snapshot.json b/packages/graphql/drizzle/meta/0000_snapshot.json index aea7ada8a..61b40bc81 100644 --- a/packages/graphql/drizzle/meta/0000_snapshot.json +++ b/packages/graphql/drizzle/meta/0000_snapshot.json @@ -1,5 +1,5 @@ { - "id": "841c62d8-fa00-42aa-9958-f5278e0548de", + "id": "f5d8eb05-58d4-4e1a-8158-0252eebea1e5", "prevId": "00000000-0000-0000-0000-000000000000", "version": "5", "dialect": "pg", diff --git a/packages/graphql/drizzle/meta/_journal.json b/packages/graphql/drizzle/meta/_journal.json index 4e76d424f..2c6c75f2d 100644 --- a/packages/graphql/drizzle/meta/_journal.json +++ b/packages/graphql/drizzle/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "5", - "when": 1709785362158, - "tag": "0000_hesitant_moon_knight", + "when": 1714691877451, + "tag": "0000_steep_grandmaster", "breakpoints": true } ] diff --git a/packages/graphql/package.json b/packages/graphql/package.json index d039cca03..4d8cd116b 100644 --- a/packages/graphql/package.json +++ b/packages/graphql/package.json @@ -30,6 +30,7 @@ "db:migrate": "tsx ./src/cli.ts migrate", "db:push": "drizzle-kit push:pg", "db:setup": "run-s codegen db:generate", + "db:clean": "tsx ./src/cli.ts clean-db", "dev": "tsx watch src/app.ts --inspect", "dev:inspect": "tsx --inspect-brk src/app.ts", "server:start": "pm2 start ecosystem.config.cjs --no-daemon", diff --git a/packages/graphql/scripts/run.sh b/packages/graphql/scripts/run.sh new file mode 100755 index 000000000..a682d7bc1 --- /dev/null +++ b/packages/graphql/scripts/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +pnpm db:clean && pnpm db:setup && pnpm db:migrate && pnpm server:start diff --git a/packages/graphql/src/app.ts b/packages/graphql/src/app.ts index 66b0fd89a..858460012 100644 --- a/packages/graphql/src/app.ts +++ b/packages/graphql/src/app.ts @@ -1,6 +1,5 @@ import { env } from './config'; import { GraphQLServer } from './graphql/GraphQLServer'; -import { db } from './infra/database/Db'; import { Server } from './infra/server/App'; const port = Number(env.get('SERVER_PORT')); @@ -16,9 +15,4 @@ httpServer.listen(app, port).then(async () => { console.log( `📟 GraphQL server is running on http://localhost:${port}${yoga.graphqlEndpoint}`, ); - - if (env.get('DB_MIGRATE')) { - console.log('📦 Running database migrations...'); - await db.migrate(); - } }); diff --git a/packages/graphql/src/infra/database/Db.ts b/packages/graphql/src/infra/database/Db.ts index 3929bbf3b..a3ec61ca9 100644 --- a/packages/graphql/src/infra/database/Db.ts +++ b/packages/graphql/src/infra/database/Db.ts @@ -4,6 +4,7 @@ import { migrate } from 'drizzle-orm/node-postgres/migrator'; import { Client } from 'pg'; import { env } from '~/config'; +import { sql } from 'drizzle-orm'; import * as DbSchema from './DbSchema'; const DB_HOST = env.get('DB_HOST'); @@ -51,6 +52,17 @@ export class Db { }); } + async clean() { + const query = sql` + DROP SCHEMA public CASCADE; + CREATE SCHEMA public; + DROP SCHEMA pgboss CASCADE; + `; + + console.log('🚨 Cleaning database...'); + await this.connection().execute(query); + } + connectionString() { return `postgres://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}`; } diff --git a/packages/graphql/src/infra/server/Program.ts b/packages/graphql/src/infra/server/Program.ts index 49300bdcd..2e5d25087 100644 --- a/packages/graphql/src/infra/server/Program.ts +++ b/packages/graphql/src/infra/server/Program.ts @@ -69,6 +69,16 @@ export class Program { await db.close(); }, }) + .command({ + command: 'clean-db', + describe: 'Clean database', + handler: async () => { + await db.connect(); + await db.clean(); + await db.close(); + }, + }) + .help('help') .parse(); }