目录
引言
代码内集成的方式相对来说更加灵活,如果业务上需要在代码中进行数据库的维护作用,可以选择该方法。
测试postgre
1.启动数据库
采用postgresSQL作为示例
进入postgres 容器内运行
psql -h localhost -U postgres -w -c "create database pig;" 创建pig数据库。
2.创建migrations
|
3.目录树
000001 是某次修改的版本号,因为本次是第二次生成sql 脚本文件,create_books_tabel 前编号是000002
当前目录:
golang project 目录文件
migrations 子级目录:
•000002_create_books_table.down.sql 回滚本次操作的sql脚本
•000002_create_books_table.up.sql 创建books表的sql脚本
代码文件示例:
000002_create_books_table.up.sql
|
000002_create_books_table.down.sql
|
4.运行go-migrations程序
go app 是main.go 示例如下:
|
4.测试结果
参数说明
1.postgres示例
postgres://user:password@host:port/dbname?query
(postgresql://
works, too)
URL Query | WithInstance Config | Description |
---|---|---|
x-migrations-table | MigrationsTable | Name of the migrations table |
x-migrations-table-quoted | MigrationsTableQuoted | By default, migrate quotes the migration table for SQL injection safety reasons. This option disable quoting and naively checks that you have quoted the migration table name. e.g. "my_schema"."schema_migrations" |
x-statement-timeout | StatementTimeout | Abort any statement that takes more than the specified number of milliseconds |
x-multi-statement | MultiStatementEnabled | Enable multi-statement execution (default: false) |
x-multi-statement-max-size | MultiStatementMaxSize | Maximum size of single statement in bytes (default: 10MB) |
dbname | DatabaseName | The name of the database to connect to |
search_path | This variable specifies the order in which schemas are searched when an object is referenced by a simple name with no schema specified. | |
user | The user to sign in as | |
password | The user's password | |
host | The host to connect to. Values that start with / are for unix domain sockets. (default is localhost) | |
port | The port to bind to. (default is 5432) | |
fallback_application_name | An application_name to fall back to if one isn't provided. | |
connect_timeout | Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely. | |
sslcert | Cert file location. The file must contain PEM encoded data. | |
sslkey | Key file location. The file must contain PEM encoded data. | |
sslrootcert | The location of the root certificate file. The file must contain PEM encoded data. | |
sslmode | Whether or not to use SSL (disable|require|verify-ca|verify-full) |
golang-migrate 其他功能示例
1.index
- Variables
- func FilterCustomQuery(u *nurl.URL) *nurl.URL
- type ErrDirty
- type ErrShortLimit
- type Logger
- type Migrate
-
- func New(sourceUrl, databaseUrl string) (*Migrate, error)
- func NewWithDatabaseInstance(sourceUrl string, databaseName string, databaseInstance database.Driver) (*Migrate, error)
- func NewWithInstance(sourceName string, sourceInstance source.Driver, databaseName string, ...) (*Migrate, error)
- func NewWithSourceInstance(sourceName string, sourceInstance source.Driver, databaseUrl string) (*Migrate, error)
-
- func (m *Migrate) Close() (source error, database error)
- func (m *Migrate) Down() error
- func (m *Migrate) Drop() error
- func (m *Migrate) Force(version int) error
- func (m *Migrate) Migrate(version uint) error
- func (m *Migrate) Run(migration ...*Migration) error
- func (m *Migrate) Steps(n int) error
- func (m *Migrate) Up() error
- func (m *Migrate) Version() (version uint, dirty bool, err error)
- type Migration
- type MultiError
2.functions
常用函数示例:
func New(sourceUrl, databaseUrl string) (*Migrate, error) New 从源 URL 和数据库 URL 返回一个新的 Migrate 实例。
func (m *Migrate) Version() (version uint, dirty bool, err error) 版本返回当前活动的迁移版本。
func (m *Migrate) Up() error Up 查看当前活动的迁移版本,并将一直向上迁移(应用所有向上迁移)。
func (m *Migrate) Down() error Down 查看当前活动的迁移版本,并将一直向下迁移(应用所有向下迁移)。
func (m *Migrate) Steps(n int) error Steps 查看当前活动的迁移版本。如果 n > 0,它将向上迁移,如果 n < 0,它将向下迁移。
注意事项
1.命名规范
create_
以create_
为前缀的,是创建数据表脚本。
addcolumn
以add_column_
为前缀的,是向已经创建的数据表添加字段,修改数据表时使用。
addindex
以add_index_
为前缀的,是向已经创建的数据表添加索引,修改数据表时使用。
dropcolumn
以drop_column_
为前缀的,是向已经创建的数据表删除字段,修改数据表时使用。
dropindex
以drop_index_
为前缀的,是向已经创建的数据表删除索引,修改数据表时使用。
renamecolumn
以rename_column_
为前缀的,是向已经创建的数据表重命名字段,修改数据表时使用。
renameindex
以rename_index_
为前缀的,是向已经创建的数据表重命名索引,修改数据表时使用。
参考文章
1.migrate/database/postgres at master · golang-migrate/migrate · GitHub
2.migrate package - github.com/golang-migrate/migrate - pkg.go.dev