diff --git a/migrations/20180628162142-subdomains-create-table.js b/migrations/20180628162142-subdomains-create-table.js index 16a761dba..d14e06456 100644 --- a/migrations/20180628162142-subdomains-create-table.js +++ b/migrations/20180628162142-subdomains-create-table.js @@ -6,6 +6,7 @@ exports.up = function(db, callback) { 'domain VARCHAR(128) NOT NULL,' + 'subdomain VARCHAR(128) NOT NULL,' + 'type VARCHAR(128) NOT NULL,' + + 'dnsRecordId VARCHAR(512),' + 'FOREIGN KEY(domain) REFERENCES domains(domain),' + 'FOREIGN KEY(appId) REFERENCES apps(id),' + 'UNIQUE (subdomain, domain)) CHARACTER SET utf8 COLLATE utf8_bin'; diff --git a/migrations/20180628163616-migrate-app-subdomains.js b/migrations/20180628163616-migrate-app-subdomains.js index 09c573b90..81ed316a5 100644 --- a/migrations/20180628163616-migrate-app-subdomains.js +++ b/migrations/20180628163616-migrate-app-subdomains.js @@ -3,7 +3,7 @@ var async = require('async'); exports.up = function(db, callback) { - db.all('SELECT id,location,domain from apps', [ ], function (error, results) { + db.all('SELECT * from apps', [ ], function (error, results) { if (error) return done(error); var queries = [ @@ -11,7 +11,7 @@ exports.up = function(db, callback) { ]; results.forEach(function (app) { - queries.push(db.runSql.bind(db, 'INSERT INTO subdomains (appId, domain, subdomain, type) VALUES (?, ?, ?, ?)', [ app.id, app.domain, app.location, 'primary' ])); + queries.push(db.runSql.bind(db, 'INSERT INTO subdomains (appId, domain, subdomain, type, dnsRecordId) VALUES (?, ?, ?, ?, ?)', [ app.id, app.domain, app.location, 'primary', app.dnsRecordId ])); }); queries.push(db.runSql.bind(db, 'COMMIT')); diff --git a/migrations/20180629090543-remove-app-location-and-domain.js b/migrations/20180629090543-remove-app-location-and-domain.js index db208951d..8b7d46481 100644 --- a/migrations/20180629090543-remove-app-location-and-domain.js +++ b/migrations/20180629090543-remove-app-location-and-domain.js @@ -3,7 +3,7 @@ var async = require('async'); exports.up = function(db, callback) { - db.runSql('ALTER TABLE apps DROP INDEX location_domain_unique_index, DROP FOREIGN KEY apps_domain_constraint, DROP COLUMN domain, DROP COLUMN location', function (error) { + db.runSql('ALTER TABLE apps DROP INDEX location_domain_unique_index, DROP FOREIGN KEY apps_domain_constraint, DROP COLUMN domain, DROP COLUMN location, DROP COLUMN dnsRecordId', function (error) { if (error) console.error(error); callback(error); }); @@ -14,15 +14,16 @@ exports.down = function(db, callback) { if (error) return callback(error); var cmd = 'ALTER TABLE apps' - + ' ADD COLUMN location VARCHAR(128) NOT NULL,' - + ' ADD COLUMN domain VARCHAR(128) NOT NULL'; + + ' ADD COLUMN location VARCHAR(128),' + + ' ADD COLUMN domain VARCHAR(128),' + + ' ADD COLUMN dnsRecordId VARCHAR(512)'; db.runSql(cmd, function (error) { if (error) return callback(error); var queries = [ db.runSql.bind(db, 'START TRANSACTION;') ]; - results.forEach(function (subdomains) { - queries.push(db.runSql.bind(db, 'UPDATE apps SET domain = ?, location = ? WHERE id = ?', [ subdomains.domain, subdomains.domain, subdomains.appId ])); + results.forEach(function (d) { + queries.push(db.runSql.bind(db, 'UPDATE apps SET domain = ?, location = ?, dnsRecordId = ? WHERE id = ?', [ d.domain, d.subdomain, d.appId, d.dnsRecordId ])); }); queries.push(db.runSql.bind(db, 'COMMIT')); diff --git a/migrations/schema.sql b/migrations/schema.sql index 6252880fc..64a0cd003 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -69,7 +69,6 @@ CREATE TABLE IF NOT EXISTS apps( httpPort INTEGER, // this is the nginx proxy port and not manifest.httpPort location VARCHAR(128) NOT NULL, domain VARCHAR(128) NOT NULL, - dnsRecordId VARCHAR(512), // tracks any id that we got back to track dns updates accessRestrictionJson TEXT, // { users: [ ], groups: [ ] } creationTime TIMESTAMP(2) NOT NULL DEFAULT CURRENT_TIMESTAMP, // when the app was installed updateTime TIMESTAMP(2) NOT NULL DEFAULT CURRENT_TIMESTAMP, // when the last app update was done @@ -189,6 +188,7 @@ CREATE TABLE IF NOT EXISTS subdomains( domain VARCHAR(128) NOT NULL, subdomain VARCHAR(128) NOT NULL, type VARCHAR(128) NOT NULL, + dnsRecordId VARCHAR(512), // tracks any id that we got back to track dns updates FOREIGN KEY(domain) REFERENCES domains(domain), FOREIGN KEY(appId) REFERENCES apps(id), diff --git a/src/appdb.js b/src/appdb.js index f58a3768e..03f157a55 100644 --- a/src/appdb.js +++ b/src/appdb.js @@ -66,7 +66,7 @@ var assert = require('assert'), util = require('util'); var APPS_FIELDS_PREFIXED = [ 'apps.id', 'apps.appStoreId', 'apps.installationState', 'apps.installationProgress', 'apps.runState', - 'apps.health', 'apps.containerId', 'apps.manifestJson', 'apps.httpPort', 'subdomains.subdomain AS location', 'subdomains.domain', 'apps.dnsRecordId', + 'apps.health', 'apps.containerId', 'apps.manifestJson', 'apps.httpPort', 'subdomains.subdomain AS location', 'subdomains.domain', 'subdomains.dnsRecordId', 'apps.accessRestrictionJson', 'apps.restoreConfigJson', 'apps.oldConfigJson', 'apps.updateConfigJson', 'apps.memoryLimit', 'apps.xFrameOptions', 'apps.sso', 'apps.debugModeJson', 'apps.robotsTxt', 'apps.enableBackup', 'apps.creationTime', 'apps.updateTime', 'apps.ownerId', 'apps.ts' ].join(','); @@ -407,6 +407,10 @@ function updateWithConstraints(id, app, constraints, callback) { queries.push({ query: 'UPDATE subdomains SET domain = ? WHERE appId = ? AND type = ?', args: [ app.domain, id, exports.SUBDOMAIN_TYPE_PRIMARY ]}); } + if ('dnsRecordId' in app) { + queries.push({ query: 'UPDATE subdomains SET dnsRecordId = ? WHERE appId = ? AND type = ?', args: [ app.dnsRecordId, id, exports.SUBDOMAIN_TYPE_PRIMARY ]}); + } + if ('alternateDomains' in app) { queries.push({ query: 'DELETE FROM subdomains WHERE appId = ? AND type = ?', args: [ id, exports.SUBDOMAIN_TYPE_REDIRECT ]}); app.alternateDomains.forEach(function (d) { @@ -419,7 +423,7 @@ function updateWithConstraints(id, app, constraints, callback) { if (p === 'manifest' || p === 'oldConfig' || p === 'updateConfig' || p === 'restoreConfig' || p === 'accessRestriction' || p === 'debugMode') { fields.push(`${p}Json = ?`); values.push(JSON.stringify(app[p])); - } else if (p !== 'portBindings' && p !== 'location' && p !== 'domain' && p !== 'alternateDomains') { + } else if (p !== 'portBindings' && p !== 'location' && p !== 'domain' && p !== 'dnsRecordId' && p !== 'alternateDomains') { fields.push(p + ' = ?'); values.push(app[p]); }