Skip to content

Commit

Permalink
Merge pull request #45 from swiftcarrot/migration-test
Browse files Browse the repository at this point in the history
migration test
  • Loading branch information
wangzuo authored Aug 28, 2023
2 parents da50930 + dd8e424 commit fcb93fa
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 5 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/migrate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: migrate
on: push
jobs:
sqlite:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19
- name: build
run: go build -o /usr/local/bin/queryx cmd/queryx/main.go
- name: migrate
run: |
cd internal/migrate
queryx db:migrate --schema sqlite1.hcl
- name: sleep
run: sleep 1 # so the generated migration files have non-conflicting filenames
- name: migrate again
run: |
cd internal/migrate
queryx db:migrate --schema sqlite2.hcl
- name: test
run: |
cd internal/migrate
sqlite3 test.sqlite3 "insert into users(name, email) values('test', 'test@example.com')"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
internal/integration/db
internal/migrate/db
node_modules
/bin
.idea
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ test-sqlite: install
cd internal/integration && go test ./...

test: test-postgresql test-sqlite test-mysql

test-migrate: install
rm -rf internal/migrate/db internal/migrate/test.sqlite3
cd internal/migrate && queryx db:migrate --schema sqlite1.hcl
sleep 1
cd internal/migrate && queryx db:migrate --schema sqlite2.hcl
cd internal/migrate && sqlite3 test.sqlite3 "insert into users(name, email) values('test', 'test@example.com')"
1 change: 1 addition & 0 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type Adapter interface {
Open() error
Close() error
CreateDatabase() error
DropDatabase() error
CreateMigrationsTable(ctx context.Context) error
Expand Down
8 changes: 6 additions & 2 deletions adapter/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewMigrator(adapter Adapter) (*Migrator, error) {
}

func (m *Migrator) RunMigration(mg *Migration) error {
fmt.Println("run", mg.Version, mg.Path)
fmt.Println("Migrating", mg.Path)
f, err := os.ReadFile(mg.Path)
if err != nil {
return err
Expand All @@ -51,7 +51,7 @@ func (m *Migrator) RunMigration(mg *Migration) error {
for _, line := range strings.Split(sql, "\n") {
_, err = m.Adapter.ExecContext(context.Background(), line)
if err != nil {
return err
return fmt.Errorf("migration error: %v", err)
}
}

Expand Down Expand Up @@ -99,6 +99,10 @@ func (m *Migrator) exists(ctx context.Context, version string) (bool, error) {
if !rows.Next() {
exists = false
}
if err := rows.Close(); err != nil {
return false, fmt.Errorf("closing rows %w", err)
}

return exists, nil
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/queryx/action/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func newAdapter() (adapter.Adapter, error) {
return adapter.NewAdapter(sch.Databases[0].LoadConfig(environment))
}

// TODO: should check unapplied migrations
func findSchemaChanges(a string, db adapter.Adapter, database *schema.Database) ([]*migrate.Change, error) {
ctx := context.Background()

Expand Down Expand Up @@ -141,16 +140,15 @@ var dbMigrateCmd = &cobra.Command{
if err := a.Open(); err != nil {
return err
}
// defer a.Close()

migrator, err := adapter.NewMigrator(a)
if err != nil {
return err
}

if err := migrator.Up(); err != nil {
return err
}

if err := dbMigrateGenerate(); err != nil {
return err
}
Expand Down Expand Up @@ -186,6 +184,7 @@ func dbMigrateGenerate() error {
if err := adapter.Open(); err != nil {
return err
}
// defer adapter.Close()

database := sch.Databases[0]
changes, err := findSchemaChanges(sch.Databases[0].Adapter, adapter, database)
Expand Down
15 changes: 15 additions & 0 deletions internal/migrate/sqlite1.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
database "db" {
adapter = "sqlite"

config "development" {
url = "sqlite:test.sqlite3"
}

model "User" {
timestamps = false

column "name" {
type = string
}
}
}
18 changes: 18 additions & 0 deletions internal/migrate/sqlite2.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
database "db" {
adapter = "sqlite"

config "development" {
url = "sqlite:test.sqlite3"
}

model "User" {
timestamps = false

column "name" {
type = string
}
column "email" {
type = string
}
}
}

0 comments on commit fcb93fa

Please sign in to comment.