Browse Source

Use smaller sqlite migration library

pull/3/head
Jan-Lukas Else 2 years ago
parent
commit
223191d95c
  1. 22
      database.go
  2. 3
      go.mod
  3. 4
      go.sum
  4. 96
      helpers/packrSource.go
  5. 1
      migrations/1_Init_Database.down.sql
  6. 8
      migrations/1_Init_Database.sql

22
database.go

@ -4,10 +4,8 @@ import (
"database/sql"
"fmt"
"github.com/gobuffalo/packr/v2"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "github.com/mattn/go-sqlite3"
"kis3.dev/kis3/helpers"
"github.com/rubenv/sql-migrate"
"os"
"path/filepath"
"strings"
@ -31,24 +29,10 @@ func initDatabase() (database *Database, e error) {
}
func migrateDatabase(database *sql.DB) (e error) {
sourceDriver, e := (&helpers.PackrSource{
migrations := &migrate.PackrMigrationSource{
Box: packr.New("migrations", "migrations"),
}).Open("")
if e != nil {
return
}
databaseDriver, e := sqlite3.WithInstance(database, &sqlite3.Config{})
if e != nil {
return
}
m, e := migrate.NewWithInstance("packr", sourceDriver, "kis3", databaseDriver)
if e != nil {
return
}
e = m.Up()
if e == migrate.ErrNoChange {
e = nil
}
_, e = migrate.Exec(database, "sqlite3", migrations, migrate.Up)
return
}

3
go.mod

@ -4,11 +4,12 @@ go 1.12
require (
github.com/gobuffalo/packr/v2 v2.1.0
github.com/golang-migrate/migrate/v4 v4.2.5
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/gorilla/handlers v1.4.0
github.com/gorilla/mux v1.7.1
github.com/mattn/go-sqlite3 v1.10.0
github.com/rubenv/sql-migrate v0.0.0-20190327083759-54bad0a9b051
github.com/wcharczuk/go-chart v2.0.1+incompatible
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f // indirect
gopkg.in/gorp.v1 v1.7.2 // indirect
)

4
go.sum

@ -144,6 +144,8 @@ github.com/rogpeppe/go-internal v1.2.2 h1:J7U/N7eRtzjhs26d6GqMh2HBuXP8/Z64Densii
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rubenv/sql-migrate v0.0.0-20190327083759-54bad0a9b051 h1:p32bQkgLiadYiOqs294BAx/7f1Aerfva8rj+rVvzR0A=
github.com/rubenv/sql-migrate v0.0.0-20190327083759-54bad0a9b051/go.mod h1:WS0rl9eEliYI8DPnr3TOwz4439pay+qNgzJoVya/DmY=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
@ -225,6 +227,8 @@ google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9M
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/gorp.v1 v1.7.2 h1:j3DWlAyGVv8whO7AcIWznQ2Yj7yJkn34B8s63GViAAw=
gopkg.in/gorp.v1 v1.7.2/go.mod h1:Wo3h+DBQZIxATwftsglhdD/62zRFPhGhTiu5jUJmCaw=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.39.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=

96
helpers/packrSource.go

@ -1,96 +0,0 @@
package helpers
// This is a quick hack to get Packr working with golang-migrate
import (
"bytes"
"github.com/gobuffalo/packr/v2"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/source"
"io"
"io/ioutil"
"os"
"sync"
)
type PackrSource struct {
lock sync.Mutex
Box *packr.Box
migrations *source.Migrations
}
func (s *PackrSource) loadMigrations() (*source.Migrations, error) {
migrations := source.NewMigrations()
for _, filename := range s.Box.List() {
migration, err := source.Parse(filename)
if err != nil {
return nil, err
}
migrations.Append(migration)
}
return migrations, nil
}
func (s *PackrSource) Open(url string) (source.Driver, error) {
s.lock.Lock()
defer s.lock.Unlock()
if migrations, err := s.loadMigrations(); err != nil {
return nil, err
} else {
s.migrations = migrations
return s, nil
}
}
func (s *PackrSource) Close() error {
s.lock.Lock()
defer s.lock.Unlock()
s.migrations = nil
return nil
}
func (s *PackrSource) First() (version uint, err error) {
if v, ok := s.migrations.First(); !ok {
return 0, os.ErrNotExist
} else {
return v, nil
}
}
func (s *PackrSource) Prev(version uint) (prevVersion uint, err error) {
if v, ok := s.migrations.Prev(version); !ok {
return 0, os.ErrNotExist
} else {
return v, nil
}
}
func (s *PackrSource) Next(version uint) (nextVersion uint, err error) {
if v, ok := s.migrations.Next(version); !ok {
return 0, os.ErrNotExist
} else {
return v, nil
}
}
func (s *PackrSource) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) {
if migration, ok := s.migrations.Up(version); !ok {
return nil, "", os.ErrNotExist
} else {
b, _ := s.Box.Find(migration.Raw)
return ioutil.NopCloser(bytes.NewBuffer(b)),
migration.Identifier,
nil
}
}
func (s *PackrSource) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) {
if migration, ok := s.migrations.Down(version); !ok {
return nil, "", migrate.ErrNilVersion
} else {
b := s.Box.Bytes(migration.Raw)
return ioutil.NopCloser(bytes.NewBuffer(b)),
migration.Identifier,
nil
}
}

1
migrations/1_Init_Database.down.sql

@ -1 +0,0 @@
DROP TABLE `views`;

8
migrations/1_Init_Database.up.sql → migrations/1_Init_Database.sql

@ -1,6 +1,10 @@
CREATE TABLE `views`
-- +migrate Up
CREATE TABLE IF NOT EXISTS `views`
(
`url` TEXT NOT NULL,
`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
`ref` TEXT DEFAULT '' NOT NULL
);
);
-- +migrate Down
DROP TABLE `views`;
Loading…
Cancel
Save