Skip to content

Commit

Permalink
chore: clean database before deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed May 2, 2024
1 parent b8f9f87 commit 6173678
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion deployment/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion packages/graphql/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "5",
"when": 1709785362158,
"tag": "0000_hesitant_moon_knight",
"when": 1714691877451,
"tag": "0000_steep_grandmaster",
"breakpoints": true
}
]
Expand Down
1 change: 1 addition & 0 deletions packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions packages/graphql/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

pnpm db:clean && pnpm db:setup && pnpm db:migrate && pnpm server:start
6 changes: 0 additions & 6 deletions packages/graphql/src/app.ts
Original file line number Diff line number Diff line change
@@ -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'));
Expand All @@ -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();
}
});
12 changes: 12 additions & 0 deletions packages/graphql/src/infra/database/Db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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}`;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/graphql/src/infra/server/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 6173678

Please sign in to comment.