mirror of
https://github.com/muun/recovery.git
synced 2025-11-11 06:20:16 -05:00
Release v0.3.0
This commit is contained in:
14
vendor/gopkg.in/gormigrate.v1/.editorconfig
generated
vendored
Normal file
14
vendor/gopkg.in/gormigrate.v1/.editorconfig
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
# http://editorconfig.org/
|
||||
|
||||
root = true
|
||||
|
||||
[*]
|
||||
insert_final_newline = true
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.go]
|
||||
indent_style = tab
|
||||
indent_size = 8
|
||||
1
vendor/gopkg.in/gormigrate.v1/.gitattributes
generated
vendored
Normal file
1
vendor/gopkg.in/gormigrate.v1/.gitattributes
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
* text=auto
|
||||
29
vendor/gopkg.in/gormigrate.v1/.gitignore
generated
vendored
Normal file
29
vendor/gopkg.in/gormigrate.v1/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
# ---> Go
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
|
||||
*.sqlite3
|
||||
/bin
|
||||
/.env
|
||||
4
vendor/gopkg.in/gormigrate.v1/.sample.env
generated
vendored
Normal file
4
vendor/gopkg.in/gormigrate.v1/.sample.env
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
SQLITE_CONN_STRING=:memory:
|
||||
PG_CONN_STRING="user=postgres dbname=gormigrate_test sslmode=disable"
|
||||
MYSQL_CONN_STRING="root:@/gormigrate_test?multiStatements=true"
|
||||
SQLSERVER_CONN_STRING="server=localhost\\SQLExpress;database=gormigrate_test;user id=sa;password=sqlserver;encrypt=disable"
|
||||
28
vendor/gopkg.in/gormigrate.v1/.travis.yml
generated
vendored
Normal file
28
vendor/gopkg.in/gormigrate.v1/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
|
||||
services:
|
||||
- postgresql
|
||||
- mysql
|
||||
|
||||
addons:
|
||||
postgresql: '9.4'
|
||||
|
||||
before_script:
|
||||
- mysql -e 'CREATE DATABASE gormigrate_test;'
|
||||
- psql -c 'CREATE DATABASE gormigrate_test;' -U postgres
|
||||
|
||||
install:
|
||||
- curl -s https://raw.githubusercontent.com/go-task/task/master/install-task.sh | sh
|
||||
- bin/task dl-deps
|
||||
- bin/task go-get-all
|
||||
- cp .sample.env .env
|
||||
|
||||
script:
|
||||
- bin/task lint
|
||||
- bin/task test-sqlite
|
||||
- bin/task test-pg
|
||||
- bin/task test-mysql
|
||||
57
vendor/gopkg.in/gormigrate.v1/CHANGELOG.md
generated
vendored
Normal file
57
vendor/gopkg.in/gormigrate.v1/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
# Changelog
|
||||
|
||||
## v1.6.0 - 2019-07-07
|
||||
|
||||
- Add option to return an error if the database have unknown migrations
|
||||
(defaults to `false`)
|
||||
([#37](https://github.com/go-gormigrate/gormigrate/pull/37)).
|
||||
|
||||
## v1.5.0 - 2019-04-29
|
||||
|
||||
- Fixed and written tests for transaction handling
|
||||
([#34](https://github.com/go-gormigrate/gormigrate/pull/34), [#10](https://github.com/go-gormigrate/gormigrate/issues/10)).
|
||||
Enabling transation is recommend, but only supported for databases that
|
||||
support DDL transactions (PostgreSQL, Microsoft SQL Server and SQLite).
|
||||
- Making the code more safe by checking more errors
|
||||
([#35](https://github.com/go-gormigrate/gormigrate/pull/35)).
|
||||
|
||||
## v1.4.0 - 2019-02-03
|
||||
|
||||
- Allow an empty migration list if a `InitSchema` function is defined
|
||||
([#28](https://github.com/go-gormigrate/gormigrate/pull/28)).
|
||||
|
||||
## v1.3.1 - 2019-01-26
|
||||
|
||||
- Fixed `testify` import path from `gopkg.in/stretchr/testify.v1` to
|
||||
`github.com/stretchr/testify` ([#27](https://github.com/go-gormigrate/gormigrate/pull/27)).
|
||||
|
||||
## v1.3.0 - 2018-12-02
|
||||
|
||||
- Starting from this release, this package is available as a [Go Module](https://github.com/golang/go/wiki/Modules).
|
||||
Import path is still `gopkg.in/gormigrate.v1` in this major version, but will
|
||||
change to `github.com/go-gormigrate/gormigrate/v2` in the next major release;
|
||||
- Validate the ID exists on the migration list (#20, #21).
|
||||
|
||||
## v1.2.1 - 2018-09-07
|
||||
|
||||
- An empty migration list is not allowed anymore. Please, make sure that you
|
||||
have at least one migration, even if dummy;
|
||||
- Added `MigrateTo` and `RollbackTo` methods (#15);
|
||||
- CI now runs tests for SQLite, PostgreSQL, MySQL and Microsoft SQL Server.
|
||||
|
||||
## v1.2.0 - 2018-07-12
|
||||
|
||||
- Add `IDColumnSize` options, which defaults to `255` (#7);
|
||||
|
||||
## v1.1.4 - 2018-05-06
|
||||
|
||||
- Assuming default options if blank;
|
||||
- Returning an error if the migration list has a duplicated migration ID.
|
||||
|
||||
## v1.1.3 - 2018-02-25
|
||||
|
||||
- Fix `RollbackLast` (#4).
|
||||
|
||||
---
|
||||
|
||||
Sorry, we don't have changelog for older releases 😢.
|
||||
6
vendor/gopkg.in/gormigrate.v1/Dockerfile
generated
vendored
Normal file
6
vendor/gopkg.in/gormigrate.v1/Dockerfile
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
FROM golang:1.12.4
|
||||
|
||||
WORKDIR /gormigrate
|
||||
COPY . .
|
||||
|
||||
RUN go mod download
|
||||
8
vendor/gopkg.in/gormigrate.v1/LICENSE
generated
vendored
Normal file
8
vendor/gopkg.in/gormigrate.v1/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
MIT License
|
||||
Copyright (c) 2016 Andrey Nering
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
200
vendor/gopkg.in/gormigrate.v1/README.md
generated
vendored
Normal file
200
vendor/gopkg.in/gormigrate.v1/README.md
generated
vendored
Normal file
@@ -0,0 +1,200 @@
|
||||
# Gormigrate
|
||||
|
||||
[](https://godoc.org/gopkg.in/gormigrate.v1)
|
||||
[](https://goreportcard.com/report/gopkg.in/gormigrate.v1)
|
||||
[](https://travis-ci.org/go-gormigrate/gormigrate)
|
||||
[](https://ci.appveyor.com/project/andreynering/gormigrate)
|
||||
|
||||
Gormigrate is a minimalistic migration helper for [Gorm][gorm].
|
||||
Gorm already has useful [migrate functions][gormmigrate], just misses
|
||||
proper schema versioning and migration rollback support.
|
||||
|
||||
## Supported databases
|
||||
|
||||
It supports any of the [databases Gorm supports][gormdatabases]:
|
||||
|
||||
- PostgreSQL
|
||||
- MySQL
|
||||
- SQLite
|
||||
- Microsoft SQL Server
|
||||
|
||||
## Installing
|
||||
|
||||
```bash
|
||||
go get -u gopkg.in/gormigrate.v1
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"gopkg.in/gormigrate.v1"
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db, err := gorm.Open("sqlite3", "mydb.sqlite3")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
db.LogMode(true)
|
||||
|
||||
m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
|
||||
// create persons table
|
||||
{
|
||||
ID: "201608301400",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
// it's a good pratice to copy the struct inside the function,
|
||||
// so side effects are prevented if the original struct changes during the time
|
||||
type Person struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
}
|
||||
return tx.AutoMigrate(&Person{}).Error
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
return tx.DropTable("people").Error
|
||||
},
|
||||
},
|
||||
// add age column to persons
|
||||
{
|
||||
ID: "201608301415",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
// when table already exists, it just adds fields as columns
|
||||
type Person struct {
|
||||
Age int
|
||||
}
|
||||
return tx.AutoMigrate(&Person{}).Error
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
return tx.Table("people").DropColumn("age").Error
|
||||
},
|
||||
},
|
||||
// add pets table
|
||||
{
|
||||
ID: "201608301430",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
type Pet struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
PersonID int
|
||||
}
|
||||
return tx.AutoMigrate(&Pet{}).Error
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
return tx.DropTable("pets").Error
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if err = m.Migrate(); err != nil {
|
||||
log.Fatalf("Could not migrate: %v", err)
|
||||
}
|
||||
log.Printf("Migration did run successfully")
|
||||
}
|
||||
```
|
||||
|
||||
## Having a separated function for initializing the schema
|
||||
|
||||
If you have a lot of migrations, it can be a pain to run all them, as example,
|
||||
when you are deploying a new instance of the app, in a clean database.
|
||||
To prevent this, you can set a function that will run if no migration was run
|
||||
before (in a new clean database). Remember to create everything here, all tables,
|
||||
foreign keys and what more you need in your app.
|
||||
|
||||
```go
|
||||
type Person struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
type Pet struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
PersonID int
|
||||
}
|
||||
|
||||
m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
|
||||
// you migrations here
|
||||
})
|
||||
|
||||
m.InitSchema(func(tx *gorm.DB) error {
|
||||
err := tx.AutoMigrate(
|
||||
&Person{},
|
||||
&Pet{},
|
||||
// all other tables of you app
|
||||
).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Model(Pet{}).AddForeignKey("person_id", "people (id)", "RESTRICT", "RESTRICT").Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// all other foreign keys...
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
This is the options struct, in case you don't want the defaults:
|
||||
|
||||
```go
|
||||
type Options struct {
|
||||
// TableName is the migration table.
|
||||
TableName string
|
||||
// IDColumnName is the name of column where the migration id will be stored.
|
||||
IDColumnName string
|
||||
// IDColumnSize is the length of the migration id column
|
||||
IDColumnSize int
|
||||
// UseTransaction makes Gormigrate execute migrations inside a single transaction.
|
||||
// Keep in mind that not all databases support DDL commands inside transactions.
|
||||
UseTransaction bool
|
||||
// ValidateUnknownMigrations will cause migrate to fail if there's unknown migration
|
||||
// IDs in the database
|
||||
ValidateUnknownMigrations bool
|
||||
}
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
To run tests, first copy `.sample.env` as `sample.env` and edit the connection
|
||||
string of the database you want to run tests against. Then, run tests like
|
||||
below:
|
||||
|
||||
```bash
|
||||
# running tests for PostgreSQL
|
||||
go test -tags postgresql
|
||||
|
||||
# running test for MySQL
|
||||
go test -tags mysql
|
||||
|
||||
# running tests for SQLite
|
||||
go test -tags sqlite
|
||||
|
||||
# running tests for SQL Server
|
||||
go test -tags sqlserver
|
||||
|
||||
# running test for multiple databases at once
|
||||
go test -tags 'sqlite postgresql mysql'
|
||||
```
|
||||
|
||||
Or altenatively, you could use Docker to easily run tests on all databases
|
||||
at once. To do that, make sure Docker is installed and running in your machine
|
||||
and then run:
|
||||
|
||||
```bash
|
||||
task docker
|
||||
```
|
||||
|
||||
[gorm]: http://gorm.io/
|
||||
[gormmigrate]: http://doc.gorm.io/database.html#migration
|
||||
[gormdatabases]: http://doc.gorm.io/database.html#connecting-to-a-database
|
||||
60
vendor/gopkg.in/gormigrate.v1/Taskfile.yml
generated
vendored
Normal file
60
vendor/gopkg.in/gormigrate.v1/Taskfile.yml
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
# https://taskfile.org
|
||||
|
||||
version: '2'
|
||||
|
||||
tasks:
|
||||
dl-deps:
|
||||
desc: Downloads cli dependencies
|
||||
cmds:
|
||||
- go get -u golang.org/x/lint/golint
|
||||
|
||||
go-get-all:
|
||||
cmds:
|
||||
- go get -t -tags 'sqlite postgresql mysql sqlserver' ./...
|
||||
|
||||
lint:
|
||||
desc: Runs golint on this project
|
||||
cmds:
|
||||
- golint .
|
||||
|
||||
test-sqlite:
|
||||
desc: Run tests for SQLite
|
||||
cmds:
|
||||
- task: test
|
||||
vars: {DATABASE: sqlite}
|
||||
|
||||
test-pg:
|
||||
desc: Run tests for PostgreSQL
|
||||
cmds:
|
||||
- task: test
|
||||
vars: {DATABASE: postgresql}
|
||||
|
||||
test-mysql:
|
||||
desc: Run tests for MySQL
|
||||
cmds:
|
||||
- task: test
|
||||
vars: {DATABASE: mysql}
|
||||
|
||||
test-sqlserver:
|
||||
desc: Run tests for Microsoft SQL Server
|
||||
cmds:
|
||||
- task: test
|
||||
vars: {DATABASE: sqlserver}
|
||||
|
||||
test:
|
||||
cmds:
|
||||
- go test -v -tags {{.DATABASE}}
|
||||
|
||||
docker:
|
||||
cmds:
|
||||
- task: docker:build
|
||||
- task: docker:test
|
||||
|
||||
docker:build:
|
||||
cmds:
|
||||
- docker build -t gormigrate .
|
||||
|
||||
docker:test:
|
||||
cmds:
|
||||
- docker-compose down -v
|
||||
- docker-compose run gormigrate go test -v -tags 'postgresql sqlite mysql sqlserver'
|
||||
51
vendor/gopkg.in/gormigrate.v1/appveyor.yml
generated
vendored
Normal file
51
vendor/gopkg.in/gormigrate.v1/appveyor.yml
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
version: '{build}'
|
||||
|
||||
clone_folder: C:\GOPATH\src\gopkg.in\gormigrate.v1
|
||||
|
||||
build: false
|
||||
deploy: false
|
||||
|
||||
services:
|
||||
- postgresql96
|
||||
- mysql
|
||||
- mssql2017
|
||||
|
||||
environment:
|
||||
POSTGRES_PATH: C:\Program Files\PostgreSQL\9.6
|
||||
PGUSER: postgres
|
||||
PGPASSWORD: Password12!
|
||||
PG_CONN_STRING: 'user=postgres password=Password12! dbname=gormigrate_test sslmode=disable'
|
||||
|
||||
MYSQL_PATH: C:\Program Files\MySql\MySQL Server 5.7
|
||||
MYSQL_PWD: Password12!
|
||||
MYSQL_CONN_STRING: 'root:Password12!@/gormigrate_test?multiStatements=true'
|
||||
|
||||
SQLITE_CONN_STRING: 'testdb.sqlite3'
|
||||
|
||||
SQLSERVER_CONN_STRING: 'server=localhost;database=gormigrate_test;user id=sa;password=Password12!;encrypt=disable'
|
||||
|
||||
MINGW_PATH: C:\MinGW
|
||||
|
||||
GOPATH: C:\GOPATH
|
||||
GOVERSION: 1.12.4
|
||||
|
||||
install:
|
||||
- SET PATH=%POSTGRES_PATH%\bin;%MYSQL_PATH%\bin;%MINGW_PATH%\bin;%PATH%
|
||||
|
||||
- rmdir C:\go /s /q
|
||||
- appveyor DownloadFile https://storage.googleapis.com/golang/go%GOVERSION%.windows-386.msi
|
||||
- msiexec /i go%GOVERSION%.windows-386.msi /q
|
||||
- go version
|
||||
|
||||
build_script:
|
||||
- createdb gormigrate_test
|
||||
- mysql -e "CREATE DATABASE gormigrate_test;" --user=root
|
||||
- sqlcmd -S localhost,1433 -U sa -P Password12! -Q "CREATE DATABASE gormigrate_test" -d "master"
|
||||
|
||||
test_script:
|
||||
- go get -t -tags "sqlite postgresql mysql sqlserver" ./...
|
||||
- go install -v ./...
|
||||
- go test -v -tags postgresql
|
||||
- go test -v -tags mysql
|
||||
- go test -v -tags sqlserver
|
||||
- go test -v -tags sqlite
|
||||
68
vendor/gopkg.in/gormigrate.v1/doc.go
generated
vendored
Normal file
68
vendor/gopkg.in/gormigrate.v1/doc.go
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
// Package gormigrate is a migration helper for Gorm (http://jinzhu.me/gorm/).
|
||||
// Gorm already have useful migrate functions
|
||||
// (http://jinzhu.me/gorm/database.html#migration), just misses
|
||||
// proper schema versioning and rollback cababilities.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// package main
|
||||
//
|
||||
// import (
|
||||
// "log"
|
||||
//
|
||||
// "github.com/go-gormigrate/gormigrate"
|
||||
// "github.com/jinzhu/gorm"
|
||||
// _ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||
// )
|
||||
//
|
||||
// type Person struct {
|
||||
// gorm.Model
|
||||
// Name string
|
||||
// }
|
||||
//
|
||||
// type Pet struct {
|
||||
// gorm.Model
|
||||
// Name string
|
||||
// PersonID int
|
||||
// }
|
||||
//
|
||||
// func main() {
|
||||
// db, err := gorm.Open("sqlite3", "mydb.sqlite3")
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// if err = db.DB().Ping(); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
//
|
||||
// db.LogMode(true)
|
||||
//
|
||||
// m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
|
||||
// {
|
||||
// ID: "201608301400",
|
||||
// Migrate: func(tx *gorm.DB) error {
|
||||
// return tx.AutoMigrate(&Person{}).Error
|
||||
// },
|
||||
// Rollback: func(tx *gorm.DB) error {
|
||||
// return tx.DropTable("people").Error
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// ID: "201608301430",
|
||||
// Migrate: func(tx *gorm.DB) error {
|
||||
// return tx.AutoMigrate(&Pet{}).Error
|
||||
// },
|
||||
// Rollback: func(tx *gorm.DB) error {
|
||||
// return tx.DropTable("pets").Error
|
||||
// },
|
||||
// },
|
||||
// })
|
||||
//
|
||||
// err = m.Migrate()
|
||||
// if err == nil {
|
||||
// log.Printf("Migration did run successfully")
|
||||
// } else {
|
||||
// log.Printf("Could not migrate: %v", err)
|
||||
// }
|
||||
// }
|
||||
package gormigrate
|
||||
35
vendor/gopkg.in/gormigrate.v1/docker-compose.yml
generated
vendored
Normal file
35
vendor/gopkg.in/gormigrate.v1/docker-compose.yml
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
gormigrate:
|
||||
image: gormigrate
|
||||
depends_on:
|
||||
- postgresql
|
||||
- mysql
|
||||
- sqlserver
|
||||
environment:
|
||||
PGPASSWORD: postgres
|
||||
PG_CONN_STRING: host=postgresql user=postgres dbname=gormigrate_test port=5432 sslmode=disable
|
||||
|
||||
MYSQL_CONN_STRING: root:mysql@tcp(mysql)/gormigrate_test?multiStatements=true
|
||||
|
||||
SQLSERVER_CONN_STRING: server=sqlserver;database=master;user id=sa;password=SQL@1server;encrypt=disable
|
||||
|
||||
postgresql:
|
||||
image: postgres:11.2
|
||||
environment:
|
||||
POSTGRES_DB: gormigrate_test
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
|
||||
mysql:
|
||||
image: mysql:5.7
|
||||
environment:
|
||||
MYSQL_DATABASE: gormigrate_test
|
||||
MYSQL_ROOT_PASSWORD: mysql
|
||||
|
||||
sqlserver:
|
||||
image: mcr.microsoft.com/mssql/server:2017-latest-ubuntu
|
||||
environment:
|
||||
ACCEPT_EULA: 'Y'
|
||||
SA_PASSWORD: SQL@1server
|
||||
19
vendor/gopkg.in/gormigrate.v1/go.mod
generated
vendored
Normal file
19
vendor/gopkg.in/gormigrate.v1/go.mod
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
module gopkg.in/gormigrate.v1
|
||||
|
||||
require (
|
||||
cloud.google.com/go v0.33.1 // indirect
|
||||
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f // indirect
|
||||
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
|
||||
github.com/go-sql-driver/mysql v1.4.1 // indirect
|
||||
github.com/gofrs/uuid v3.2.0+incompatible // indirect
|
||||
github.com/google/go-cmp v0.2.0 // indirect
|
||||
github.com/jinzhu/gorm v1.9.2
|
||||
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
|
||||
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 // indirect
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/lib/pq v1.0.0 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.10.0 // indirect
|
||||
github.com/stretchr/testify v1.3.0
|
||||
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869 // indirect
|
||||
google.golang.org/appengine v1.3.0 // indirect
|
||||
)
|
||||
38
vendor/gopkg.in/gormigrate.v1/go.sum
generated
vendored
Normal file
38
vendor/gopkg.in/gormigrate.v1/go.sum
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
cloud.google.com/go v0.33.1 h1:fmJQWZ1w9PGkHR1YL/P7HloDvqlmKQ4Vpb7PC2e+aCk=
|
||||
cloud.google.com/go v0.33.1/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f h1:WH0w/R4Yoey+04HhFxqZ6VX6I0d7RMyw5aXQ9UTvQPs=
|
||||
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f/go.mod h1:xN/JuLBIz4bjkxNmByTiV1IbhfnYb6oo99phBn4Eqhc=
|
||||
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
|
||||
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
|
||||
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
|
||||
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
|
||||
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/jinzhu/gorm v1.9.2 h1:lCvgEaqe/HVE+tjAR2mt4HbbHAZsQOv3XAZiEZV37iw=
|
||||
github.com/jinzhu/gorm v1.9.2/go.mod h1:Vla75njaFJ8clLU1W44h34PjIkijhjHIYnZxMqCdxqo=
|
||||
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a h1:eeaG9XMUvRBYXJi4pg1ZKM7nxc5AfXfojeLLW7O5J3k=
|
||||
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 h1:xvj06l8iSwiWpYgm8MbPp+naBg+pwfqmdXabzqPCn/8=
|
||||
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3/go.mod h1:oHTiXerJ20+SfYcrdlBO7rzZRJWGwSTQ0iUY2jI6Gfc=
|
||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
|
||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
|
||||
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869 h1:kkXA53yGe04D0adEYJwEVQjeBppL01Exg+fnMjfUraU=
|
||||
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
google.golang.org/appengine v1.3.0 h1:FBSsiFRMz3LBeXIomRnVzrQwSDj4ibvcRexLG0LZGQk=
|
||||
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
465
vendor/gopkg.in/gormigrate.v1/gormigrate.go
generated
vendored
Normal file
465
vendor/gopkg.in/gormigrate.v1/gormigrate.go
generated
vendored
Normal file
@@ -0,0 +1,465 @@
|
||||
package gormigrate
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
initSchemaMigrationID = "SCHEMA_INIT"
|
||||
)
|
||||
|
||||
// MigrateFunc is the func signature for migrating.
|
||||
type MigrateFunc func(*gorm.DB) error
|
||||
|
||||
// RollbackFunc is the func signature for rollbacking.
|
||||
type RollbackFunc func(*gorm.DB) error
|
||||
|
||||
// InitSchemaFunc is the func signature for initializing the schema.
|
||||
type InitSchemaFunc func(*gorm.DB) error
|
||||
|
||||
// Options define options for all migrations.
|
||||
type Options struct {
|
||||
// TableName is the migration table.
|
||||
TableName string
|
||||
// IDColumnName is the name of column where the migration id will be stored.
|
||||
IDColumnName string
|
||||
// IDColumnSize is the length of the migration id column
|
||||
IDColumnSize int
|
||||
// UseTransaction makes Gormigrate execute migrations inside a single transaction.
|
||||
// Keep in mind that not all databases support DDL commands inside transactions.
|
||||
UseTransaction bool
|
||||
// ValidateUnknownMigrations will cause migrate to fail if there's unknown migration
|
||||
// IDs in the database
|
||||
ValidateUnknownMigrations bool
|
||||
}
|
||||
|
||||
// Migration represents a database migration (a modification to be made on the database).
|
||||
type Migration struct {
|
||||
// ID is the migration identifier. Usually a timestamp like "201601021504".
|
||||
ID string
|
||||
// Migrate is a function that will br executed while running this migration.
|
||||
Migrate MigrateFunc
|
||||
// Rollback will be executed on rollback. Can be nil.
|
||||
Rollback RollbackFunc
|
||||
}
|
||||
|
||||
// Gormigrate represents a collection of all migrations of a database schema.
|
||||
type Gormigrate struct {
|
||||
db *gorm.DB
|
||||
tx *gorm.DB
|
||||
options *Options
|
||||
migrations []*Migration
|
||||
initSchema InitSchemaFunc
|
||||
}
|
||||
|
||||
// ReservedIDError is returned when a migration is using a reserved ID
|
||||
type ReservedIDError struct {
|
||||
ID string
|
||||
}
|
||||
|
||||
func (e *ReservedIDError) Error() string {
|
||||
return fmt.Sprintf(`gormigrate: Reserved migration ID: "%s"`, e.ID)
|
||||
}
|
||||
|
||||
// DuplicatedIDError is returned when more than one migration have the same ID
|
||||
type DuplicatedIDError struct {
|
||||
ID string
|
||||
}
|
||||
|
||||
func (e *DuplicatedIDError) Error() string {
|
||||
return fmt.Sprintf(`gormigrate: Duplicated migration ID: "%s"`, e.ID)
|
||||
}
|
||||
|
||||
var (
|
||||
// DefaultOptions can be used if you don't want to think about options.
|
||||
DefaultOptions = &Options{
|
||||
TableName: "migrations",
|
||||
IDColumnName: "id",
|
||||
IDColumnSize: 255,
|
||||
UseTransaction: false,
|
||||
ValidateUnknownMigrations: false,
|
||||
}
|
||||
|
||||
// ErrRollbackImpossible is returned when trying to rollback a migration
|
||||
// that has no rollback function.
|
||||
ErrRollbackImpossible = errors.New("gormigrate: It's impossible to rollback this migration")
|
||||
|
||||
// ErrNoMigrationDefined is returned when no migration is defined.
|
||||
ErrNoMigrationDefined = errors.New("gormigrate: No migration defined")
|
||||
|
||||
// ErrMissingID is returned when the ID od migration is equal to ""
|
||||
ErrMissingID = errors.New("gormigrate: Missing ID in migration")
|
||||
|
||||
// ErrNoRunMigration is returned when any run migration was found while
|
||||
// running RollbackLast
|
||||
ErrNoRunMigration = errors.New("gormigrate: Could not find last run migration")
|
||||
|
||||
// ErrMigrationIDDoesNotExist is returned when migrating or rolling back to a migration ID that
|
||||
// does not exist in the list of migrations
|
||||
ErrMigrationIDDoesNotExist = errors.New("gormigrate: Tried to migrate to an ID that doesn't exist")
|
||||
|
||||
// ErrUnknownPastMigration is returned if a migration exists in the DB that doesn't exist in the code
|
||||
ErrUnknownPastMigration = errors.New("gormigrate: Found migration in DB that does not exist in code")
|
||||
)
|
||||
|
||||
// New returns a new Gormigrate.
|
||||
func New(db *gorm.DB, options *Options, migrations []*Migration) *Gormigrate {
|
||||
if options.TableName == "" {
|
||||
options.TableName = DefaultOptions.TableName
|
||||
}
|
||||
if options.IDColumnName == "" {
|
||||
options.IDColumnName = DefaultOptions.IDColumnName
|
||||
}
|
||||
if options.IDColumnSize == 0 {
|
||||
options.IDColumnSize = DefaultOptions.IDColumnSize
|
||||
}
|
||||
return &Gormigrate{
|
||||
db: db,
|
||||
options: options,
|
||||
migrations: migrations,
|
||||
}
|
||||
}
|
||||
|
||||
// InitSchema sets a function that is run if no migration is found.
|
||||
// The idea is preventing to run all migrations when a new clean database
|
||||
// is being migrating. In this function you should create all tables and
|
||||
// foreign key necessary to your application.
|
||||
func (g *Gormigrate) InitSchema(initSchema InitSchemaFunc) {
|
||||
g.initSchema = initSchema
|
||||
}
|
||||
|
||||
// Migrate executes all migrations that did not run yet.
|
||||
func (g *Gormigrate) Migrate() error {
|
||||
if !g.hasMigrations() {
|
||||
return ErrNoMigrationDefined
|
||||
}
|
||||
var targetMigrationID string
|
||||
if len(g.migrations) > 0 {
|
||||
targetMigrationID = g.migrations[len(g.migrations)-1].ID
|
||||
}
|
||||
return g.migrate(targetMigrationID)
|
||||
}
|
||||
|
||||
// MigrateTo executes all migrations that did not run yet up to the migration that matches `migrationID`.
|
||||
func (g *Gormigrate) MigrateTo(migrationID string) error {
|
||||
if err := g.checkIDExist(migrationID); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.migrate(migrationID)
|
||||
}
|
||||
|
||||
func (g *Gormigrate) migrate(migrationID string) error {
|
||||
if !g.hasMigrations() {
|
||||
return ErrNoMigrationDefined
|
||||
}
|
||||
|
||||
if err := g.checkReservedID(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.checkDuplicatedID(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.begin()
|
||||
defer g.rollback()
|
||||
|
||||
if err := g.createMigrationTableIfNotExists(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if g.options.ValidateUnknownMigrations {
|
||||
unknownMigrations, err := g.unknownMigrationsHaveHappened()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if unknownMigrations {
|
||||
return ErrUnknownPastMigration
|
||||
}
|
||||
}
|
||||
|
||||
if g.initSchema != nil {
|
||||
canInitializeSchema, err := g.canInitializeSchema()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if canInitializeSchema {
|
||||
if err := g.runInitSchema(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.commit()
|
||||
}
|
||||
}
|
||||
|
||||
for _, migration := range g.migrations {
|
||||
if err := g.runMigration(migration); err != nil {
|
||||
return err
|
||||
}
|
||||
if migrationID != "" && migration.ID == migrationID {
|
||||
break
|
||||
}
|
||||
}
|
||||
return g.commit()
|
||||
}
|
||||
|
||||
// There are migrations to apply if either there's a defined
|
||||
// initSchema function or if the list of migrations is not empty.
|
||||
func (g *Gormigrate) hasMigrations() bool {
|
||||
return g.initSchema != nil || len(g.migrations) > 0
|
||||
}
|
||||
|
||||
// Check whether any migration is using a reserved ID.
|
||||
// For now there's only have one reserved ID, but there may be more in the future.
|
||||
func (g *Gormigrate) checkReservedID() error {
|
||||
for _, m := range g.migrations {
|
||||
if m.ID == initSchemaMigrationID {
|
||||
return &ReservedIDError{ID: m.ID}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gormigrate) checkDuplicatedID() error {
|
||||
lookup := make(map[string]struct{}, len(g.migrations))
|
||||
for _, m := range g.migrations {
|
||||
if _, ok := lookup[m.ID]; ok {
|
||||
return &DuplicatedIDError{ID: m.ID}
|
||||
}
|
||||
lookup[m.ID] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gormigrate) checkIDExist(migrationID string) error {
|
||||
for _, migrate := range g.migrations {
|
||||
if migrate.ID == migrationID {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return ErrMigrationIDDoesNotExist
|
||||
}
|
||||
|
||||
// RollbackLast undo the last migration
|
||||
func (g *Gormigrate) RollbackLast() error {
|
||||
if len(g.migrations) == 0 {
|
||||
return ErrNoMigrationDefined
|
||||
}
|
||||
|
||||
g.begin()
|
||||
defer g.rollback()
|
||||
|
||||
lastRunMigration, err := g.getLastRunMigration()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.rollbackMigration(lastRunMigration); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.commit()
|
||||
}
|
||||
|
||||
// RollbackTo undoes migrations up to the given migration that matches the `migrationID`.
|
||||
// Migration with the matching `migrationID` is not rolled back.
|
||||
func (g *Gormigrate) RollbackTo(migrationID string) error {
|
||||
if len(g.migrations) == 0 {
|
||||
return ErrNoMigrationDefined
|
||||
}
|
||||
|
||||
if err := g.checkIDExist(migrationID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.begin()
|
||||
defer g.rollback()
|
||||
|
||||
for i := len(g.migrations) - 1; i >= 0; i-- {
|
||||
migration := g.migrations[i]
|
||||
if migration.ID == migrationID {
|
||||
break
|
||||
}
|
||||
migrationRan, err := g.migrationRan(migration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if migrationRan {
|
||||
if err := g.rollbackMigration(migration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return g.commit()
|
||||
}
|
||||
|
||||
func (g *Gormigrate) getLastRunMigration() (*Migration, error) {
|
||||
for i := len(g.migrations) - 1; i >= 0; i-- {
|
||||
migration := g.migrations[i]
|
||||
|
||||
migrationRan, err := g.migrationRan(migration)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if migrationRan {
|
||||
return migration, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrNoRunMigration
|
||||
}
|
||||
|
||||
// RollbackMigration undo a migration.
|
||||
func (g *Gormigrate) RollbackMigration(m *Migration) error {
|
||||
g.begin()
|
||||
defer g.rollback()
|
||||
|
||||
if err := g.rollbackMigration(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.commit()
|
||||
}
|
||||
|
||||
func (g *Gormigrate) rollbackMigration(m *Migration) error {
|
||||
if m.Rollback == nil {
|
||||
return ErrRollbackImpossible
|
||||
}
|
||||
|
||||
if err := m.Rollback(g.tx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sql := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", g.options.TableName, g.options.IDColumnName)
|
||||
return g.tx.Exec(sql, m.ID).Error
|
||||
}
|
||||
|
||||
func (g *Gormigrate) runInitSchema() error {
|
||||
if err := g.initSchema(g.tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.insertMigration(initSchemaMigrationID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, migration := range g.migrations {
|
||||
if err := g.insertMigration(migration.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gormigrate) runMigration(migration *Migration) error {
|
||||
if len(migration.ID) == 0 {
|
||||
return ErrMissingID
|
||||
}
|
||||
|
||||
migrationRan, err := g.migrationRan(migration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !migrationRan {
|
||||
if err := migration.Migrate(g.tx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.insertMigration(migration.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gormigrate) createMigrationTableIfNotExists() error {
|
||||
if g.tx.HasTable(g.options.TableName) {
|
||||
return nil
|
||||
}
|
||||
|
||||
sql := fmt.Sprintf("CREATE TABLE %s (%s VARCHAR(%d) PRIMARY KEY)", g.options.TableName, g.options.IDColumnName, g.options.IDColumnSize)
|
||||
return g.tx.Exec(sql).Error
|
||||
}
|
||||
|
||||
func (g *Gormigrate) migrationRan(m *Migration) (bool, error) {
|
||||
var count int
|
||||
err := g.tx.
|
||||
Table(g.options.TableName).
|
||||
Where(fmt.Sprintf("%s = ?", g.options.IDColumnName), m.ID).
|
||||
Count(&count).
|
||||
Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// The schema can be initialised only if it hasn't been initialised yet
|
||||
// and no other migration has been applied already.
|
||||
func (g *Gormigrate) canInitializeSchema() (bool, error) {
|
||||
migrationRan, err := g.migrationRan(&Migration{ID: initSchemaMigrationID})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if migrationRan {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// If the ID doesn't exist, we also want the list of migrations to be empty
|
||||
var count int
|
||||
err = g.tx.
|
||||
Table(g.options.TableName).
|
||||
Count(&count).
|
||||
Error
|
||||
return count == 0, err
|
||||
}
|
||||
|
||||
func (g *Gormigrate) unknownMigrationsHaveHappened() (bool, error) {
|
||||
sql := fmt.Sprintf("SELECT %s FROM %s", g.options.IDColumnName, g.options.TableName)
|
||||
rows, err := g.tx.Raw(sql).Rows()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
validIDSet := make(map[string]struct{}, len(g.migrations)+1)
|
||||
validIDSet[initSchemaMigrationID] = struct{}{}
|
||||
for _, migration := range g.migrations {
|
||||
validIDSet[migration.ID] = struct{}{}
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var pastMigrationID string
|
||||
if err := rows.Scan(&pastMigrationID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, ok := validIDSet[pastMigrationID]; !ok {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (g *Gormigrate) insertMigration(id string) error {
|
||||
sql := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?)", g.options.TableName, g.options.IDColumnName)
|
||||
return g.tx.Exec(sql, id).Error
|
||||
}
|
||||
|
||||
func (g *Gormigrate) begin() {
|
||||
if g.options.UseTransaction {
|
||||
g.tx = g.db.Begin()
|
||||
} else {
|
||||
g.tx = g.db
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gormigrate) commit() error {
|
||||
if g.options.UseTransaction {
|
||||
return g.tx.Commit().Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gormigrate) rollback() {
|
||||
if g.options.UseTransaction {
|
||||
g.tx.Rollback()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user