diff --git a/migrations/20180628162142-subdomains-create-table.js b/migrations/20180628162142-subdomains-create-table.js new file mode 100644 index 000000000..16a761dba --- /dev/null +++ b/migrations/20180628162142-subdomains-create-table.js @@ -0,0 +1,24 @@ +'use strict'; + +exports.up = function(db, callback) { + var cmd = 'CREATE TABLE IF NOT EXISTS subdomains(' + + 'appId VARCHAR(128) NOT NULL,' + + '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)) CHARACTER SET utf8 COLLATE utf8_bin'; + + db.runSql(cmd, function (error) { + if (error) console.error(error); + callback(error); + }); +}; + +exports.down = function(db, callback) { + db.runSql('DROP TABLE subdomains', function (error) { + if (error) console.error(error); + callback(error); + }); +}; diff --git a/migrations/20180628163616-migrate-app-subdomains.js b/migrations/20180628163616-migrate-app-subdomains.js new file mode 100644 index 000000000..3537eaaf6 --- /dev/null +++ b/migrations/20180628163616-migrate-app-subdomains.js @@ -0,0 +1,28 @@ +'use strict'; + +var async = require('async'); + +exports.up = function(db, callback) { + db.all('SELECT id,location,domain from apps', [ ], function (error, results) { + if (error) return done(error); + + 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' ])); + }); + + queries.push(db.runSql.bind(db, 'COMMIT')); + + async.series(queries, callback); + }); +}; + +exports.down = function(db, callback) { + db.runSql('DELETE FROM subdomains', function (error) { + if (error) console.error(error); + callback(error); + }); +}; diff --git a/migrations/schema.sql b/migrations/schema.sql index 3db00393d..6ea7dfbdc 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -185,3 +185,13 @@ CREATE TABLE IF NOT EXISTS mailboxes( FOREIGN KEY(domain) REFERENCES mail(domain), UNIQUE (name, domain)); +CREATE TABLE IF NOT EXISTS subdomains( + appId VARCHAR(128) NOT NULL, + 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)) + + CHARACTER SET utf8 COLLATE utf8_bin; \ No newline at end of file