/
Pg.ts
146 lines (123 loc) · 4.11 KB
/
Pg.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/// <reference path="../../adonis-typings/index.ts" />
import { DialectContract, PostgreConfig, QueryClientContract } from '@ioc:Adonis/Lucid/Database'
export class PgDialect implements DialectContract {
public readonly name = 'postgres'
public readonly supportsAdvisoryLocks = true
public readonly supportsViews = true
public readonly supportsTypes = true
public readonly supportsReturningStatement = true
/**
* Reference to the database version. Knex.js fetches the version after
* the first database query, so it will be set to undefined initially
*/
public readonly version = this.client.getReadClient()['context']['client'].version
/**
* The default format for datetime column. The date formats is
* valid for luxon date parsing library
*/
public readonly dateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"
constructor(private client: QueryClientContract, private config: PostgreConfig) {}
/**
* Returns an array of table names for one or many schemas.
*/
public async getAllTables(schemas: string[]) {
const tables = await this.client
.query()
.from('pg_catalog.pg_tables')
.select('tablename as table_name')
.whereIn('schemaname', schemas)
.orderBy('tablename', 'asc')
return tables.map(({ table_name }) => table_name)
}
/**
* Returns an array of all views names for one or many schemas
*/
public async getAllViews(schemas: string[]) {
const views = await this.client
.query()
.from('pg_catalog.pg_views')
.select('viewname as view_name')
.whereIn('schemaname', schemas)
.orderBy('viewname', 'asc')
return views.map(({ view_name }) => view_name)
}
/**
* Returns an array of all types names
*/
public async getAllTypes(_schemas: string[]) {
const types = await this.client
.query()
.select('pg_type.typname')
.distinct()
.from('pg_type')
.innerJoin('pg_enum', 'pg_enum.enumtypid', 'pg_type.oid')
return types.map(({ typname }) => typname)
}
/**
* Truncate pg table with option to cascade and restart identity
*/
public async truncate(table: string, cascade: boolean = false) {
return cascade
? this.client.rawQuery(`TRUNCATE "${table}" RESTART IDENTITY CASCADE;`)
: this.client.rawQuery(`TRUNCATE "${table}";`)
}
/**
* Drop all tables inside the database
*/
public async dropAllTables(schemas: string[]) {
let tables = await this.getAllTables(schemas)
/**
* Filter out tables that are not allowed to be dropped
*/
tables = tables.filter(
(table) => !(this.config.wipe?.ignoreTables || ['spatial_ref_sys']).includes(table)
)
if (!tables.length) {
return
}
await this.client.rawQuery(`DROP TABLE "${tables.join('", "')}" CASCADE;`)
}
/**
* Drop all views inside the database
*/
public async dropAllViews(schemas: string[]) {
const views = await this.getAllViews(schemas)
if (!views.length) return
await this.client.rawQuery(`DROP VIEW "${views.join('", "')}" CASCADE;`)
}
/**
* Drop all types inside the database
*/
public async dropAllTypes(schemas: string[]) {
const types = await this.getAllTypes(schemas)
if (!types.length) return
await this.client.rawQuery(`DROP TYPE "${types.join('", "')}" CASCADE;`)
}
/**
* Attempts to add advisory lock to the database and
* returns it's status.
*/
public async getAdvisoryLock(key: string): Promise<boolean> {
const response = await this.client.rawQuery(
`SELECT PG_TRY_ADVISORY_LOCK('${key}') as lock_status;`
)
return response.rows[0] && response.rows[0].lock_status === true
}
/**
* Releases the advisory lock
*/
public async releaseAdvisoryLock(key: string): Promise<boolean> {
const response = await this.client.rawQuery(
`SELECT PG_ADVISORY_UNLOCK('${key}') as lock_status;`
)
return response.rows[0] && response.rows[0].lock_status === true
}
}