balena-supervisor/src/db.coffee

53 lines
1.1 KiB
CoffeeScript
Raw Normal View History

Promise = require 'bluebird'
2014-03-19 19:17:07 +00:00
Knex = require 'knex'
knex = Knex.initialize(
client: 'sqlite3'
connection:
2014-04-27 21:41:59 +00:00
filename: '/data/database.sqlite'
)
addColumn = (table, column, type) ->
knex.schema.hasColumn(table, column)
.then (exists) ->
if not exists
knex.schema.table table, (t) ->
t[type](column)
knex.init = Promise.all([
2014-03-19 19:17:07 +00:00
knex.schema.hasTable('config')
.then (exists) ->
if not exists
2014-03-19 19:17:07 +00:00
knex.schema.createTable 'config', (t) ->
t.string('key').primary()
t.string('value')
2014-03-19 19:17:07 +00:00
knex.schema.hasTable('app')
.then (exists) ->
if not exists
2014-03-19 19:17:07 +00:00
knex.schema.createTable 'app', (t) ->
t.increments('id').primary()
t.string('name')
t.string('containerId')
t.string('commit')
t.string('imageId')
t.string('appId')
t.boolean('privileged')
t.json('env')
else
Promise.all [
addColumn('app', 'commit', 'string')
addColumn('app', 'appId', 'string')
]
knex.schema.hasTable('image')
.then (exists) ->
if not exists
knex.schema.createTable 'image', (t) ->
t.increments('id').primary()
t.string('repoTag')
])
module.exports = knex