栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

Use migrate in Go project(golang-migrate/migrate)

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Use migrate in Go project(golang-migrate/migrate)

目录

         引言

测试postgre

1.启动数据库

2.创建migrations

3.目录树

4.运行go-migrations程序

4.测试结果

参数说明

1.postgres示例

golang-migrate 其他功能示例

1.index

2.functions

注意事项

1.命名规范

参考文章

引言

代码内集成的方式相对来说更加灵活,如果业务上需要在代码中进行数据库的维护作用,可以选择该方法。

测试postgre

1.启动数据库

 采用postgresSQL作为示例

进入postgres 容器内运行 

psql -h localhost -U postgres -w -c "create database pig;" 创建pig数据库。

2.创建migrations

migrate create -ext sql -dir ./migrations -seq create_books_table

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

CREATE TABLE books (

  user_id integer,

  name    varchar(40),

  author  varchar(40)

);

000002_create_books_table.down.sql

DROP TABLE IF EXISTS books;

4.运行go-migrations程序

  go app 是main.go 示例如下:

package main

import (

        "github.com/golang-migrate/migrate/v4"

        _ "github.com/golang-migrate/migrate/v4/database/postgres"

        _ "github.com/golang-migrate/migrate/v4/source/file"

)

func main()  {

        m, err := migrate.New(

                "file:///root/TEST/migrate/migratesql/migrations",

                "postgres://postgres:123456@localhost:5432/pig?sslmode=disable")

        if err != nil {

                log.Fatal(err)

        }

        if err := m.Up(); err != nil {

                log.Fatal(err)

        }

}

4.测试结果

参数说明

1.postgres示例

postgres://user:password@host:port/dbname?query (postgresql:// works, too)

URL Query

WithInstance Config

Description

x-migrations-tableMigrationsTableName of the migrations table
x-migrations-table-quotedMigrationsTableQuotedBy 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-timeoutStatementTimeoutAbort any statement that takes more than the specified number of milliseconds
x-multi-statementMultiStatementEnabledEnable multi-statement execution (default: false)
x-multi-statement-max-sizeMultiStatementMaxSizeMaximum size of single statement in bytes (default: 10MB)
dbnameDatabaseNameThe name of the database to connect to
search_pathThis variable specifies the order in which schemas are searched when an object is referenced by a simple name with no schema specified.
userThe user to sign in as
passwordThe user's password
hostThe host to connect to. Values that start with / are for unix domain sockets. (default is localhost)
portThe port to bind to. (default is 5432)
fallback_application_nameAn application_name to fall back to if one isn't provided.
connect_timeoutMaximum wait for connection, in seconds. Zero or not specified means wait indefinitely.
sslcertCert file location. The file must contain PEM encoded data.
sslkeyKey file location. The file must contain PEM encoded data.
sslrootcertThe location of the root certificate file. The file must contain PEM encoded data.
sslmodeWhether 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
    • func (e ErrDirty) Error() string
  • type ErrShortLimit
    • func (e ErrShortLimit) Error() string
  • 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
    • func NewMigration(body io.ReadCloser, identifier string, version uint, targetVersion int) (*Migration, error)
    • func (m *Migration) Buffer() error
    • func (m *Migration) LogString() string
    • func (m *Migration) String() string
  • type MultiError
    • func NewMultiError(errs ...error) MultiError
    • func (m MultiError) Error() string

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/944122.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号