8000 Rewrite raw SQL operations using a SQL transformer by andrew-farries · Pull Request #330 · xataio/pgroll · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Rewrite raw SQL operations using a SQL transformer #330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/migrations/op_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func TestMain(m *testing.M) {
testutils.SharedTestMain(m)
}

func ExecuteTests(t *testing.T, tests TestCases) {
func ExecuteTests(t *testing.T, tests TestCases, opts ...roll.Option) {
testSchema := testutils.TestSchema()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testutils.WithMigratorInSchemaAndConnectionToContainer(t, testSchema, func(mig *roll.Roll, db *sql.DB) {
testutils.WithMigratorInSchemaAndConnectionToContainerWithOptions(t, testSchema, opts, func(mig *roll.Roll, db *sql.DB) {
ctx := context.Background()

// run all migrations except the last one
Expand Down
36 changes: 27 additions & 9 deletions pkg/migrations/op_raw_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,45 @@ import (
var _ Operation = (*OpRawSQL)(nil)

func (o *OpRawSQL) Start(ctx context.Context, conn *sql.DB, stateSchema string, tr SQLTransformer, s *schema.Schema, cbs ...CallbackFn) (*schema.Table, error) {
if !o.OnComplete {
_, err := conn.ExecContext(ctx, o.Up)
if o.OnComplete {
return nil, nil
}

up, err := tr.TransformSQL(o.Up)
if err != nil {
return nil, err
}
return nil, nil

_, err = conn.ExecContext(ctx, up)
return nil, err
}

func (o *OpRawSQL) Complete(ctx context.Context, conn *sql.DB, tr SQLTransformer, s *schema.Schema) error {
if o.OnComplete {
_, err := conn.ExecContext(ctx, o.Up)
if !o.OnComplete {
return nil
}

up, err := tr.TransformSQL(o.Up)
if err != nil {
return err
}
return nil

_, err = conn.ExecContext(ctx, up)
return err
}

func (o *OpRawSQL) Rollback(ctx context.Context, conn *sql.DB, tr SQLTransformer) error {
if o.Down != "" {
_, err := conn.ExecContext(ctx, o.Down)
if o.Down == "" {
return nil
}

down, err := tr.TransformSQL(o.Down)
if err != nil {
return err
}
return nil

_, err = conn.ExecContext(ctx, down)
return err
}

func (o *OpRawSQL) Validate(ctx context.Context, s *schema.Schema) error {
Expand Down
74 changes: 74 additions & 0 deletions pkg/migrations/op_raw_sql_test.go
< 8000 td class="blob-code blob-code-addition js-file-line"> {
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ package migrations_test

import (
"database/sql"
"fmt"
"testing"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/roll"
)

func TestRawSQL(t *testing.T) {
Expand Down Expand Up @@ -184,3 +186,75 @@ func TestRawSQL(t *testing.T) {
},
})
}

func TestRawSQLTransformation(t *testing.T) {
t.Parallel()

t.Run("for normal raw SQL operations with up and down SQL", func(t *testing.T) {
ExecuteTests(t, TestCases{
{
name: "SQL transformer rewrites up and down SQL",
migrations: []migrations.Migration{
Name: "01_create_table",
Operations: migrations.Operations{
&migrations.OpRawSQL{
Up: "CREATE TABLE apples(id int)",
Down: "CREATE TABLE bananas(id int)",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB, schema string) {
// The transformed `up` SQL was used in place of the original SQL
TableMustExist(t, db, schema, "table_1")
TableMustNotExist(t, db, schema, "apples")
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// The transformed `down` SQL was used in place of the original SQL
TableMustExist(t, db, schema, "table_2")
TableMustNotExist(t, db, schema, "bananas")
},
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
},
},
}, roll.WithSQLTransformer(&simpleSQLTransformer{}))
})

t.Run("for raw SQL operations that run on complete", func(t *testing.T) {
ExecuteTests(t, TestCases{
{
name: "SQL transformer rewrites up SQL when up is run on completion",
migrations: []migrations.Migration{
{
Name: "01_create_table",
Operations: migrations.Operations{
&migrations.OpRawSQL{
Up: "CREATE TABLE apples(id int)",
OnComplete: true,
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB, schema string) {
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
},
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
// The transformed `up` SQL was used in place of the original SQL
TableMustExist(t, db, schema, "table_1")
TableMustNotExist(t, db, schema, "apples")
},
},
}, roll.WithSQLTransformer(&simpleSQLTransformer{}))
})
}

type simpleSQLTransformer struct {
counter int
}

func (s *simpleSQLTransformer) TransformSQL(sql string) (string, error) {
s.counter++
return fmt.Sprintf("CREATE TABLE table_%d(id int)", s.counter), nil
}
0