• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

golang-migrate / migrate / 16223610850

11 Jul 2025 03:22PM UTC coverage: 53.82% (-2.5%) from 56.314%
16223610850

Pull #1294

github

dsyers
fix
Pull Request #1294: Triggers

494 of 1249 new or added lines in 23 files covered. (39.55%)

4 existing lines in 4 files now uncovered.

4981 of 9255 relevant lines covered (53.82%)

54.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/database/mysql/examples/triggers/main.go
1
package main
2

3
import (
4
        "context"
5
        "database/sql"
6
        "fmt"
7
        "github.com/golang-migrate/migrate/v4"
8
        "github.com/golang-migrate/migrate/v4/database"
9
        "github.com/golang-migrate/migrate/v4/database/mysql"
10
        _ "github.com/golang-migrate/migrate/v4/source/file"
11
        "os"
12
)
13

14
type App struct {
15
        Connection     *sql.Conn
16
        MigrationTable string
17
        HistoryID      *int64
18
}
19

NEW
20
func main() {
×
NEW
21
        db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/db?multiStatements=true")
×
NEW
22
        if err != nil {
×
NEW
23
                fmt.Printf("%v\n", err)
×
NEW
24
                os.Exit(1)
×
NEW
25
        }
×
NEW
26
        ctx := context.Background()
×
NEW
27
        conn, err := db.Conn(ctx)
×
NEW
28
        if err != nil {
×
NEW
29
                fmt.Printf("%v\n", err)
×
NEW
30
                os.Exit(1)
×
NEW
31
        }
×
NEW
32
        defer conn.Close()
×
NEW
33
        defer db.Close()
×
NEW
34

×
NEW
35
        app := &App{
×
NEW
36
                Connection:     conn,
×
NEW
37
                MigrationTable: mysql.DefaultMigrationsTable,
×
NEW
38
        }
×
NEW
39

×
NEW
40
        databaseDrv, err := mysql.WithConnection(ctx, conn, &mysql.Config{
×
NEW
41
                DatabaseName: "db",
×
NEW
42
                Triggers: map[string]func(response interface{}) error{
×
NEW
43
                        database.TrigVersionTableExists: app.MigrationHistoryTable,
×
NEW
44
                        database.TrigVersionTablePost:   app.MigrationHistoryTable,
×
NEW
45
                        database.TrigRunPost:            app.DatabaseRunPost,
×
NEW
46
                },
×
NEW
47
        })
×
NEW
48
        if err != nil {
×
NEW
49
                fmt.Printf("%v\n", err)
×
NEW
50
                os.Exit(1)
×
NEW
51
        }
×
NEW
52
        m, err := migrate.NewFromOptions(migrate.Options{
×
NEW
53
                DatabaseInstance: databaseDrv,
×
NEW
54
                DatabaseName:     "db",
×
NEW
55
                SourceURL:        "file://migrations",
×
NEW
56
                MigrateTriggers: map[string]func(response migrate.TriggerResponse) error{
×
NEW
57
                        migrate.TrigRunMigrationVersionPre:  app.RunMigrationVersionPre,
×
NEW
58
                        migrate.TrigRunMigrationVersionPost: app.RunMigrationVersionPost,
×
NEW
59
                },
×
NEW
60
        })
×
NEW
61
        if err != nil {
×
NEW
62
                fmt.Println(err)
×
NEW
63
                os.Exit(1)
×
NEW
64
        }
×
NEW
65
        err = m.Up()
×
NEW
66
        //err = m.Down()
×
NEW
67
        if err != nil {
×
NEW
68
                fmt.Printf("%v\n", err)
×
NEW
69
                return
×
NEW
70
        }
×
71
}
72

NEW
73
func (app *App) MigrationHistoryTable(response interface{}) error {
×
NEW
74
        r, _ := response.(mysql.TriggerResponse)
×
NEW
75
        fmt.Printf("Executing database trigger %s\n, %v\n", r.Trigger, r)
×
NEW
76
        query := "CREATE TABLE IF NOT EXISTS `" + app.MigrationTable + "_history` (`id` bigint not null primary key auto_increment, `version` bigint not null, `target` bigint, identifier varchar(255), `dirty` tinyint not null, migration text, `timestamp` datetime not null)"
×
NEW
77
        _, err := app.Connection.ExecContext(context.TODO(), query)
×
NEW
78
        return err
×
NEW
79
}
×
80

NEW
81
func (app *App) DatabaseRunPost(response interface{}) error {
×
NEW
82
        r, _ := response.(mysql.TriggerResponse)
×
NEW
83
        detail := r.Detail.(struct{ Query string })
×
NEW
84
        fmt.Printf("Executing database trigger %s\n, %v\n", r.Trigger, r.Detail)
×
NEW
85
        query := "UPDATE `" + app.MigrationTable + "_history` SET `migration` = ? WHERE `id` = ?"
×
NEW
86
        _, err := app.Connection.ExecContext(context.TODO(), query, detail.Query, app.HistoryID)
×
NEW
87
        if err != nil {
×
NEW
88
                fmt.Printf("Error updating migration history: %v\n", err)
×
NEW
89
                return err
×
NEW
90
        }
×
91

NEW
92
        return nil
×
93
}
94

NEW
95
func (app *App) RunMigrationVersionPre(r migrate.TriggerResponse) error {
×
NEW
96
        detail := r.Detail.(struct{ Migration *migrate.Migration })
×
NEW
97
        fmt.Printf("Executing migration trigger %s\n, %v\n", r.Trigger, r.Detail)
×
NEW
98
        query := "INSERT INTO `" + app.MigrationTable + "_history` (`version`, `identifier`, `target`, `dirty`, `timestamp`) VALUES (?, ?, ?, 1, NOW())"
×
NEW
99
        _, err := app.Connection.ExecContext(context.TODO(), query, detail.Migration.Version, detail.Migration.Identifier, detail.Migration.TargetVersion)
×
NEW
100
        if err != nil {
×
NEW
101
                fmt.Printf("Error inserting migration history: %v\n", err)
×
NEW
102
                return err
×
NEW
103
        }
×
NEW
104
        query = "SELECT LAST_INSERT_ID()"
×
NEW
105
        row := app.Connection.QueryRowContext(context.TODO(), query)
×
NEW
106
        return row.Scan(&app.HistoryID)
×
107
}
108

NEW
109
func (app *App) RunMigrationVersionPost(r migrate.TriggerResponse) error {
×
NEW
110
        fmt.Printf("Executing migration trigger %s\n, %v\n", r.Trigger, r.Detail)
×
NEW
111
        query := "UPDATE `" + app.MigrationTable + "_history` SET `dirty` = 0, `timestamp` = NOW() WHERE `id` = ?"
×
NEW
112
        _, err := app.Connection.ExecContext(context.TODO(), query, app.HistoryID)
×
NEW
113
        if err != nil {
×
NEW
114
                fmt.Printf("Error updating migration history: %v\n", err)
×
NEW
115
                return err
×
NEW
116
        }
×
NEW
117
        app.HistoryID = nil
×
NEW
118

×
NEW
119
        return nil
×
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc