🛂 Add backup and restore database scripts
This commit is contained in:
22
packages/scripts/backupDatabase.ts
Normal file
22
packages/scripts/backupDatabase.ts
Normal file
@ -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()
|
@ -1,22 +0,0 @@
|
||||
import { PrismaClient } from 'db'
|
||||
import path from 'path'
|
||||
import { setCustomPlan } from './setCustomPlan'
|
||||
|
||||
require('dotenv').config({
|
||||
path: path.join(
|
||||
__dirname,
|
||||
process.env.NODE_ENV === 'production'
|
||||
? '.env.production'
|
||||
: process.env.NODE_ENV === 'staging'
|
||||
? '.env.staging'
|
||||
: '.env.local'
|
||||
),
|
||||
})
|
||||
|
||||
const prisma = new PrismaClient({ log: ['query', 'info', 'warn', 'error'] })
|
||||
|
||||
const main = async () => {
|
||||
setCustomPlan()
|
||||
}
|
||||
|
||||
main().then()
|
@ -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",
|
||||
|
9
packages/scripts/playground.ts
Normal file
9
packages/scripts/playground.ts
Normal file
@ -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
packages/scripts/restoreDatabase.ts
Normal file
23
packages/scripts/restoreDatabase.ts
Normal file
@ -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()
|
@ -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 ||
|
||||
@ -81,3 +82,5 @@ export const setCustomPlan = async () => {
|
||||
|
||||
console.log('Claimable plan updated!')
|
||||
}
|
||||
|
||||
setCustomPlan()
|
||||
|
27
packages/scripts/utils.ts
Normal file
27
packages/scripts/utils.ts
Normal file
@ -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}`),
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user