· 6 years ago · Mar 27, 2019, 05:28 PM
1'use strict';
2
3exports.up = async knex => {
4 // Create Schema
5 await knex.raw('CREATE SCHEMA IF NOT EXISTS meraki');
6 // Load Extensions
7 await knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"');
8 // Roles
9 await knex.schema.withSchema('meraki').createTable('roles', table => {
10 table
11 .string('id')
12 .primary()
13 .defaultTo(knex.raw('uuid_generate_v4()'));
14 table
15 .string('name')
16 .unique()
17 .notNullable();
18 table.string('description').notNullable();
19 table.timestamps();
20 });
21 // Permissions
22 await knex.schema.withSchema('meraki').createTable('permissions', table => {
23 table
24 .string('id')
25 .primary()
26 .defaultTo(knex.raw('uuid_generate_v4()'));
27 table
28 .string('name')
29 .unique()
30 .notNullable();
31 table.timestamps();
32 });
33 // Role Permissions
34 await knex.schema.withSchema('meraki').createTable('role_permissions', table => {
35 table
36 .string('role_id')
37 .notNullable()
38 .references('id')
39 .inTable('roles');
40 table
41 .string('permission_id')
42 .notNullable()
43 .references('id')
44 .inTable('permissions');
45 table.timestamps();
46 });
47 // Users
48 await knex.schema.withSchema('meraki').createTable('users', table => {
49 table
50 .string('id')
51 .primary()
52 .defaultTo(knex.raw('uuid_generate_v4()'));
53 table
54 .string('email', 320)
55 .unique()
56 .notNullable();
57 table.string('first_name', 35).notNullable();
58 table.string('middle_name', 35).notNullable();
59 table.string('last_name', 35).notNullable();
60 table.boolean('email_verified');
61 table.string('verification_token');
62 table.timestamps();
63 });
64 // User Roles
65 await knex.schema.withSchema('meraki').createTable('user_roles', table => {
66 table
67 .string('user_id')
68 .notNullable()
69 .references('id')
70 .inTable('users');
71 table
72 .string('role_id')
73 .notNullable()
74 .references('id')
75 .inTable('roles');
76 table.timestamps();
77 });
78 // Tokens
79 await knex.schema.withSchema('meraki').createTable('tokens', table => {
80 table.string('id').primary();
81 table
82 .string('user_id')
83 .notNullable()
84 .references('id')
85 .inTable('users');
86 table
87 .bigInteger('ttl')
88 .notNullable()
89 .defaultTo(1209600);
90 table.timestamps();
91 });
92};
93
94exports.down = async knex => {
95 // Tokens
96 await knex.schema.withSchema('meraki').dropTableIfExists('tokens');
97 // User Roles
98 await knex.schema.withSchema('meraki').dropTableIfExists('user_roles');
99 // Users
100 await knex.schema.withSchema('meraki').dropTableIfExists('users');
101 // Role Permissions
102 await knex.schema.withSchema('meraki').dropTableIfExists('role_permissions');
103 // Permissions
104 await knex.schema.withSchema('meraki').dropTableIfExists('permissions');
105 // Roles
106 await knex.schema.withSchema('meraki').dropTableIfExists('roles');
107 // Drop Extensions
108 await knex.raw('DROP EXTENSION IF EXISTS "uuid-ossp"');
109 // Delete Schema
110 await knex.raw('DROP SCHEMA IF EXISTS meraki');
111};