-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplied-migration.go
55 lines (46 loc) · 1.54 KB
/
applied-migration.go
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
package pgxschema
import (
"fmt"
"time"
)
// AppliedMigration represents a successfully-executed migration. It embeds
// Migration, and adds fields for execution results. This type is what
// records persisted in the schema_migrations table align with.
type AppliedMigration struct {
Migration
// Checksum is the MD5 hash of the Script for this migration
Checksum string
// ExecutionTimeInMillis is populated after the migration is run, indicating
// how much time elapsed while the Script was executing.
ExecutionTimeInMillis int
// AppliedAt is the time at which this particular migration's Script began
// executing (not when it completed executing).
AppliedAt time.Time
}
// GetAppliedMigrations retrieves all already-applied migrations in a map keyed
// by the migration IDs
//
func (m Migrator) GetAppliedMigrations(db Queryer) (applied map[string]*AppliedMigration, err error) {
applied = make(map[string]*AppliedMigration)
migrations := make([]*AppliedMigration, 0)
tn := QuotedTableName(m.schemaName, m.tableName)
query := fmt.Sprintf(`
SELECT id, checksum, execution_time_in_millis, applied_at
FROM %s
ORDER BY id ASC
`, tn)
rows, err := db.Query(m.ctx, query)
if err != nil {
return applied, err
}
defer rows.Close()
for rows.Next() {
migration := AppliedMigration{}
err = rows.Scan(&migration.ID, &migration.Checksum, &migration.ExecutionTimeInMillis, &migration.AppliedAt)
migrations = append(migrations, &migration)
}
for _, migration := range migrations {
applied[migration.ID] = migration
}
return applied, err
}