From fb42b542101a047e69b3e9cff70b4efaaefa2a49 Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Fri, 29 Jun 2018 11:29:30 +0200 Subject: [PATCH] Remove apps.location and apps.domain This is now managed in the subdomains table --- .../20180628163616-migrate-app-subdomains.js | 2 +- ...29090543-remove-app-location-and-domain.js | 40 +++++++++++++++++++ migrations/schema.sql | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 migrations/20180629090543-remove-app-location-and-domain.js diff --git a/migrations/20180628163616-migrate-app-subdomains.js b/migrations/20180628163616-migrate-app-subdomains.js index 3537eaaf6..82594ea1c 100644 --- a/migrations/20180628163616-migrate-app-subdomains.js +++ b/migrations/20180628163616-migrate-app-subdomains.js @@ -9,7 +9,7 @@ exports.up = function(db, callback) { var queries = [ db.runSql.bind(db, 'START TRANSACTION;') ]; - + results.forEach(function (app) { queries.push(db.runSql.bind(db, 'INSERT INTO subdomains (appId, domain, subdomain, type), (?, ?, ?, ?)', [ app.id, app.domain, app.location, 'primary' ])); }); diff --git a/migrations/20180629090543-remove-app-location-and-domain.js b/migrations/20180629090543-remove-app-location-and-domain.js new file mode 100644 index 000000000..db208951d --- /dev/null +++ b/migrations/20180629090543-remove-app-location-and-domain.js @@ -0,0 +1,40 @@ +'use strict'; + +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) { + if (error) console.error(error); + callback(error); + }); +}; + +exports.down = function(db, callback) { + db.all('SELECT * from subdomains WHERE type = ?', [ 'primary' ], function (error, results) { + if (error) return callback(error); + + var cmd = 'ALTER TABLE apps' + + ' ADD COLUMN location VARCHAR(128) NOT NULL,' + + ' ADD COLUMN domain VARCHAR(128) NOT NULL'; + + 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 ])); + }); + queries.push(db.runSql.bind(db, 'COMMIT')); + + async.series(queries, function (error) { + if (error) return callback(error); + + var cmd = 'ALTER TABLE apps' + + ' ADD CONSTRAINT apps_domain_constraint FOREIGN KEY(domain) REFERENCES domains(domain),' + + ' ADD UNIQUE location_domain_unique_index (location, domain)'; + + db.runSql(cmd, callback); + }); + }); + }); +}; diff --git a/migrations/schema.sql b/migrations/schema.sql index 6ddcbbbf5..6252880fc 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -89,7 +89,6 @@ CREATE TABLE IF NOT EXISTS apps( ownerId VARCHAR(128), FOREIGN KEY(ownerId) REFERENCES users(id), - FOREIGN KEY(domain) REFERENCES domains(domain), PRIMARY KEY(id)); CREATE TABLE IF NOT EXISTS appPortBindings( @@ -190,6 +189,7 @@ CREATE TABLE IF NOT EXISTS subdomains( domain VARCHAR(128) NOT NULL, subdomain VARCHAR(128) NOT NULL, type VARCHAR(128) NOT NULL, + FOREIGN KEY(domain) REFERENCES domains(domain), FOREIGN KEY(appId) REFERENCES apps(id), UNIQUE (subdomain, domain))