Goで書かれたデータベースの移行。CLI として使用するか、ライブラリとしてインポートします。
マットからフォーク/移行
データベース ドライバーは移行を実行します。新しいデータベースを追加しますか?
データベース接続文字列は、URL で指定します。URL 形式はドライバーに依存しますが、通常は次の形式になります。
dbdriver://username:password@host:port/dbname?param1=true¶m2=false
予約済みの URL 文字はすべてエスケープする必要があります。文字もエスケープする必要があることに注意してください
%
明示的には、次の文字をエスケープする必要があります: , , , ,
!
#
$
%
&
'
(
)
*
+
,
/
:
;
=
?
@
[
]
DB接続URLのURL部分(ユーザー名、パスワードなど)をURLエンコーダを介して常に実行するのが最も簡単です。以下の Python スニペットの例を参照してください。
$ python3 -c 'import urllib.parse; print(urllib.parse.quote(input("String to encode: "), ""))'
String to encode: FAKEpassword!#$%&'()*+,/:;=?@[]
FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D
$ python2 -c 'import urllib; print urllib.quote(raw_input("String to encode: "), "")'
String to encode: FAKEpassword!#$%&'()*+,/:;=?@[]
FAKEpassword%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D
$
ソースドライバーは、ローカルまたはリモートのソースから移行を読み取ります。新しいソースを追加しますか?
$ migrate -source file://path/to/migrations -database postgres://localhost:5432/database up 2
$ docker run -v {{ migration dir }}:/migrations --network host migrate/migrate
-path=/migrations/ -database postgres://localhost:5432/database up 2
GracefulStop chan bool
io.Reader
import (
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/github"
)
func main() {
m, err := migrate.New(
"github://mattes:personal-access-token@mattes/migrate_test",
"postgres://localhost:5432/database?sslmode=enable")
m.Steps(2)
}
既存のデータベースクライアントを使用したいですか?
import (
"database/sql"
_ "github.com/lib/pq"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
func main() {
db, err := sql.Open("postgres", "postgres://localhost:5432/database?sslmode=enable")
driver, err := postgres.WithInstance(db, &postgres.Config{})
m, err := migrate.NewWithDatabaseInstance(
"file:///migrations",
"postgres", driver)
m.Up() // or m.Step(2) if you want to explicitly set the number of migrations to run
}
はじめに
(今後のチュートリアル)
各移行には、上下の移行があります。なぜでしょうか。
1481574547_create_users_table.up.sql
1481574547_create_users_table.down.sql
バージョン | サポート。 | 輸入 | 筆記 |
---|---|---|---|
主人 | import "github.com/golang-migrate/migrate/v4" |
新機能とバグ修正が最初にここに到着 | |
v4の | import "github.com/golang-migrate/migrate/v4" |
安定版リリースに使用 | |
v3の |
import "github.com/golang-migrate/migrate"(パッケージマネージャを使用)または(推奨されません) import "gopkg.in/golang-migrate/migrate.v3" |
使用しない - サポートされなくなりました |
はいお願いします!メイクファイルは
あなたの友人です、開発ガイドを読んでください。
FAQもご覧ください。
代替案をお探しですか?https://awesome-go.com/#database。