go-migrate

module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2024 License: Apache-2.0

README

Migration library

Note:

The following directories are reserved and will be cleared before each execution.

  1. {rootProjectDir}/tmp/mongodb
  2. {rootProjectDir}/tmp/mysql
  3. {rootProjectDir}/tmp/postgres

Example:

func main() {
    if err := run(); err != nil {
        os.Exit(1)
    }
}

func run() error {
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    output, cancel, err := logger.NewOutput("")
    if err != nil {
        logger.JsonRawLog("logger: unable to initialize output", loggerenum.FatalLvl, err)
        return err
    }
    defer cancel()

    lgr, cancel, err := logger.NewLogrus(output)
    if err != nil {
        logger.JsonRawLog("logger: unable to initialize logrus", loggerenum.FatalLvl, err)
        return err
    }
    defer cancel()

    migrator, err := migrate.New(ctx, lgr, storage.NewFactory(lgr))
    if err != nil {
        return lgr.Fatal(ctx, errors.New("migrations: failed to init migrate"), logger.Fields{"err": err.Error()})
    }

    if err = migrator.Up(); err != nil {
        return lgr.Fatal(ctx, errors.New("migrations: up: completed with errors"), logger.Fields{"err": err.Error()})
    } else {
        lgr.InfoMsg(ctx, "migrations: up: completed", nil)
    }

    return nil
}

ENV:

MongoDB:
type Config struct {
    MongoMigrationsEnabled    bool   `envconfig:"MONGO_MIGRATIONS_ENABLED" default:"false"`
    MongoHost                 string `envconfig:"MONGO_HOST"`
    MongoPort                 string `envconfig:"MONGO_PORT"`
    MongoLogin                string `envconfig:"MONGO_LOGIN"`
    MongoPassword             string `envconfig:"MONGO_PASSWORD"`
    MongoDatabase             string `envconfig:"MONGO_DATABASE"`
    MongoMigrationsCollection string `envconfig:"MONGO_MIGRATIONS_COLLECTION" default:"migrationVersions"`
    MongoMigrationsDir        string `envconfig:"MONGO_MIGRATIONS_DIR"`
}
MySQL:
type Config struct {
    MySQLMigrationsEnabled bool   `envconfig:"MYSQL_MIGRATIONS_ENABLED" default:"false"`
    MySQLHost              string `envconfig:"MYSQL_HOST"`
    MySQLPort              string `envconfig:"MYSQL_PORT"`
    MySQLUsername          string `envconfig:"MYSQL_LOGIN"`
    MySQLPassword          string `envconfig:"MYSQL_PASSWORD"`
    MySQLDatabase          string `envconfig:"MYSQL_DATABASE"`
    MySQLMigrationsTable   string `envconfig:"MYSQL_MIGRATIONS_TABLE" default:"migration_versions"`
    MySQLMigrationsDir     string `envconfig:"MYSQL_MIGRATIONS_DIR"`
}
PostgreSQL:
type Config struct {
    PostgresMigrationsEnabled bool   `envconfig:"POSTGRES_MIGRATIONS_ENABLED" default:"false"`
    PostgresHost              string `envconfig:"POSTGRES_HOST"`
    PostgresPort              string `envconfig:"POSTGRES_PORT"`
    PostgresUsername          string `envconfig:"POSTGRES_LOGIN"`
    PostgresPassword          string `envconfig:"POSTGRES_PASSWORD"`
    PostgresDatabase          string `envconfig:"POSTGRES_DATABASE"`
    PostgresMigrationsTable   string `envconfig:"POSTGRES_MIGRATIONS_TABLE" default:"migration_versions"`
    PostgresMigrationsDir     string `envconfig:"POSTGRES_MIGRATIONS_DIR"`
}

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL