8000 GitHub - worldline-go/igmigrator: migration SQL files
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

worldline-go/igmigrator

Repository files navigation

igmigrator

License Coverage GitHub Workflow Status Go Report Card Go PKG

This tool get list of sql files in a folder and apply them with recording last migrationed file's version to remember in future updates as new files comes.

go get github.com/worldline-go/igmigrator/v2

Example testdata/normal folder has 2 file that file names are 1-test.sql and 5-test2.sql. After run the migration tool related migration table record last number which is 5 in our case. So next run folder will check again and apply sql files which is has number bigger than 5.

File names must start with a number and it should have .sql suffix.

Example of correct file names:

1_create_table.sql
002_create_users_table.sql
3-add-user.sql
103alteruser.sql

Without a number start, it will be assumed -1 and it is skipped in DefaultMigrationFileSkipper.


Configuration

The library can be configured through the following parameters:

  • MigrationsDir: provide a directory that will hold the migration files. It can be set via environment variable IGMIGRATOR_MIGRATION_DIR and default value is migrations.
  • Schema: can specify which schema(using set search_path) should be used to run migrations in.
  • MigrationTable: the name of the migration table. It can be set via environment variable IGMIGRATION_MIGRATION_TABLE and default value is migration.

Example Usage

// For demo postgres database
// docker run --rm -it -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres:14.12-alpine
db, err := sqlx.Connect("pgx", "postgres://postgres@localhost:5432/postgres")
if err != nil {
    log.Error().Msgf("migrate database connect: %v", err)
    return
}

defer db.Close()

igmigrator.Migrate(ctx, db, &igmigrator.Config{
    MigrationsDir:  "testdata/normal",
    Schema:         "migration",
    MigrationTable: "test_normal_migration",
})

// Output:
// INF igmigrator.go:136 > current database version version=0
// TRC igmigrator.go:266 > run one migration migrated_to=1 migration_path=testdata/normal/1_install_table.sql
// TRC igmigrator.go:266 > run one migration migrated_to=2 migration_path=testdata/normal/2_install_pos.sql
// TRC igmigrator.go:266 > run one migration migrated_to=3 migration_path=testdata/normal/3_install_test.sql

Embed migrations in binary

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

// /////////////////////////////////////////////////////////////

testMultiFSSub, err := fs.Sub(migrationFS, "migrations")
// check err

igmigrator.Migrate(ctx, db, &igmigrator.Config{
    Migrations:     testMultiFSSub,
    Schema:         "migration",
    Mi
6B87
grationTable: "migration_table",
})

Testing

Unit tests are implemented to cover most of the use cases. Some of them requires to have a postgres database up and running.

make db
# make db-down

If the database is running and reachable on the default (5432), simply run the tests by running the following command in root directory.

make test

Migrate v1 -> v2

v2 checking the path and should be exist if migration table already exist.

-- This SQL statement adds a new column 'path' to the existing table
ALTER TABLE <TABLE> ADD COLUMN path VARCHAR(1000) NOT NULL DEFAULT '/';

-- This SQL statement drops the primary key constraint on the 'version' column, usually named '<TABLE>_pkey'
ALTER TABLE <TABLE> DROP CONSTRAINT IF EXISTS <primary_key_name>;

-- This SQL statement creates a new primary key constraint on both 'path' and 'version' columns
ALTER TABLE <TABLE> ADD PRIMARY KEY (path, version);
0