File tree Expand file tree Collapse file tree 3 files changed +51
-5
lines changed
Expand file tree Collapse file tree 3 files changed +51
-5
lines changed Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+ set -e
3+
4+ psql -v ON_ERROR_STOP=1 --username " $POSTGRES_USER " --dbname " $POSTGRES_DB " << -EOSQL
5+ DROP DATABASE IF EXISTS TESTING;
6+ CREATE DATABASE testing;
7+ CREATE DATABASE development;
8+ EOSQL
Original file line number Diff line number Diff line change 1+ import { IocContract } from '@adonisjs/fold'
2+ import { Knex } from 'knex'
3+ import { AppServices } from '../app'
4+
5+ export async function truncateTable (
6+ knex : Knex ,
7+ tableName : string
8+ ) : Promise < void > {
9+ const RAW = `TRUNCATE TABLE "${ tableName } " RESTART IDENTITY CASCADE`
10+ await knex . raw ( RAW )
11+ }
12+
13+ export async function truncateTables (
14+ deps : IocContract < AppServices >
15+ ) : Promise < void > {
16+ const knex = await deps . use ( 'knex' )
17+
18+ const ignoreTables = [
19+ 'knex_migrations' ,
20+ 'knex_migrations_lock' ,
21+ 'card_service_knex_migrations' ,
22+ 'card_service_knex_migrations_lock'
23+ ]
24+
25+ const tables = await getTables ( knex , ignoreTables )
26+ if ( tables . length > 0 ) {
27+ const RAW = `TRUNCATE TABLE "${ tables . join ( '","' ) } " RESTART IDENTITY CASCADE`
28+ await knex . raw ( RAW )
29+ }
30+ }
31+
32+ async function getTables (
33+ knex : Knex ,
34+ ignoredTables : string [ ]
35+ ) : Promise < string [ ] > {
36+ const result = await knex . raw (
37+ `SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname='public'`
38+ )
39+ return result . rows
40+ . map ( ( val : { tablename : string } ) => val . tablename )
41+ . filter ( ( tableName : string ) => ! ignoredTables . includes ( tableName ) )
42+ }
You can’t perform that action at this time.
0 commit comments