{"id":19339724,"url":"https://github.com/rubenv/modl-migrate","last_synced_at":"2025-07-29T03:37:34.777Z","repository":{"id":24581087,"uuid":"27989007","full_name":"rubenv/modl-migrate","owner":"rubenv","description":"SQL Schema migration tool for Go, using modl.","archived":false,"fork":false,"pushed_at":"2017-11-28T07:01:04.000Z","size":244,"stargazers_count":15,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T06:43:12.106Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rubenv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-14T09:02:34.000Z","updated_at":"2023-01-29T20:14:08.000Z","dependencies_parsed_at":"2022-08-23T02:21:34.850Z","dependency_job_id":null,"html_url":"https://github.com/rubenv/modl-migrate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenv%2Fmodl-migrate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenv%2Fmodl-migrate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenv%2Fmodl-migrate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rubenv%2Fmodl-migrate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rubenv","download_url":"https://codeload.github.com/rubenv/modl-migrate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250357566,"owners_count":21417307,"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":[],"created_at":"2024-11-10T03:23:31.788Z","updated_at":"2025-04-23T02:30:53.721Z","avatar_url":"https://github.com/rubenv.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# modl-migrate\n\n\u003e SQL Schema migration tool for [Go](http://golang.org/), using [modl](https://github.com/jmoiron/modl).\n\nIf you're using [gorp](https://github.com/coopernurse/gorp), have a look at [sql-migrate](https://github.com/rubenv/sql-migrate).\n\n[![Build Status](https://travis-ci.org/rubenv/modl-migrate.svg?branch=master)](https://travis-ci.org/rubenv/modl-migrate) [![GoDoc](https://godoc.org/github.com/rubenv/modl-migrate?status.png)](https://godoc.org/github.com/rubenv/modl-migrate)\n\n## Features\n\n* Usable as a CLI tool or as a library\n* Supports SQLite, PostgreSQL and MySQL databases (through [modl](https://github.com/jmoiron/modl))\n* Can embed migrations into your application\n* Migrations are defined with SQL for full flexibility\n* Atomic migrations\n* Up/down migrations to allow rollback\n* Supports multiple database types in one project\n\n## Installation\n\nTo install the library and command line program, use the following:\n\n```bash\ngo get github.com/rubenv/modl-migrate/...\n```\n\n## Usage\n### As a standalone tool\n```\n$ modl-migrate --help\nusage: modl-migrate [--version] [--help] \u003ccommand\u003e [\u003cargs\u003e]\n\nAvailable commands are:\n    down      Undo a database migration\n    redo      Reapply the last migration\n    status    Show migration status\n    up        Migrates the database to the most recent version available\n```\n\nEach command requires a configuration file (which defaults to `dbconfig.yml`, but can be specified with the `-config` flag). This config file should specify one or more environments:\n\n```yml\ndevelopment:\n    dialect: sqlite3\n    datasource: test.db\n    dir: migrations/sqlite3\n\nproduction:\n    dialect: postgres\n    datasource: dbname=myapp sslmode=disable\n    dir: migrations/postgres\n    table: migrations\n```\n\nThe `table` setting is optional and will default to `modl_migrations`.\n\nThe environment that will be used can be specified with the `-env` flag (defaults to `development`).\n\nUse the `--help` flag in combination with any of the commands to get an overview of its usage:\n\n```\n$ modl-migrate up --help\nUsage: modl-migrate up [options] ...\n\n  Migrates the database to the most recent version available.\n\nOptions:\n\n  -config=config.yml   Configuration file to use.\n  -env=\"development\"   Environment.\n  -limit=0             Limit the number of migrations (0 = unlimited).\n  -dryrun              Don't apply migrations, just print them.\n```\n\nThe `up` command applies all available migrations. By contrast, `down` will only apply one migration by default. This behavior can be changed for both by using the `-limit` parameter.\n\nThe `redo` command will unapply the last migration and reapply it. This is useful during development, when you're writing migrations.\n\nUse the `status` command to see the state of the applied migrations:\n\n```bash\n$ modl-migrate status\n+---------------+-----------------------------------------+\n|   MIGRATION   |                 APPLIED                 |\n+---------------+-----------------------------------------+\n| 1_initial.sql | 2014-09-13 08:19:06.788354925 +0000 UTC |\n| 2_record.sql  | no                                      |\n+---------------+-----------------------------------------+\n```\n\n### As a library\nImport modl-migrate into your application:\n\n```go\nimport \"github.com/rubenv/modl-migrate\"\n```\n\nSet up a source of migrations, this can be from memory, from a set of files or from bindata (more on that later):\n\n```go\n// Hardcoded strings in memory:\nmigrations := \u0026migrate.MemoryMigrationSource{\n    Migrations: []*migrate.Migration{\n        \u0026migrate.Migration{\n            Id:   \"123\",\n            Up:   []string{\"CREATE TABLE people (id int)\"},\n            Down: []string{\"DROP TABLE people\"},\n        },\n    },\n}\n\n// OR: Read migrations from a folder:\nmigrations := \u0026migrate.FileMigrationSource{\n    Dir: \"db/migrations\",\n}\n\n// OR: Use migrations from bindata:\nmigrations := \u0026migrate.AssetMigrationSource{\n    Asset:    Asset,\n    AssetDir: AssetDir,\n    Dir:      \"migrations\",\n}\n```\n\nThen use the `Exec` function to upgrade your database:\n\n```go\ndb, err := sql.Open(\"sqlite3\", filename)\nif err != nil {\n    // Handle errors!\n}\n\nn, err := migrate.Exec(db, \"sqlite3\", migrations, migrate.Up)\nif err != nil {\n    // Handle errors!\n}\nfmt.Printf(\"Applied %d migrations!\\n\", n)\n```\n\nNote that `n` can be greater than `0` even if there is an error: any migration that succeeded will remain applied even if a later one fails.\n\nCheck [the GoDoc reference](https://godoc.org/github.com/rubenv/modl-migrate) for the full documentation.\n\n## Writing migrations\nMigrations are defined in SQL files, which contain a set of SQL statements. Special comments are used to distinguish up and down migrations.\n\n```sql\n-- +migrate Up\n-- SQL in section 'Up' is executed when this migration is applied\nCREATE TABLE people (id int);\n\n\n-- +migrate Down\n-- SQL section 'Down' is executed when this migration is rolled back\nDROP TABLE people;\n```\n\nYou can put multiple statements in each block, as long as you end them with a semicolon (`;`).\n\nIf you have complex statements which contain semicolons, use `StatementBegin` and `StatementEnd` to indicate boundaries:\n\n```sql\n-- +migrate Up\nCREATE TABLE people (id int);\n\n-- +migrate StatementBegin\nCREATE OR REPLACE FUNCTION do_something()\nreturns void AS $$\nDECLARE\n  create_query text;\nBEGIN\n  -- Do something here\nEND;\n$$\nlanguage plpgsql;\n-- +migrate StatementEnd\n\n-- +migrate Down\nDROP FUNCTION do_something();\nDROP TABLE people;\n```\n\nThe order in which migrations are applied is defined through the filename: modl-migrate will sort migrations based on their name. It's recommended to use an increasing version number or a timestamp as the first part of the filename.\n\n## Embedding migrations with [bindata](https://github.com/jteeuwen/go-bindata)\nIf you like your Go applications self-contained (that is: a single binary): use [bindata](https://github.com/jteeuwen/go-bindata) to embed the migration files.\n\nJust write your migration files as usual, as a set of SQL files in a folder.\n\nThen use bindata to generate a `.go` file with the migrations embedded:\n\n```bash\ngo-bindata -pkg myapp -o bindata.go db/migrations/\n```\n\nThe resulting `bindata.go` file will contain your migrations. Remember to regenerate your `bindata.go` file whenever you add/modify a migration (`go generate` will help here, once it arrives).\n\nUse the `AssetMigrationSource` in your application to find the migrations:\n\n```go\nmigrations := \u0026migrate.AssetMigrationSource{\n    Asset:    Asset,\n    AssetDir: AssetDir,\n    Dir:      \"db/migrations\",\n}\n```\n\nBoth `Asset` and `AssetDir` are functions provided by bindata.\n\nThen proceed as usual.\n\n## Extending\nAdding a new migration source means implementing `MigrationSource`.\n\n```go\ntype MigrationSource interface {\n    FindMigrations() ([]*Migration, error)\n}\n```\n\nThe resulting slice of migrations will be executed in the given order, so it should usually be sorted by the `Id` field.\n\n## License \n\n    (The MIT License)\n\n    Copyright (C) 2014 by Ruben Vermeersch \u003cruben@rocketeer.be\u003e\n\n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n\n    The above copyright notice and this permission notice shall be included in\n    all copies or substantial portions of the Software.\n\n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n    THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubenv%2Fmodl-migrate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frubenv%2Fmodl-migrate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubenv%2Fmodl-migrate/lists"}