Skip to content

Commit

Permalink
🛂 Add backup and restore database scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Nov 23, 2022
1 parent d80cc1b commit 3645607
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ firebaseServiceAccount.json
tags

dump.sql
dump.tar

__env.js
22 changes: 22 additions & 0 deletions packages/scripts/backupDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { exec } from 'child_process'
import { promptAndSetEnvironment } from './utils'

const backupDatabase = async () => {
await promptAndSetEnvironment()
exec(
`pg_dump ${process.env.DATABASE_URL} -F c > dump.tar`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
console.log(`stdout: ${stdout}`)
}
)
}

backupDatabase()
22 changes: 0 additions & 22 deletions packages/scripts/index.ts

This file was deleted.

10 changes: 6 additions & 4 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
"license": "AGPL-3.0-or-later",
"private": true,
"scripts": {
"start:local": "tsx index.ts",
"start:staging": "NODE_ENV=staging tsx index.ts",
"start:prod": "NODE_ENV=production tsx index.ts"
"playground": "tsx playground.ts",
"db:backup": "tsx backupDatabase.ts",
"db:restore": "tsx restoreDatabase.ts",
"db:setCustomPlan": "tsx setCustomPlan.ts"
},
"devDependencies": {
"@types/node": "18.11.9",
"axios": "^1.1.3",
"@types/prompts": "^2.4.1",
"db": "workspace:*",
"emails": "workspace:*",
"got": "12.5.3",
"models": "workspace:*",
"prompts": "^2.4.2",
"stripe": "11.1.0",
"tsx": "3.12.1",
"typescript": "4.9.3",
Expand Down
9 changes: 9 additions & 0 deletions packages/scripts/playground.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PrismaClient } from 'db'
import { promptAndSetEnvironment } from './utils'

const executePlayground = async () => {
await promptAndSetEnvironment()
const prisma = new PrismaClient({ log: ['query', 'info', 'warn', 'error'] })
}

executePlayground()
23 changes: 23 additions & 0 deletions packages/scripts/restoreDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { exec } from 'child_process'
import { promptAndSetEnvironment } from './utils'

const restoreDatabase = async () => {
await promptAndSetEnvironment()

exec(
`pg_restore -d ${process.env.DATABASE_URL} -c dump.tar`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
console.log(`stdout: ${stdout}`)
}
)
}

restoreDatabase()
9 changes: 6 additions & 3 deletions packages/scripts/setCustomPlan.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Plan, PrismaClient } from 'db'
import Stripe from 'stripe'
import { promptAndSetEnvironment } from './utils'

const prisma = new PrismaClient()

export const setCustomPlan = async () => {
const setCustomPlan = async () => {
await promptAndSetEnvironment()
const prisma = new PrismaClient()
if (
!process.env.STRIPE_SECRET_KEY ||
!process.env.STRIPE_PRODUCT_ID ||
Expand Down Expand Up @@ -81,3 +82,5 @@ export const setCustomPlan = async () => {

console.log('Claimable plan updated!')
}

setCustomPlan()
27 changes: 27 additions & 0 deletions packages/scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { join } from 'path'
import prompts from 'prompts'
import { isEmpty } from 'utils'

export const promptAndSetEnvironment = async () => {
const response = await prompts({
type: 'select',
name: 'env',
message: 'Pick an environment',
choices: [
{
title: 'Local',
value: 'local',
},
{ title: 'Staging', value: 'staging' },
{ title: 'Production', value: 'production' },
],
initial: 0,
})

if (isEmpty(response.env)) process.exit()

require('dotenv').config({
override: true,
path: join(__dirname, `.env.${response.env}`),
})
}
23 changes: 11 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"outputs": []
},
"dev": {
"dependsOn": ["^dev", "^db:generate", "^db:push"],
"dependsOn": ["^dev", "db#db:generate", "db#db:push"],
"cache": false
},
"build": {
"dependsOn": ["^build", "^db:generate"],
"dependsOn": ["^build", "db#db:generate"],
"outputs": [".next/**", "dist/**", "build/**"],
"outputMode": "new-only"
},
Expand Down

0 comments on commit 3645607

Please sign in to comment.