Skip to content

Commit

Permalink
fix: throw on migration error (#10439)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Krick <matt.krick@gmail.com>
  • Loading branch information
mattkrick authored Oct 31, 2024
1 parent f10b58e commit 90403c6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
16 changes: 11 additions & 5 deletions scripts/toolboxSrc/preDeploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dotenv from 'dotenv'
import dotenvExpand from 'dotenv-expand'
import getKysely from 'parabol-server/postgres/getKysely'
import {Logger} from 'parabol-server/utils/Logger'
import path from 'path'
import queryMap from '../../queryMap.json'
import getProjectRoot from '../webpack/utils/getProjectRoot'
Expand All @@ -12,7 +13,7 @@ import standaloneMigrations from './standaloneMigrations'
const PROJECT_ROOT = getProjectRoot()

const storePersistedQueries = async () => {
console.log('πŸ”— QueryMap Persistence Started')
Logger.log('πŸ”— QueryMap Persistence Started')
const hashes = Object.keys(queryMap)
const now = new Date()
const records = hashes.map((hash) => ({
Expand All @@ -22,23 +23,28 @@ const storePersistedQueries = async () => {
}))

const pg = getKysely()
const res = await pg.insertInto('QueryMap').values(records).onConflict((oc) => oc.doNothing()).returning('id').execute()
console.log(`πŸ”— QueryMap Persistence Complete: ${res.length} records added`)
const res = await pg
.insertInto('QueryMap')
.values(records)
.onConflict((oc) => oc.doNothing())
.returning('id')
.execute()
Logger.log(`πŸ”— QueryMap Persistence Complete: ${res.length} records added`)
}

const preDeploy = async () => {
// .env is typically only used in testing prod deploys
const envPath = path.join(PROJECT_ROOT, '.env')
const myEnv = dotenv.config({path: envPath})
dotenvExpand(myEnv)
console.log(`πŸš€ Predeploy Started v${__APP_VERSION__} sha:${__COMMIT_HASH__}`)
Logger.log(`πŸš€ Predeploy Started v${__APP_VERSION__} sha:${__COMMIT_HASH__}`)
// first we migrate DBs & add env vars to client assets
await Promise.all([standaloneMigrations(), applyEnvVarsToClientAssets()])

// The we can prime the DB & CDN
await Promise.all([storePersistedQueries(), primeIntegrations(), pushToCDN()])
await getKysely().destroy()
console.log(`πŸš€ Predeploy Complete`)
Logger.log(`πŸš€ Predeploy Complete`)
process.exit()
}

Expand Down
13 changes: 7 additions & 6 deletions scripts/toolboxSrc/standaloneMigrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// This file is bundled by webpack into a small migrate.js file which includes all migration files & their deps
// It is used by PPMIs who are only provided with the bundles
import {Migrator} from 'kysely'
import {Logger} from 'parabol-server/utils/Logger'
import path from 'path'
import {migrations} from '../../.config/kyselyMigrations'
import getKysely from '../../packages/server/postgres/getKysely'
import '../webpack/utils/dotenv'
import pgEnsureExtensions from './pgEnsureExtensions'

const migratePG = async () => {
console.log('🐘 Postgres Migration Started')
Logger.log('🐘 Postgres Migration Started')
await pgEnsureExtensions()
// pgm uses a dynamic require statement, which doesn't work with webpack
// if we ignore that dynamic require, we'd still have to include the migrations directory AND any dependencies it might have
Expand Down Expand Up @@ -38,17 +39,17 @@ const migratePG = async () => {

results?.forEach((it) => {
if (it.status === 'Success') {
console.log(` βœ… Migration: ${it.migrationName}`)
Logger.log(` βœ… Migration: ${it.migrationName}`)
} else if (it.status === 'Error') {
console.error(` ⛔️ Migration: ${it.migrationName}`)
Logger.error(` ⛔️ Migration: ${it.migrationName}`)
}
})

if (error) {
console.log('🐘 Postgres Migration Failed')
console.error(error)
Logger.log('🐘 Postgres Migration Failed')
throw error
} else {
console.log('🐘 Postgres Migration Complete')
Logger.log('🐘 Postgres Migration Complete')
}
}

Expand Down

0 comments on commit 90403c6

Please sign in to comment.