From bbe02bc70acaffe4de20f362282e59ff7b2339eb Mon Sep 17 00:00:00 2001 From: Jamie Curnow Date: Mon, 3 Sep 2018 09:50:18 +1000 Subject: [PATCH] Fixes #10 - Don't use defaults for json fields in migration --- .../migrations/20180618015850_initial.js | 20 +++++++++---------- src/backend/models/access_list.js | 5 +++++ src/backend/models/access_list_auth.js | 5 +++++ src/backend/models/audit-log.js | 5 +++++ src/backend/models/auth.js | 5 +++++ src/backend/models/certificate.js | 18 +++++++++++++++++ src/backend/models/dead_host.js | 17 ++++++++++++++++ src/backend/models/proxy_host.js | 13 ++++++++++++ src/backend/models/redirection_host.js | 17 ++++++++++++++++ src/backend/models/stream.js | 5 +++++ src/backend/models/token.js | 1 - src/backend/models/user.js | 5 +++++ 12 files changed, 105 insertions(+), 11 deletions(-) diff --git a/src/backend/migrations/20180618015850_initial.js b/src/backend/migrations/20180618015850_initial.js index f9777072..cf288e37 100644 --- a/src/backend/migrations/20180618015850_initial.js +++ b/src/backend/migrations/20180618015850_initial.js @@ -22,7 +22,7 @@ exports.up = function (knex/*, Promise*/) { table.integer('user_id').notNull().unsigned(); table.string('type', 30).notNull(); table.string('secret').notNull(); - table.json('meta').notNull().defaultTo('{}'); + table.json('meta').notNull(); table.integer('is_deleted').notNull().unsigned().defaultTo(0); }) .then(() => { @@ -77,7 +77,7 @@ exports.up = function (knex/*, Promise*/) { table.integer('caching_enabled').notNull().unsigned().defaultTo(0); table.integer('block_exploits').notNull().unsigned().defaultTo(0); table.text('advanced_config').notNull().defaultTo(''); - table.json('meta').notNull().defaultTo('{}'); + table.json('meta').notNull(); }); }) .then(() => { @@ -96,7 +96,7 @@ exports.up = function (knex/*, Promise*/) { table.integer('ssl_forced').notNull().unsigned().defaultTo(0); table.integer('block_exploits').notNull().unsigned().defaultTo(0); table.text('advanced_config').notNull().defaultTo(''); - table.json('meta').notNull().defaultTo('{}'); + table.json('meta').notNull(); }); }) .then(() => { @@ -112,7 +112,7 @@ exports.up = function (knex/*, Promise*/) { table.integer('certificate_id').notNull().unsigned().defaultTo(0); table.integer('ssl_forced').notNull().unsigned().defaultTo(0); table.text('advanced_config').notNull().defaultTo(''); - table.json('meta').notNull().defaultTo('{}'); + table.json('meta').notNull(); }); }) .then(() => { @@ -129,7 +129,7 @@ exports.up = function (knex/*, Promise*/) { table.integer('forwarding_port').notNull().unsigned(); table.integer('tcp_forwarding').notNull().unsigned().defaultTo(0); table.integer('udp_forwarding').notNull().unsigned().defaultTo(0); - table.json('meta').notNull().defaultTo('{}'); + table.json('meta').notNull(); }); }) .then(() => { @@ -142,7 +142,7 @@ exports.up = function (knex/*, Promise*/) { table.integer('owner_user_id').notNull().unsigned(); table.integer('is_deleted').notNull().unsigned().defaultTo(0); table.string('name').notNull(); - table.json('meta').notNull().defaultTo('{}'); + table.json('meta').notNull(); }); }) .then(() => { @@ -156,9 +156,9 @@ exports.up = function (knex/*, Promise*/) { table.integer('is_deleted').notNull().unsigned().defaultTo(0); table.string('provider').notNull(); table.string('nice_name').notNull().defaultTo(''); - table.json('domain_names').notNull().defaultTo('[]'); + table.json('domain_names').notNull(); table.dateTime('expires_on').notNull(); - table.json('meta').notNull().defaultTo('{}'); + table.json('meta').notNull(); }); }) .then(() => { @@ -171,7 +171,7 @@ exports.up = function (knex/*, Promise*/) { table.integer('access_list_id').notNull().unsigned(); table.string('username').notNull(); table.string('password').notNull(); - table.json('meta').notNull().defaultTo('{}'); + table.json('meta').notNull(); }); }) .then(() => { @@ -185,7 +185,7 @@ exports.up = function (knex/*, Promise*/) { table.string('object_type').notNull().defaultTo(''); table.integer('object_id').notNull().unsigned().defaultTo(0); table.string('action').notNull(); - table.json('meta').notNull().defaultTo('{}'); + table.json('meta').notNull(); }); }) .then(() => { diff --git a/src/backend/models/access_list.js b/src/backend/models/access_list.js index 5bb0e355..9d620afb 100644 --- a/src/backend/models/access_list.js +++ b/src/backend/models/access_list.js @@ -14,6 +14,11 @@ class AccessList extends Model { $beforeInsert () { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + + // Default for meta + if (typeof this.meta === 'undefined') { + this.meta = {}; + } } $beforeUpdate () { diff --git a/src/backend/models/access_list_auth.js b/src/backend/models/access_list_auth.js index cb44ce1b..7c251b65 100644 --- a/src/backend/models/access_list_auth.js +++ b/src/backend/models/access_list_auth.js @@ -12,6 +12,11 @@ class AccessListAuth extends Model { $beforeInsert () { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + + // Default for meta + if (typeof this.meta === 'undefined') { + this.meta = {}; + } } $beforeUpdate () { diff --git a/src/backend/models/audit-log.js b/src/backend/models/audit-log.js index d9f312ee..6220ade3 100644 --- a/src/backend/models/audit-log.js +++ b/src/backend/models/audit-log.js @@ -13,6 +13,11 @@ class AuditLog extends Model { $beforeInsert () { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + + // Default for meta + if (typeof this.meta === 'undefined') { + this.meta = {}; + } } $beforeUpdate () { diff --git a/src/backend/models/auth.js b/src/backend/models/auth.js index 5024a0d8..38d2dd54 100644 --- a/src/backend/models/auth.js +++ b/src/backend/models/auth.js @@ -29,6 +29,11 @@ class Auth extends Model { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + // Default for meta + if (typeof this.meta === 'undefined') { + this.meta = {}; + } + return encryptPassword.apply(this, queryContext); } diff --git a/src/backend/models/certificate.js b/src/backend/models/certificate.js index 476f7b41..b211ee7c 100644 --- a/src/backend/models/certificate.js +++ b/src/backend/models/certificate.js @@ -14,13 +14,31 @@ class Certificate extends Model { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + // Default for expires_on if (typeof this.expires_on === 'undefined') { this.expires_on = Model.raw('NOW()'); } + + // Default for domain_names + if (typeof this.domain_names === 'undefined') { + this.domain_names = []; + } + + // Default for meta + if (typeof this.meta === 'undefined') { + this.meta = {}; + } + + this.domain_names.sort(); } $beforeUpdate () { this.modified_on = Model.raw('NOW()'); + + // Sort domain_names + if (typeof this.domain_names !== 'undefined') { + this.domain_names.sort(); + } } static get name () { diff --git a/src/backend/models/dead_host.js b/src/backend/models/dead_host.js index 3eb093a8..c900ebc8 100644 --- a/src/backend/models/dead_host.js +++ b/src/backend/models/dead_host.js @@ -14,10 +14,27 @@ class DeadHost extends Model { $beforeInsert () { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + + // Default for domain_names + if (typeof this.domain_names === 'undefined') { + this.domain_names = []; + } + + // Default for meta + if (typeof this.meta === 'undefined') { + this.meta = {}; + } + + this.domain_names.sort(); } $beforeUpdate () { this.modified_on = Model.raw('NOW()'); + + // Sort domain_names + if (typeof this.domain_names !== 'undefined') { + this.domain_names.sort(); + } } static get name () { diff --git a/src/backend/models/proxy_host.js b/src/backend/models/proxy_host.js index 6a3bffbe..a1217236 100644 --- a/src/backend/models/proxy_host.js +++ b/src/backend/models/proxy_host.js @@ -15,11 +15,24 @@ class ProxyHost extends Model { $beforeInsert () { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + + // Default for domain_names + if (typeof this.domain_names === 'undefined') { + this.domain_names = []; + } + + // Default for meta + if (typeof this.meta === 'undefined') { + this.meta = {}; + } + this.domain_names.sort(); } $beforeUpdate () { this.modified_on = Model.raw('NOW()'); + + // Sort domain_names if (typeof this.domain_names !== 'undefined') { this.domain_names.sort(); } diff --git a/src/backend/models/redirection_host.js b/src/backend/models/redirection_host.js index 28f12a56..49f12710 100644 --- a/src/backend/models/redirection_host.js +++ b/src/backend/models/redirection_host.js @@ -14,10 +14,27 @@ class RedirectionHost extends Model { $beforeInsert () { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + + // Default for domain_names + if (typeof this.domain_names === 'undefined') { + this.domain_names = []; + } + + // Default for meta + if (typeof this.meta === 'undefined') { + this.meta = {}; + } + + this.domain_names.sort(); } $beforeUpdate () { this.modified_on = Model.raw('NOW()'); + + // Sort domain_names + if (typeof this.domain_names !== 'undefined') { + this.domain_names.sort(); + } } static get name () { diff --git a/src/backend/models/stream.js b/src/backend/models/stream.js index 7e9f76b9..a22bf323 100644 --- a/src/backend/models/stream.js +++ b/src/backend/models/stream.js @@ -13,6 +13,11 @@ class Stream extends Model { $beforeInsert () { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + + // Default for meta + if (typeof this.meta === 'undefined') { + this.meta = {}; + } } $beforeUpdate () { diff --git a/src/backend/models/token.js b/src/backend/models/token.js index 4a96fc4c..e3853108 100644 --- a/src/backend/models/token.js +++ b/src/backend/models/token.js @@ -19,7 +19,6 @@ module.exports = function () { let token_data = {}; let self = { - //return { /** * @param {Object} payload * @param {Object} [user_options] diff --git a/src/backend/models/user.js b/src/backend/models/user.js index 3a2ab100..0873b65a 100644 --- a/src/backend/models/user.js +++ b/src/backend/models/user.js @@ -13,6 +13,11 @@ class User extends Model { $beforeInsert () { this.created_on = Model.raw('NOW()'); this.modified_on = Model.raw('NOW()'); + + // Default for roles + if (typeof this.roles === 'undefined') { + this.roles = []; + } } $beforeUpdate () {