{"id":13514200,"url":"https://github.com/go-pg/migrations","last_synced_at":"2025-04-04T07:06:22.054Z","repository":{"id":52882458,"uuid":"41993934","full_name":"go-pg/migrations","owner":"go-pg","description":"SQL database migrations for Golang go-pg and PostgreSQL","archived":false,"fork":false,"pushed_at":"2023-10-16T08:57:41.000Z","size":187,"stargazers_count":309,"open_issues_count":13,"forks_count":58,"subscribers_count":8,"default_branch":"v8","last_synced_at":"2025-03-28T06:05:40.852Z","etag":null,"topics":["dat","database","go","golang","postgresql","sql-migration"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/go-pg/migrations","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/go-pg.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}},"created_at":"2015-09-06T07:57:03.000Z","updated_at":"2025-03-10T08:55:57.000Z","dependencies_parsed_at":"2024-06-18T12:37:54.169Z","dependency_job_id":"fef8d2d9-b853-454e-bd73-46ed75de429c","html_url":"https://github.com/go-pg/migrations","commit_stats":{"total_commits":126,"total_committers":23,"mean_commits":5.478260869565218,"dds":0.2698412698412699,"last_synced_commit":"955058857e73e078e7d4482040f17c4c6c84e63f"},"previous_names":[],"tags_count":51,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-pg%2Fmigrations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-pg%2Fmigrations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-pg%2Fmigrations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-pg%2Fmigrations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-pg","download_url":"https://codeload.github.com/go-pg/migrations/tar.gz/refs/heads/v8","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247135143,"owners_count":20889420,"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":["dat","database","go","golang","postgresql","sql-migration"],"created_at":"2024-08-01T05:00:49.483Z","updated_at":"2025-04-04T07:06:22.033Z","avatar_url":"https://github.com/go-pg.png","language":"Go","readme":"# SQL migrations for Golang and PostgreSQL\n\n[![Build Status](https://travis-ci.org/go-pg/migrations.svg)](https://travis-ci.org/go-pg/migrations)\n[![GoDoc](https://godoc.org/github.com/go-pg/migrations?status.svg)](https://godoc.org/github.com/go-pg/migrations)\n\nThis package allows you to run migrations on your PostgreSQL database using [Golang Postgres client](https://github.com/go-pg/pg). See [example](example) for details.\n\nYou may also want to check [go-pg-migrations](https://github.com/robinjoseph08/go-pg-migrations) before making a decision.\n\n# Installation\n\ngo-pg/migrations requires a Go version with [Modules](https://github.com/golang/go/wiki/Modules) support and uses import path versioning. So please make sure to initialize a Go module:\n\n```shell\ngo mod init github.com/my/repo\ngo get github.com/go-pg/migrations/v8\n```\n\n# Usage\n\nTo run migrations on your project you should fulfill the following steps:\n\n1. define the migration list;\n1. implement an executable app that calls migration tool;\n1. run migrations.\n\n## Define Migrations\n\n### Migration Files\n\nYou can save SQL migration files at the same directory as your `main.go` file, they should have proper file extensions ([more about migration files](#sql-migrations)).\n\n### Registered Migrations\n\nMigrations can be registered in the code using `migrations.RegisterTx` and `migrations.MustRegisterTx` functions. [More details](#registering-migrations) about migration registering.\n\n## Implement app to run the tool\n\nYou can run migrations from any place of your app or ecosystem. It can be a standalone application of a part of a big program, or maybe an HTTP handler, etc. Check [example](#example) for some helpful information about practical usage.\n\n## Run Migrations\n\nRun migration tool by providing CLI arguments to the `migrations.Run` function.\n\nCurrently, the following arguments are supported:\n\n- `up` - runs all available migrations;\n- `up [target]` - runs available migrations up to the target one;\n- `down` - reverts last migration;\n- `reset` - reverts all migrations;\n- `version` - prints current db version;\n- `set_version [version]` - sets db version without running migrations.\n\n# Example\n\nYou need to create database `pg_migrations_example` before running the [example](example).\n\n```bash\n\u003e cd example\n\n\u003e psql -c \"CREATE DATABASE pg_migrations_example\"\nCREATE DATABASE\n\n\u003e go run *.go init\nversion is 0\n\n\u003e go run *.go version\nversion is 0\n\n\u003e go run *.go\ncreating table my_table...\nadding id column...\nseeding my_table...\nmigrated from version 0 to 4\n\n\u003e go run *.go version\nversion is 4\n\n\u003e go run *.go reset\ntruncating my_table...\ndropping id column...\ndropping table my_table...\nmigrated from version 4 to 0\n\n\u003e go run *.go up 2\ncreating table my_table...\nadding id column...\nmigrated from version 0 to 2\n\n\u003e go run *.go\nseeding my_table...\nmigrated from version 2 to 4\n\n\u003e go run *.go down\ntruncating my_table...\nmigrated from version 4 to 3\n\n\u003e go run *.go version\nversion is 3\n\n\u003e go run *.go set_version 1\nmigrated from version 3 to 1\n\n\u003e go run *.go create add email to users\ncreated migration 5_add_email_to_users.go\n```\n\n## Registering Migrations\n\n### `migrations.RegisterTx` and `migrations.MustRegisterTx`\n\nRegisters migrations to be executed inside transactions.\n\n### `migrations.Register` and `migrations.MustRegister`\n\nRegisters migrations to be executed without any transaction.\n\n## SQL migrations\n\nSQL migrations are automatically picked up if placed in the same folder with `main.go` or Go migrations.\nSQL migrations may be manually registered using `DiscoverSQLMigrations` (from OS directory) or `DiscoverSQLMigrationsFromFilesystem`.\nIt may be used with new go 1.16 embedding feature. Example:\n```go\n//go:embed migrations/*.sql\nvar migrations embed.FS\n\ncollection := migrations.NewCollection()\ncollection.DiscoverSQLMigrationsFromFilesystem(http.FS(migrations), \"migrations\")\n```\nSQL migrations must have one of the following extensions:\n\n- .up.sql - up migration;\n- .down.sql - down migration;\n- .tx.up.sql - transactional up migration;\n- .tx.down.sql - transactional down migration.\n\nBy default SQL migrations are executed as single PostgreSQL statement. `--gopg:split` directive can be used to split migration into several statements:\n\n```sql\nSET statement_timeout = 60000;\nSET lock_timeout = 60000;\n\n--gopg:split\n\nCREATE INDEX CONCURRENTLY ...;\n```\n\n## Transactions\n\nBy default, the migrations are executed outside without any transactions. Individual migrations can however be marked to be executed inside transactions by using the `RegisterTx` function instead of `Register`.\n\n### Global Transactions\n\n```go\nvar oldVersion, newVersion int64\n\nerr := db.RunInTransaction(func(tx *pg.Tx) (err error) {\n    oldVersion, newVersion, err = migrations.Run(tx, flag.Args()...)\n    return\n})\nif err != nil {\n    exitf(err.Error())\n}\n```\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-pg%2Fmigrations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-pg%2Fmigrations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-pg%2Fmigrations/lists"}