{"id":22923191,"url":"https://github.com/ikala/gorm-cli","last_synced_at":"2025-07-22T17:35:07.312Z","repository":{"id":40291779,"uuid":"214776238","full_name":"iKala/gorm-cli","owner":"iKala","description":"The cli tool for gorm. Such as migration, seed.","archived":false,"fork":false,"pushed_at":"2024-02-04T16:23:59.000Z","size":212,"stargazers_count":9,"open_issues_count":11,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-12T23:13:25.497Z","etag":null,"topics":["gorm","gorm-cli","gorm-migrate","gorm-migration","gorm-seed","migration-tool"],"latest_commit_sha":null,"homepage":null,"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/iKala.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":"2019-10-13T07:07:19.000Z","updated_at":"2024-12-14T15:35:52.000Z","dependencies_parsed_at":"2024-06-21T16:51:48.559Z","dependency_job_id":null,"html_url":"https://github.com/iKala/gorm-cli","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/iKala/gorm-cli","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iKala%2Fgorm-cli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iKala%2Fgorm-cli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iKala%2Fgorm-cli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iKala%2Fgorm-cli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iKala","download_url":"https://codeload.github.com/iKala/gorm-cli/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iKala%2Fgorm-cli/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266540217,"owners_count":23945178,"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-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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":["gorm","gorm-cli","gorm-migrate","gorm-migration","gorm-seed","migration-tool"],"created_at":"2024-12-14T08:14:39.860Z","updated_at":"2025-07-22T17:35:07.287Z","avatar_url":"https://github.com/iKala.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"![gorm-cli](./gorm-cli.png)\n\nOnline doc: https://gorm-cli.bugfree.app\n\n# gorm-cli\nThe CLI tool for [gorm ORM](https://gorm.io/). Such as migration, and seed.\n\nCurrently, [gorm-cli](https://github.com/iKala/gorm-cli) supports two dialects: `mysql` and `postgres`.\n\n## How gorm-cli works\n[gorm-cli](https://github.com/iKala/gorm-cli) maintains a table - `gorm_meta` to record the migration execution history. Support developers to keep the DB migration in code, and easy to roll back.\n\n## Installation\n```shell\n$ go install github.com/iKala/gorm-cli@latest\n$ gorm-cli\nOption of migration is needed - (db:prebuild / db:init / db:migrate / db:rollback / db:create_migration)\n```\n\n### macOS\nLet's say you got `command not found`.\n```shell\n# Add the env to your cli (whatever fish, zsh, bash...) then try again\nexport PATH=$PATH:$(go env GOPATH)/bin\n```\n\n# How to use [gorm-cli](https://github.com/iKala/gorm-cli)\n\n## Initialization\nFirst, prepare your connection settings.\n\n[gorm-cli](https://github.com/iKala/gorm-cli) will load the gorm drivers automatically to support various dialects such as `mysql`, `postgres`...like `gorm` do, but you need to install the drive yourself.\n\n```shell\n# Get the mysql driver when you use the mysql dialect.\n# When the module was recorded in your go.mod, you don't have to do this for far.\ngo get gorm.io/driver/mysql\n```\n\nGenerate your connection file.\n```shell\n$ gorm-cli db:init\n```\n\n[gorm-cli](https://github.com/iKala/gorm-cli) supports two way to configure your DB connection.\n\n### YAML\nPlace the yaml config at your project root where you run `gorm-cli` command and name it `.gorm-cli.yaml`.\n\n```yaml\ndb:\n  host: localhost\n  port: 3306\n  dialects: mysql\n  user: root\n  password: password\n  dbname: hentai\n  charset: utf8mb4\n\nmigration:\n  path: ./migration\n```\n\n```yaml\ndb:\n  host: localhost\n  port: 5432\n  dialects: postgres\n  user: root\n  password: password\n  dbname: hentai\n  sslmode: disable\n  timezone: UTC\n\nmigration:\n  path: ./migration\n```\n\n### ENV\n\n| Name | Description | Default Value |\n|------|-------------|---------------|\n| DB_HOST | DB Host | |\n| DB_DIALECTS | mysql, postgres...etc, check the doc [here](https://gorm.io/docs/write_driver.html#Write-new-driver) | |\n| DB_USER | DB user | |\n| DB_PASSWORD | DB password | |\n| DB_DBNAME | Target DB name | |\n| DB_CHARSET | Connection charset (Available when dialect is `mysql`) | |\n| DB_SSLMODE | Connection charset (Available when dialect is `postgres`) | `disable` |\n| DB_TIMEZONE | Connection charset (Available when dialect is `postgres`) | `UTC` |\n| MIGRATION | The folder you place the migrations. | `./migration` |\n\n## Build your migration\n\n```shell\n# Replace `create_user` to YOUR_MIGRATION_TITLE\n$ db:create_migration create_user\nMigration created. ./migration/20221120125320_create_user.go\n\n# The empty migration will be created in the migration folder you configured.\n```\n\nSince we're building migrations on top of gorm, you should check the gorm's doc to know what you need. [Toturials - Migration](https://gorm.io/docs/migration.html)\n\nHere are some examples\n\n### Create table via model declearation.\n```go\npackage main\n\nimport (\n  \"github.com/foo/bar/model\"\n  \"gorm.io/gorm\"\n)\n\ntype migration string\n\n// Up - Changes for the migration.\nfunc (m migration) Up(db *gorm.DB) error {\n  return db.AutoMigrate(\u0026model.User{})\n}\n\n// Down - Rollback changes for the migration.\nfunc (m migration) Down(db *gorm.DB) error {\n  return db.Migrator().DropTable(\u0026model.User{})\n}\n\nvar Migration migration\n```\n\n### Prepare some seed data.\n```go\npackage main\n\nimport (\n  \"github.com/foo/bar/model\"\n  \"gorm.io/gorm\"\n)\n\ntype migration string\n\n// Up - Changes for the migration.\nfunc (m migration) Up(db *gorm.DB) error {\n  return db.Transaction(func(tx *gorm.DB) error {\n    if err := tx.Exec(\"INSERT INTO tag_type_weights (type, weight) VALUES (?, ?)\", model.TagTypeCharacter, 1.6).Error; err != nil {\n      return err\n    }\n    if err := tx.Exec(\"INSERT INTO tag_type_weights (type, weight) VALUES (?, ?)\", model.TagTypeCopyright, 1.4).Error; err != nil {\n      return err\n    }\n    if err := tx.Exec(\"INSERT INTO tag_type_weights (type, weight) VALUES (?, ?)\", model.TagTypeArtist, 1.3).Error; err != nil {\n      return err\n    }\n    if err := tx.Exec(\"INSERT INTO tag_type_weights (type, weight) VALUES (?, ?)\", model.TagTypeGeneral, 1.1).Error; err != nil {\n      return err\n    }\n\n    return nil\n  })\n}\n\n// Down - Rollback changes for the migration.\nfunc (m migration) Down(db *gorm.DB) error {\n  return db.Transaction(func(tx *gorm.DB) error {\n    if err := tx.Exec(\"DELETE FROM tag_type_weights WHERE type = ? AND weight = ?\", model.TagTypeCharacter, 1.6).Error; err != nil {\n      return err\n    }\n    if err := tx.Exec(\"DELETE FROM tag_type_weights WHERE type = ? AND weight = ?\", model.TagTypeCopyright, 1.4).Error; err != nil {\n      return err\n    }\n    if err := tx.Exec(\"DELETE FROM tag_type_weights WHERE type = ? AND weight = ?\", model.TagTypeArtist, 1.3).Error; err != nil {\n      return err\n    }\n    if err := tx.Exec(\"DELETE FROM tag_type_weights WHERE type = ? AND weight = ?\", model.TagTypeGeneral, 1.1).Error; err != nil {\n      return err\n    }\n\n    return nil\n  })\n}\n\nvar Migration migration\n```\n\n## Execute migrations\n```shell\n# `db:migrate` command will execute the `Up` function in your migrations.\n$ gorm-cli db:migrate\nMigrated. 1 20221120125320_create_user.so\n\n# `db:rollback` or `db:rollback [STEPS]` executes the `Down` function to revert the migration.\n$ gorm-cli db:rollback\n✔ Are you sure to rollback the migration with all steps? (Yes/No):\n```\n\n[gorm-cli](https://github.com/iKala/gorm-cli) will compile the migrations into `.so` files and cache them at `.plugins` folder. Once the `.so` file built, [gorm-cli](https://github.com/iKala/gorm-cli) will not change it unless the `-f` flag is applied.\n\n```shell\n# The `-f` flag to force rebuild the `.so` file, it's useful when you're testing and need to retry.\n$ gorm-cli db:migrate -f\n$ gorm-cli db:rollback -f\n```\n\n## Prebuild migrations\nAs mentioned above, [gorm-cli](https://github.com/iKala/gorm-cli) will compile the migrations into `.so` automatically. However, when `go` is not installed in the production environment, you need to pre-build the migrations to be portable.\n\n```shell\n$ gorm-cli db:prebuild\nMigrated. 1 20221120125320_create_user.so\nconnection.so created\n\n# Check generated files\n$ ls -l ./migration/.plugins\n# Move files to where ever you want.\n$ cp -r ./migration/.plugins [ANYWHERE]\n```\n\n## Dockerfile\nYou can also use docker to run the migration.\n\nHere is the example.\n```dockerfile\n##\n## Build\n##\nFROM golang:alpine AS build\n\nWORKDIR /\n\nRUN apk update \u0026\u0026 apk add musl-dev gcc\n\nCOPY ./model ./model\nCOPY ./go.mod ./\nCOPY ./go.sum ./\n\nARG DB_HOST\nARG DB_DIALECTS\nARG DB_USER\nARG DB_PASSWORD\nARG DB_DBNAME\nARG DB_CHARSET\n\nENV MIGRATION_PATH=/model/migration\n\nRUN go install github.com/iKala/gorm-cli@latest \u0026\u0026 go mod download\nRUN gorm-cli db:prebuild \u0026\u0026 gorm-cli db:init\n\n##\n## Deploy\n##\nFROM alpine\n\nCOPY --from=build /model/migration/ /migration/\nCOPY --from=build /go/bin/gorm-cli /go/bin/gorm-cli\n\nENV MIGRATION_PATH=/migration\n\nCMD [\"/go/bin/gorm-cli\", \"db:migrate\"]\n\n```\n\n```shell\n$ docker run --rm gorm-cli-migration\n```\n\n---\n\n## Thanks for all of you guys liking this project\nIt's very welcome to give your PRs to make this little tool much easier to use. 🙂\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fikala%2Fgorm-cli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fikala%2Fgorm-cli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fikala%2Fgorm-cli/lists"}