{"id":13837250,"url":"https://github.com/xakep666/mongo-migrate","last_synced_at":"2025-07-10T16:32:32.183Z","repository":{"id":44131021,"uuid":"145526268","full_name":"xakep666/mongo-migrate","owner":"xakep666","description":"Versioned migrations for MongoDB.","archived":false,"fork":false,"pushed_at":"2024-04-02T17:38:31.000Z","size":83,"stargazers_count":98,"open_issues_count":0,"forks_count":45,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-02T18:46:55.949Z","etag":null,"topics":["database-management","database-migrations","golang","mgo","migrations","mongodb","versioned-migrations"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xakep666.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-21T07:45:06.000Z","updated_at":"2024-05-30T03:15:17.515Z","dependencies_parsed_at":"2024-01-13T17:10:29.885Z","dependency_job_id":"c8679e8b-a398-4a40-a905-ab5622374297","html_url":"https://github.com/xakep666/mongo-migrate","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/xakep666/mongo-migrate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xakep666%2Fmongo-migrate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xakep666%2Fmongo-migrate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xakep666%2Fmongo-migrate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xakep666%2Fmongo-migrate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xakep666","download_url":"https://codeload.github.com/xakep666/mongo-migrate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xakep666%2Fmongo-migrate/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264608135,"owners_count":23636684,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database-management","database-migrations","golang","mgo","migrations","mongodb","versioned-migrations"],"created_at":"2024-08-04T15:01:04.394Z","updated_at":"2025-07-10T16:32:31.919Z","avatar_url":"https://github.com/xakep666.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Versioned migrations for MongoDB\n[![Build Status](https://github.com/xakep666/mongo-migrate/actions/workflows/testing.yml/badge.svg)](https://travis-ci.org/xakep666/mongo-migrate)\n[![codecov](https://codecov.io/gh/xakep666/mongo-migrate/branch/master/graph/badge.svg)](https://codecov.io/gh/xakep666/mongo-migrate)\n[![Go Report Card](https://goreportcard.com/badge/github.com/xakep666/mongo-migrate)](https://goreportcard.com/report/github.com/xakep666/mongo-migrate)\n[![GoDoc](https://godoc.org/github.com/xakep666/mongo-migrate?status.svg)](https://godoc.org/github.com/xakep666/mongo-migrate)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nThis package allows to perform versioned migrations on your MongoDB using [mongo-go-driver](https://github.com/mongodb/mongo-go-driver).\nInspired by [go-pg migrations](https://github.com/go-pg/migrations).\n\nTable of Contents\n=================\n\n* [Prerequisites](#prerequisites)\n* [Installation](#installation)\n* [Usage](#usage)\n  * [Use case \\#1\\. Migrations in files\\.](#use-case-1-migrations-in-files)\n  * [Use case \\#2\\. Migrations in application code\\.](#use-case-2-migrations-in-application-code)\n* [How it works?](#how-it-works)\n* [License](#license)\n\n## Prerequisites\n* Golang \u003e= 1.22\n\n## Installation\n```bash\ngo get -v -u github.com/xakep666/mongo-migrate\n```\n\n## Usage\n### Use case #1. Migrations in files.\n\n* Create a package with migration files.\nFile name should be like `\u003cversion\u003e_\u003cdescription\u003e.go`.\n\n`1_add-my-index.go`\n\n```go\npackage migrations\n\nimport (\n  \"context\"\n\n  \"go.mongodb.org/mongo-driver/v2/bson\"\n  \"go.mongodb.org/mongo-driver/v2/mongo\"\n  \"go.mongodb.org/mongo-driver/v2/mongo/options\"\n  migrate \"github.com/xakep666/mongo-migrate\"\n)\n\nfunc init() {\n  migrate.MustRegister(func(ctx context.Context, db *mongo.Database) error {\n    opt := options.Index().SetName(\"my-index\")\n    keys := bson.D{{Key: \"my-key\", Value: 1}}\n    model := mongo.IndexModel{Keys: keys, Options: opt}\n    _, err := db.Collection(\"my-coll\").Indexes().CreateOne(ctx, model)\n    if err != nil {\n      return err\n    }\n    return nil\n  }, func(ctx context.Context, db *mongo.Database) error {\n    err := db.Collection(\"my-coll\").Indexes().DropOne(ctx, \"my-index\")\n    if err != nil {\n      return err\n    }\n    return nil\n  })\n}\n```\n\n* Import it in your application.\n```go\nimport (\n    ...\n    migrate \"github.com/xakep666/mongo-migrate\"\n    _ \"path/to/migrations_package\" // database migrations\n    ...\n)\n```\n\n* Run migrations.\n```go\nfunc MongoConnect(host, user, password, database string) (*mongo.Database, error) {\n\turi := fmt.Sprintf(\"mongodb://%s:%s@%s:27017\", user, password, host)\n\topt := options.Client().ApplyURI(addr.String())\n\tclient, err := mongo.Connect(opt)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdb := client.Database(database)\n\tmigrate.SetDatabase(db)\n\tif err := migrate.Up(ctx, migrate.AllAvailable); err != nil {\n\t\treturn nil, err\n\t}\n\treturn db, nil\n}\n```\n\n### Use case #2. Migrations in application code.\n* Just define it anywhere you want and run it.\n```go\nfunc MongoConnect(host, user, password, database string) (*mongo.Database, error) {\n\turi := fmt.Sprintf(\"mongodb://%s:%s@%s:27017\", user, password, host)\n\topt := options.Client().ApplyURI(uri)\n\terr = mongo.Connect(opt)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdb = client.Database(database)\n\tm := migrate.NewMigrate(db, migrate.Migration{\n\t\tVersion: 1,\n\t\tDescription: \"add my-index\",\n\t\tUp: func(ctx context.Context, db *mongo.Database) error {\n\t\t\topt := options.Index().SetName(\"my-index\")\n\t\t\tkeys := bson.D{{\"my-key\", 1}}\n\t\t\tmodel := mongo.IndexModel{Keys: keys, Options: opt}\n\t\t\t_, err := db.Collection(\"my-coll\").Indexes().CreateOne(ctx, model)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\n\t\t\treturn nil\n\t\t},\n\t\tDown: func(ctx context.Context, db *mongo.Database) error {\n\t\t\t_, err := db.Collection(\"my-coll\").Indexes().DropOne(ctx, \"my-index\")\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn nil\n\t\t},\n\t})\n\tif err := m.Up(ctx, migrate.AllAvailable); err != nil {\n\t\treturn nil, err\n\t}\n\treturn db, nil\n}\n```\n\n## How it works?\nThis package creates a special collection (by default it`s name is \"migrations\") for versioning.\nIn this collection stored documents like\n```json\n{\n    \"_id\": \"\u003cmongodb-generated id\u003e\",\n    \"version\": 1,\n    \"description\": \"add my-index\",\n    \"timestamp\": \"\u003cwhen applied\u003e\"\n}\n```\nCurrent database version determined as version from latest inserted document.\n\nYou can change collection name using `SetMigrationsCollection` methods.\nRemember that if you want to use custom collection name you need to set it before running migrations.\n\n## License\nmongo-migrate project is licensed under the terms of the MIT license. Please see LICENSE in this repository for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxakep666%2Fmongo-migrate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxakep666%2Fmongo-migrate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxakep666%2Fmongo-migrate/lists"}