forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_extended.go
More file actions
335 lines (279 loc) · 9.72 KB
/
postgres_extended.go
File metadata and controls
335 lines (279 loc) · 9.72 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"regexp"
"github.com/abramad-labs/histomigrate/database"
"github.com/hashicorp/go-multierror"
"github.com/lib/pq"
)
func init() {
db := PostgresExtras{
Postgres: &Postgres{},
}
database.Register("postgres", &db)
database.Register("postgresql", &db)
}
type PostgresExtras struct {
*Postgres
}
// WithConnection initializes a new PostgresExtras instance using an existing, active sql.Conn and a Config struct.
// It ensures the connection is valid and, if not explicitly provided in the config, it automatically fetches the current database name and schema name from the connection.
// It also sets default values for the migrations table if none are specified and correctly parses quoted table names.
// Finally, it verifies the existence and readiness of the migrations version table in the database before returning the initialized PostgresExtras object.
func WithConnection(ctx context.Context, conn *sql.Conn, config *Config) (*PostgresExtras, error) {
if config == nil {
return nil, ErrNilConfig
}
if err := conn.PingContext(ctx); err != nil {
return nil, err
}
if config.DatabaseName == "" {
query := `SELECT CURRENT_DATABASE()`
var databaseName string
if err := conn.QueryRowContext(ctx, query).Scan(&databaseName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}
if len(databaseName) == 0 {
return nil, ErrNoDatabaseName
}
config.DatabaseName = databaseName
}
if config.SchemaName == "" {
query := `SELECT CURRENT_SCHEMA()`
var schemaName sql.NullString
if err := conn.QueryRowContext(ctx, query).Scan(&schemaName); err != nil {
return nil, &database.Error{OrigErr: err, Query: []byte(query)}
}
if !schemaName.Valid {
return nil, ErrNoSchema
}
config.SchemaName = schemaName.String
}
if len(config.MigrationsTable) == 0 {
config.MigrationsTable = DefaultMigrationsTable
}
config.migrationsSchemaName = config.SchemaName
config.migrationsTableName = config.MigrationsTable
if config.MigrationsTableQuoted {
re := regexp.MustCompile(`"(.*?)"`)
result := re.FindAllStringSubmatch(config.MigrationsTable, -1)
config.migrationsTableName = result[len(result)-1][1]
if len(result) == 2 {
config.migrationsSchemaName = result[0][1]
} else if len(result) > 2 {
return nil, fmt.Errorf("\"%s\" MigrationsTable contains too many dot characters", config.MigrationsTable)
}
}
px := &Postgres{
conn: conn,
config: config,
}
if err := px.ensureVersionTable(); err != nil {
return nil, err
}
return &PostgresExtras{
Postgres: px,
}, nil
}
// GetAllAppliedMigrations retrieves a list of all applied migration timestamps from the migrations table in the database.
// It constructs a SQL query to select migration_timestamp values, orders them in descending order, and executes the query against the database.
// The function then scans the results into a slice of integers and handles any potential database errors, including proper closing of the result rows.
func (p *PostgresExtras) GetAllAppliedMigrations() ([]int, error) {
schema := pq.QuoteIdentifier(p.config.migrationsSchemaName)
table := pq.QuoteIdentifier(p.config.migrationsTableName)
query := fmt.Sprintf(
`SELECT migration_timestamp FROM %s.%s ORDER BY migration_timestamp DESC`,
schema,
table,
)
rows, err := p.conn.QueryContext(context.Background(), query)
if err != nil {
return nil, &database.Error{
OrigErr: err,
Query: []byte(query),
}
}
defer func() {
if errClose := rows.Close(); errClose != nil {
err = multierror.Append(err, errClose)
}
}()
var appliedMigrations []int
for rows.Next() {
var migrTs int
if err := rows.Scan(&migrTs); err != nil {
return nil, &database.Error{
OrigErr: err,
Query: []byte(query),
}
}
appliedMigrations = append(appliedMigrations, migrTs)
}
if err := rows.Err(); err != nil {
return nil, &database.Error{
OrigErr: err,
Query: []byte(query),
}
}
return appliedMigrations, nil
}
// AddDirtyMigration marks a specific migration version as "dirty" or "in-progress" in the database's migrations table.
// It does this by inserting a new record with the provided version into the table within a database transaction.
// If the insertion fails or the transaction cannot be committed, it attempts to roll back the transaction and returns a detailed error.
func (p *PostgresExtras) AddDirtyMigration(version uint) error {
tx, err := p.conn.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return &database.Error{
OrigErr: err,
Err: "failed to start transaction",
}
}
schema := pq.QuoteIdentifier(p.config.migrationsSchemaName)
table := pq.QuoteIdentifier(p.config.migrationsTableName)
query := fmt.Sprintf(
`INSERT INTO %s.%s (migration_timestamp) VALUES ($1)`,
schema,
table,
)
if _, execErr := tx.Exec(query, version); execErr != nil {
if rbErr := tx.Rollback(); rbErr != nil {
execErr = multierror.Append(execErr, rbErr)
}
return &database.Error{
OrigErr: execErr,
Query: []byte(query),
}
}
if err := tx.Commit(); err != nil {
return &database.Error{
OrigErr: err,
Err: "failed to commit transaction",
}
}
return nil
}
// UpdateMigrationDirtyFlag updates the dirty status and applied_at timestamp for a specific migration version in the database's migrations table.
// It sets the dirty flag to true or false based on the provided boolean value and records the current timestamp.
// The operation is performed within a database transaction, with robust error handling for transaction start, execution, and commit/rollback.
func (p *PostgresExtras) UpdateMigrationDirtyFlag(version uint, dirty bool) error {
tx, err := p.conn.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return &database.Error{
OrigErr: err,
Err: "failed to start transaction",
}
}
schema := pq.QuoteIdentifier(p.config.migrationsSchemaName)
table := pq.QuoteIdentifier(p.config.migrationsTableName)
query := fmt.Sprintf(
`UPDATE %s.%s SET dirty = $1, applied_at = NOW() WHERE migration_timestamp = $2`,
schema,
table,
)
if _, execErr := tx.Exec(query, dirty, version); execErr != nil {
if rbErr := tx.Rollback(); rbErr != nil {
execErr = multierror.Append(execErr, rbErr)
}
return &database.Error{
OrigErr: execErr,
Query: []byte(query),
}
}
if err := tx.Commit(); err != nil {
return &database.Error{
OrigErr: err,
Err: "failed to commit transaction",
}
}
return nil
}
// IsMigrationApplied checks if a specific migration version has already been applied
// by querying the migrations table. It returns true if the migration is found,
// false if not found or if the table doesn't exist. It wraps other unexpected
// errors in a custom database.Error.
func (p *PostgresExtras) IsMigrationApplied(version uint) (bool, error) {
schema := pq.QuoteIdentifier(p.config.migrationsSchemaName)
table := pq.QuoteIdentifier(p.config.migrationsTableName)
query := fmt.Sprintf(
`SELECT COUNT(*) > 0 FROM %s.%s WHERE migration_timestamp = $1;`,
schema,
table,
)
var isApplied bool
err := p.conn.QueryRowContext(context.Background(), query, version).Scan(&isApplied)
if err != nil {
if err == sql.ErrNoRows {
return false, nil
}
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code.Name() == "undefined_table" {
return false, nil
}
return false, &database.Error{
OrigErr: err,
Query: []byte(query),
}
}
return isApplied, nil
}
// RemoveMigration deletes a specific migration version from the database's migrations table.
// It constructs and executes a SQL DELETE statement based on the provided version within a database transaction.
// The function includes comprehensive error handling for transaction management, ensuring that any failures during the deletion process are properly rolled back and reported.
func (p *PostgresExtras) RemoveMigration(version uint) error {
tx, err := p.conn.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return &database.Error{
OrigErr: err,
Err: "transaction start failed",
}
}
schema := pq.QuoteIdentifier(p.config.migrationsSchemaName)
table := pq.QuoteIdentifier(p.config.migrationsTableName)
query := fmt.Sprintf(
`DELETE FROM %s.%s WHERE migration_timestamp = $1;`,
schema,
table,
)
if _, execErr := tx.Exec(query, version); execErr != nil {
if rbErr := tx.Rollback(); rbErr != nil {
execErr = multierror.Append(execErr, rbErr)
}
return &database.Error{
OrigErr: execErr,
Query: []byte(query),
}
}
if err := tx.Commit(); err != nil {
return &database.Error{
OrigErr: err,
Err: "transaction commit failed",
}
}
return nil
}
// IsDatabaseDirty checks if the database's migrations table contains any "dirty" (in-progress or failed) migrations.
// It queries the migrations table for any entry where the dirty flag is set to true.
// If a dirty migration is found, it returns its migration_timestamp and true. If no dirty migrations are found,
// or if the migrations table itself doesn't exist (e.g., first run), it returns 0 and false. Any other database errors are wrapped and returned.
func (p *Postgres) IsDatabaseDirty() (int, bool, error) {
schema := pq.QuoteIdentifier(p.config.migrationsSchemaName)
table := pq.QuoteIdentifier(p.config.migrationsTableName)
query := fmt.Sprintf(`SELECT migration_timestamp FROM %s.%s WHERE dirty = true LIMIT 1`, schema, table)
var migr int
err := p.conn.QueryRowContext(context.Background(), query).Scan(&migr)
if err != nil {
if e, ok := err.(*pq.Error); ok && e.Code.Name() == "undefined_table" {
return 0, false, nil
}
if errors.Is(err, sql.ErrNoRows) {
return 0, false, nil
}
return 0, false, &database.Error{
OrigErr: err,
Query: []byte(query),
}
}
return migr, true, nil
}