{"id":13365921,"url":"https://github.com/Go-Gormigrate/Gormigrate","last_synced_at":"2025-03-12T17:32:02.777Z","repository":{"id":38805069,"uuid":"67032544","full_name":"go-gormigrate/gormigrate","owner":"go-gormigrate","description":"Minimalistic database migration helper for Gorm ORM","archived":false,"fork":false,"pushed_at":"2024-09-23T00:00:15.000Z","size":292,"stargazers_count":1043,"open_issues_count":17,"forks_count":99,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-10-23T03:04:17.244Z","etag":null,"topics":["database","db","go","gorm","migrations","schema","schema-migrations"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/go-gormigrate/gormigrate/v2","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/go-gormigrate.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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},"funding":{"github":"andreynering","custom":"https://www.paypal.com/cgi-bin/webscr?cmd=_donations\u0026business=GSVDU63RKG45A\u0026currency_code=USD\u0026source=url"}},"created_at":"2016-08-31T11:46:23.000Z","updated_at":"2024-10-23T00:42:02.000Z","dependencies_parsed_at":"2024-03-17T22:29:34.855Z","dependency_job_id":"c01e526c-c932-4ae2-ad17-d173d85e4714","html_url":"https://github.com/go-gormigrate/gormigrate","commit_stats":{"total_commits":204,"total_committers":22,"mean_commits":9.272727272727273,"dds":0.6862745098039216,"last_synced_commit":"db7575cc3e2a804a89b3f7c5839c5c65298a63cf"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-gormigrate%2Fgormigrate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-gormigrate%2Fgormigrate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-gormigrate%2Fgormigrate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-gormigrate%2Fgormigrate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-gormigrate","download_url":"https://codeload.github.com/go-gormigrate/gormigrate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221305495,"owners_count":16795145,"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","db","go","gorm","migrations","schema","schema-migrations"],"created_at":"2024-07-30T00:01:16.376Z","updated_at":"2025-03-12T17:32:02.747Z","avatar_url":"https://github.com/go-gormigrate.png","language":"Go","funding_links":["https://github.com/sponsors/andreynering","https://www.paypal.com/cgi-bin/webscr?cmd=_donations\u0026business=GSVDU63RKG45A\u0026currency_code=USD\u0026source=url"],"categories":["数据库","數據庫"],"sub_categories":["高级控制台界面","高級控制台界面"],"readme":"# Gormigrate\n\n[![Latest Release](https://img.shields.io/github/release/go-gormigrate/gormigrate.svg)](https://github.com/go-gormigrate/gormigrate/releases)\n[![Go Reference](https://pkg.go.dev/badge/github.com/go-gormigrate/gormigrate/v2.svg)](https://pkg.go.dev/github.com/go-gormigrate/gormigrate/v2)\n[![Go Report Card](https://goreportcard.com/badge/github.com/go-gormigrate/gormigrate/v2)](https://goreportcard.com/report/github.com/go-gormigrate/gormigrate/v2)\n[![CI | Lint](https://github.com/go-gormigrate/gormigrate/actions/workflows/lint.yml/badge.svg)](https://github.com/go-gormigrate/gormigrate/actions)\n[![CI | Test](https://github.com/go-gormigrate/gormigrate/actions/workflows/integration-test.yml/badge.svg)](https://github.com/go-gormigrate/gormigrate/actions)\n\nGormigrate is a minimalistic migration helper for [Gorm](http://gorm.io).\nGorm already has useful [migrate functions](https://gorm.io/docs/migration.html), just misses\nproper schema versioning and migration rollback support.\n\n\u003e IMPORTANT: If you need support to Gorm v1 (which uses\n\u003e `github.com/jinzhu/gorm` as its import path), please import Gormigrate by\n\u003e using the `gopkg.in/gormigrate.v1` import path.\n\u003e\n\u003e The current Gorm version (v2) is supported by using the\n\u003e `github.com/go-gormigrate/gormigrate/v2` import path as described in the\n\u003e documentation below.\n\n## Supported databases\n\nIt supports any of the [databases Gorm supports](https://gorm.io/docs/connecting_to_the_database.html):\n\n- MySQL\n- MariaDB\n- PostgreSQL\n- SQLite\n- Microsoft SQL Server\n- TiDB\n- Clickhouse\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\n\t\"github.com/go-gormigrate/gormigrate/v2\"\n\t\"github.com/google/uuid\"\n\t\"gorm.io/driver/sqlite\"\n\t\"gorm.io/gorm\"\n\t\"gorm.io/gorm/logger\"\n)\n\nfunc main() {\n\tdb, err := gorm.Open(sqlite.Open(\"./data.db\"), \u0026gorm.Config{\n\t\tLogger: logger.Default.LogMode(logger.Info),\n\t})\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tm := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{{\n\t\t// create `users` table\n\t\tID: \"201608301400\",\n\t\tMigrate: func(tx *gorm.DB) error {\n\t\t\t// it's a good pratice to copy the struct inside the function,\n\t\t\t// so side effects are prevented if the original struct changes during the time\n\t\t\ttype user struct {\n\t\t\t\tID   uuid.UUID `gorm:\"type:uuid;primaryKey;uniqueIndex\"`\n\t\t\t\tName string\n\t\t\t}\n\t\t\treturn tx.Migrator().CreateTable(\u0026user{})\n\t\t},\n\t\tRollback: func(tx *gorm.DB) error {\n\t\t\treturn tx.Migrator().DropTable(\"users\")\n\t\t},\n\t}, {\n\t\t// add `age` column to `users` table\n\t\tID: \"201608301415\",\n\t\tMigrate: func(tx *gorm.DB) error {\n\t\t\t// when table already exists, define only columns that are about to change\n\t\t\ttype user struct {\n\t\t\t\tAge int\n\t\t\t}\n\t\t\treturn tx.Migrator().AddColumn(\u0026user{}, \"Age\")\n\t\t},\n\t\tRollback: func(tx *gorm.DB) error {\n\t\t\ttype user struct {\n\t\t\t\tAge int\n\t\t\t}\n\t\t\treturn db.Migrator().DropColumn(\u0026user{}, \"Age\")\n\t\t},\n\t}, {\n\t\t// create `organizations` table where users belong to\n\t\tID: \"201608301430\",\n\t\tMigrate: func(tx *gorm.DB) error {\n\t\t\ttype organization struct {\n\t\t\t\tID      uuid.UUID `gorm:\"type:uuid;primaryKey;uniqueIndex\"`\n\t\t\t\tName    string\n\t\t\t\tAddress string\n\t\t\t}\n\t\t\tif err := tx.Migrator().CreateTable(\u0026organization{}); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\ttype user struct {\n\t\t\t\tOrganizationID uuid.UUID `gorm:\"type:uuid\"`\n\t\t\t}\n\t\t\treturn tx.Migrator().AddColumn(\u0026user{}, \"OrganizationID\")\n\t\t},\n\t\tRollback: func(tx *gorm.DB) error {\n\t\t\ttype user struct {\n\t\t\t\tOrganizationID uuid.UUID `gorm:\"type:uuid\"`\n\t\t\t}\n\t\t\tif err := db.Migrator().DropColumn(\u0026user{}, \"OrganizationID\"); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\treturn tx.Migrator().DropTable(\"organizations\")\n\t\t},\n\t}})\n\n\tif err := m.Migrate(); err != nil {\n\t\tlog.Fatalf(\"Migration failed: %v\", err)\n\t}\n\tlog.Println(\"Migration did run successfully\")\n}\n```\n\n## Having a separate function for initializing the schema\n\nIf you have a lot of migrations, it can be a pain to run all them, as example,\nwhen you are deploying a new instance of the app, in a clean database.\nTo prevent this, you can set a function that will run if no migration was run\nbefore (in a new clean database). Remember to create everything here, all tables,\nforeign keys and what more you need in your app.\n\n```go\ntype Organization struct {\n\tgorm.Model\n\tName    string\n\tAddress string\n}\n\ntype User struct {\n\tgorm.Model\n\tName string\n\tAge int\n\tOrganizationID uint\n}\n\nm := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{\n    // your migrations here\n})\n\nm.InitSchema(func(tx *gorm.DB) error {\n\terr := tx.AutoMigrate(\n\t\t\u0026Organization{},\n\t\t\u0026User{},\n\t\t// all other tables of you app\n\t)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif err := tx.Exec(\"ALTER TABLE users ADD CONSTRAINT fk_users_organizations FOREIGN KEY (organization_id) REFERENCES organizations (id)\").Error; err != nil {\n\t\treturn err\n\t}\n\t// all other constraints, indexes, etc...\n\treturn nil\n})\n```\n\n## Options\n\nThis is the options struct, in case you don't want the defaults:\n\n```go\ntype Options struct {\n\t// TableName is the migration table.\n\tTableName string\n\t// IDColumnName is the name of column where the migration id will be stored.\n\tIDColumnName string\n\t// IDColumnSize is the length of the migration id column\n\tIDColumnSize int\n\t// UseTransaction makes Gormigrate execute migrations inside a single transaction.\n\t// Keep in mind that not all databases support DDL commands inside transactions.\n\tUseTransaction bool\n\t// ValidateUnknownMigrations will cause migrate to fail if there's unknown migration\n\t// IDs in the database\n\tValidateUnknownMigrations bool\n}\n```\n\n## Who is Gormigrate for?\n\nGormigrate was born to be a simple and minimalistic migration tool for small\nprojects that uses [Gorm](http://gorm.io). You may want to take a look at more advanced\nsolutions like [golang-migrate/migrate](https://github.com/golang-migrate/migrate)\nif you plan to scale.\n\nBe aware that Gormigrate has no builtin lock mechanism, so if you're running\nit automatically and have a distributed setup (i.e. more than one executable\nrunning at the same time), you might want to use a\n[distributed lock/mutex mechanism](https://redis.io/topics/distlock) to\nprevent race conditions while running migrations.\n\n## Contributing\n\nTo run integration tests, some preparations are needed. Please ensure you\nhave [task](https://taskfile.dev/installation) and [docker](https://docs.docker.com/engine/install) installed.\nThen:\n\n1. Ensure target or all databases are available and ready to accept connections.\n   You can start databases locally with `task docker:compose:up`\n2. Copy `integration-test/.example.env` as `integration-test/.env` and\n   adjust the database connection ports and credentials when needed.\n3. Run integration test for single database or for all\n\n```bash\n# run test for MySQL\ntask test:mysql\n\n# run test for MariaDB\ntask test:mariadb\n\n# run test for PostgreSQL\ntask test:postgres\n\n# run test for SQLite\ntask test:sqlite\n\n# run test for Microsoft SQL Server\ntask test:sqlserver\n\n# run test for all databases\ntask test:all\n```\n\nAlternatively, you can run everything in one step: `task docker:test`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGo-Gormigrate%2FGormigrate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGo-Gormigrate%2FGormigrate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGo-Gormigrate%2FGormigrate/lists"}