-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcreate-db.js
111 lines (97 loc) · 2.79 KB
/
create-db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const fs = require('fs')
const path = require('path')
const { createClient, getConfig, getEnv } = require('../server/db')
// See https://www.postgresql.org/docs/13/errcodes-appendix.html
const DUPLICATE_DATABASE_ERROR = '42P04'
bootstrapDatabase()
async function bootstrapDatabase() {
const targetEnv = getEnv()
if (targetEnv === 'production') {
throw new Error('This script should not be used in production!')
}
const config = getConfig(targetEnv)
await createDatabase(config)
await migrateToLatestSchemas(config)
await runDataSeeders(config)
}
async function createDatabase(config) {
const { database } = config.connection
let db
try {
// Connect with system database selected
db = createClient({
...config,
connection: {
...config.connection,
database: 'postgres'
}
})
// Create the database if it doesn't already exist
await db.raw(`CREATE DATABASE ${database}`)
console.log(`Created database "${database}"!`)
} catch (error) {
if (error.code === DUPLICATE_DATABASE_ERROR) {
console.warn(`Error creating database "${database}": it already exists!`)
} else {
console.error(`Error creating database "${database}": ${error.message}`)
throw error
}
} finally {
// Disconnect
await db.destroy()
}
}
async function migrateToLatestSchemas(config) {
const { database } = config.connection
let db
try {
db = createClient(config)
await db.migrate.latest()
console.log(`Migrated database "${database}" to latest schemas!`)
} catch (error) {
console.error(
`Error migrating to latest schemas for database "${database}": ${error.message}`
)
throw error
} finally {
// Disconnect
await db.destroy()
}
}
// eslint-disable-next-line no-unused-vars
async function runDataSeeders(config) {
const { database } = config.connection
let db
if (!config || !config.seeds || !config.seeds.directory) {
console.warn('Skipping! No data seed directory is configured.')
return
}
const seedDir = path.resolve(process.cwd(), config.seeds.directory)
const seedDirStats = fs.statSync(seedDir, { throwIfNoEntry: false })
if (!seedDirStats) {
console.warn(
`Skipping! The data seed directory does not exist: ${config.seeds.directory}`
)
return
}
try {
if (!seedDirStats.isDirectory()) {
throw new Error(
`The data seed path exists but is not a directory: ${config.seeds.directory}`
)
}
db = createClient(config)
await db.seed.run()
console.log(`Created seed data in database "${database}"!`)
} catch (error) {
console.error(
`Error creating seed data for database "${database}": ${error.message}`
)
throw error
} finally {
// Disconnect
if (db) {
await db.destroy()
}
}
}