import * as Knex from 'knex' ; import { channels } from './20200707165856_initial' ; import { addValidChainServiceRequests , chainServiceRequests } from './20200915140300_chain_service' ; export const chain_service_requests = 'chain_service_requests' ; const requestsConstraint = `valid_requests` ; export async function up ( knex ) { await knex . schema . alterTable ( channels , table => table . dropColumn ( chainServiceRequests ) ) ; await knex . schema . createTable ( chain_service_requests , function ( table ) { table . string ( 'channel_id' ) . notNullable ( ) ; table . string ( 'request' ) . notNullable ( ) ; table . timestamp ( 'timestamp' ) . notNullable ( ) ; table . integer ( 'attempts' ) . defaultTo ( 1 ) ; table . primary ( [ 'channel_id' , 'request' ] ) ; table . foreign ( 'channel_id' ) . references ( 'channels.channel_id' ) ; } ) ; await knex . raw ( `\
    ALTER TABLE ${ chainServiceRequests }
    ADD CONSTRAINT ${ requestsConstraint }
    CHECK (
      array_position (ARRAY['fund', 'withdraw', 'challenge']::VARCHAR[], request ) IS NOT NULL
    )
  ` ) ; } export async function down ( knex ) { await knex . schema . dropTable ( chain_service_requests ) ; await knex . schema . alterTable ( channels , table => { table . specificType ( chainServiceRequests , 'text[]' ) . notNullable ( ) . defaultTo ( '{}' ) ; } ) ; await addValidChainServiceRequests ( knex , "'fund', 'withdraw'" ) ; }