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

33 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-08-11 06:51:30 +00:00
exports.up = (knex, Promise) => (async() => {
2017-08-13 18:11:58 +00:00
const entityTypesAddNamespace = ['list', 'custom_form', 'template', 'campaign', 'report', 'report_template', 'user'];
2017-08-11 06:51:30 +00:00
await knex.schema.createTable('namespaces', table => {
table.increments('id').primary();
table.string('name');
table.text('description');
2017-08-13 18:11:58 +00:00
table.integer('namespace').unsigned().references('namespaces.id');
2017-08-11 06:51:30 +00:00
});
await knex('namespaces').insert({
id: 1, /* Global namespace id */
name: 'Root',
description: 'Root namespace'
});
for (const entityType of entityTypesAddNamespace) {
2017-08-11 06:51:30 +00:00
await knex.schema.table(`${entityType}s`, table => {
table.integer('namespace').unsigned().notNullable();
});
2017-05-15 20:22:06 +00:00
2017-08-11 06:51:30 +00:00
await knex(`${entityType}s`).update({
namespace: 1 /* Global namespace id */
});
await knex.schema.table(`${entityType}s`, table => {
2017-08-13 18:11:58 +00:00
table.foreign('namespace').references('namespaces.id');
2017-08-11 06:51:30 +00:00
});
}
})();
2017-05-15 20:22:06 +00:00
2017-08-11 06:51:30 +00:00
exports.down = (knex, Promise) => (async() => {
await knex.schema.dropTable('namespaces');
})();