{"id":44811359,"url":"https://github.com/iamsalnikov/mymigrate","last_synced_at":"2026-02-16T16:54:24.300Z","repository":{"id":43173338,"uuid":"302382079","full_name":"iamsalnikov/mymigrate","owner":"iamsalnikov","description":"another golang sql migration package","archived":false,"fork":false,"pushed_at":"2022-04-30T18:11:35.000Z","size":121,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-19T16:46:48.134Z","etag":null,"topics":["go","go-library","go-package","go-sql","golang","golang-library","golang-package","golang-sql","migration","migration-tool","migrations"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"wtfpl","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iamsalnikov.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}},"created_at":"2020-10-08T15:19:38.000Z","updated_at":"2022-04-30T08:25:59.000Z","dependencies_parsed_at":"2022-08-27T01:31:25.930Z","dependency_job_id":null,"html_url":"https://github.com/iamsalnikov/mymigrate","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/iamsalnikov/mymigrate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsalnikov%2Fmymigrate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsalnikov%2Fmymigrate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsalnikov%2Fmymigrate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsalnikov%2Fmymigrate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamsalnikov","download_url":"https://codeload.github.com/iamsalnikov/mymigrate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsalnikov%2Fmymigrate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29513367,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-16T09:05:14.864Z","status":"ssl_error","status_checked_at":"2026-02-16T08:55:59.364Z","response_time":115,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["go","go-library","go-package","go-sql","golang","golang-library","golang-package","golang-sql","migration","migration-tool","migrations"],"created_at":"2026-02-16T16:54:23.544Z","updated_at":"2026-02-16T16:54:24.294Z","avatar_url":"https://github.com/iamsalnikov.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mymigrate\n\nYet another migration library for golang.\n\n- [Why](#why)\n- [How to use it](#how-to-use-it)\n  - [Installation](#installation)\n  - [Setup a database connection](#setup-a-database-connection)\n  - [Add migrations](#add-migrations)\n  - [Apply, Down, View history with direct commands](#apply-down-view-history-with-direct-commands)\n  - [Cobra commands](#cobra-commands)\n\n## Why\n\u003cimg align=\"left\" width=\"150\" height=\"130\" src=\"https://www.meme-arsenal.com/memes/7d70b26fc3a93cd663768d1c52a445b5.jpg\"\u003e\nSometimes we need to perform some complex logic during migration, such as receiving data from an external resource, complex data mapping, and so on. \n\nI've tried to find a tool that will allow me to do these things, but I've failed (maybe because I didn't look for it well enough).\nSo I've decided to write a simple migration tool that will allow you to write migrations in golang and work with an app's DB connection.\n\nIt works with golang's SQL package, so, theoretically, it may work with any SQL DB (I've tested it only with MySQL).\n\n## How to use it\n\nYou can use direct functions to work with migrations or add cobra's commands to your app and use them. I'll try to describe all these ways.\n\n### Installation\n\nTo install this package you need to run:\n\n```bash\ngo get github.com/iamsalnikov/mymigrate\n```\n\n### Setup a database connection\n\nTo work with migrations we need to know a database connection. After opening a connection with DB we need to pass the connection to `mymigrate` package via:\n\n```golang\nmymigrate.SetDatabase(db)\n```\n\nExample:\n\n```golang\nimport (\n    \"database/sql\"\n    \"log\"\n\n    _ \"github.com/go-sql-driver/mysql\"\n    \"github.com/iamsalnikov/mymigrate\"\n\t\"github.com/iamsalnikov/mymigrate/provider/mysql\"\n)\n\nfunc main() {\n    db, err := sql.Open(\"mysql\", getConnString(name))\n    if err != nil {\n        log.Fatalln(err)\n    }\n\n\tprovider := mysql.NewMysqlProvider(db)\n    mymigrate.SetDatabaseProvider(provider)\n```\n\n### Add migrations\n\nTo add a new migration to a migration pool we need to call the method `Add` and pass the name of the migration, a function to UP the migration, a function to DOWN the migration. Example:\n\n```golang\nmymigrate.Add(\n    \"mig_001\",\n    func (db *sql.DB) error {\n        // TODO: implemet up logic\n        panic(\"Implement me!\")\n    },\n    func (db *sql.DB) error {\n        // TODO: implemet down logic\n        panic(\"Implement me!\")\n    },\n)\n```\n\nWe can create a package `migrations` inside a project and put all migrations here. And then just import in to the entrypoint of project. Example:\n\nProject structure:\n\n```\n- app/\n    - migrations/\n        - mig_001.go\n        - mig_002.go\n    main.go\n```\n\nContent of `app/migrations/mig_001.go`:\n\n```golang\npackage migrations\n\nimport (\n    \"database/sql\"\n\n    \"github.com/iamsalnikov/mymigrate\"\n)\n\nfunc init() {\n    mymigrate.Add(\n        \"mig_001\",\n        func (db *sql.DB) error {\n            // TODO: implemet up logic\n            panic(\"Implement me!\")\n        },\n        func (db *sql.DB) error {\n            // TODO: implemet down logic\n            panic(\"Implement me!\")\n        },\n    )\n}\n```\n\nContent of `app/main.go`: \n\n```golang\nimport (\n    \"database/sql\"\n    \"log\"\n\n    _ \"github.com/go-sql-driver/mysql\"\n    \"github.com/iamsalnikov/mymigrate\"\n\n    // Import project migrations\n    _ \"app/migrations\"\n)\n\nfunc main() {\n    db, err := sql.Open(\"mysql\", getConnString(name))\n    if err != nil {\n        log.Fatalln(err)\n    }\n\n    mymigrate.SetDatabase(db)\n    appliedMigrations, err := mymigrate.Apply()\n    // TODO: work with it\n)\n```\n\n### Apply, Down, View history with direct commands\n\nTo Apply migrations with direct command we need to run `mymigrate.Apply()` function. It will return a list of applied migrations and an error.\n\nTo Down migrations with direct command we need to run `mymigrate.Down(int)` function and pass number of migrations to be downed. It will return a list of downed migrations and an error.\n\nTo view a history of applied migrations with direct command we need to run `mymigrate.History()`. It will return a list of applied migrations and an error.\n\n### Cobra commands\n\nIf you use [spf13/cobra](https://github.com/spf13/cobra) package to build nice CLI app then there is one piece of good new for you: this package has commands to work with migrations. You can find them at [cobracmd](cobracmd) directory.\n\nPackage `github.com/iamsalnikov/mymigrate/cobracmd` export next commands:\n- [ApplyCmd](cobracmd/apply_cmd.go) - command to apply new migrations\n- [CreateCmd](cobracmd/create_cmd.go) - command to create new migration\n- [DownCmd](cobracmd/down_cmd.go) - command to down applied migrations\n- [HistoryCmd](cobracmd/history_cmd.go) - command to view a list of applied migrations\n- [NewListCmd](cobracmd/new_cmd.go) - command to view a list of new migrations\n- [MigrateCmd](cobracmd/cmd.go) - root command to work with migrations\n\nAlso you can find at `github.com/iamsalnikov/mymigrate/cobracmd` functions for cobra commands if you want to configure commands by yourself.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamsalnikov%2Fmymigrate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamsalnikov%2Fmymigrate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamsalnikov%2Fmymigrate/lists"}