Skip to content
This repository has been archived by the owner on Feb 28, 2020. It is now read-only.

Commit

Permalink
Set up seed scripts for development vs production. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
thebearingedge committed Mar 18, 2019
1 parent a70ea86 commit bcb1d40
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
7 changes: 5 additions & 2 deletions app.json
Expand Up @@ -6,10 +6,13 @@
"heroku-redis"
],
"env": {
"DATABASE_URL": {
"ADMIN_USER_ID": {
"required": true
},
"REDIS_URL": {
"ADMIN_USERNAME": {
"required": true
},
"ADMIN_PASSWORD": {
"required": true
},
"JWT_SECRET": {
Expand Down
2 changes: 1 addition & 1 deletion database/seeds/__dev__.js
Expand Up @@ -6,7 +6,7 @@ import getConnections from '../get-connections'
try {
await redis.flushallAsync()
await knex.seed.run({
directory: path.join(__dirname, 'dev/')
directory: path.join(__dirname, 'development/')
})
await knex.destroy()
await redis.quitAsync()
Expand Down
Expand Up @@ -12,15 +12,14 @@ export async function seed(knex) {
}
catch (err) {
console.error(err)
console.error(chalk.red(`ERROR:Failed to run seed script for ${description}.`))
console.error(chalk.red(`ERROR: Failed to run seed script for ${description}.`))
process.exit(1)
}
})(knex)

console.log('\n')

await trySeed(admin, 'admin user')
await admin.seed(knex)

console.log('\n')
}
12 changes: 12 additions & 0 deletions database/seeds/production/index.js
@@ -0,0 +1,12 @@
import * as admin from '../admin'

export async function seed(knex) {
const user = await knex
.select(['userId'])
.from('users')
.where({ username: admin.user.username })
.first()
if (!user) return admin.seed(knex)
}

export default seed
6 changes: 5 additions & 1 deletion server/index.js
@@ -1,5 +1,6 @@
import pgBump from 'pg-bump'
import createServer from './create-server'
import seed from '../database/seeds/prod'
import getConnections from '../database/get-connections'

;(async () => {
Expand All @@ -8,13 +9,16 @@ import getConnections from '../database/get-connections'
console.log(Object.keys(process.env))
console.log('Acquiring database connections...')
const { knex, redis } = await getConnections()
console.log('Connections established.')
console.log('Running latest migrations...')
await pgBump.up({
files: 'database/migrations/',
connectionVar: 'DATABASE_URL',
journalTable: 'schema_journal'
})
console.log('Connections established.')
console.log('Seeding database if needed...')
await seed(knex)
console.log('Database seeded.')
const dev = process.env.NODE_ENV !== 'production'
const server = await createServer({ dev, knex, redis })
server.listen(process.env.PORT, () => {
Expand Down

0 comments on commit bcb1d40

Please sign in to comment.