Skip to content

Commit 705d7de

Browse files
beniaminmunteanunjlie
authored andcommitted
feat(card-service): introduce testcontainers for database and redis (#3533)
* feat(card-service): add testcontainers setup * chore(cards-service): lockfile * feat(cards-service): fix test containers setup * fix(cards-service): fix type issue
1 parent 7905bb4 commit 705d7de

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

pnpm-lock.yaml

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)