Compare commits

...

2 Commits
v1 ... master

Author SHA1 Message Date
8d8170acc9 Fix: update to go version 1.23 and shorten format too 2024-10-29 16:14:22 +01:00
9140c8ca4c Upgrade to use gorm.io 2024-05-06 14:26:31 +02:00
18 changed files with 119 additions and 82 deletions

View File

@ -28,19 +28,19 @@ jobs:
script: script:
- go get -t -v ./... - go get -t -v ./...
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w
-X 'git.webz.asia/go-migration/gorm-goose/version.BuildVersion=`git tag|tail -n1`' -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildVersion=`git tag|tail -n1`'
-X 'git.webz.asia/go-migration/gorm-goose/version.BuildHash=`git rev-parse HEAD`' -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildHash=`git rev-parse HEAD`'
-X 'git.webz.asia/go-migration/gorm-goose/version.BuildTime=`date`'" -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildTime=`date`'"
-o ./gorm-goose-x64.linux ./cmd/gorm-goose/ -o ./gorm-goose-x64.linux ./cmd/gorm-goose/
- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w - CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w
-X 'git.webz.asia/go-migration/gorm-goose/version.BuildVersion=`git tag|tail -n1`' -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildVersion=`git tag|tail -n1`'
-X 'git.webz.asia/go-migration/gorm-goose/version.BuildHash=`git rev-parse HEAD`' -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildHash=`git rev-parse HEAD`'
-X 'git.webz.asia/go-migration/gorm-goose/version.BuildTime=`date`'" -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildTime=`date`'"
-o ./gorm-goose-x64.macos ./cmd/gorm-goose/ -o ./gorm-goose-x64.macos ./cmd/gorm-goose/
- CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w - CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w
-X 'git.webz.asia/go-migration/gorm-goose/version.BuildVersion=`git tag|tail -n1`' -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildVersion=`git tag|tail -n1`'
-X 'git.webz.asia/go-migration/gorm-goose/version.BuildHash=`git rev-parse HEAD`' -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildHash=`git rev-parse HEAD`'
-X 'git.webz.asia/go-migration/gorm-goose/version.BuildTime=`date`'" -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildTime=`date`'"
-o ./gorm-goose-x64.exe ./cmd/gorm-goose/ -o ./gorm-goose-x64.exe ./cmd/gorm-goose/
deploy: deploy:
provider: releases provider: releases

View File

