From fca3019e4b84d9b35b3ae1168956b4f9e620f53f Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Thu, 3 Jan 2019 17:57:06 -0500 Subject: [PATCH] Support building without SQLite support This adds a new `sqlite` build tag that you should include only if you want SQLite3 support built in. Both `make run` and `make release` create builds with SQLite included. --- Makefile | 10 +++++----- app.go | 5 ++++- database-no-sqlite.go | 30 ++++++++++++++++++++++++++++++ database-sqlite.go | 39 +++++++++++++++++++++++++++++++++++++++ database.go | 23 ++++------------------- 5 files changed, 82 insertions(+), 25 deletions(-) create mode 100644 database-no-sqlite.go create mode 100644 database-sqlite.go diff --git a/Makefile b/Makefile index 3fc11ac..05e035c 100644 --- a/Makefile +++ b/Makefile @@ -13,25 +13,25 @@ IMAGE_NAME=writeas/writefreely all : build build: assets deps - cd cmd/writefreely; $(GOBUILD) -v + cd cmd/writefreely; $(GOBUILD) -v -tags='sqlite' build-linux: deps @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ $(GOGET) -u github.com/karalabe/xgo; \ fi - xgo --targets=linux/amd64, -dest build/ $(LDFLAGS) -out writefreely ./cmd/writefreely + xgo --targets=linux/amd64, -dest build/ $(LDFLAGS) -tags='sqlite' -out writefreely ./cmd/writefreely build-windows: deps @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ $(GOGET) -u github.com/karalabe/xgo; \ fi - xgo --targets=windows/amd64, -dest build/ $(LDFLAGS) -out writefreely ./cmd/writefreely + xgo --targets=windows/amd64, -dest build/ $(LDFLAGS) -tags='sqlite' -out writefreely ./cmd/writefreely build-darwin: deps @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ $(GOGET) -u github.com/karalabe/xgo; \ fi - xgo --targets=darwin/amd64, -dest build/ $(LDFLAGS) -out writefreely ./cmd/writefreely + xgo --targets=darwin/amd64, -dest build/ $(LDFLAGS) -tags='sqlite' -out writefreely ./cmd/writefreely build-docker : $(DOCKERCMD) build -t $(IMAGE_NAME):latest -t $(IMAGE_NAME):$(GITREV) . @@ -40,7 +40,7 @@ test: $(GOTEST) -v ./... run: dev-assets - $(GOINSTALL) ./... + $(GOINSTALL) -tags='sqlite' ./... $(BINARY_NAME) --debug deps : diff --git a/app.go b/app.go index 3698d5d..ba04dde 100644 --- a/app.go +++ b/app.go @@ -25,7 +25,6 @@ import ( "time" _ "github.com/go-sql-driver/mysql" - _ "github.com/mattn/go-sqlite3" "github.com/gorilla/mux" "github.com/gorilla/schema" @@ -476,6 +475,10 @@ func connectToDatabase(app *app) { db, err = sql.Open(app.cfg.Database.Type, fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=%s", app.cfg.Database.User, app.cfg.Database.Password, app.cfg.Database.Host, app.cfg.Database.Port, app.cfg.Database.Database, url.QueryEscape(time.Local.String()))) db.SetMaxOpenConns(50) } else if app.cfg.Database.Type == "sqlite3" { + if !SQLiteEnabled { + log.Error("Invalid database type '%s'. Binary wasn't compiled with SQLite3 support.", app.cfg.Database.Type) + os.Exit(1) + } if app.cfg.Database.FileName == "" { log.Error("SQLite database filename value in config.ini is empty.") os.Exit(1) diff --git a/database-no-sqlite.go b/database-no-sqlite.go new file mode 100644 index 0000000..10db7d5 --- /dev/null +++ b/database-no-sqlite.go @@ -0,0 +1,30 @@ +// +build !sqlite + +/* + * Copyright © 2019 A Bunch Tell LLC. + * + * This file is part of WriteFreely. + * + * WriteFreely is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, included + * in the LICENSE file in this source code package. + */ + +package writefreely + +import ( + "github.com/go-sql-driver/mysql" + "github.com/writeas/web-core/log" +) + +func (db *datastore) isDuplicateKeyErr(err error) bool { + if db.driverName == driverMySQL { + if mysqlErr, ok := err.(*mysql.MySQLError); ok { + return mysqlErr.Number == mySQLErrDuplicateKey + } + } else { + log.Error("isDuplicateKeyErr: failed check for unrecognized driver '%s'", db.driverName) + } + + return false +} diff --git a/database-sqlite.go b/database-sqlite.go new file mode 100644 index 0000000..e1ed991 --- /dev/null +++ b/database-sqlite.go @@ -0,0 +1,39 @@ +// +build sqlite + +/* + * Copyright © 2019 A Bunch Tell LLC. + * + * This file is part of WriteFreely. + * + * WriteFreely is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, included + * in the LICENSE file in this source code package. + */ + +package writefreely + +import ( + "github.com/go-sql-driver/mysql" + "github.com/mattn/go-sqlite3" + "github.com/writeas/web-core/log" +) + +func init() { + SQLiteEnabled = true +} + +func (db *datastore) isDuplicateKeyErr(err error) bool { + if db.driverName == driverSQLite { + if err, ok := err.(sqlite3.Error); ok { + return err.Code == sqlite3.ErrConstraint + } + } else if db.driverName == driverMySQL { + if mysqlErr, ok := err.(*mysql.MySQLError); ok { + return mysqlErr.Number == mySQLErrDuplicateKey + } + } else { + log.Error("isDuplicateKeyErr: failed check for unrecognized driver '%s'", db.driverName) + } + + return false +} diff --git a/database.go b/database.go index c27c664..250b6e0 100644 --- a/database.go +++ b/database.go @@ -17,9 +17,6 @@ import ( "strings" "time" - "github.com/go-sql-driver/mysql" - "github.com/mattn/go-sqlite3" - "github.com/guregu/null" "github.com/guregu/null/zero" uuid "github.com/nu7hatch/gouuid" @@ -41,6 +38,10 @@ const ( driverSQLite = "sqlite3" ) +var ( + SQLiteEnabled bool +) + type writestore interface { CreateUser(*User, string) error UpdateUserEmail(keys *keychain, userID int64, email string) error @@ -149,22 +150,6 @@ func (db *datastore) dateSub(l int, unit string) string { return fmt.Sprintf("DATE_SUB(NOW(), INTERVAL %d %s)", l, unit) } -func (db *datastore) isDuplicateKeyErr(err error) bool { - if db.driverName == driverSQLite { - if err, ok := err.(sqlite3.Error); ok { - return err.Code == sqlite3.ErrConstraint - } - } else if db.driverName == driverMySQL { - if mysqlErr, ok := err.(*mysql.MySQLError); ok { - return mysqlErr.Number == mySQLErrDuplicateKey - } - } else { - log.Error("isDuplicateKeyErr: failed check for unrecognized driver '%s'", db.driverName) - } - - return false -} - func (db *datastore) CreateUser(u *User, collectionTitle string) error { // New users get a `users` and `collections` row. t, err := db.Begin()