Migrate - 數據庫遷移處理助手

Mar7720 8年前發布 | 78K 次閱讀 數據庫 Go語言 數據庫管理工具

Migrate - 是一個采用Go開發的數據庫遷移處理助手。可以在現有的代碼中使用或通過CLI以命令的方式運行。
GoCode   import github.com/mattes/migrate/migrate
CLI      go get -u github.com/mattes/migrate

特性

  • Super easy to implementDriver interface.
  • Gracefully quit running migrations on ^C .
  • No magic search paths routines, no hard-coded config files.
  • CLI is build on top of the migrate package .
  • </ul>

    可用的驅動 

    • PostgreSQL
    • Cassandra
    • SQLite
    • MySQL ( experimental )
    • Bash (planned)
    • </ul>

      Need another driver? Just implement theDriver interface and open a PR.

      在終端中使用 

      # install
      go get github.com/mattes/migrate
      
      # create new migration file in path
      migrate -url driver://url -path ./migrations create migration_file_xyz
      
      # apply all available migrations
      migrate -url driver://url -path ./migrations up
      
      # roll back all migrations
      migrate -url driver://url -path ./migrations down
      
      # roll back the most recently applied migration, then run it again.
      migrate -url driver://url -path ./migrations redo
      
      # run down and then up command
      migrate -url driver://url -path ./migrations reset
      
      # show the current migration version
      migrate -url driver://url -path ./migrations version
      
      # apply the next n migrations
      migrate -url driver://url -path ./migrations migrate +1
      migrate -url driver://url -path ./migrations migrate +2
      migrate -url driver://url -path ./migrations migrate +n
      
      # roll back the previous n migrations
      migrate -url driver://url -path ./migrations migrate -1
      migrate -url driver://url -path ./migrations migrate -2
      migrate -url driver://url -path ./migrations migrate -n
      
      # go to specific migration
      migrate -url driver://url -path ./migrations goto 1
      migrate -url driver://url -path ./migrations goto 10
      migrate -url driver://url -path ./migrations goto v

      Usage in Go

      See GoDoc here: http://godoc.org/github.com/mattes/migrate/migrate

      import "github.com/mattes/migrate/migrate"
      
      // Import any required drivers so that they are registered and available
      import _ "github.com/mattes/migrate/driver/mysql"
      
      // use synchronous versions of migration functions ...
      allErrors, ok := migrate.UpSync("driver://url", "./path")
      if !ok {
        fmt.Println("Oh no ...")
        // do sth with allErrors slice
      }
      
      // use the asynchronous version of migration functions ...
      pipe := migrate.NewPipe()
      go migrate.Up(pipe, "driver://url", "./path")
      // pipe is basically just a channel
      // write your own channel listener. see writePipe() in main.go as an example.

      Migration files

      The format of migration files looks like this:

      001_initial_plan_to_do_sth.up.sql     # up migration instructions
      001_initial_plan_to_do_sth.down.sql   # down migration instructions
      002_xxx.up.sql
      002_xxx.down.sql
      ...

      Why two files? This way you could still do sth like psql -f ./db/migrations/001_initial_plan_to_do_sth.up.sql and there is no need for any custom markup language to divide up and down migrations. Please note that the filename extension depends on the driver.

      可選項目

官方網站:http://www.baiduhome.net/lib/view/home/1453336642980

 本文由用戶 Mar7720 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!