File size: 6,663 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: queries.sql

package swodb

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const activeTxCount = `-- name: ActiveTxCount :one
SELECT COUNT(*)
FROM pg_stat_activity
WHERE "state" <> 'idle'
    AND "xact_start" <= $1
`

func (q *Queries) ActiveTxCount(ctx context.Context, xactStart pgtype.Timestamptz) (int64, error) {
	row := q.db.QueryRow(ctx, activeTxCount, xactStart)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const connectionInfo = `-- name: ConnectionInfo :many
SELECT application_name AS NAME,
    COUNT(*)
FROM pg_stat_activity
WHERE datname = current_database()
GROUP BY NAME
`

type ConnectionInfoRow struct {
	Name  pgtype.Text
	Count int64
}

func (q *Queries) ConnectionInfo(ctx context.Context) ([]ConnectionInfoRow, error) {
	rows, err := q.db.Query(ctx, connectionInfo)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ConnectionInfoRow
	for rows.Next() {
		var i ConnectionInfoRow
		if err := rows.Scan(&i.Name, &i.Count); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const databaseInfo = `-- name: DatabaseInfo :one
SELECT db_id AS id,
    version()
FROM switchover_state
`

type DatabaseInfoRow struct {
	ID      pgtype.UUID
	Version string
}

func (q *Queries) DatabaseInfo(ctx context.Context) (DatabaseInfoRow, error) {
	row := q.db.QueryRow(ctx, databaseInfo)
	var i DatabaseInfoRow
	err := row.Scan(&i.ID, &i.Version)
	return i, err
}

const disableChangeLogTriggers = `-- name: DisableChangeLogTriggers :exec
UPDATE switchover_state
SET current_state = 'idle'
WHERE current_state = 'in_progress'
`

func (q *Queries) DisableChangeLogTriggers(ctx context.Context) error {
	_, err := q.db.Exec(ctx, disableChangeLogTriggers)
	return err
}

const enableChangeLogTriggers = `-- name: EnableChangeLogTriggers :exec
UPDATE switchover_state
SET current_state = 'in_progress'
WHERE current_state = 'idle'
`

func (q *Queries) EnableChangeLogTriggers(ctx context.Context) error {
	_, err := q.db.Exec(ctx, enableChangeLogTriggers)
	return err
}

const foreignKeyRefs = `-- name: ForeignKeyRefs :many
SELECT src.relname::text,
    dst.relname::text
FROM pg_catalog.pg_constraint con
    JOIN pg_catalog.pg_namespace ns ON ns.nspname = 'public'
    AND ns.oid = con.connamespace
    JOIN pg_catalog.pg_class src ON src.oid = con.conrelid
    JOIN pg_catalog.pg_class dst ON dst.oid = con.confrelid
WHERE con.contype = 'f'
    AND NOT con.condeferrable
`

type ForeignKeyRefsRow struct {
	SrcRelname string
	DstRelname string
}

func (q *Queries) ForeignKeyRefs(ctx context.Context) ([]ForeignKeyRefsRow, error) {
	rows, err := q.db.Query(ctx, foreignKeyRefs)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ForeignKeyRefsRow
	for rows.Next() {
		var i ForeignKeyRefsRow
		if err := rows.Scan(&i.SrcRelname, &i.DstRelname); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const lastLogID = `-- name: LastLogID :one
SELECT COALESCE(MAX(id), 0)::bigint
FROM switchover_log
`

func (q *Queries) LastLogID(ctx context.Context) (int64, error) {
	row := q.db.QueryRow(ctx, lastLogID)
	var column_1 int64
	err := row.Scan(&column_1)
	return column_1, err
}

const logEvents = `-- name: LogEvents :many
SELECT id,
    TIMESTAMP,
    DATA
FROM switchover_log
WHERE id > $1
ORDER BY id ASC
LIMIT 100
`

func (q *Queries) LogEvents(ctx context.Context, id int64) ([]SwitchoverLog, error) {
	rows, err := q.db.Query(ctx, logEvents, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []SwitchoverLog
	for rows.Next() {
		var i SwitchoverLog
		if err := rows.Scan(&i.ID, &i.Timestamp, &i.Data); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const now = `-- name: Now :one
SELECT now()::timestamptz
`

func (q *Queries) Now(ctx context.Context) (pgtype.Timestamptz, error) {
	row := q.db.QueryRow(ctx, now)
	var column_1 pgtype.Timestamptz
	err := row.Scan(&column_1)
	return column_1, err
}

const sWOConnLock = `-- name: SWOConnLock :one
WITH LOCK AS (
    SELECT
        pg_advisory_lock_shared(4369))
SELECT
    current_state = 'use_next_db'
FROM
    LOCK,
    switchover_state
`

func (q *Queries) SWOConnLock(ctx context.Context) (bool, error) {
	row := q.db.QueryRow(ctx, sWOConnLock)
	var column_1 bool
	err := row.Scan(&column_1)
	return column_1, err
}

const sWOConnUnlockAll = `-- name: SWOConnUnlockAll :exec
SELECT
    pg_advisory_unlock_all()
`

func (q *Queries) SWOConnUnlockAll(ctx context.Context) error {
	_, err := q.db.Exec(ctx, sWOConnUnlockAll)
	return err
}

const sequenceNames = `-- name: SequenceNames :many
SELECT sequence_name::text
FROM information_schema.sequences
WHERE sequence_catalog = current_database()
    AND sequence_schema = 'public'
    AND sequence_name != 'change_log_id_seq'
`

func (q *Queries) SequenceNames(ctx context.Context) ([]string, error) {
	rows, err := q.db.Query(ctx, sequenceNames)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []string
	for rows.Next() {
		var sequence_name string
		if err := rows.Scan(&sequence_name); err != nil {
			return nil, err
		}
		items = append(items, sequence_name)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const tableColumns = `-- name: TableColumns :many
SELECT col.table_name::text,
    col.column_name::text,
    col.data_type::text,
    col.ordinal_position::INT
FROM information_schema.columns col
    JOIN information_schema.tables t ON t.table_catalog = col.table_catalog
    AND t.table_schema = col.table_schema
    AND t.table_name = col.table_name
    AND t.table_type = 'BASE TABLE'
WHERE col.table_catalog = current_database()
    AND col.table_schema = 'public'
`

type TableColumnsRow struct {
	ColTableName       string
	ColColumnName      string
	ColDataType        string
	ColOrdinalPosition int32
}

func (q *Queries) TableColumns(ctx context.Context) ([]TableColumnsRow, error) {
	rows, err := q.db.Query(ctx, tableColumns)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []TableColumnsRow
	for rows.Next() {
		var i TableColumnsRow
		if err := rows.Scan(
			&i.ColTableName,
			&i.ColColumnName,
			&i.ColDataType,
			&i.ColOrdinalPosition,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}