mailtrain/setup/knex/migrations/20170507083345_create_namespaces.js

35 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-05-15 20:22:06 +00:00
exports.up = function(knex, Promise) {
const entityTypesAddNamespace = ['list', 'report', 'report_template', 'user'];
let schema = knex.schema;
schema = schema.createTable('namespaces', table => {
2017-05-15 20:22:06 +00:00
table.increments('id').primary();
table.string('name');
table.text('description');
table.integer('namespace').unsigned().references('namespaces.id').onDelete('CASCADE');
2017-05-15 20:22:06 +00:00
})
.then(() => knex('namespaces').insert({
id: 1, /* Global namespace id */
2017-05-15 20:22:06 +00:00
name: 'Global',
description: 'Global namespace'
}));
for (const entityType of entityTypesAddNamespace) {
schema = schema
.then(() => knex.schema.table(`${entityType}s`, table => {
table.integer('namespace').unsigned().notNullable();
}))
.then(() => knex(`${entityType}s`).update({
namespace: 1 /* Global namespace id */
}))
.then(() => knex.schema.table(`${entityType}s`, table => {
table.foreign('namespace').references('namespaces.id').onDelete('CASCADE');
}));
}
2017-05-15 20:22:06 +00:00
return schema;
2017-05-15 20:22:06 +00:00
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('namespaces');
};