|
1 | 1 | package sinks |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | migrator "github.com/cybertec-postgresql/pgx-migrator" |
| 8 | + "github.com/jackc/pgx/v5" |
7 | 9 | "github.com/pashagolub/pgxmock/v4" |
8 | 10 | "github.com/stretchr/testify/assert" |
9 | 11 | ) |
10 | 12 |
|
11 | 13 | func TestPostgresWriterMigrate(t *testing.T) { |
| 14 | + oldInitMigrator := initMigrator |
| 15 | + t.Cleanup(func() { |
| 16 | + initMigrator = oldInitMigrator |
| 17 | + }) |
| 18 | + |
12 | 19 | a := assert.New(t) |
13 | 20 | conn, err := pgxmock.NewPool() |
14 | 21 | a.NoError(err) |
15 | 22 |
|
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 | + |
17 | 46 | conn.ExpectExec(`CREATE TABLE IF NOT EXISTS admin\.migration`).WillReturnResult(pgxmock.NewResult("CREATE", 1)) |
18 | 47 | 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 |
19 | 56 | conn.ExpectBegin() |
| 57 | + conn.ExpectQuery(`SELECT 2 AS col2`).WillReturnRows(pgxmock.NewRows([]string{"col2"}).AddRow(2)) |
20 | 58 | conn.ExpectExec(`INSERT INTO`).WillReturnResult(pgxmock.NewResult("INSERT", 1)) |
| 59 | + conn.ExpectCommit() |
21 | 60 |
|
22 | 61 | pgw := &PostgresWriter{ctx: ctx, sinkDb: conn} |
23 | 62 | err = pgw.Migrate() |
@@ -52,7 +91,7 @@ func TestPostgresWriterNeedsMigrationNoMigrationNeeded(t *testing.T) { |
52 | 91 | conn.ExpectQuery(`SELECT to_regclass`). |
53 | 92 | WithArgs("admin.migration"). |
54 | 93 | 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)) |
56 | 95 |
|
57 | 96 | pgw := &PostgresWriter{ctx: ctx, sinkDb: conn} |
58 | 97 | needs, err := pgw.NeedsMigration() |
|
0 commit comments