mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-12 16:10:03 +01:00
Add cgo build constraint for sqlite support (#25)
Required to upgrade https://github.com/mattn/go-sqlite3 which does not compile correctly with missing cgo.
This commit is contained in:
parent
cc769e9605
commit
fbddb76b84
6 changed files with 30 additions and 10 deletions
|
|
@ -19,6 +19,13 @@ type Driver interface {
|
||||||
DeleteMigration(Transaction, string) error
|
DeleteMigration(Transaction, string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var drivers = map[string]Driver{}
|
||||||
|
|
||||||
|
// RegisterDriver registers a driver for a URL scheme
|
||||||
|
func RegisterDriver(drv Driver, scheme string) {
|
||||||
|
drivers[scheme] = drv
|
||||||
|
}
|
||||||
|
|
||||||
// Transaction can represent a database or open transaction
|
// Transaction can represent a database or open transaction
|
||||||
type Transaction interface {
|
type Transaction interface {
|
||||||
Exec(query string, args ...interface{}) (sql.Result, error)
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
||||||
|
|
@ -26,16 +33,11 @@ type Transaction interface {
|
||||||
|
|
||||||
// GetDriver loads a database driver by name
|
// GetDriver loads a database driver by name
|
||||||
func GetDriver(name string) (Driver, error) {
|
func GetDriver(name string) (Driver, error) {
|
||||||
switch name {
|
if val, ok := drivers[name]; ok {
|
||||||
case "mysql":
|
return val, nil
|
||||||
return MySQLDriver{}, nil
|
|
||||||
case "postgres", "postgresql":
|
|
||||||
return PostgresDriver{}, nil
|
|
||||||
case "sqlite", "sqlite3":
|
|
||||||
return SQLiteDriver{}, nil
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unknown driver: %s", name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("unsupported driver: %s", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDriverOpen is a shortcut for GetDriver(u.Scheme).Open(u)
|
// GetDriverOpen is a shortcut for GetDriver(u.Scheme).Open(u)
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,6 @@ func TestGetDriver_MySQL(t *testing.T) {
|
||||||
|
|
||||||
func TestGetDriver_Error(t *testing.T) {
|
func TestGetDriver_Error(t *testing.T) {
|
||||||
drv, err := GetDriver("foo")
|
drv, err := GetDriver("foo")
|
||||||
require.Equal(t, "unknown driver: foo", err.Error())
|
require.Equal(t, "unsupported driver: foo", err.Error())
|
||||||
require.Nil(t, drv)
|
require.Nil(t, drv)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ import (
|
||||||
_ "github.com/go-sql-driver/mysql" // mysql driver for database/sql
|
_ "github.com/go-sql-driver/mysql" // mysql driver for database/sql
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterDriver(MySQLDriver{}, "mysql")
|
||||||
|
}
|
||||||
|
|
||||||
// MySQLDriver provides top level database functions
|
// MySQLDriver provides top level database functions
|
||||||
type MySQLDriver struct {
|
type MySQLDriver struct {
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,11 @@ import (
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterDriver(PostgresDriver{}, "postgres")
|
||||||
|
RegisterDriver(PostgresDriver{}, "postgresql")
|
||||||
|
}
|
||||||
|
|
||||||
// PostgresDriver provides top level database functions
|
// PostgresDriver provides top level database functions
|
||||||
type PostgresDriver struct {
|
type PostgresDriver struct {
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
// +build cgo
|
||||||
|
|
||||||
package dbmate
|
package dbmate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -12,6 +14,11 @@ import (
|
||||||
_ "github.com/mattn/go-sqlite3" // sqlite driver for database/sql
|
_ "github.com/mattn/go-sqlite3" // sqlite driver for database/sql
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterDriver(SQLiteDriver{}, "sqlite")
|
||||||
|
RegisterDriver(SQLiteDriver{}, "sqlite3")
|
||||||
|
}
|
||||||
|
|
||||||
// SQLiteDriver provides top level database functions
|
// SQLiteDriver provides top level database functions
|
||||||
type SQLiteDriver struct {
|
type SQLiteDriver struct {
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
// +build cgo
|
||||||
|
|
||||||
package dbmate
|
package dbmate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue