From f8b0ffd39b4f9b4276228eaa7bc9ae84183a24ca Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sat, 18 Feb 2023 14:26:10 +0100 Subject: [PATCH] feat: Introduce role column to user model --- .../20230218110748_add_role_to_users.ts | 15 +++++++++++++++ ...131824_alter_role_to_not_nullable_for_users.ts | 13 +++++++++++++ packages/backend/src/models/user.ts | 2 ++ 3 files changed, 30 insertions(+) create mode 100644 packages/backend/src/db/migrations/20230218110748_add_role_to_users.ts create mode 100644 packages/backend/src/db/migrations/20230218131824_alter_role_to_not_nullable_for_users.ts diff --git a/packages/backend/src/db/migrations/20230218110748_add_role_to_users.ts b/packages/backend/src/db/migrations/20230218110748_add_role_to_users.ts new file mode 100644 index 00000000..66f75456 --- /dev/null +++ b/packages/backend/src/db/migrations/20230218110748_add_role_to_users.ts @@ -0,0 +1,15 @@ +import { Knex } from 'knex'; + +export async function up(knex: Knex): Promise { + return knex.schema.table('users', async (table) => { + table.string('role'); + + await knex('users').update({ role: 'admin' }); + }); +} + +export async function down(knex: Knex): Promise { + return knex.schema.table('users', (table) => { + table.dropColumn('role'); + }); +} diff --git a/packages/backend/src/db/migrations/20230218131824_alter_role_to_not_nullable_for_users.ts b/packages/backend/src/db/migrations/20230218131824_alter_role_to_not_nullable_for_users.ts new file mode 100644 index 00000000..505d18a4 --- /dev/null +++ b/packages/backend/src/db/migrations/20230218131824_alter_role_to_not_nullable_for_users.ts @@ -0,0 +1,13 @@ +import { Knex } from 'knex'; + +export async function up(knex: Knex): Promise { + return knex.schema.alterTable('users', (table) => { + table.string('role').notNullable().alter(); + }); +} + +export async function down(knex: Knex): Promise { + return knex.schema.alterTable('users', (table) => { + table.string('role').nullable().alter(); + }); +} diff --git a/packages/backend/src/models/user.ts b/packages/backend/src/models/user.ts index 99908d41..a1c92444 100644 --- a/packages/backend/src/models/user.ts +++ b/packages/backend/src/models/user.ts @@ -10,6 +10,7 @@ class User extends Base { id!: string; email!: string; password!: string; + role: string; connections?: Connection[]; flows?: Flow[]; steps?: Step[]; @@ -25,6 +26,7 @@ class User extends Base { id: { type: 'string', format: 'uuid' }, email: { type: 'string', format: 'email', minLength: 1, maxLength: 255 }, password: { type: 'string', minLength: 1, maxLength: 255 }, + role: { type: 'string', enum: ['admin', 'user'] }, }, };