-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtruncate.js
38 lines (32 loc) · 1 KB
/
truncate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const KNEX_TABLES = ['knex_migrations', 'knex_migrations_lock']
let truncateQuery
async function getTruncateQuery(knex) {
if (!truncateQuery) {
const result = await knex.schema.raw(
"SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public'",
)
const tables = result.rows.reduce(
(_tables, { tablename }) =>
KNEX_TABLES.includes(tablename) ? _tables : [..._tables, tablename],
[],
)
const disableTriggers = tables.map(
table => `ALTER TABLE ${table} DISABLE TRIGGER ALL`,
)
const deletes = tables.map(table => `DELETE FROM ${table}`)
const enableTriggers = tables.map(
table => `ALTER TABLE ${table} ENABLE TRIGGER ALL`,
)
truncateQuery = [...disableTriggers, ...deletes, ...enableTriggers].join(
';',
)
}
return truncateQuery
}
async function truncate({ getKnex }) {
const knex = getKnex()
const query = await getTruncateQuery(knex)
await knex.schema.raw(query)
knex.destroy()
}
export default truncate