@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier same "printed page" as the copyright notice for easier
identification within third-party archives. identification within third-party archives.
Copyright 2020 Alex aka mailoman <alex@webz.asia> Copyright 2020-now Alex aka mailoman <alex@webz.asia>
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
[![Go Report Card](https://goreportcard.com/badge/git.webz.asia/go-migration/gorm-goose)](https://goreportcard.com/report/git.webz.asia/go-migration/gorm-goose) [![Go Report Card](https://goreportcard.com/badge/git.webz.asia/go-migration/gorm-goose)](https://goreportcard.com/report/git.webz.asia/go-migration/gorm-goose)
[![Actions Status](https://git.webz.asia/go-migration/gorm-goose/workflows/Go/badge.svg)](https://git.webz.asia/go-migration/gorm-goose/actions) [![Actions Status](https://git.webz.asia/go-migration/gorm-goose/v2/workflows/Go/badge.svg)](https://git.webz.asia/go-migration/gorm-goose/v2/actions)
[![Build Status](https://travis-ci.org/go-migration/gorm-goose.svg?branch=master)](https://travis-ci.org/go-migration/gorm-goose) [![Build Status](https://travis-ci.org/go-migration/gorm-goose.svg?branch=master)](https://travis-ci.org/go-migration/gorm-goose)
[![codecov](https://codecov.io/gh/go-migration/gorm-goose/branch/master/graph/badge.svg)](https://codecov.io/gh/go-migration/gorm-goose) [![codecov](https://codecov.io/gh/go-migration/gorm-goose/branch/master/graph/badge.svg)](https://codecov.io/gh/go-migration/gorm-goose)
@ -14,11 +14,11 @@ You can manage your database's evolution by creating incremental SQL or Go scrip
## Install ## Install
$ go get git.webz.asia/go-migration/gorm-goose/cmd/gorm-goose $ go get git.webz.asia/go-migration/gorm-goose/v2/cmd/gorm-goose
This will install the `gorm-goose` binary to your `$GOPATH/bin` directory. This will install the `gorm-goose` binary to your `$GOPATH/bin` directory.
You can also build gorm-goose into your own applications by importing `git.webz.asia/go-migration/gorm-goose/lib/gorm-goose`. You can also build gorm-goose into your own applications by importing `git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose`.
## Usage ## Usage
@ -215,7 +215,7 @@ These instructions assume that you're using [Keith Rarick's Heroku Go buildpack]
// note: need at least one blank line after build constraint // note: need at least one blank line after build constraint
package main package main
import _ "git.webz.asia/go-migration/gorm-goose/cmd/gorm-goose" import _ "git.webz.asia/go-migration/gorm-goose/v2/cmd/gorm-goose"
``` ```
[Set up your Heroku database(s) as usual.](https://devcenter.heroku.com/articles/heroku-postgresql) [Set up your Heroku database(s) as usual.](https://devcenter.heroku.com/articles/heroku-postgresql)

View File

@ -28,7 +28,7 @@ import (
"path/filepath" "path/filepath"
"time" "time"
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose"
) )
var createCmd = &Command{ var createCmd = &Command{

View File

@ -25,7 +25,7 @@ import (
"fmt" "fmt"
"log" "log"
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose"
) )
var dbVersionCmd = &Command{ var dbVersionCmd = &Command{

View File

@ -24,7 +24,7 @@ package main
import ( import (
"log" "log"
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose"
) )
var downCmd = &Command{ var downCmd = &Command{

View File

@ -24,7 +24,7 @@ package main
import ( import (
"log" "log"
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose"
) )
var redoCmd = &Command{ var redoCmd = &Command{

View File

@ -27,8 +27,9 @@ import (
"path/filepath" "path/filepath"
"time" "time"
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" "gorm.io/gorm"
"github.com/jinzhu/gorm"
goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose"
) )
var statusCmd = &Command{ var statusCmd = &Command{
@ -63,7 +64,6 @@ func statusRun(cmd *Command, args ...string) {
if e != nil { if e != nil {
log.Fatal("couldn't open DB:", e) log.Fatal("couldn't open DB:", e)
} }
defer db.Close()
// must ensure that the version table exists if we're running on a pristine DB // must ensure that the version table exists if we're running on a pristine DB
if _, e := goose.EnsureDBVersion(conf, db); e != nil { if _, e := goose.EnsureDBVersion(conf, db); e != nil {
@ -82,7 +82,7 @@ func printMigrationStatus(db *gorm.DB, version int64, script string) {
row := goose.MigrationRecord{} row := goose.MigrationRecord{}
result := db.Where("version_id = ?", version).Order("t_stamp desc").First(&row) result := db.Where("version_id = ?", version).Order("t_stamp desc").First(&row)
if result.Error != nil && !result.RecordNotFound() { if result.Error != nil && result.RowsAffected == 1 {
log.Fatal(result.Error) log.Fatal(result.Error)
} }

View File

@ -24,7 +24,7 @@ package main
import ( import (
"log" "log"
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose"
) )
var upCmd = &Command{ var upCmd = &Command{

View File

@ -28,8 +28,8 @@ import (
"strings" "strings"
"text/template" "text/template"
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose"
"git.webz.asia/go-migration/gorm-goose/version" "git.webz.asia/go-migration/gorm-goose/v2/version"
) )
// global options. available to any subcommands. // global options. available to any subcommands.

View File

@ -3,7 +3,7 @@ package migrations
import ( import (
"fmt" "fmt"
"github.com/jinzhu/gorm" "gorm.io/gorm"
) )
func Up_20130106222315(txn *gorm.DB) { func Up_20130106222315(txn *gorm.DB) {

21
go.mod
View File

@ -1,8 +1,23 @@
module git.webz.asia/go-migration/gorm-goose module git.webz.asia/go-migration/gorm-goose/v2
go 1.13 go 1.23
require ( require (
github.com/jinzhu/gorm v1.9.12
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28 github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28
gorm.io/driver/mysql v1.5.6
gorm.io/driver/postgres v1.5.7
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.10
)
require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v2.0.1+incompatible // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
) )

63
go.sum
View File

@ -1,32 +1,43 @@
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q= github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28 h1:mkl3tvPHIuPaWsLtmHTybJeoVEW7cbePK73Ir8VtruA= github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28 h1:mkl3tvPHIuPaWsLtmHTybJeoVEW7cbePK73Ir8VtruA=
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28/go.mod h1:T/T7jsxVqf9k/zYOqbgNAsANsjxTd1Yq3htjDhQ1H0c= github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28/go.mod h1:T/T7jsxVqf9k/zYOqbgNAsANsjxTd1Yq3htjDhQ1H0c=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw= github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM=
gorm.io/driver/postgres v1.5.7/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=

View File

@ -26,8 +26,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/jinzhu/gorm"
"github.com/kylelemons/go-gypsy/yaml" "github.com/kylelemons/go-gypsy/yaml"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
) )
// DBDriver encapsulates the info needed to work with // DBDriver encapsulates the info needed to work with
@ -98,14 +101,14 @@ func newDBDriver(name, open string) DBDriver {
switch name { switch name {
case "postgres": case "postgres":
d.Import = "github.com/jinzhu/gorm/dialects/postgres" d.Import = "gorm.io/driver/postgres"
case "mysql": case "mysql":
d.Import = "github.com/jinzhu/gorm/dialects/mysql" d.Import = "gorm.io/driver/mysql"
d.OpenStr = d.OpenStr + "?charset=utf8&parseTime=True&loc=Local" d.OpenStr = d.OpenStr + "?charset=utf8&parseTime=True&loc=Local"
case "sqlite3": case "sqlite3":
d.Import = "github.com/jinzhu/gorm/dialects/sqlite" d.Import = "gorm.io/driver/sqlite"
} }
return d return d
@ -120,18 +123,31 @@ func (drv *DBDriver) IsValid() bool {
// the newly opened DB based on the given DBConf. // the newly opened DB based on the given DBConf.
// //
// Callers must Close() the returned DB. // Callers must Close() the returned DB.
func OpenDBFromDBConf(conf *DBConf) (*gorm.DB, error) { func OpenDBFromDBConf(conf *DBConf) (db *gorm.DB, err error) {
db, err := gorm.Open(conf.Driver.Name, conf.Driver.OpenStr) switch conf.Driver.Name {
case "postgres":
db, err = gorm.Open(postgres.Open(conf.Driver.OpenStr))
case "mysql":
db, err = gorm.Open(mysql.Open(conf.Driver.OpenStr))
case "sqlite3":
db, err = gorm.Open(sqlite.Open(conf.Driver.OpenStr))
default:
return nil, fmt.Errorf("unknown database driver: %s", conf.Driver.Name)
}
if err != nil { if err != nil {
return nil, err return
} }
// if a postgres schema has been specified, apply it // if a postgres schema has been specified, apply it
if conf.Driver.Name == "postgres" && conf.PgSchema != "" { if conf.Driver.Name == "postgres" && conf.PgSchema != "" {
if err := db.Exec("SET search_path TO " + conf.PgSchema).Error; err != nil { if err = db.Exec("SET search_path TO " + conf.PgSchema).Error; err != nil {
return nil, err return
} }
} }
return db, nil return
} }

View File

@ -33,10 +33,10 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/jinzhu/gorm" _ "gorm.io/driver/mysql"
_ "github.com/jinzhu/gorm/dialects/mysql" _ "gorm.io/driver/postgres"
_ "github.com/jinzhu/gorm/dialects/postgres" _ "gorm.io/driver/sqlite"
_ "github.com/jinzhu/gorm/dialects/sqlite" "gorm.io/gorm"
) )
var ( var (
@ -75,7 +75,6 @@ func RunMigrations(conf *DBConf, migrationsDir string, target int64) (err error)
if err != nil { if err != nil {
return err return err
} }
defer db.Close()
return RunMigrationsOnDb(conf, migrationsDir, target, db) return RunMigrationsOnDb(conf, migrationsDir, target, db)
} }
@ -187,7 +186,9 @@ func (ms migrationSorter) Sort(direction bool) {
} }
// look for migration scripts with names in the form: // look for migration scripts with names in the form:
// XXX_descriptivename.ext //
// XXX_descriptivename.ext
//
// where XXX specifies the version number // where XXX specifies the version number
// and ext specifies the type of migration // and ext specifies the type of migration
func NumericComponent(name string) (int64, error) { func NumericComponent(name string) (int64, error) {
@ -261,7 +262,7 @@ func createVersionTable(conf *DBConf, db *gorm.DB) error {
return txn.Error return txn.Error
} }
if err := txn.CreateTable(&MigrationRecord{}).Error; err != nil { if err := txn.Migrator().CreateTable(&MigrationRecord{}); err != nil {
txn.Rollback() txn.Rollback()
return err return err
} }
@ -283,7 +284,6 @@ func GetDBVersion(conf *DBConf) (version int64, err error) {
if err != nil { if err != nil {
return -1, err return -1, err
} }
defer db.Close()
version, err = EnsureDBVersion(conf, db) version, err = EnsureDBVersion(conf, db)
if err != nil { if err != nil {
@ -398,7 +398,7 @@ var goMigrationTemplate = template.Must(template.New("goose.go-migration").Parse
package main package main
import ( import (
"github.com/jinzhu/gorm" "gorm.io/gorm"
) )
// Up is executed when this migration is applied // Up is executed when this migration is applied

View File

@ -22,7 +22,7 @@
package gormgoose package gormgoose
import ( import (
"github.com/jinzhu/gorm" "gorm.io/gorm"
) )
func MigrateFromDir(con *gorm.DB, migrationsDir string) error { func MigrateFromDir(con *gorm.DB, migrationsDir string) error {

View File

@ -41,13 +41,11 @@ type templateData struct {
Func string Func string
} }
//
// Run a .go migration. // Run a .go migration.
// //
// In order to do this, we copy a modified version of the // In order to do this, we copy a modified version of the
// original .go migration, and execute it via `go run` along // original .go migration, and execute it via `go run` along
// with a main() of our own creation. // with a main() of our own creation.
//
func runGoMigration(conf *DBConf, path string, version int64, direction bool) error { func runGoMigration(conf *DBConf, path string, version int64, direction bool) error {
// everything gets written to a temp dir, and zapped afterwards // everything gets written to a temp dir, and zapped afterwards
@ -104,11 +102,9 @@ func runGoMigration(conf *DBConf, path string, version int64, direction bool) er
return nil return nil
} }
//
// template for the main entry point to a go-based migration. // template for the main entry point to a go-based migration.
// this gets linked against the substituted versions of the user-supplied // this gets linked against the substituted versions of the user-supplied
// scripts in order to execute a migration via `go run` // scripts in order to execute a migration via `go run`
//
var goMigrationDriverTemplate = template.Must(template.New("goose.go-driver").Parse(` var goMigrationDriverTemplate = template.Must(template.New("goose.go-driver").Parse(`
package main package main
@ -118,7 +114,7 @@ import (
"encoding/gob" "encoding/gob"
_ "{{.Import}}" _ "{{.Import}}"
goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose"
) )
func main() { func main() {
@ -133,7 +129,6 @@ func main() {
if err != nil { if err != nil {
log.Fatal("failed to open DB:", err) log.Fatal("failed to open DB:", err)
} }
defer db.Close()
txn := db.Begin() txn := db.Begin()
if txn.Error != nil { if txn.Error != nil {

View File

@ -30,7 +30,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/jinzhu/gorm" "gorm.io/gorm"
) )
const sqlCmdPrefix = "-- +goose " const sqlCmdPrefix = "-- +goose "