Skip to content

Commit

Permalink
Run migrations on server startup
Browse files Browse the repository at this point in the history
  • Loading branch information
oamaok committed Oct 17, 2021
1 parent 6091933 commit 7b07142
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
1 change: 0 additions & 1 deletion scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ git clone https://github.com/oamaok/modulate.git build
cd build
yarn
yarn build
yarn run-migrations

cd ~
rm -rf modulate.previous
Expand Down
4 changes: 2 additions & 2 deletions server/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { v4 as uuid } from 'uuid'
import argon2 from 'argon2'
import { Patch, User, UserLogin, UserRegistration } from '../../common/types'

const db = new (sqlite.verbose().Database)(
export const database = new (sqlite.verbose().Database)(
process.env.DATABASE_FILE ??
path.resolve(__dirname, '../../data/database.sqlite3')
)

export const query = (query: SQLStatement) => {
return new Promise<any[]>((resolve, reject) =>
db.all(query.sql, query.values, (err, res) => {
database.all(query.sql, query.values, (err, res) => {
if (err) {
reject(err)
} else {
Expand Down
6 changes: 5 additions & 1 deletion server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import router, { serverStatic, Response } from './router'
import * as db from './database'
import * as validators from '../../common/validators'
import * as auth from './authorization'
import migrate from './migrate'

const unauthorized = (res: Response) => {
res.status(401)
Expand Down Expand Up @@ -111,4 +112,7 @@ const server = http.createServer(
})
)

server.listen(8888)
;(async () => {
await migrate()
server.listen(8888)
})()
39 changes: 19 additions & 20 deletions server/scripts/run-migrations.js → server/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const sqlite = require('sqlite3').verbose()
const fs = require('fs/promises')
const path = require('path')
import fs from 'fs/promises'
import path from 'path'

import { database } from './database'

const promisify =
(fn) =>
(...args) =>
(fn: (...args: any) => any): ((...args: any) => any) =>
(...args: any[]) =>
new Promise((resolve, reject) => {
try {
fn(...args, (err, res) => {
fn(...args, (err: Error, res: any) => {
if (err) {
reject(err)
} else {
Expand All @@ -20,21 +21,18 @@ const promisify =
})

const getDatabase = () => {
const db = new sqlite.Database(
path.resolve(__dirname, '../../data/database.sqlite3')
)

return {
run: promisify(db.run.bind(db)),
get: promisify(db.get.bind(db)),
all: promisify(db.all.bind(db)),
exec: promisify(db.exec.bind(db)),
run: promisify(database.run.bind(database)),
get: promisify(database.get.bind(database)) as (...args: any) => any,
all: promisify(database.all.bind(database)),
exec: promisify(database.exec.bind(database)),
}
}

const getMigrationVersion = (filename) => parseInt(filename.split('-')[0])
const getMigrationVersion = (filename: string) =>
parseInt(filename.split('-')[0])

;(async () => {
const migrate = async () => {
const db = getDatabase()

await db.run(
Expand Down Expand Up @@ -67,21 +65,21 @@ const getMigrationVersion = (filename) => parseInt(filename.split('-')[0])
default: { up },
} = require(`../migrations/${migration}`)
await up({
query(query, parameters) {
query(query: any, parameters: any[]) {
if (typeof query === 'object') {
return db.all(query.sql, query.values)
} else {
return db.all(query, parameters)
}
},
run(query, parameters) {
run(query: any, parameters: any[]) {
if (typeof query === 'object') {
return db.run(query.sql, query.values)
} else {
return db.run(query, parameters)
}
},
exec(query) {
exec(query: any) {
console.log(query)
return db.exec(query)
},
Expand All @@ -97,4 +95,5 @@ const getMigrationVersion = (filename) => parseInt(filename.split('-')[0])
return
}
}
})()
}
export default migrate

0 comments on commit 7b07142

Please sign in to comment.