Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何将迁移与可执行文件绑定_Mysql_Database_Go_Database Migration - Fatal编程技术网

Mysql 如何将迁移与可执行文件绑定

Mysql 如何将迁移与可执行文件绑定,mysql,database,go,database-migration,Mysql,Database,Go,Database Migration,我有一个用于Mysql迁移的Go项目。我希望将迁移绑定到包的可执行文件,以便可以独立于任何系统部署和使用可执行文件,类似于JAVA项目中的JAR文件 Go中是否有类似的工具可以实现这一点?如果您使用Docker,您可以将文件夹放到图像中 如果项目不喜欢或可以提供帮助。如果您已经在使用Goose,一个选项是用GOO而不是SQL编写迁移。根据Goose回购协议中的数据,当您在此处构建Goose二进制文件时,它会将所有*。go文件绑定到二进制文件中 这是我构建示例并删除除二进制文件之外的所有文件后的输

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


Go中是否有类似的工具可以实现这一点?

如果您使用Docker,您可以将文件夹放到图像中


如果项目不喜欢或可以提供帮助。

如果您已经在使用Goose,一个选项是用GOO而不是SQL编写迁移。根据Goose回购协议中的数据,当您在此处构建
Goose
二进制文件时,它会将所有
*。go
文件绑定到二进制文件中

这是我构建示例并删除除二进制文件之外的所有文件后的输出。嵌入了基于Go的迁移:

2017/10/31 11:22:31     Applied At                  Migration
2017/10/31 11:22:31     =======================================
2017/10/31 11:22:31     Mon Jun 19 21:56:00 2017 -- 00002_rename_root.go
如果您希望使用基于SQL的迁移,但不想承担额外的依赖项,则可以将基于SQL的迁移作为字符串常量嵌入到
*.go
文件中,然后从
go migrations
示例开始,将初始化阶段添加到
main.go
,在继续之前将其写入当前目录。

如何获取可以迁移数据库并工作的单个文件
  • 安装

    go get -u github.com/pressly/goose/cmd/goose
    
  • 制作应用程序。我基于示例
    main.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
    `
    )
    
  • 创建第一次迁移。我们将使用
    go
    风格的迁移:

    mkdir migrations && cd migrations
    
    goose mysql "user:password@/dbname?parseTime=true" create init go
    
    您将获得一个带有go代码的文件
    00001_init.go
    。迁移作为SQL命令在其中烘焙。只需根据需要编辑它们

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

    cd ..
    go build -v -o myapp *.go
    
  • 您将得到一个包含所有迁移的文件
    myapp
    。要选中此项,请将其移动到其他位置,例如移动到
    /tmp
    文件夹,然后从那里运行:

    ./myapp mysql "user:password@/dbname?parseTime=true" status
    
  • 运行你的应用程序:

    ./myapp run
    
  • 结果
    您有一个文件,可以用作迁移工具,也可以用作工作应用程序本身。所有的迁移都是建立在它之上的。在源代码中,它们存储在迁移的子包中,因此易于编辑。

    感谢您的提示。我没有用Docker。你能建议如何用go bindata来对付鹅吗?我对它的文档有点迷茫。我看了看鹅。它似乎为构建迁移提供了一种方便的方法。