mailtrain/setup/knex/migrations/20170507083345_create_namespaces.js
Tomas Bures 89c9615592 WiP on permissions
Doesn't run. This commit is just to backup the changes.
2017-07-26 22:42:05 +03:00

35 lines
No EOL
1.3 KiB
JavaScript

exports.up = function(knex, Promise) {
const entityTypesAddNamespace = ['list', 'report', 'report_template', 'user'];
let schema = knex.schema;
schema = schema.createTable('namespaces', table => {
table.increments('id').primary();
table.string('name');
table.text('description');
table.integer('namespace').unsigned().references('namespaces.id').onDelete('CASCADE');
})
.then(() => knex('namespaces').insert({
id: 1, /* Global namespace id */
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');
}));
}
return schema;
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('namespaces');
};