1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-06 21:46:24 -07:00

Remove SQLite support due to instability

This commit is contained in:
Eggbertx 2020-06-15 11:27:14 -07:00
parent 31f9c57a35
commit a69b6748c7
16 changed files with 22 additions and 97 deletions

View file

@ -39,14 +39,9 @@ func ConnectToDB(host string, dbType string, dbName string, username string, pas
connStr = fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable",
username, password, host, dbName)
nilTimestamp = "0001-01-01 00:00:00"
case "sqlite3":
gclog.Print(gclog.LStdLog, "sqlite3 support is still flaky, consider using mysql or postgres")
connStr = fmt.Sprintf("file:%s?mode=rwc&_auth&_auth_user=%s&_auth_pass=%s&cache=shared",
host, username, password)
nilTimestamp = "0001-01-01 00:00:00+00:00"
default:
gclog.Printf(FatalSQLFlags,
`Invalid DBtype %q in gochan.json, valid values are "mysql", "postgres", and "sqlite3"`, dbType)
`Invalid DBtype %q in gochan.json, valid values are "mysql" and "postgres" (sqlite3 is no longer supported for stability reasons)`, dbType)
}
dbDriver = dbType
var err error

View file

@ -16,6 +16,7 @@ import (
var (
ErrMultipleDBVersions = errors.New("More than one version in database")
ErrNilBoard = errors.New("Board is nil")
ErrUnsupportedDB = errors.New("Unsupported DBtype")
)
// GetAllNondeletedMessageRaw gets all the raw message texts from the database, saved per id
@ -597,7 +598,7 @@ func InsertPost(post *Post, bump bool) error {
}
//Retrieves next free ID, explicitly inserts it, keeps retrying until succesfull insert or until a non-pk error is encountered.
//This is done because mysql/sqlite doesnt support RETURNING and both LAST_INSERT_ID() and last_row_id() are not thread-safe
//This is done because mysql doesnt support RETURNING and both LAST_INSERT_ID() and last_row_id() are not thread-safe
isPrimaryKeyError := true
for isPrimaryKeyError {
nextFreeID, err := getNextFreeID("DBPREFIXposts")
@ -630,7 +631,7 @@ func InsertPost(post *Post, bump bool) error {
func createThread(boardID int, locked bool, stickied bool, anchored bool, cyclical bool) (threadID int, err error) {
const sql = `INSERT INTO DBPREFIXthreads (board_id, locked, stickied, anchored, cyclical) VALUES (?,?,?,?,?)`
//Retrieves next free ID, explicitly inserts it, keeps retrying until succesfull insert or until a non-pk error is encountered.
//This is done because mysql/sqlite doesnt support RETURNING and both LAST_INSERT_ID() and last_row_id() are not thread-safe
//This is done because mysql doesnt support RETURNING and both LAST_INSERT_ID() and last_row_id() are not thread-safe
isPrimaryKeyError := true
for isPrimaryKeyError {
threadID, err = getNextFreeID("DBPREFIXthreads")
@ -992,25 +993,12 @@ func getNextFreeID(tableName string) (ID int, err error) {
}
func doesTableExist(tableName string) (bool, error) {
const mysqlPostgresql = `SELECT COUNT(*)
const existQuery = `SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = ?`
const sqlite = `SELECT
COUNT(name)
FROM
sqlite_master
WHERE
type ='table' AND
name NOT LIKE 'sqlite_%' AND name = ?;`
var sql string
switch config.Config.DBtype {
case "mysql":
sql = mysqlPostgresql
case "postgres":
sql = mysqlPostgresql
}
var count int
err := QueryRowSQL(sql, []interface{}{config.Config.DBprefix + tableName}, []interface{}{&count})
err := QueryRowSQL(existQuery, []interface{}{config.Config.DBprefix + tableName}, []interface{}{&count})
if err != nil {
return false, err
}
@ -1023,27 +1011,12 @@ func doesGochanPrefixTableExist() (bool, error) {
if config.Config.DBprefix == "" {
return false, nil
}
var mysqlPostgresql = `SELECT count(*)
var prefixTableExist = `SELECT count(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '` + config.Config.DBprefix + `%'`
var sqlite = `SELECT
COUNT(name)
FROM
sqlite_master
WHERE
type ='table' AND
name LIKE '` + config.Config.DBprefix + `%';`
var sql string
switch config.Config.DBtype {
case "mysql":
sql = mysqlPostgresql
case "postgres":
sql = mysqlPostgresql
case "sqlite3":
sql = sqlite
}
WHERE TABLE_NAME LIKE 'DBPREFIX%'`
var count int
err := QueryRowSQL(sql, []interface{}{}, []interface{}{&count})
err := QueryRowSQL(prefixTableExist, []interface{}{}, []interface{}{&count})
if err != nil {
return false, err
}

View file

@ -7,13 +7,12 @@ import (
"github.com/gochan-org/gochan/pkg/config"
"github.com/gochan-org/gochan/pkg/gclog"
"github.com/gochan-org/gochan/pkg/gcutil"
)
const (
MySQLDatetimeFormat = "2006-01-02 15:04:05"
unsupportedSQLVersionMsg = `Received syntax error while preparing a SQL string.
This means that either there is a bug in gochan's code (hopefully not) or that you are using an unsupported My/Postgre/SQLite version.
This means that either there is a bug in gochan's code (hopefully not) or that you are using an unsupported My/Postgre version.
Before reporting an error, make sure that you are using the up to date version of your selected SQL server.
Error text: %s`
)
@ -32,10 +31,6 @@ func sqlVersionErr(err error, query *string) error {
if !strings.Contains(errText, "syntax error at or near") {
return err
}
case "sqlite3":
if !strings.Contains(errText, "Error: near ") {
return err
}
}
if config.Config.DebugMode {
return fmt.Errorf(unsupportedSQLVersionMsg+"\nQuery: "+*query, errText)
@ -49,8 +44,6 @@ func PrepareSQL(query string) (*sql.Stmt, error) {
switch dbDriver {
case "mysql":
fallthrough
case "sqlite3":
preparedStr = query
case "postgres":
arr := strings.Split(query, "?")
for i := range arr {
@ -60,6 +53,8 @@ func PrepareSQL(query string) (*sql.Stmt, error) {
arr[i] += fmt.Sprintf("$%d", i+1)
}
preparedStr = strings.Join(arr, "")
default:
return nil, ErrUnsupportedDB
}
stmt, err := db.Prepare(sqlReplacer.Replace(preparedStr))
if err != nil {
@ -168,11 +163,6 @@ func errFilterDuplicatePrimaryKey(err error) (isPKerror bool, nonPKerror error)
if !strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return false, err
}
case "sqlite3":
return false, gcutil.ErrNotImplemented
// if !strings.Contains(err.Error(), "Error: near ") {//TODO fill in correct error string
// return false, err
// }
}
return true, nil
}