Skip to content

Commit d01389d

Browse files
0xgoudapashagolub
andauthored
[+] apply admin functions migrations for v5, fixes #1180 (#1181)
* Apply admin functions migrations for v5 * drop function `ensure_partition_metric_dbname_time` in v5 migration for easier migration from v4.0.0 => v5.0.0 * Fix use `,` not `;` * make linter happy * fix tests * make linter happy * bump `sinkSchema` to the latest version --------- Co-authored-by: Pavlo Golub <pavlo.golub@gmail.com>
1 parent d583564 commit d01389d

4 files changed

Lines changed: 62 additions & 4 deletions

File tree

cmd/pgwatch/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var (
88
version = "unknown"
99
date = "unknown"
1010
configSchema = "00824"
11-
sinkSchema = "01110"
11+
sinkSchema = "01180"
1212
)
1313

1414
func printVersion() {

internal/sinks/postgres.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,23 @@ var migrations func() migrator.Option = func() migrator.Option {
603603
},
604604
},
605605

606+
&migrator.Migration{
607+
Name: "01180 Apply admin functions migrations for v5",
608+
Func: func(ctx context.Context, tx pgx.Tx) error {
609+
_, err := tx.Exec(ctx, `DROP FUNCTION IF EXISTS admin.ensure_partition_metric_dbname_time`)
610+
if err != nil {
611+
return err
612+
}
613+
614+
_, err = tx.Exec(ctx, sqlMetricEnsurePartitionPostgres)
615+
if err != nil {
616+
return err
617+
}
618+
_, err = tx.Exec(ctx, sqlMetricAdminFunctions)
619+
return err
620+
},
621+
},
622+
606623
// adding new migration here, update "admin"."migration" in "admin_schema.sql"!
607624

608625
// &migrator.Migration{

internal/sinks/postgres_schema_test.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,62 @@
11
package sinks
22

33
import (
4+
"context"
45
"testing"
56

67
migrator "github.com/cybertec-postgresql/pgx-migrator"
8+
"github.com/jackc/pgx/v5"
79
"github.com/pashagolub/pgxmock/v4"
810
"github.com/stretchr/testify/assert"
911
)
1012

1113
func TestPostgresWriterMigrate(t *testing.T) {
14+
oldInitMigrator := initMigrator
15+
t.Cleanup(func() {
16+
initMigrator = oldInitMigrator
17+
})
18+
1219
a := assert.New(t)
1320
conn, err := pgxmock.NewPool()
1421
a.NoError(err)
1522

16-
// Expect migration table creation and migration execution
23+
// Mock the migrator to use simple migrations for testing
24+
initMigrator = func(_ *PostgresWriter) (*migrator.Migrator, error) {
25+
return migrator.New(
26+
migrator.TableName("admin.migration"),
27+
migrator.Migrations(
28+
&migrator.Migration{
29+
Name: "Test migration 1",
30+
Func: func(ctx context.Context, tx pgx.Tx) error {
31+
_, err := tx.Query(ctx, "SELECT 1 AS col1")
32+
return err
33+
},
34+
},
35+
&migrator.Migration{
36+
Name: "Test migration 2",
37+
Func: func(ctx context.Context, tx pgx.Tx) error {
38+
_, err := tx.Query(ctx, "SELECT 2 AS col2")
39+
return err
40+
},
41+
},
42+
),
43+
)
44+
}
45+
1746
conn.ExpectExec(`CREATE TABLE IF NOT EXISTS admin\.migration`).WillReturnResult(pgxmock.NewResult("CREATE", 1))
1847
conn.ExpectQuery(`SELECT count`).WillReturnRows(pgxmock.NewRows([]string{"count"}).AddRow(0))
48+
49+
// Expect transaction for migration 1 execution
50+
conn.ExpectBegin()
51+
conn.ExpectQuery(`SELECT 1 AS col1`).WillReturnRows(pgxmock.NewRows([]string{"col1"}).AddRow(1))
52+
conn.ExpectExec(`INSERT INTO`).WillReturnResult(pgxmock.NewResult("INSERT", 1))
53+
conn.ExpectCommit()
54+
55+
// Expect transaction for migration 2 execution
1956
conn.ExpectBegin()
57+
conn.ExpectQuery(`SELECT 2 AS col2`).WillReturnRows(pgxmock.NewRows([]string{"col2"}).AddRow(2))
2058
conn.ExpectExec(`INSERT INTO`).WillReturnResult(pgxmock.NewResult("INSERT", 1))
59+
conn.ExpectCommit()
2160

2261
pgw := &PostgresWriter{ctx: ctx, sinkDb: conn}
2362
err = pgw.Migrate()
@@ -52,7 +91,7 @@ func TestPostgresWriterNeedsMigrationNoMigrationNeeded(t *testing.T) {
5291
conn.ExpectQuery(`SELECT to_regclass`).
5392
WithArgs("admin.migration").
5493
WillReturnRows(pgxmock.NewRows([]string{"to_regclass"}).AddRow(true))
55-
conn.ExpectQuery(`SELECT count`).WillReturnRows(pgxmock.NewRows([]string{"count"}).AddRow(1))
94+
conn.ExpectQuery(`SELECT count`).WillReturnRows(pgxmock.NewRows([]string{"count"}).AddRow(2))
5695

5796
pgw := &PostgresWriter{ctx: ctx, sinkDb: conn}
5897
needs, err := pgw.NeedsMigration()

internal/sinks/sql/admin_schema.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@ CREATE TABLE admin.migration(
9494
INSERT INTO
9595
admin.migration (id, version)
9696
VALUES
97-
(0, '01110 Apply postgres sink schema migrations');
97+
(0, '01110 Apply postgres sink schema migrations'),
98+
(1, '01180 Apply admin functions migrations for v5');
99+
-- apply new migration value to `sinkSchema` in `cmd/pgwatch/version.go` file as well

0 commit comments

Comments
 (0)