BASIC
* go lang migration will create a table scheme_migration to track migrations
How It Works in Detail
Versioning
Each migration has a numeric version number (e.g., 000001
, 000002
, etc.). This version number is derived from the prefix in the migration file name.
schema_migrations Table (or Equivalent)
- When you run migrations for the first time, golang-migrate creates a table in your database to track the “current version.”
- By default, this table might be named
schema_migrations
(the name can vary depending on the driver or your configuration).
Tracking Migrations
- Each time a migration is applied, golang-migrate updates the version in
schema_migrations
. - For example, if your highest version is currently
1
, and you have a new migration file with version 2
, golang-migrate will run that migration and update the “current version” to 2
.
Skipping Already-Applied Migrations
- When you call
m.Up()
, golang-migrate compares the latest recorded version in schema_migrations
to the versions of all available .up.sql
files. - Any migration with a version equal to or lower than the recorded version is considered already applied.
- It only runs migrations whose version is higher than what’s recorded in
schema_migrations
.
Result
- If there are no new migrations (i.e., the current version in the database is already equal to the highest version in your migration files),
m.Up()
returns migrate.ErrNoChange
. - If there are pending migrations,
m.Up()
runs them in ascending order and updates the version in the DB after each successful migration.
-----------------------------------------------------------------------------------------
1. Installation
First, install the golang-migrate tool by running the following command:
bash
Copy code
go install github.com/golang-migrate/migrate/v4/cmd/migrate@latest
This will install the migrate command-line tool.
To install the Go module itself, run this in your Go project directory:
bash
Copy code
go get -u github.com/golang-migrate/migrate/v4
2. Creating Migrations
To create a new migration, use the migrate command. Here’s how to create a new migration file:
bash
Copy code
migrate create -ext sql -dir db/migrations create_users_table
Example SQL Migration Files
000001_create_users_table.up.sql
This file contains the SQL commands to apply the migration, such as creating a table.
sql
Copy code
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
000001_create_users_table.down.sql
This file contains the SQL commands to revert the migration, typically by dropping the created table.
sql
Copy code
DROP TABLE users;
USING CODE, so migration is ran checked in build
// If in a different package can use return fmt.Errorf("failed to open file source: %w", err) to return error
// the github packages need to be added go.mod using go get
package main
import (
"fmt"
"log"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
func main() {
// The source includes 'file://'
// The database URL includes your driver scheme (e.g., 'postgres://')
m, err := migrate.New(
"file://db/migrations",
"mysql://user:password@tcp(localhost:3306)/mydb?multiStatements=true",
)
if err != nil {
log.Fatalf("migrate.New failed: %v", err)
}
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
log.Fatalf("m.Up failed: %v", err)
}
fmt.Println("Migrations applied successfully!")
}
------------------
Fix to dirty databse
Cause - migration applied but encountered error so version in correct
error such as there is an error in ur schema etc
Fix : remove table in scheme_migrations, and dete migrated tables
other fixes: