Skip to content

Commit

Permalink
feat: init readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Alviner committed Jan 24, 2024
1 parent 469e1e8 commit e29e178
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 23 deletions.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,84 @@ go get github.com/Alviner/drillerfy
Provides functionality to easily create and drop databases.
This is particularly useful in testing environments where you need to set up a fresh database instance for each test run and clean it up afterward.

```(go)
package main
import (
"context"
"database/sql"
"log"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/Alviner/drillerfy/database"
)
func main() {
conn, err := sql.Open("pgx", "database dns")
if err != nil {
log.Fatal(err)
}
database, err := database.New(database.WithDialect(database.DialectPostgres))
if err != nil {
log.Fatal(err)
}
dbName, closer, err := database.Create(ctx, "test", conn)
if err != nil {
log.Fatal(err)
}
defer closer()
// ... some useful staff with created db
}
```

### Migrations Module

Provides functionality to easily run stairway tests for migrations via goose Provider.
This module simplifies the process of applying and reverting database schema changes,
which is essential in maintaining consistent database states for testing.

```(go)
package main
import (
"context"
"database/sql"
"log"
"time"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
"github.com/Alviner/drillerfy/migrations"
)
func main() {
db, err := sql.Open("pgx", "database dns")
if err != nil {
log.Fatal(err)
}
provider, err := goose.NewProvider(
goose.DialectPostgres,
db,
os.DirFS("migrations"),
)
if err != nil {
log.Fatal(err)
}
migrator := migrations.New(provider)
if err := migrations.Stairway(2 * time.Second); err != nil {
log.Fatal(err)
}
// ... some useful staff with created db
}
```

## Contributing

Contributions to Drillerfy are welcome.
Expand Down
33 changes: 12 additions & 21 deletions migrations/goose/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,46 @@ package migrations

import (
"context"
"testing"
"fmt"
"time"

"github.com/pressly/goose/v3"
)

func New(t *testing.T, provider *goose.Provider) *MT {
t.Helper()
func New(provider *goose.Provider) *Migrator {

return &MT{t: t, provider: provider}
return &Migrator{provider: provider}
}

type MT struct {
t *testing.T
type Migrator struct {
provider *goose.Provider
}

func (mt *MT) Stairway(stepTimeout time.Duration) error {
func (mt *Migrator) Stairway(stepTimeout time.Duration) error {
for range mt.provider.ListSources() {
ctx, done := context.WithTimeout(context.Background(), stepTimeout)
defer done()

if err := mt.Step(ctx); err != nil {
mt.t.Logf("Cannot make step: %s", err)

return err
}
}

return nil
}

func (mt *MT) Step(ctx context.Context) error {
if res, err := mt.provider.UpByOne(ctx); err != nil {
mt.t.Logf("Cannot make first up step: %s", res)
func (mt *Migrator) Step(ctx context.Context) error {
if _, err := mt.provider.UpByOne(ctx); err != nil {
return fmt.Errorf("cannot make first up step : %w", err)

return err
}

if res, err := mt.provider.Down(ctx); err != nil {
mt.t.Logf("Cannot make down step: %s", res)

return err
if _, err := mt.provider.Down(ctx); err != nil {
return fmt.Errorf("cannot make down step : %w", err)
}

if res, err := mt.provider.UpByOne(ctx); err != nil {
mt.t.Logf("Cannot make second up step: %s", res)

return err
if _, err := mt.provider.UpByOne(ctx); err != nil {
return fmt.Errorf("cannot make second up step : %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions migrations/goose/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestMigrations(t *testing.T) {
require.NoError(err)
defer closer()

migrations := New(t, provider)
migrations := New(provider)
ctx, done := context.WithTimeout(context.Background(), 2*time.Second)
defer done()

Expand All @@ -106,7 +106,7 @@ func TestMigrations(t *testing.T) {
ctx, done := context.WithTimeout(context.Background(), 2*time.Second)
defer done()

migrations := New(t, provider)
migrations := New(provider)

// act
require.NoError(migrations.Stairway(2 * time.Second))
Expand Down

0 comments on commit e29e178

Please sign in to comment.