Skip to content

Commit

Permalink
Merge pull request #22 from xakep666/upgrades
Browse files Browse the repository at this point in the history
Upgrades
  • Loading branch information
xakep666 authored Mar 25, 2024
2 parents 5a27c90 + ce97744 commit 13d778e
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 258 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
mongodb-version: [ '4.4', '5.0', '6.0' ]

steps:
- uses: actions/checkout@v4

Expand All @@ -20,8 +24,15 @@ jobs:
- name: Download dependencies
run: go mod download -x

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.10.0
with:
mongodb-version: ${{ matrix.mongodb-version }}

- name: Test
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
env:
MONGO_URL: mongodb://localhost:27017/testing
run: go test -race -tags integration -coverprofile=coverage.txt -covermode=atomic ./...

- uses: codecov/codecov-action@v3
with:
Expand Down
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

13 changes: 7 additions & 6 deletions 1_global_migrate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package migrate

import (
"context"
"testing"

"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -35,9 +36,9 @@ func TestMigrationsRegistration(t *testing.T) {
}()
globalMigrate = NewMigrate(nil)

err := Register(func(db *mongo.Database) error {
err := Register(func(ctx context.Context, db *mongo.Database) error {
return nil
}, func(db *mongo.Database) error {
}, func(ctx context.Context, db *mongo.Database) error {
return nil
})
if err != nil {
Expand All @@ -53,9 +54,9 @@ func TestMigrationsRegistration(t *testing.T) {
t.Errorf("Unexpected version/description: %d %s", registered[0].Version, registered[0].Description)
}

err = Register(func(db *mongo.Database) error {
err = Register(func(ctx context.Context, db *mongo.Database) error {
return nil
}, func(db *mongo.Database) error {
}, func(ctx context.Context, db *mongo.Database) error {
return nil
})
if err == nil {
Expand All @@ -72,9 +73,9 @@ func TestMigrationMustRegistration(t *testing.T) {
}
}()
globalMigrate = NewMigrate(nil)
MustRegister(func(db *mongo.Database) error {
MustRegister(func(ctx context.Context, db *mongo.Database) error {
return nil
}, func(db *mongo.Database) error {
}, func(ctx context.Context, db *mongo.Database) error {
return nil
})
registered := RegisteredMigrations()
Expand Down
10 changes: 5 additions & 5 deletions 1_sample_data_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build integration
//go:build integration

package migrate

Expand All @@ -12,14 +12,14 @@ import (
const globalTestCollection = "test-global"

func init() {
Register(func(db *mongo.Database) error {
_, err := db.Collection(globalTestCollection).InsertOne(context.TODO(), bson.D{{"a", "b"}})
MustRegister(func(ctx context.Context, db *mongo.Database) error {
_, err := db.Collection(globalTestCollection).InsertOne(ctx, bson.D{{"a", "b"}})
if err != nil {
return err
}
return nil
}, func(db *mongo.Database) error {
_, err := db.Collection(globalTestCollection).DeleteOne(context.TODO(), bson.D{{"a", "b"}})
}, func(ctx context.Context, db *mongo.Database) error {
_, err := db.Collection(globalTestCollection).DeleteOne(ctx, bson.D{{"a", "b"}})
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions 2_sample_index_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build integration
//go:build integration

package migrate

Expand All @@ -13,17 +13,17 @@ import (
const globalTestIndexName = "test_idx_2"

func init() {
Register(func(db *mongo.Database) error {
MustRegister(func(ctx context.Context, db *mongo.Database) error {
keys := bson.D{{"a", 1}}
opt := options.Index().SetName(globalTestIndexName)
model := mongo.IndexModel{Keys: keys, Options: opt}
_, err := db.Collection(globalTestCollection).Indexes().CreateOne(context.TODO(), model)
_, err := db.Collection(globalTestCollection).Indexes().CreateOne(ctx, model)
if err != nil {
return err
}
return nil
}, func(db *mongo.Database) error {
_, err := db.Collection(globalTestCollection).Indexes().DropOne(context.TODO(), globalTestIndexName)
}, func(ctx context.Context, db *mongo.Database) error {
_, err := db.Collection(globalTestCollection).Indexes().DropOne(ctx, globalTestIndexName)
if err != nil {
return err
}
Expand Down
62 changes: 30 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Table of Contents
* [License](#license)

## Prerequisites
* Golang >= 1.10 or Vgo
* Golang >= 1.22

## Installation
```bash
Expand All @@ -39,29 +39,31 @@ File name should be like `<version>_<description>.go`.
package migrations

import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
migrate "github.com/xakep666/mongo-migrate"
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
migrate "github.com/xakep666/mongo-migrate"
)

func init() {
migrate.Register(func(db *mongo.Database) error {
opt := options.Index().SetName("my-index")
keys := bson.D{{Key: "my-key", Value: 1}}
model := mongo.IndexModel{Keys: keys, Options: opt}
_, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
if err != nil {
return err
}
return nil
}, func(db *mongo.Database) error {
_, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
if err != nil {
return err
}
return nil
})
migrate.MustRegister(func(ctx context.Context, db *mongo.Database) error {
opt := options.Index().SetName("my-index")
keys := bson.D{{Key: "my-key", Value: 1}}
model := mongo.IndexModel{Keys: keys, Options: opt}
_, err := db.Collection("my-coll").Indexes().CreateOne(ctx, model)
if err != nil {
return err
}
return nil
}, func(ctx context.Context, db *mongo.Database) error {
_, err := db.Collection("my-coll").Indexes().DropOne(ctx, "my-index")
if err != nil {
return err
}
return nil
})
}
```

Expand All @@ -87,7 +89,7 @@ func MongoConnect(host, user, password, database string) (*mongo.Database, error
}
db := client.Database(database)
migrate.SetDatabase(db)
if err := migrate.Up(migrate.AllAvailable); err != nil {
if err := migrate.Up(ctx, migrate.AllAvailable); err != nil {
return nil, err
}
return db, nil
Expand All @@ -100,40 +102,36 @@ func MongoConnect(host, user, password, database string) (*mongo.Database, error
func MongoConnect(host, user, password, database string) (*mongo.Database, error) {
uri := fmt.Sprintf("mongodb://%s:%s@%s:27017", user, password, host)
opt := options.Client().ApplyURI(uri)
client, err := mongo.NewClient(opt)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
err = client.Connect(ctx)
err = mongo.Connect(ctx, opt)
if err != nil {
return nil, err
}
db = client.Database(database)
m := migrate.NewMigrate(db, migrate.Migration{
Version: 1,
Description: "add my-index",
Up: func(db *mongo.Database) error {
Up: func(ctx context.Context, db *mongo.Database) error {
opt := options.Index().SetName("my-index")
keys := bson.D{{"my-key", 1}}
model := mongo.IndexModel{Keys: keys, Options: opt}
_, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
_, err := db.Collection("my-coll").Indexes().CreateOne(ctx, model)
if err != nil {
return err
}

return nil
},
Down: func(db *mongo.Database) error {
_, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
Down: func(ctx context.Context, db *mongo.Database) error {
_, err := db.Collection("my-coll").Indexes().DropOne(ctx, "my-index")
if err != nil {
return err
}
return nil
},
})
if err := m.Up(migrate.AllAvailable); err != nil {
if err := m.Up(ctx, migrate.AllAvailable); err != nil {
return nil, err
}
return db, nil
Expand Down
9 changes: 5 additions & 4 deletions bad_migration_file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package migrate

import (
"context"
"testing"

"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -13,9 +14,9 @@ func TestBadMigrationFile(t *testing.T) {
}()
globalMigrate = NewMigrate(nil)

err := Register(func(db *mongo.Database) error {
err := Register(func(ctx context.Context, db *mongo.Database) error {
return nil
}, func(db *mongo.Database) error {
}, func(ctx context.Context, db *mongo.Database) error {
return nil
})
if err == nil {
Expand All @@ -32,9 +33,9 @@ func TestBadMigrationFilePanic(t *testing.T) {
}
}()
globalMigrate = NewMigrate(nil)
MustRegister(func(db *mongo.Database) error {
MustRegister(func(ctx context.Context, db *mongo.Database) error {
return nil
}, func(db *mongo.Database) error {
}, func(ctx context.Context, db *mongo.Database) error {
return nil
})
}
63 changes: 32 additions & 31 deletions global_migrate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package migrate

import (
"context"
"fmt"
"runtime"

Expand Down Expand Up @@ -34,33 +35,33 @@ func internalRegister(up, down MigrationFunc, skip int) error {
//
// - Use the following template inside:
//
// package migrations
// package migrations
//
// import (
// "go.mongodb.org/mongo-driver/bson"
// "go.mongodb.org/mongo-driver/mongo"
// "go.mongodb.org/mongo-driver/mongo/options"
// migrate "github.com/xakep666/mongo-migrate"
// )
// import (
// "go.mongodb.org/mongo-driver/bson"
// "go.mongodb.org/mongo-driver/mongo"
// "go.mongodb.org/mongo-driver/mongo/options"
// migrate "github.com/xakep666/mongo-migrate"
// )
//
// func init() {
// migrate.Register(func(db *mongo.Database) error {
// opt := options.Index().SetName("my-index")
// keys := bson.D{{Key: "my-key", Value: 1}}
// model := mongo.IndexModel{Keys: keys, Options: opt}
// _, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
// if err != nil {
// return err
// }
// return nil
// }, func(db *mongo.Database) error {
// _, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
// if err != nil {
// return err
// }
// return nil
// })
// }
// func init() {
// migrate.Register(func(db *mongo.Database) error {
// opt := options.Index().SetName("my-index")
// keys := bson.D{{Key: "my-key", Value: 1}}
// model := mongo.IndexModel{Keys: keys, Options: opt}
// _, err := db.Collection("my-coll").Indexes().CreateOne(context.TODO(), model)
// if err != nil {
// return err
// }
// return nil
// }, func(db *mongo.Database) error {
// _, err := db.Collection("my-coll").Indexes().DropOne(context.TODO(), "my-index")
// if err != nil {
// return err
// }
// return nil
// })
// }
func Register(up, down MigrationFunc) error {
return internalRegister(up, down, 2)
}
Expand Down Expand Up @@ -90,20 +91,20 @@ func SetMigrationsCollection(name string) {
}

// Version returns current database version.
func Version() (uint64, string, error) {
return globalMigrate.Version()
func Version(ctx context.Context) (uint64, string, error) {
return globalMigrate.Version(ctx)
}

// Up performs "up" migration using registered migrations.
// Detailed description available in Migrate.Up().
func Up(n int) error {
return globalMigrate.Up(n)
func Up(ctx context.Context, n int) error {
return globalMigrate.Up(ctx, n)
}

// Down performs "down" migration using registered migrations.
// Detailed description available in Migrate.Down().
func Down(n int) error {
return globalMigrate.Down(n)
func Down(ctx context.Context, n int) error {
return globalMigrate.Down(ctx, n)
}

// SetLogger sets a logger to print the migration process
Expand Down
Loading

0 comments on commit 13d778e

Please sign in to comment.