在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):golang-migrate/migrate开源软件地址(OpenSource Url):https://github.com/golang-migrate/migrate开源编程语言(OpenSource Language):Go 98.8%开源软件介绍(OpenSource Introduction):migrateDatabase migrations written in Go. Use as CLI or import as library.
Forked from mattes/migrate DatabasesDatabase drivers run migrations. Add a new database?
Database URLsDatabase connection strings are specified via URLs. The URL format is driver dependent but generally has the form: Any reserved URL characters need to be escaped. Note, the Explicitly, the following characters need to be escaped:
It's easiest to always run the URL parts of your DB connection URL (e.g. username, password, etc) through an URL encoder. See the example Python snippets below: $ 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
$ Migration SourcesSource drivers read migrations from local or remote sources. Add a new source?
CLI usage
Basic usage$ migrate -source file://path/to/migrations -database postgres://localhost:5432/database up 2 Docker usage$ docker run -v {{ migration dir }}:/migrations --network host migrate/migrate
-path=/migrations/ -database postgres://localhost:5432/database up 2 Use in your Go project
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)
} Want to use an existing database client? 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
} Getting startedGo to getting started Tutorials(more tutorials to come) Migration filesEach migration has an up and down migration. Why? 1481574547_create_users_table.up.sql
1481574547_create_users_table.down.sql Best practices: How to write migrations. Versions
Development and ContributingYes, please! Also have a look at the FAQ. Looking for alternatives? https://awesome-go.com/#database. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论