在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:rubenv/sql-migrate开源软件地址:https://github.com/rubenv/sql-migrate开源编程语言:Go 97.5%开源软件介绍:sql-migrateUsing modl? Check out modl-migrate. Features
InstallationTo install the library and command line program, use the following: go get -v github.com/rubenv/sql-migrate/... UsageAs a standalone tool
Each command requires a configuration file (which defaults to development:
dialect: sqlite3
datasource: test.db
dir: migrations/sqlite3
production:
dialect: postgres
datasource: dbname=myapp sslmode=disable
dir: migrations/postgres
table: migrations (See more examples for different set ups here) Also one can obtain env variables in datasource field via production:
dialect: postgres
datasource: host=prodhost dbname=proddb user=${DB_USER} password=${DB_PASSWORD} sslmode=required
dir: migrations
table: migrations The The environment that will be used can be specified with the Use the
The The The Use the $ sql-migrate status
+---------------+-----------------------------------------+
| MIGRATION | APPLIED |
+---------------+-----------------------------------------+
| 1_initial.sql | 2014-09-13 08:19:06.788354925 +0000 UTC |
| 2_record.sql | no |
+---------------+-----------------------------------------+ Running Test IntegrationsYou can see how to run setups for different setups by executing the # Run mysql-env.sh example (you need to be in the project root directory)
./test-integration/mysql-env.sh MySQL CaveatIf you are using MySQL, you must append production:
dialect: mysql
datasource: root@/dbname?parseTime=true
dir: migrations/mysql
table: migrations See here for more information. Oracle (oci8)Oracle Driver is oci8, it is not pure Go code and relies on Oracle Office Client (Instant Client), more detailed information is in the oci8 repo. Install with Oracle supportTo install the library and command line program, use the following: go get -tags oracle -v github.com/rubenv/sql-migrate/... development:
dialect: oci8
datasource: user/password@localhost:1521/sid
dir: migrations/oracle
table: migrations Oracle (godror)Oracle Driver is godror, it is not pure Go code and relies on Oracle Office Client (Instant Client), more detailed information is in the godror repository. Install with Oracle supportTo install the library and command line program, use the following:
go get -tags godror -v github.com/rubenv/sql-migrate/...
wget https://download.oracle.com/otn_software/mac/instantclient/193000/instantclient-basic-macos.x64-19.3.0.0.0dbru.zip
development:
dialect: godror
datasource: user/password@localhost:1521/sid
dir: migrations/oracle
table: migrations As a libraryImport sql-migrate into your application: import "github.com/rubenv/sql-migrate" Set up a source of migrations, this can be from memory, from a set of files, from bindata (more on that later), or from any library that implements // Hardcoded strings in memory:
migrations := &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
&migrate.Migration{
Id: "123",
Up: []string{"CREATE TABLE people (id int)"},
Down: []string{"DROP TABLE people"},
},
},
}
// OR: Read migrations from a folder:
migrations := &migrate.FileMigrationSource{
Dir: "db/migrations",
}
// OR: Use migrations from a packr box
migrations := &migrate.PackrMigrationSource{
Box: packr.New("migrations", "./migrations"),
}
// OR: Use pkger which implements `http.FileSystem`
migrationSource := &migrate.HttpFileSystemMigrationSource{
FileSystem: pkger.Dir("/db/migrations"),
}
// OR: Use migrations from bindata:
migrations := &migrate.AssetMigrationSource{
Asset: Asset,
AssetDir: AssetDir,
Dir: "migrations",
}
// OR: Read migrations from a `http.FileSystem`
migrationSource := &migrate.HttpFileSystemMigrationSource{
FileSystem: httpFS,
} Then use the db, err := sql.Open("sqlite3", filename)
if err != nil {
// Handle errors!
}
n, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up)
if err != nil {
// Handle errors!
}
fmt.Printf("Applied %d migrations!\n", n) Note that Check the GoDoc reference for the full documentation. Writing migrationsMigrations are defined in SQL files, which contain a set of SQL statements. Special comments are used to distinguish up and down migrations. -- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE people (id int);
-- +migrate Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE people; You can put multiple statements in each block, as long as you end them with a semicolon ( You can alternatively set up a separator string that matches an entire line by setting If you have complex statements which contain semicolons, use -- +migrate Up
CREATE TABLE people (id int);
-- +migrate StatementBegin
CREATE OR REPLACE FUNCTION do_something()
returns void AS $$
DECLARE
create_query text;
BEGIN
-- Do something here
END;
$$
language plpgsql;
-- +migrate StatementEnd
-- +migrate Down
DROP FUNCTION do_something();
DROP TABLE people; The order in which migrations are applied is defined through the filename: sql-migrate will sort migrations based on their name. It's recommended to use an increasing version number or a timestamp as the first part of the filename. Normally each migration is run within a transaction in order to guarantee that it is fully atomic. However some SQL commands (for example creating an index concurrently in PostgreSQL) cannot be executed inside a transaction. In order to execute such a command in a migration, the migration can be run using the -- +migrate Up notransaction
CREATE UNIQUE INDEX CONCURRENTLY people_unique_id_idx ON people (id);
-- +migrate Down
DROP INDEX people_unique_id_idx; packrEmbedding migrations withIf you like your Go applications self-contained (that is: a single binary): use packr to embed the migration files. Just write your migration files as usual, as a set of SQL files in a folder. Import the packr package into your application: import "github.com/gobuffalo/packr/v2" Use the migrations := &migrate.PackrMigrationSource{
Box: packr.New("migrations", "./migrations"),
} If you already have a box and would like to use a subdirectory: migrations := &migrate.PackrMigrationSource{
Box: myBox,
Dir: "./migrations",
} bindataEmbedding migrations withAs an alternative, but slightly less maintained, you can use bindata to embed the migration files. Just write your migration files as usual, as a set of SQL files in a folder. Then use bindata to generate a go-bindata -pkg myapp -o bindata.go db/migrations/ The resulting Use the migrations := &migrate.AssetMigrationSource{
Asset: Asset,
AssetDir: AssetDir,
Dir: "db/migrations",
} Both Then proceed as usual.
Embedding migrations with libraries that implement |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论