All Projects → go-pg → Migrations

go-pg / Migrations

Licence: bsd-2-clause
SQL database migrations for Golang go-pg and PostgreSQL

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Migrations

Piccolo
A fast, user friendly ORM and query builder which supports asyncio.
Stars: ✭ 219 (-12.75%)
Mutual labels:  database, postgresql
Django Migration Linter
🚀 Detect backward incompatible migrations for your django project
Stars: ✭ 231 (-7.97%)
Mutual labels:  database, postgresql
Db
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
Stars: ✭ 2,832 (+1028.29%)
Mutual labels:  database, postgresql
3dcitydb
3D City Database - The Open Source CityGML Database
Stars: ✭ 210 (-16.33%)
Mutual labels:  database, postgresql
Rpostgres
A DBI-compliant interface to PostgreSQL
Stars: ✭ 245 (-2.39%)
Mutual labels:  database, postgresql
Dbutils
Database connections for multi-threaded environments
Stars: ✭ 212 (-15.54%)
Mutual labels:  database, postgresql
Massive Js
A data mapper for Node.js and PostgreSQL.
Stars: ✭ 2,521 (+904.38%)
Mutual labels:  database, postgresql
Rustorm
an orm for rust
Stars: ✭ 205 (-18.33%)
Mutual labels:  database, postgresql
Sqlfiddle3
New version based on vert.x and docker
Stars: ✭ 242 (-3.59%)
Mutual labels:  database, postgresql
Gorm Bulk Insert
implement BulkInsert using gorm, just pass a Slice of Struct. Simple and compatible.
Stars: ✭ 241 (-3.98%)
Mutual labels:  database, postgresql
Endb
Key-value storage for multiple databases. Supports MongoDB, MySQL, Postgres, Redis, and SQLite.
Stars: ✭ 208 (-17.13%)
Mutual labels:  database, postgresql
Mikro Orm
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL and SQLite databases.
Stars: ✭ 3,874 (+1443.43%)
Mutual labels:  database, postgresql
Postgres
🐘 Run PostgreSQL in Kubernetes
Stars: ✭ 205 (-18.33%)
Mutual labels:  database, postgresql
Temboard
PostgreSQL Remote Control
Stars: ✭ 218 (-13.15%)
Mutual labels:  database, postgresql
Shardingsphere
Build criterion and ecosystem above multi-model databases
Stars: ✭ 14,989 (+5871.71%)
Mutual labels:  database, postgresql
Scany
Library for scanning data from a database into Go structs and more
Stars: ✭ 228 (-9.16%)
Mutual labels:  database, postgresql
Obevo
Obevo is a database deployment tool that handles enterprise scale schemas and complexity
Stars: ✭ 192 (-23.51%)
Mutual labels:  database, postgresql
Npgsql
Npgsql is the .NET data provider for PostgreSQL.
Stars: ✭ 2,415 (+862.15%)
Mutual labels:  database, postgresql
Prest
PostgreSQL ➕ REST, low-code, simplify and accelerate development, ⚡ instant, realtime, high-performance on any Postgres application, existing or new
Stars: ✭ 3,023 (+1104.38%)
Mutual labels:  database, postgresql
Php Crud Api
Single file PHP script that adds a REST API to a SQL database
Stars: ✭ 2,904 (+1056.97%)
Mutual labels:  database, postgresql

SQL migrations for Golang and PostgreSQL

Build Status GoDoc

This package allows you to run migrations on your PostgreSQL database using Golang Postgres client. See example for details.

You may also want to check go-pg-migrations before making a decision.

Installation

go-pg/migrations requires a Go version with Modules support and uses import path versioning. So please make sure to initialize a Go module:

go mod init github.com/my/repo
go get github.com/go-pg/migrations/v8

Usage

To run migrations on your project you should fulfill the following steps:

  1. define the migration list;
  2. implement an executable app that calls migration tool;
  3. run migrations.

Define Migrations

Migration Files

You can save SQL migration files at the same directory as your main.go file, they should have proper file extensions (more about migration files).

Registered Migrations

Migrations can be registered in the code using migrations.RegisterTx and migrations.MustRegisterTx functions. More details about migration registering.

Implement app to run the tool

You 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 for some helpful information about practical usage.

Run Migrations

Run migration tool by providing CLI arguments to the migrations.Run function.

Currently, the following arguments are supported:

  • up - runs all available migrations;
  • up [target] - runs available migrations up to the target one;
  • down - reverts last migration;
  • reset - reverts all migrations;
  • version - prints current db version;
  • set_version [version] - sets db version without running migrations.

Example

You need to create database pg_migrations_example before running the example.

> cd example

> psql -c "CREATE DATABASE pg_migrations_example"
CREATE DATABASE

> go run *.go init
version is 0

> go run *.go version
version is 0

> go run *.go
creating table my_table...
adding id column...
seeding my_table...
migrated from version 0 to 4

> go run *.go version
version is 4

> go run *.go reset
truncating my_table...
dropping id column...
dropping table my_table...
migrated from version 4 to 0

> go run *.go up 2
creating table my_table...
adding id column...
migrated from version 0 to 2

> go run *.go
seeding my_table...
migrated from version 2 to 4

> go run *.go down
truncating my_table...
migrated from version 4 to 3

> go run *.go version
version is 3

> go run *.go set_version 1
migrated from version 3 to 1

> go run *.go create add email to users
created migration 5_add_email_to_users.go

Registering Migrations

migrations.RegisterTx and migrations.MustRegisterTx

Registers migrations to be executed inside transactions.

migrations.Register and migrations.MustRegister

Registers migrations to be executed without any transaction.

SQL migrations

SQL migrations are automatically picked up if placed in the same folder with main.go or Go migrations. SQL migrations may be manually registered using DiscoverSQLMigrations (from OS directory) or DiscoverSQLMigrationsFromFilesystem. It may be used with new go 1.16 embedding feature. Example:

//go:embed migrations/*.sql
var migrations embed.FS

collection := migrations.NewCollection()
collection.DiscoverSQLMigrationsFromFilesystem(http.FS(migrations), "migrations")

SQL migrations must have one of the following extensions:

  • .up.sql - up migration;
  • .down.sql - down migration;
  • .tx.up.sql - transactional up migration;
  • .tx.down.sql - transactional down migration.

By default SQL migrations are executed as single PostgreSQL statement. --gopg:split directive can be used to split migration into several statements:

SET statement_timeout = 60000;
SET lock_timeout = 60000;

--gopg:split

CREATE INDEX CONCURRENTLY ...;

Transactions

By 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.

Global Transactions

var oldVersion, newVersion int64

err := db.RunInTransaction(func(tx *pg.Tx) (err error) {
    oldVersion, newVersion, err = migrations.Run(tx, flag.Args()...)
    return
})
if err != nil {
    exitf(err.Error())
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].