mailtrain/setup/knex/migrations/20170507084114_create_permissions.js

44 lines
1.6 KiB
JavaScript
Raw Normal View History

const shareableEntityTypes = ['list', 'report', 'report_template', 'namespace'];
2017-05-15 20:22:06 +00:00
exports.up = function(knex, Promise) {
let schema = knex.schema;
for (const entityType of shareableEntityTypes) {
schema = schema
.createTable(`shares_${entityType}`, table => {
table.increments('id').primary();
table.integer('entity').unsigned().notNullable().references(`${entityType}s.id`).onDelete('CASCADE');
table.integer('user').unsigned().notNullable().references('users.id').onDelete('CASCADE');
table.string('role', 64).notNullable();
table.unique(['entity', 'user']);
})
.createTable(`permissions_${entityType}`, table => {
table.increments('id').primary();
table.integer('entity').unsigned().notNullable().references(`${entityType}s.id`).onDelete('CASCADE');
table.integer('user').unsigned().notNullable().references('users.id').onDelete('CASCADE');
table.string('operation', 64).notNullable();
table.unique(['entity', 'user', 'operation']);
});
}
schema = schema.then(() => knex('shares_namespace').insert({
2017-05-15 20:22:06 +00:00
id: 1,
entity: 1,
2017-05-15 20:22:06 +00:00
user: 1,
role: 'master'
}));
2017-05-15 20:22:06 +00:00
return schema;
2017-05-15 20:22:06 +00:00
};
exports.down = function(knex, Promise) {
let schema = knex.schema;
for (const entityType of shareableEntityTypes) {
schema = schema
.dropTable(`shares_${entityType}`)
.dropTable(`permissions_${entityType}`);
}
return schema;
2017-05-15 20:22:06 +00:00
};