/* eslint-disable @typescript-eslint/camelcase */ import { MigrationBuilder } from 'node-pg-migrate'; import environment from '@bmd-studio/genstack-environment'; const { POSTGRES_PUBLIC_SCHEMA_NAME } = environment.env; const TABLE_NAME = 'project_permissions'; 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, }, project_id: { type : 'uuid', notNull: true, references: 'projects', }, identity_id: { type: 'uuid', notNull: true, references: 'identities', }, 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, `project_id`); pgm.createIndex(TABLE_NAME, `identity_id`); 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); };