{"id":15187427,"url":"https://github.com/lemmego/migration","last_synced_at":"2025-10-04T04:59:23.453Z","repository":{"id":58792960,"uuid":"518922259","full_name":"lemmego/migration","owner":"lemmego","description":"A simple to use database schema migration tool for go applications.","archived":false,"fork":false,"pushed_at":"2025-09-03T05:34:34.000Z","size":59,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-09-03T07:24:25.700Z","etag":null,"topics":["database","database-migrations","database-schema","db-migrate","db-migration","go","migration","migration-tool","mysql","pgsql","postgres","postgresql"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/lemmego/migration","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/lemmego.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-07-28T16:39:52.000Z","updated_at":"2025-09-03T05:34:38.000Z","dependencies_parsed_at":"2024-02-18T15:28:09.730Z","dependency_job_id":"8ea29f06-a22d-42a0-b2b4-8f41c38b1f3f","html_url":"https://github.com/lemmego/migration","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/lemmego/migration","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemmego%2Fmigration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemmego%2Fmigration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemmego%2Fmigration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemmego%2Fmigration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lemmego","download_url":"https://codeload.github.com/lemmego/migration/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemmego%2Fmigration/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278266894,"owners_count":25958733,"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","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","database-migrations","database-schema","db-migrate","db-migration","go","migration","migration-tool","mysql","pgsql","postgres","postgresql"],"created_at":"2024-09-27T18:21:39.978Z","updated_at":"2025-10-04T04:59:23.426Z","avatar_url":"https://github.com/lemmego.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go Migration\n\nA simple to use database schema migration tool for go applications.\n\n## Installation\n\nOpen your favorite terminal app and cd into your go module enabled project root:\n\n`cd projectroot`\n\n[Replace \"packagename\" with your package's name]\n\nTo install the migration package, run:\n\n`go get -u github.com/lemmego/migration`\n\n## Usage\n\nThis package resolves the DSN (Data Source Name) from the following env variables:\n\n```env\nDB_DRIVER=mysql\nDB_HOST=localhost\nDB_PORT=3306\nDB_DATABASE=test\nDB_USERNAME=root\nDB_PASSWORD=\nDB_PARAMS=charset=utf8mb4\u0026collation=utf8mb4_unicode_ci\nMIGRATIONS_DIR=\"./cmd/migrations\" # Optional\n```\n\nThe supported `DB_DRIVER` values are `sqlite`, `mysql` and `postgres`\n\n```go\n// projectroot/main.go\n// Or,\n// projectroot/cmd/myapp/main.go\n\npackage main\n\nimport (\n  \"github.com/joho/godotenv\"\n  \"github.com/lemmego/migration/cmd\"\n  _ \"projectroot/cmd/migrations\"\n)\n\nfunc main() {\n  if err := godotenv.Load(); err != nil {\n    panic(err)\n  }\n  cmd.Execute()\n}\n```\n\nNext, to create a sample migration, run:\n\n`go run . create create_users_table` (If the main.go is located in projectroot/main.go)\n\nOr,\n\n`go run ./cmd/myapp create create_users_table` (If the main.go is located in projectroot/cmd/myapp/main.go)\n\n**Note:** For all the examples (_migrate up, migrate down, migrate status_) below, we will assume that the `main.go` is located in `projectroot/main.go` and the `go run . \u003csubcommand\u003e` command will be used. Replace this with `go run ./cmd/myapp \u003csubcommand\u003e` if your main.go is in `projectroot/cmd/myapp/main.go`\n\nIf you didn't provide a `MIGRATIONS_DIR` env variable, A migration file will be created inside the `projectroot/cmd/migrations/` directory. If the directory is not present in your project root, the `migrate create ...` command will create one for you. Open the migration file and populate the `up()` and `down()` method like this:\n\n```go\n// 20220729200658_create_users_table.go\n\npackage migrations\n\nimport (\n\t\"database/sql\"\n\t\"github.com/lemmego/migration\"\n)\n\nfunc init() {\n\tmigration.GetMigrator().AddMigration(\u0026migration.Migration{\n\t\tVersion: \"20220729200658\",\n\t\tUp:      mig_20220729200658_create_users_table_up,\n\t\tDown:    mig_20220729200658_create_users_table_down,\n\t})\n}\n\nfunc mig_20220729200658_create_users_table_up(tx *sql.Tx) error {\n\t_, err := tx.Exec(\"CREATE TABLE users ( name varchar(255) );\")\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\nfunc mig_20220729200658_create_users_table_down(tx *sql.Tx) error {\n\t_, err := tx.Exec(\"DROP TABLE users\")\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n```\n\nAlternatively, you could also use the db agnostic schema builder API. Which is useful for switching databases without having to rewrite the migrations.\n\n```go\n// 20220729200658_create_users_table.go\n\npackage migrations\n\nimport (\n\t\"database/sql\"\n\t\"github.com/lemmego/migration\"\n)\n\nfunc init() {\n\tmigration.GetMigrator().AddMigration(\u0026migration.Migration{\n\t\tVersion: \"20220729200658\",\n\t\tUp:      mig_20220729200658_create_users_table_up,\n\t\tDown:    mig_20220729200658_create_users_table_down,\n\t})\n}\n\nfunc mig_20220729200658_create_users_table_up(tx *sql.Tx) error {\n  schema := migration.Create(\"users\", func(t *migration.Table) {\n    t.BigIncrements(\"id\").Primary()\n    t.ForeignID(\"org_id\").Constrained() // \"org_id\" references the \"id\" column in the \"orgs\" table\n    t.String(\"first_name\", 255)\n    t.String(\"last_name\", 255)\n    t.String(\"email\", 255).Unique()\n    t.String(\"password\", 255)\n    t.Text(\"bio\").Nullable()\n    t.DateTime(\"created_at\", 0).Default(\"now()\")\n    t.DateTime(\"updated_at\", 0).Default(\"now()\")\n  }).Build()\n\n  if _, err := tx.Exec(schema); err != nil {\n    return err\n  }\n\n  return nil\n}\n\nfunc mig_20220729200658_create_users_table_down(tx *sql.Tx) error {\n  schema := migration.Drop(\"users\").Build()\n  if _, err := tx.Exec(schema); err != nil {\n    return err\n  }\n  return nil\n}\n```\n\nOnce you've made sure that the expected environment variables are present in your `.env` file, you can run `go run . migrate up`\n\nYou should see something like the following:\n\n```\nConnecting to database...\nDatabase connected!\nRunning migration 20220729200658\nFinished running migration 20220729200658\n```\n\nOpen your database client application (e.g. SequelPro, TablePlus) and open the database. You should see two new tables: schema_migrations and users.\n\nYou can revert the migration by running `go run . migrate down`. You should see something like this:\n\n```\nConnecting to database...\nDatabase connected!\nReverting Migration 20220729200658\nFinished reverting migration 20220729200658\n```\n\nBoth the `migrate up` and the `migrate down` commands take a `--step` integer flag to indicate how many step should the migration run forward or backward:\n\nE.g.:\n\n`go run . migrate down --step=1`\n\nThere is also a `migrate status` command to see which migrations are currently pending and/or completed.\n\n### Adding \"migrate\" command to an existing command:\nIf your project already has a command, say `rootCmd`, you could add the `MigrateCmd` to that command to take full control of the package:\n\n```go\npackage mypackage\n\nimport \"github.com/spf13/cobra\"\nimport \"github.com/lemmego/migration/cmd\"\n\nvar rootCmd = \u0026cobra.Command{}\n\nfunc init() {\n    rootCmd.AddCommand(cmd.MigrateCmd)\n\trootCmd.Execute()\n}\n```\n\n### Renaming package for self-contained binary:\nBy default, the migration files will be created within the `./cmd/migrations` directory. You can override the directory with the `MIGRATIONS_DIR` env variable. The package name of the migration files will follow the Go's convention of adopting the package name according to the directory the files are in, meaning if they are generated in a \"migrations\" directory, the package name will be \"migrations\". If you would like the package name to be `\"main\"` so that you can deploy it as a self-contained binary, follow these two steps:\n\n1. Rename the package name of each migration files to `package main`\n2. Add the following `main.go` file to the same directory where migrations are generated:\n\n```go\n// Assuming your generated files are in the projectroot/cmd/migrations dir:\n// projectroot/cmd/migrations/main.go\n\npackage main\n\nimport (\n\t\"github.com/joho/godotenv\"\n\t\"github.com/lemmego/migration/cmd\"\n)\n\nfunc main() {\n\tif err := godotenv.Load(); err != nil {\n\t\tpanic(err)\n\t}\n\tcmd.Execute()\n}\n```\n\nNow the package `./cmd/migrations` is ready to be built and deployed as an independent binary.\n\nBuild:\n\n`go build ./cmd/migrations`\n\nRun:\n\n`./migrations migrate create/up/down/status`\n\n\n\n## Documentation\n\nThe package documentation can be found at: https://pkg.go.dev/github.com/lemmego/migration\n\n## Contributing\n\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemmego%2Fmigration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemmego%2Fmigration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemmego%2Fmigration/lists"}