From 9140c8ca4c78da38c939ddaaf81a50358045297a Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 6 May 2024 14:26:31 +0200 Subject: [PATCH] Upgrade to use gorm.io --- .travis.yml | 18 +++--- LICENSE | 2 +- README.md | 8 +-- cmd/gorm-goose/cmd_create.go | 2 +- cmd/gorm-goose/cmd_dbversion.go | 2 +- cmd/gorm-goose/cmd_down.go | 2 +- cmd/gorm-goose/cmd_redo.go | 2 +- cmd/gorm-goose/cmd_status.go | 8 +-- cmd/gorm-goose/cmd_up.go | 2 +- cmd/gorm-goose/main.go | 4 +- .../migrations/20130106222315_and_again.go | 2 +- go.mod | 21 ++++++- go.sum | 63 +++++++++++-------- lib/gorm-goose/dbconf.go | 36 ++++++++--- lib/gorm-goose/migrate.go | 18 +++--- lib/gorm-goose/migrate_dir.go | 2 +- lib/gorm-goose/migration_go.go | 7 +-- lib/gorm-goose/migration_sql.go | 2 +- 18 files changed, 119 insertions(+), 82 deletions(-) diff --git a/.travis.yml b/.travis.yml index dd960b3..0737fce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,19 +28,19 @@ jobs: script: - go get -t -v ./... - 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/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.BuildVersion=`git tag|tail -n1`' + -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildHash=`git rev-parse HEAD`' + -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildTime=`date`'" -o ./gorm-goose-x64.linux ./cmd/gorm-goose/ - 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/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.BuildVersion=`git tag|tail -n1`' + -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildHash=`git rev-parse HEAD`' + -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildTime=`date`'" -o ./gorm-goose-x64.macos ./cmd/gorm-goose/ - 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/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.BuildVersion=`git tag|tail -n1`' + -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildHash=`git rev-parse HEAD`' + -X 'git.webz.asia/go-migration/gorm-goose/v2/version.BuildTime=`date`'" -o ./gorm-goose-x64.exe ./cmd/gorm-goose/ deploy: provider: releases diff --git a/LICENSE b/LICENSE index a8d7dad..5088ffc 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2020 Alex aka mailoman + Copyright 2020-now Alex aka mailoman Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index 4a45895..d2e0010 100644 --- a/README.md +++ b/README.md @@ -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) -[![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) [![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 - $ 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. -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 @@ -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 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) diff --git a/cmd/gorm-goose/cmd_create.go b/cmd/gorm-goose/cmd_create.go index 15b8a8d..75d4259 100644 --- a/cmd/gorm-goose/cmd_create.go +++ b/cmd/gorm-goose/cmd_create.go @@ -28,7 +28,7 @@ import ( "path/filepath" "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{ diff --git a/cmd/gorm-goose/cmd_dbversion.go b/cmd/gorm-goose/cmd_dbversion.go index 88bf1e1..f4b0759 100644 --- a/cmd/gorm-goose/cmd_dbversion.go +++ b/cmd/gorm-goose/cmd_dbversion.go @@ -25,7 +25,7 @@ import ( "fmt" "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{ diff --git a/cmd/gorm-goose/cmd_down.go b/cmd/gorm-goose/cmd_down.go index 494c4b5..554afe5 100644 --- a/cmd/gorm-goose/cmd_down.go +++ b/cmd/gorm-goose/cmd_down.go @@ -24,7 +24,7 @@ package main import ( "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{ diff --git a/cmd/gorm-goose/cmd_redo.go b/cmd/gorm-goose/cmd_redo.go index 7b217b5..1af127c 100644 --- a/cmd/gorm-goose/cmd_redo.go +++ b/cmd/gorm-goose/cmd_redo.go @@ -24,7 +24,7 @@ package main import ( "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{ diff --git a/cmd/gorm-goose/cmd_status.go b/cmd/gorm-goose/cmd_status.go index ff0ea0c..ddb857e 100644 --- a/cmd/gorm-goose/cmd_status.go +++ b/cmd/gorm-goose/cmd_status.go @@ -27,8 +27,9 @@ import ( "path/filepath" "time" - goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" - "github.com/jinzhu/gorm" + "gorm.io/gorm" + + goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose" ) var statusCmd = &Command{ @@ -63,7 +64,6 @@ func statusRun(cmd *Command, args ...string) { if e != nil { 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 if _, e := goose.EnsureDBVersion(conf, db); e != nil { @@ -82,7 +82,7 @@ func printMigrationStatus(db *gorm.DB, version int64, script string) { row := goose.MigrationRecord{} 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) } diff --git a/cmd/gorm-goose/cmd_up.go b/cmd/gorm-goose/cmd_up.go index b26c89f..86bc765 100644 --- a/cmd/gorm-goose/cmd_up.go +++ b/cmd/gorm-goose/cmd_up.go @@ -24,7 +24,7 @@ package main import ( "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{ diff --git a/cmd/gorm-goose/main.go b/cmd/gorm-goose/main.go index aa69538..b51c1b1 100644 --- a/cmd/gorm-goose/main.go +++ b/cmd/gorm-goose/main.go @@ -28,8 +28,8 @@ import ( "strings" "text/template" - goose "git.webz.asia/go-migration/gorm-goose/lib/gorm-goose" - "git.webz.asia/go-migration/gorm-goose/version" + goose "git.webz.asia/go-migration/gorm-goose/v2/lib/gorm-goose" + "git.webz.asia/go-migration/gorm-goose/v2/version" ) // global options. available to any subcommands. diff --git a/db-sample/migrations/20130106222315_and_again.go b/db-sample/migrations/20130106222315_and_again.go index e706321..7580232 100644 --- a/db-sample/migrations/20130106222315_and_again.go +++ b/db-sample/migrations/20130106222315_and_again.go @@ -3,7 +3,7 @@ package migrations import ( "fmt" - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) func Up_20130106222315(txn *gorm.DB) { diff --git a/go.mod b/go.mod index 3309963..a8cf8d3 100644 --- a/go.mod +++ b/go.mod @@ -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.22.2 require ( - github.com/jinzhu/gorm v1.9.12 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 ) diff --git a/go.sum b/go.sum index e2892e4..a463ff2 100644 --- a/go.sum +++ b/go.sum @@ -1,32 +1,43 @@ -github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= -github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= -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/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= -github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q= -github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY= +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/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.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +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/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/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM= -golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +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/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +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= diff --git a/lib/gorm-goose/dbconf.go b/lib/gorm-goose/dbconf.go index e8eead1..ce28091 100644 --- a/lib/gorm-goose/dbconf.go +++ b/lib/gorm-goose/dbconf.go @@ -26,8 +26,11 @@ import ( "os" "path/filepath" - "github.com/jinzhu/gorm" "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 @@ -98,14 +101,14 @@ func newDBDriver(name, open string) DBDriver { switch name { case "postgres": - d.Import = "github.com/jinzhu/gorm/dialects/postgres" + d.Import = "gorm.io/driver/postgres" 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" case "sqlite3": - d.Import = "github.com/jinzhu/gorm/dialects/sqlite" + d.Import = "gorm.io/driver/sqlite" } return d @@ -120,18 +123,31 @@ func (drv *DBDriver) IsValid() bool { // the newly opened DB based on the given DBConf. // // Callers must Close() the returned DB. -func OpenDBFromDBConf(conf *DBConf) (*gorm.DB, error) { - db, err := gorm.Open(conf.Driver.Name, conf.Driver.OpenStr) +func OpenDBFromDBConf(conf *DBConf) (db *gorm.DB, err error) { + 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 { - return nil, err + return } // if a postgres schema has been specified, apply it if conf.Driver.Name == "postgres" && conf.PgSchema != "" { - if err := db.Exec("SET search_path TO " + conf.PgSchema).Error; err != nil { - return nil, err + if err = db.Exec("SET search_path TO " + conf.PgSchema).Error; err != nil { + return } } - return db, nil + return } diff --git a/lib/gorm-goose/migrate.go b/lib/gorm-goose/migrate.go index 361df22..39bd8ee 100644 --- a/lib/gorm-goose/migrate.go +++ b/lib/gorm-goose/migrate.go @@ -33,10 +33,10 @@ import ( "text/template" "time" - "github.com/jinzhu/gorm" - _ "github.com/jinzhu/gorm/dialects/mysql" - _ "github.com/jinzhu/gorm/dialects/postgres" - _ "github.com/jinzhu/gorm/dialects/sqlite" + _ "gorm.io/driver/mysql" + _ "gorm.io/driver/postgres" + _ "gorm.io/driver/sqlite" + "gorm.io/gorm" ) var ( @@ -75,7 +75,6 @@ func RunMigrations(conf *DBConf, migrationsDir string, target int64) (err error) if err != nil { return err } - defer db.Close() 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: -// XXX_descriptivename.ext +// +// XXX_descriptivename.ext +// // where XXX specifies the version number // and ext specifies the type of migration func NumericComponent(name string) (int64, error) { @@ -261,7 +262,7 @@ func createVersionTable(conf *DBConf, db *gorm.DB) error { return txn.Error } - if err := txn.CreateTable(&MigrationRecord{}).Error; err != nil { + if err := txn.Migrator().CreateTable(&MigrationRecord{}); err != nil { txn.Rollback() return err } @@ -283,7 +284,6 @@ func GetDBVersion(conf *DBConf) (version int64, err error) { if err != nil { return -1, err } - defer db.Close() version, err = EnsureDBVersion(conf, db) if err != nil { @@ -398,7 +398,7 @@ var goMigrationTemplate = template.Must(template.New("goose.go-migration").Parse package main import ( - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) // Up is executed when this migration is applied diff --git a/lib/gorm-goose/migrate_dir.go b/lib/gorm-goose/migrate_dir.go index 096d8dd..b953d97 100644 --- a/lib/gorm-goose/migrate_dir.go +++ b/lib/gorm-goose/migrate_dir.go @@ -22,7 +22,7 @@ package gormgoose import ( - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) func MigrateFromDir(con *gorm.DB, migrationsDir string) error { diff --git a/lib/gorm-goose/migration_go.go b/lib/gorm-goose/migration_go.go index ce89b5a..5ee6e63 100644 --- a/lib/gorm-goose/migration_go.go +++ b/lib/gorm-goose/migration_go.go @@ -41,13 +41,11 @@ type templateData struct { Func string } -// // Run a .go migration. // // In order to do this, we copy a modified version of the // original .go migration, and execute it via `go run` along // with a main() of our own creation. -// func runGoMigration(conf *DBConf, path string, version int64, direction bool) error { // 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 } -// // template for the main entry point to a go-based migration. // this gets linked against the substituted versions of the user-supplied // scripts in order to execute a migration via `go run` -// var goMigrationDriverTemplate = template.Must(template.New("goose.go-driver").Parse(` package main @@ -118,7 +114,7 @@ import ( "encoding/gob" _ "{{.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() { @@ -133,7 +129,6 @@ func main() { if err != nil { log.Fatal("failed to open DB:", err) } - defer db.Close() txn := db.Begin() if txn.Error != nil { diff --git a/lib/gorm-goose/migration_sql.go b/lib/gorm-goose/migration_sql.go index 472bf20..11e8134 100644 --- a/lib/gorm-goose/migration_sql.go +++ b/lib/gorm-goose/migration_sql.go @@ -30,7 +30,7 @@ import ( "path/filepath" "strings" - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) const sqlCmdPrefix = "-- +goose "