2018-01-23 16:17:19 -08:00
|
|
|
// +build cgo
|
|
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
package sqlite
|
2015-12-01 11:38:31 -08:00
|
|
|
|
|
|
|
|
import (
|
2018-01-22 20:38:40 -08:00
|
|
|
"bytes"
|
2015-12-01 11:38:31 -08:00
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
2021-02-18 23:10:57 +01:00
|
|
|
"io"
|
2015-12-01 11:38:31 -08:00
|
|
|
"net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"regexp"
|
2018-01-22 20:38:40 -08:00
|
|
|
"strings"
|
2017-01-27 23:49:06 -08:00
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
"github.com/amacneil/dbmate/pkg/dbmate"
|
|
|
|
|
"github.com/amacneil/dbmate/pkg/dbutil"
|
|
|
|
|
|
2020-11-17 18:11:24 +13:00
|
|
|
"github.com/lib/pq"
|
2020-11-19 15:04:42 +13:00
|
|
|
_ "github.com/mattn/go-sqlite3" // database/sql driver
|
2015-12-01 11:38:31 -08:00
|
|
|
)
|
|
|
|
|
|
2018-01-23 16:17:19 -08:00
|
|
|
func init() {
|
2020-11-19 15:04:42 +13:00
|
|
|
dbmate.RegisterDriver(NewDriver, "sqlite")
|
|
|
|
|
dbmate.RegisterDriver(NewDriver, "sqlite3")
|
2018-01-23 16:17:19 -08:00
|
|
|
}
|
|
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
// Driver provides top level database functions
|
|
|
|
|
type Driver struct {
|
2020-11-17 18:11:24 +13:00
|
|
|
migrationsTableName string
|
2020-11-19 15:04:42 +13:00
|
|
|
databaseURL *url.URL
|
2021-02-18 23:10:57 +01:00
|
|
|
log io.Writer
|
2015-12-01 11:38:31 -08:00
|
|
|
}
|
|
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
// NewDriver initializes the driver
|
|
|
|
|
func NewDriver(config dbmate.DriverConfig) dbmate.Driver {
|
|
|
|
|
return &Driver{
|
|
|
|
|
migrationsTableName: config.MigrationsTableName,
|
|
|
|
|
databaseURL: config.DatabaseURL,
|
2021-02-18 23:10:57 +01:00
|
|
|
log: config.Log,
|
2020-11-19 15:04:42 +13:00
|
|
|
}
|
2015-12-01 11:38:31 -08:00
|
|
|
}
|
|
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
// ConnectionString converts a URL into a valid connection string
|
|
|
|
|
func ConnectionString(u *url.URL) string {
|
|
|
|
|
// duplicate URL and remove scheme
|
|
|
|
|
newURL := *u
|
|
|
|
|
newURL.Scheme = ""
|
|
|
|
|
|
|
|
|
|
// trim duplicate leading slashes
|
|
|
|
|
str := regexp.MustCompile("^//+").ReplaceAllString(newURL.String(), "/")
|
|
|
|
|
|
|
|
|
|
return str
|
2020-11-17 18:11:24 +13:00
|
|
|
}
|
|
|
|
|
|
2015-12-01 11:38:31 -08:00
|
|
|
// Open creates a new database connection
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) Open() (*sql.DB, error) {
|
|
|
|
|
return sql.Open("sqlite3", ConnectionString(drv.databaseURL))
|
2015-12-01 11:38:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateDatabase creates the specified database
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) CreateDatabase() error {
|
2021-02-18 23:10:57 +01:00
|
|
|
fmt.Fprintf(drv.log, "Creating: %s\n", ConnectionString(drv.databaseURL))
|
2015-12-01 11:38:31 -08:00
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
db, err := drv.Open()
|
2015-12-01 11:38:31 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-11-19 15:04:42 +13:00
|
|
|
defer dbutil.MustClose(db)
|
2015-12-01 11:38:31 -08:00
|
|
|
|
|
|
|
|
return db.Ping()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DropDatabase drops the specified database (if it exists)
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) DropDatabase() error {
|
|
|
|
|
path := ConnectionString(drv.databaseURL)
|
2021-02-18 23:10:57 +01:00
|
|
|
fmt.Fprintf(drv.log, "Dropping: %s\n", path)
|
2015-12-01 11:38:31 -08:00
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
exists, err := drv.DatabaseExists()
|
2015-12-01 11:38:31 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if !exists {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return os.Remove(path)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) schemaMigrationsDump(db *sql.DB) ([]byte, error) {
|
2020-11-17 18:11:24 +13:00
|
|
|
migrationsTable := drv.quotedMigrationsTableName()
|
|
|
|
|
|
2018-01-22 20:38:40 -08:00
|
|
|
// load applied migrations
|
2020-11-19 15:04:42 +13:00
|
|
|
migrations, err := dbutil.QueryColumn(db,
|
2020-11-17 18:11:24 +13:00
|
|
|
fmt.Sprintf("select quote(version) from %s order by version asc", migrationsTable))
|
2018-01-22 20:38:40 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 18:11:24 +13:00
|
|
|
// build schema migrations table data
|
2018-01-22 20:38:40 -08:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
buf.WriteString("-- Dbmate schema migrations\n")
|
|
|
|
|
|
|
|
|
|
if len(migrations) > 0 {
|
2020-11-17 18:11:24 +13:00
|
|
|
buf.WriteString(
|
|
|
|
|
fmt.Sprintf("INSERT INTO %s (version) VALUES\n (", migrationsTable) +
|
|
|
|
|
strings.Join(migrations, "),\n (") +
|
|
|
|
|
");\n")
|
2018-01-22 20:38:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DumpSchema returns the current database schema
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) DumpSchema(db *sql.DB) ([]byte, error) {
|
|
|
|
|
path := ConnectionString(drv.databaseURL)
|
|
|
|
|
schema, err := dbutil.RunCommand("sqlite3", path, ".schema")
|
2018-01-22 20:38:40 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 18:11:24 +13:00
|
|
|
migrations, err := drv.schemaMigrationsDump(db)
|
2018-01-22 20:38:40 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
schema = append(schema, migrations...)
|
2020-11-19 15:04:42 +13:00
|
|
|
return dbutil.TrimLeadingSQLComments(schema)
|
2018-01-22 20:38:40 -08:00
|
|
|
}
|
|
|
|
|
|
2015-12-01 11:38:31 -08:00
|
|
|
// DatabaseExists determines whether the database exists
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) DatabaseExists() (bool, error) {
|
|
|
|
|
_, err := os.Stat(ConnectionString(drv.databaseURL))
|
2015-12-01 11:38:31 -08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 18:11:24 +13:00
|
|
|
// CreateMigrationsTable creates the schema migrations table
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) CreateMigrationsTable(db *sql.DB) error {
|
2020-11-17 18:11:24 +13:00
|
|
|
_, err := db.Exec(
|
|
|
|
|
fmt.Sprintf("create table if not exists %s ", drv.quotedMigrationsTableName()) +
|
|
|
|
|
"(version varchar(255) primary key)")
|
2015-12-01 11:38:31 -08:00
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SelectMigrations returns a list of applied migrations
|
|
|
|
|
// with an optional limit (in descending order)
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) SelectMigrations(db *sql.DB, limit int) (map[string]bool, error) {
|
2020-11-17 18:11:24 +13:00
|
|
|
query := fmt.Sprintf("select version from %s order by version desc", drv.quotedMigrationsTableName())
|
2015-12-01 11:38:31 -08:00
|
|
|
if limit >= 0 {
|
|
|
|
|
query = fmt.Sprintf("%s limit %d", query, limit)
|
|
|
|
|
}
|
|
|
|
|
rows, err := db.Query(query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
defer dbutil.MustClose(rows)
|
2015-12-01 11:38:31 -08:00
|
|
|
|
|
|
|
|
migrations := map[string]bool{}
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var version string
|
|
|
|
|
if err := rows.Scan(&version); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
migrations[version] = true
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 15:30:20 +13:00
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-01 11:38:31 -08:00
|
|
|
return migrations, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InsertMigration adds a new migration record
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) InsertMigration(db dbutil.Transaction, version string) error {
|
2020-11-17 18:11:24 +13:00
|
|
|
_, err := db.Exec(
|
|
|
|
|
fmt.Sprintf("insert into %s (version) values (?)", drv.quotedMigrationsTableName()),
|
|
|
|
|
version)
|
2015-12-01 11:38:31 -08:00
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteMigration removes a migration record
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) DeleteMigration(db dbutil.Transaction, version string) error {
|
2020-11-17 18:11:24 +13:00
|
|
|
_, err := db.Exec(
|
|
|
|
|
fmt.Sprintf("delete from %s where version = ?", drv.quotedMigrationsTableName()),
|
|
|
|
|
version)
|
2015-12-01 11:38:31 -08:00
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-04-15 18:37:57 -07:00
|
|
|
|
|
|
|
|
// Ping verifies a connection to the database. Due to the way SQLite works, by
|
|
|
|
|
// testing whether the database is valid, it will automatically create the database
|
|
|
|
|
// if it does not already exist.
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) Ping() error {
|
|
|
|
|
db, err := drv.Open()
|
2018-04-15 18:37:57 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-11-19 15:04:42 +13:00
|
|
|
defer dbutil.MustClose(db)
|
2018-04-15 18:37:57 -07:00
|
|
|
|
|
|
|
|
return db.Ping()
|
|
|
|
|
}
|
2020-11-17 18:11:24 +13:00
|
|
|
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) quotedMigrationsTableName() string {
|
2020-11-17 18:11:24 +13:00
|
|
|
return drv.quoteIdentifier(drv.migrationsTableName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// quoteIdentifier quotes a table or column name
|
|
|
|
|
// we fall back to lib/pq implementation since both use ansi standard (double quotes)
|
|
|
|
|
// and mattn/go-sqlite3 doesn't provide a sqlite-specific equivalent
|
2020-11-19 15:04:42 +13:00
|
|
|
func (drv *Driver) quoteIdentifier(s string) string {
|
2020-11-17 18:11:24 +13:00
|
|
|
return pq.QuoteIdentifier(s)
|
|
|
|
|
}
|