-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver_postgres.go
More file actions
247 lines (210 loc) · 6.45 KB
/
driver_postgres.go
File metadata and controls
247 lines (210 loc) · 6.45 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
// Package gomigration provides a PostgreSQL migration driver for managing and applying SQL migrations.
package gomigration
import (
"context"
"database/sql"
"fmt"
"log"
"strings"
"time"
_ "github.com/lib/pq"
)
// PostgresDriver manages database connections and migration operations for PostgreSQL.
type PostgresDriver struct {
db *sql.DB
migrationTableName string
}
// NewPostgresDriver creates and returns a new instance of PostgresDriver.
// It opens a connection to the given PostgreSQL database using the provided credentials and schema.
func NewPostgresDriver(
host string,
port string,
user string,
password string,
database string,
schema string,
) (*PostgresDriver, error) {
dsn := "host=%s port=%s user=%s password=%s dbname=%s sslmode=disable search_path=%s"
dsn = fmt.Sprintf(dsn, host, port, user, password, database, schema)
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
return nil, err
}
return &PostgresDriver{
db: db,
migrationTableName: "migrations",
}, nil
}
// Close closes the database connection.
func (p *PostgresDriver) Close() error {
if p.db != nil {
return p.db.Close()
}
return nil
}
// SetMigrationTableName sets the name of the table used to track executed migrations.
// If the provided name is empty, the default "migrations" is used.
func (p *PostgresDriver) SetMigrationTableName(name string) {
if name == "" {
name = "migrations"
}
p.migrationTableName = name
}
// CreateMigrationsTable creates the migration tracking table if it does not exist.
func (p *PostgresDriver) CreateMigrationsTable(ctx context.Context) error {
query := fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
name VARCHAR(255) PRIMARY KEY NOT NULL,
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`, p.migrationTableName)
_, err := p.db.ExecContext(ctx, query)
return err
}
// GetExecutedMigrations returns a list of executed migrations from the tracking table.
// If reverse is true, the list is ordered descending by name.
func (p *PostgresDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error) {
order := "ASC"
if reverse {
order = "DESC"
}
query := fmt.Sprintf(`SELECT name, executed_at FROM %s ORDER BY name %s;`, p.migrationTableName, order)
rows, err := p.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var migrations []ExecutedMigration
for rows.Next() {
var name string
var executedAt time.Time
if err := rows.Scan(&name, &executedAt); err != nil {
return nil, err
}
migrations = append(migrations, ExecutedMigration{
Name: name,
ExecutedAt: executedAt,
})
}
if err := rows.Err(); err != nil {
return nil, err
}
return migrations, nil
}
// CleanDatabase drops all tables in the "public" schema.
func (p *PostgresDriver) CleanDatabase(ctx context.Context) error {
rows, err := p.db.QueryContext(ctx, `
SELECT tablename
FROM pg_tables
WHERE schemaname = 'public';
`)
if err != nil {
return fmt.Errorf("query table names: %w", err)
}
defer rows.Close()
var tables []string
for rows.Next() {
var table string
if err := rows.Scan(&table); err != nil {
return fmt.Errorf("scan table name: %w", err)
}
tables = append(tables, fmt.Sprintf(`"%s"`, table)) // safely quote identifiers
}
if len(tables) == 0 {
log.Println("no tables to drop")
return nil
}
query := fmt.Sprintf(`DROP TABLE IF EXISTS %s CASCADE;`, strings.Join(tables, ", "))
if _, err := p.db.ExecContext(ctx, query); err != nil {
return fmt.Errorf("drop tables: %w", err)
}
log.Println("all public tables dropped")
return nil
}
// ApplyMigrations runs the "up" SQL scripts for the given migrations.
// Optional callbacks can be provided to track the progress of each migration.
func (p *PostgresDriver) ApplyMigrations(
ctx context.Context,
migrations []Migration,
onRunning func(migration *Migration),
onSuccess func(migration *Migration),
onFailed func(migration *Migration, err error),
) error {
for i := range migrations {
m := migrations[i]
if onRunning != nil {
onRunning(&m)
}
if err := p.executeMigrationSQL(ctx, m.UpScript()); err != nil {
if onFailed != nil {
onFailed(&m, err)
}
return fmt.Errorf("failed to apply migration %s: %w", m.Name(), err)
}
if err := p.insertExecutedMigration(ctx, m.Name(), time.Now()); err != nil {
if onFailed != nil {
onFailed(&m, err)
}
return fmt.Errorf("failed to record migration %s: %w", m.Name(), err)
}
if onSuccess != nil {
onSuccess(&m)
}
}
return nil
}
// UnapplyMigrations runs the "down" SQL scripts for the given migrations in reverse order.
// Optional callbacks can be provided to track the progress of each migration.
func (p *PostgresDriver) UnapplyMigrations(
ctx context.Context,
migrations []Migration,
onRunning func(migration *Migration),
onSuccess func(migration *Migration),
onFailed func(migration *Migration, err error),
) error {
for i := range migrations {
mig := migrations[i]
if onRunning != nil {
onRunning(&mig)
}
if err := p.executeMigrationSQL(ctx, mig.DownScript()); err != nil {
if onFailed != nil {
onFailed(&mig, err)
}
return fmt.Errorf("failed to unapply migration %s: %w", mig.Name(), err)
}
if err := p.removeExecutedMigration(ctx, mig.Name()); err != nil {
if onFailed != nil {
onFailed(&mig, err)
}
return fmt.Errorf("failed to remove migration record %s: %w", mig.Name(), err)
}
if onSuccess != nil {
onSuccess(&mig)
}
}
return nil
}
// executeMigrationSQL runs a given SQL script as part of a migration.
func (p *PostgresDriver) executeMigrationSQL(ctx context.Context, sql string) error {
if sql == "" {
return nil
}
_, err := p.db.ExecContext(ctx, sql)
return err
}
// insertExecutedMigration records the given migration name and execution time in the tracking table.
func (p *PostgresDriver) insertExecutedMigration(ctx context.Context, name string, executedAt time.Time) error {
query := fmt.Sprintf(`INSERT INTO %s (name, executed_at) VALUES ($1, $2)`, p.migrationTableName)
_, err := p.db.ExecContext(ctx, query, name, executedAt)
return err
}
// removeExecutedMigration deletes the record of the given migration from the tracking table.
func (p *PostgresDriver) removeExecutedMigration(ctx context.Context, name string) error {
query := fmt.Sprintf(`DELETE FROM %s WHERE name = $1`, p.migrationTableName)
_, err := p.db.ExecContext(ctx, query, name)
return err
}