/* eslint-disable @typescript-eslint/camelcase */ import { MigrationBuilder } from 'node-pg-migrate'; import environment from '@bmd-studio/genstack-environment'; const { POSTGRES_PUBLIC_SCHEMA_NAME, POSTGRES_IDENTITY_TABLE_NAME, POSTGRES_IDENTITY_IDENTIFICATION_COLUMN_NAME, POSTGRES_IDENTITY_ROLES_COLUMN_NAME, POSTGRES_IDENTITY_ROLE_NAME, POSTGRES_IDENTITY_SECRET_COLUMN_NAME, } = environment.env; const TABLE_NAME = POSTGRES_IDENTITY_TABLE_NAME as string; export const up = async (pgm: MigrationBuilder): Promise => { // create table pgm.createTable({ schema: POSTGRES_PUBLIC_SCHEMA_NAME, name: TABLE_NAME, }, { id: { type: 'uuid', primaryKey: true, default: pgm.func('uuid_generate_v4()'), }, status: { type: 'int', default: 1, }, name: { type : 'varchar', notNull: true, default: '' }, [POSTGRES_IDENTITY_IDENTIFICATION_COLUMN_NAME]: { type: 'citext', notNull: true, unique: true, }, [POSTGRES_IDENTITY_SECRET_COLUMN_NAME]: { type: 'varchar', notNull: true, }, [POSTGRES_IDENTITY_ROLES_COLUMN_NAME]: { type: 'jsonb', notNull: true, default: JSON.stringify([POSTGRES_IDENTITY_ROLE_NAME]), }, settings: { type: 'jsonb', notNull: true, default: JSON.stringify({}) }, created_at: { type: 'timestamptz', default: pgm.func('current_timestamp'), }, updated_at: { type: 'timestamptz', default: pgm.func('current_timestamp'), }, }); // add indexes pgm.createIndex(TABLE_NAME, `status`); pgm.createIndex(TABLE_NAME, `name`); pgm.createIndex(TABLE_NAME, POSTGRES_IDENTITY_IDENTIFICATION_COLUMN_NAME); pgm.createIndex(TABLE_NAME, POSTGRES_IDENTITY_ROLES_COLUMN_NAME); pgm.createIndex(TABLE_NAME, `created_at`, { opclass: 'DESC' }); pgm.createIndex(TABLE_NAME, `updated_at`, { opclass: 'DESC' }); // add triggers pgm.createTrigger(TABLE_NAME, 'set_update_timestamp', { when: 'BEFORE', operation: 'UPDATE', function: 'set_update_timestamp', }); }; export const down = async (pgm: MigrationBuilder): Promise => { pgm.dropTable(TABLE_NAME); };