gpt4 book ai didi

mysql - 如何将迁移与可执行文件绑定(bind)

转载 作者:IT王子 更新时间:2023-10-29 01:38:28 26 4
gpt4 key购买 nike

我有一个使用 goose 的 Go 项目用于 Mysql 迁移。我想将迁移绑定(bind)到包可执行文件,以便可以独立于任何系统部署和使用可执行文件,类似于 JAVA 项目中的 JAR 文件。

Go 中是否有等效项来完成此操作?

最佳答案

如何获取可以迁移数据库和工作的单个文件

  1. 安装

    go get -u github.com/pressly/goose/cmd/goose
  2. 制作应用。我基于 examplemain.go 并添加了 run 选项。假设您的项目位于 github.com/user/project:

    package main

    import (
    "database/sql"
    "flag"
    "log"
    "os"

    "github.com/pressly/goose"

    // Init DB drivers. -- here I recommend remove unnecessary - but it's up to you
    _ "github.com/go-sql-driver/mysql"
    _ "github.com/lib/pq"
    _ "github.com/mattn/go-sqlite3"
    _ "github.com/ziutek/mymysql/godrv"

    // here our migrations will live -- use your path
    _ "github.com/user/project/migrations"
    )

    var (
    flags = flag.NewFlagSet("goose", flag.ExitOnError)
    dir = flags.String("dir", ".", "directory with migration files")
    )

    func main() {
    flags.Usage = usage
    flags.Parse(os.Args[1:])

    args := flags.Args()

    //////
    if len(args) > 1 && args[0] == "run" {
    log.Printf("PROGRAM RUN\n") //
    .....
    os.Exit(0)
    }


    if len(args) > 1 && args[0] == "create" {
    if err := goose.Run("create", nil, *dir, args[1:]...); err != nil {
    log.Fatalf("goose run: %v", err)
    }
    return
    }

    if len(args) < 3 {
    flags.Usage()
    return
    }

    if args[0] == "-h" || args[0] == "--help" {
    flags.Usage()
    return
    }

    driver, dbstring, command := args[0], args[1], args[2]

    switch driver {
    case "postgres", "mysql", "sqlite3", "redshift":
    if err := goose.SetDialect(driver); err != nil {
    log.Fatal(err)
    }
    default:
    log.Fatalf("%q driver not supported\n", driver)
    }

    switch dbstring {
    case "":
    log.Fatalf("-dbstring=%q not supported\n", dbstring)
    default:
    }

    if driver == "redshift" {
    driver = "postgres"
    }

    db, err := sql.Open(driver, dbstring)
    if err != nil {
    log.Fatalf("-dbstring=%q: %v\n", dbstring, err)
    }

    arguments := []string{}
    if len(args) > 3 {
    arguments = append(arguments, args[3:]...)
    }

    if err := goose.Run(command, db, *dir, arguments...); err != nil {
    log.Fatalf("goose run: %v", err)
    }
    }

    func usage() {
    log.Print(usagePrefix)
    flags.PrintDefaults()
    log.Print(usageCommands)
    }

    var (
    usagePrefix = `Usage: goose [OPTIONS] DRIVER DBSTRING COMMAND
    Drivers:
    postgres
    mysql
    sqlite3
    redshift
    Examples:
    goose sqlite3 ./foo.db status
    goose sqlite3 ./foo.db create init sql
    goose sqlite3 ./foo.db create add_some_column sql
    goose sqlite3 ./foo.db create fetch_user_data go
    goose sqlite3 ./foo.db up
    goose postgres "user=postgres dbname=postgres sslmode=disable" status
    goose mysql "user:password@/dbname?parseTime=true" status
    goose redshift "postgres://user:password@qwerty.us-east-1.redshift.amazonaws.com:5439/db"
    status
    Options:
    `

    usageCommands = `
    Commands:
    up Migrate the DB to the most recent version available
    up-to VERSION Migrate the DB to a specific VERSION
    down Roll back the version by 1
    down-to VERSION Roll back to a specific VERSION
    redo Re-run the latest migration
    status Dump the migration status for the current DB
    version Print the current version of the database
    create NAME [sql|go] Creates new migration file with next version
    `
    )
  3. 创建迁移文件夹:

    mkdir migrations && cd migrations
  4. 创建第一个迁移。我们将使用 go 风格的迁移:

    goose mysql "user:password@/dbname?parseTime=true" create init go

    您将获得一个包含 Go 代码的文件 00001_init.go。迁移作为 SQL 命令在其中烘焙。只需根据需要编辑它们即可。

  5. 然后转到主文件夹并构建应用程序:

    cd ..
    go build -v -o myapp *.go
  6. 您将获得一个文件 myapp,其中包含所有迁移。要检查将它移动到其他地方,例如 /tmp 文件夹,然后从那里运行:

    ./myapp mysql "user:password@/dbname?parseTime=true" status
  7. 运行您的应用:

    ./myapp run

结果

您有一个文件可以用作迁移工具,也可以用作工作应用程序本身。所有迁移都是内置的。在源代码中,它们存储在子包 migrations 中 - 因此很容易编辑。

关于mysql - 如何将迁移与可执行文件绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47030199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com