Remove apps.location and apps.domain

This is now managed in the subdomains table
This commit is contained in:
Johannes Zellner
2018-06-29 11:29:30 +02:00
parent 4d2ba2adaa
commit fb42b54210
3 changed files with 42 additions and 2 deletions

View File

@@ -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' ]));
});

View File

@@ -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);
});
});
});
};

View File

@@ -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))