migrate for Go: complete guide with examples
\n\nThe migrate library is a powerful and flexible tool for managing database migrations in the Go ecosystem. It allows developers to version database schema changes, automatically apply and roll back migrations, and integrate with various DBMS, including PostgreSQL, MySQL, SQLite, MongoDB, CockroachDB, and others. In this article, we will thoroughly examine how to use migrate in Go, starting from installation and ending with advanced techniques.
\n\nWhat it is and why it is needed
\n\nDuring the development of any application that works with a database, sooner or later the need arises to change the table structure: add a new column, create an index, change a data type, or rename a table. If you work in a team or deploy an application across multiple environments (development, staging, production), manually executing SQL scripts becomes a source of errors and inconsistencies.
\n\nThe migrate library solves this problem by providing a simple and declarative way to describe schema changes. Each migration is a pair of files: one for applying changes (up), another for rolling back (down). The library tracks which migrations have already been applied and allows you to safely move forward or backward through the change history.
Main advantages of using migrate:
\n- \n
- Versioning: each migration has a unique number or timestamp, allowing you to know exactly which version of the schema is currently in use. \n
- Reproducibility: you can set up a database from scratch by applying all migrations sequentially. \n
- Safety: the presence of down-migrations allows you to roll back changes in case of an error. \n
- Language independence: migrations are written in pure SQL, making them portable between projects in different languages. \n
Installation
\n\nTo install the migrate library in Go, use the standard go get command. It is recommended to install it as a dependency of your project:
go get -u github.com/golang-migrate/migrate/v4\n\nYou will also need to install a driver for your database. For example, for PostgreSQL:
\n\ngo get -u github.com/golang-migrate/migrate/v4/database/postgres\n\nFor MySQL:
\n\ngo get -u github.com/golang-migrate/migrate/v4/database/mysql\n\nFor SQLite:
\n\ngo get -u github.com/golang-migrate/migrate/v4/database/sqlite3\n\nAdditionally, you will need a driver for the migration source. The file system is most commonly used:
\n\ngo get -u github.com/golang-migrate/migrate/v4/source/file\n\nDrivers for io/fs, github, gitlab, s3, and other sources are also available.
Quick start — minimal working example
\n\nLet's create a simple application that connects to an SQLite database and applies migrations.
\n\nFirst, create the folder structure for migrations:
\n\nmkdir -p migrations\ncd migrations\n\nCreate the first migration. The file name must follow the pattern: {version}_{name}.{direction}.sql. The version is usually a timestamp or a sequential number.
File: migrations/000001_create_users_table.up.sql
\nCREATE TABLE IF NOT EXISTS users (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nFile: migrations/000001_create_users_t