diff --git a/packages/backend/src/db/migrations/20230816173027_make_role_id_not_nullable_in_users.ts b/packages/backend/src/db/migrations/20230816173027_make_role_id_not_nullable_in_users.ts new file mode 100644 index 00000000..41b9bd93 --- /dev/null +++ b/packages/backend/src/db/migrations/20230816173027_make_role_id_not_nullable_in_users.ts @@ -0,0 +1,25 @@ +import { Knex } from 'knex'; + +export async function up(knex: Knex): Promise { + const role = await knex('roles') + .select('id') + .whereIn('key', ['user', 'admin']) + .orderBy('key', 'desc') + .limit(1) + .first(); + + if (role) { + // backfill nulls + await knex('users').whereNull('role_id').update({ role_id: role.id }); + } + + return await knex.schema.alterTable('users', (table) => { + table.uuid('role_id').notNullable().alter(); + }); +} + +export async function down(knex: Knex): Promise { + return await knex.schema.alterTable('users', (table) => { + table.uuid('role_id').nullable().alter(); + }); +}