2013-12-23 04:24:40 +00:00
|
|
|
Promise = require 'bluebird'
|
2014-03-19 19:17:07 +00:00
|
|
|
Knex = require 'knex'
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2016-10-10 21:14:31 +00:00
|
|
|
knex = Knex(
|
2013-12-14 05:18:20 +00:00
|
|
|
client: 'sqlite3'
|
|
|
|
connection:
|
2014-04-27 21:41:59 +00:00
|
|
|
filename: '/data/database.sqlite'
|
2017-09-14 20:56:23 +00:00
|
|
|
useNullAsDefault: true
|
2013-12-14 05:18:20 +00:00
|
|
|
)
|
|
|
|
|
2014-08-19 17:21:44 +00:00
|
|
|
addColumn = (table, column, type) ->
|
|
|
|
knex.schema.hasColumn(table, column)
|
|
|
|
.then (exists) ->
|
|
|
|
if not exists
|
|
|
|
knex.schema.table table, (t) ->
|
|
|
|
t[type](column)
|
|
|
|
|
2013-12-23 04:24:40 +00:00
|
|
|
knex.init = Promise.all([
|
2014-03-19 19:17:07 +00:00
|
|
|
knex.schema.hasTable('config')
|
|
|
|
.then (exists) ->
|
2013-12-23 04:24:40 +00:00
|
|
|
if not exists
|
2014-03-19 19:17:07 +00:00
|
|
|
knex.schema.createTable 'config', (t) ->
|
2013-12-23 04:24:40 +00:00
|
|
|
t.string('key').primary()
|
|
|
|
t.string('value')
|
2014-03-19 19:17:07 +00:00
|
|
|
|
2016-08-19 21:28:53 +00:00
|
|
|
knex.schema.hasTable('deviceConfig')
|
|
|
|
.then (exists) ->
|
|
|
|
if not exists
|
|
|
|
knex.schema.createTable 'deviceConfig', (t) ->
|
|
|
|
t.json('values')
|
|
|
|
t.json('targetValues')
|
2016-12-05 20:59:04 +00:00
|
|
|
.then ->
|
|
|
|
knex('deviceConfig').select()
|
|
|
|
.then (deviceConfigs) ->
|
|
|
|
knex('deviceConfig').insert({ values: '{}', targetValues: '{}' }) if deviceConfigs.length == 0
|
2016-08-19 21:28:53 +00:00
|
|
|
|
2014-03-19 19:17:07 +00:00
|
|
|
knex.schema.hasTable('app')
|
|
|
|
.then (exists) ->
|
2013-12-23 04:24:40 +00:00
|
|
|
if not exists
|
2014-03-19 19:17:07 +00:00
|
|
|
knex.schema.createTable 'app', (t) ->
|
2013-12-23 04:24:40 +00:00
|
|
|
t.increments('id').primary()
|
|
|
|
t.string('name')
|
2017-10-17 23:56:29 +00:00
|
|
|
t.string('containerName')
|
2014-07-19 02:24:01 +00:00
|
|
|
t.string('commit')
|
2013-12-23 04:24:40 +00:00
|
|
|
t.string('imageId')
|
2014-08-19 17:21:44 +00:00
|
|
|
t.string('appId')
|
2013-12-23 04:24:40 +00:00
|
|
|
t.boolean('privileged')
|
2014-04-03 17:16:37 +00:00
|
|
|
t.json('env')
|
2016-08-19 21:28:53 +00:00
|
|
|
t.json('config')
|
2017-09-14 20:56:23 +00:00
|
|
|
t.boolean('markedForDeletion')
|
2014-07-19 02:24:01 +00:00
|
|
|
else
|
2014-08-19 17:21:44 +00:00
|
|
|
Promise.all [
|
|
|
|
addColumn('app', 'commit', 'string')
|
|
|
|
addColumn('app', 'appId', 'string')
|
2017-10-17 23:56:29 +00:00
|
|
|
addColumn('app', 'containerName', 'string')
|
2016-08-19 21:28:53 +00:00
|
|
|
addColumn('app', 'config', 'json')
|
2017-09-14 20:56:23 +00:00
|
|
|
addColumn('app', 'markedForDeletion', 'boolean')
|
2014-08-19 17:21:44 +00:00
|
|
|
]
|
2017-03-22 23:49:27 +00:00
|
|
|
.then ->
|
|
|
|
# When updating from older supervisors, config can be null
|
|
|
|
knex('app').update({ config: '{}' }).whereNull('config')
|
2017-09-14 20:56:23 +00:00
|
|
|
.then ->
|
|
|
|
knex('app').update({ markedForDeletion: false }).whereNull('markedForDeletion')
|
2014-07-19 02:24:01 +00:00
|
|
|
|
2016-08-19 21:28:53 +00:00
|
|
|
knex.schema.hasTable('dependentApp')
|
|
|
|
.then (exists) ->
|
|
|
|
if not exists
|
|
|
|
knex.schema.createTable 'dependentApp', (t) ->
|
|
|
|
t.increments('id').primary()
|
|
|
|
t.string('appId')
|
|
|
|
t.string('parentAppId')
|
|
|
|
t.string('name')
|
|
|
|
t.string('commit')
|
|
|
|
t.string('imageId')
|
|
|
|
t.json('config')
|
2017-06-05 09:08:51 +00:00
|
|
|
t.json('environment')
|
2017-06-13 08:38:16 +00:00
|
|
|
else
|
|
|
|
addColumn('dependentApp', 'environment', 'json')
|
2016-08-19 21:28:53 +00:00
|
|
|
|
|
|
|
knex.schema.hasTable('dependentDevice')
|
|
|
|
.then (exists) ->
|
|
|
|
if not exists
|
|
|
|
knex.schema.createTable 'dependentDevice', (t) ->
|
|
|
|
t.increments('id').primary()
|
|
|
|
t.string('uuid')
|
|
|
|
t.string('appId')
|
2017-06-05 09:08:51 +00:00
|
|
|
t.string('localId')
|
2016-08-19 21:28:53 +00:00
|
|
|
t.string('device_type')
|
|
|
|
t.string('logs_channel')
|
|
|
|
t.string('deviceId')
|
|
|
|
t.boolean('is_online')
|
|
|
|
t.string('name')
|
|
|
|
t.string('status')
|
|
|
|
t.string('download_progress')
|
2017-06-05 09:08:51 +00:00
|
|
|
t.string('is_managed_by')
|
|
|
|
t.dateTime('lock_expiry_date')
|
2016-08-19 21:28:53 +00:00
|
|
|
t.string('commit')
|
|
|
|
t.string('targetCommit')
|
|
|
|
t.json('environment')
|
|
|
|
t.json('targetEnvironment')
|
|
|
|
t.json('config')
|
|
|
|
t.json('targetConfig')
|
2016-10-07 14:16:27 +00:00
|
|
|
t.boolean('markedForDeletion')
|
|
|
|
else
|
2017-06-13 08:38:16 +00:00
|
|
|
Promise.all [
|
|
|
|
addColumn('dependentDevice', 'markedForDeletion', 'boolean')
|
|
|
|
addColumn('dependentDevice', 'localId', 'string')
|
|
|
|
addColumn('dependentDevice', 'is_managed_by', 'string')
|
|
|
|
addColumn('dependentDevice', 'lock_expiry_date', 'dateTime')
|
|
|
|
]
|
2013-12-23 04:24:40 +00:00
|
|
|
])
|
2013-12-14 05:18:20 +00:00
|
|
|
|
|
|
|
module.exports = knex
|