Skip to content

MongoDB migrations using golang-migrate/migrate library

License

Notifications You must be signed in to change notification settings

taako-502/go-migrate-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Migrate Sample

golang-migrate/migrateを使ったマイグレーションです。

セットアップ

$ openssl rand -base64 756 > replicaset.key
$ chmod 600 replicaset.key
$ chown 999 replicaset.key

mongosh を開く。

docker exec -it go_migrate_mongo_primary bash
mongosh -u root -p root

レプリカセットの初期化

rs.initiate({
  _id: "rs0",
  members: [
    { _id: 0, host: "go_migrate_mongo_primary:27017", priority: 3 },
    { _id: 1, host: "go_migrate_mongo_secondary:27017", priority: 2 },
    { _id: 2, host: "go_migrate_mongo_tertiary:27017", priority: 1 },
    { _id: 3, host: "go_migrate_mongo_arbiter:27017", arbiterOnly: true },
  ],
})

確認用コマンド

rs.status()
rs.conf()

マイグレーション

# 最新化
migrate -path ./migrations -database "mongodb://localhost:27017/testdb?directConnection=true" up

# 1つだけバージョンアップ
migrate -path ./migrations -database "mongodb://localhost:27017/testdb?directConnection=true" up 1

バージョンダウン

migrate -path ./migrations -database "mongodb://localhost:27017/testdb?directConnection=true" down 1

dirty 状態の解消

migrate -path ./migrations -database "mongodb://localhost:27017/testdb?directConnection=true" force 1

参考

https://zenn.dev/ytdrep/articles/217e3aeeadd07c

About

MongoDB migrations using golang-migrate/migrate library

Topics

Resources

License

Stars

Watchers

Forks