Commit 57713631 authored by Jason Moiron's avatar Jason Moiron

fix tests for mysql and sqlite by assuming that identity clauses starting with...

fix tests for mysql and sqlite by assuming that identity clauses starting with ";" are separate queries and executing them separately
parent e79ea17b
......@@ -7,9 +7,10 @@ import (
"bytes"
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
"log"
"reflect"
"github.com/jmoiron/sqlx"
)
// DbMap is the root modl mapping object. Create one of these for each
......@@ -389,12 +390,23 @@ func (m *DbMap) truncateTables(restartIdentity bool) error {
restartClause = m.Dialect.RestartIdentityClause(table.TableName)
}
_, e := m.Exec(fmt.Sprintf("%s %s %s;", m.Dialect.TruncateClause(), m.Dialect.QuoteField(table.TableName), restartClause))
if e != nil {
err = e
if len(restartClause) > 0 && restartClause[0] == ';' {
_, err = m.Exec(fmt.Sprintf("%s %s;", m.Dialect.TruncateClause(), m.Dialect.QuoteField(table.TableName)))
if err != nil {
return err
}
_, err = m.Exec(restartClause[1:])
if err != nil {
return err
}
} else {
_, err := m.Exec(fmt.Sprintf("%s %s %s;", m.Dialect.TruncateClause(), m.Dialect.QuoteField(table.TableName), restartClause))
if err != nil {
return err
}
}
}
return err
return nil
}
func (m *DbMap) queryRow(query string, args ...interface{}) *sql.Row {
......
......@@ -3,9 +3,10 @@ package modl
import (
"errors"
"fmt"
"github.com/jmoiron/sqlx"
"reflect"
"strings"
"github.com/jmoiron/sqlx"
)
// The Dialect interface encapsulates behaviors that differ across
......@@ -47,6 +48,8 @@ type Dialect interface {
TruncateClause() string
// string used to reset identity counter when truncating tables
// if the string starts with a ';', it is assumed to be a separate query
// and is executed separately
RestartIdentityClause(table string) string
// Get the driver name from a dialect
......@@ -143,7 +146,7 @@ func (d SqliteDialect) TruncateClause() string {
}
func (d SqliteDialect) RestartIdentityClause(table string) string {
return ""
return "; DELETE FROM sqlite_sequence WHERE name='" + table + "'"
}
///////////////////////////////////////////////////////
......
......@@ -4,14 +4,15 @@ import (
"bytes"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
"reflect"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
var _ = log.Fatal
......@@ -122,7 +123,9 @@ func TestCreateTablesIfNotExists(t *testing.T) {
func TestPersistentUser(t *testing.T) {
dbmap := newDbMap()
dbmap.Exec("drop table if exists persistentuser")
//dbmap.TraceOn("", log.New(os.Stdout, "modltest: ", log.Lmicroseconds))
if len(os.Getenv("MODL_TEST_TRACE")) > 0 {
dbmap.TraceOn("test", log.New(os.Stdout, "modltest: ", log.Lmicroseconds))
}
dbmap.AddTable(PersistentUser{}).SetKeys(false, "mykey")
err := dbmap.CreateTablesIfNotExists()
if err != nil {
......
......@@ -25,6 +25,7 @@ fi
if [ -n "$MODL_MYSQL_DSN" ]; then
export MODL_TEST_DSN="$MODL_MYSQL_DSN"
export MODL_TEST_DIALECT="mysql"
echo "Testing MySQL"
go test $@
else
echo "Skipping MySQL, \$MODL_MYSQL_DSN=$MODL_MYSQL_DSN"
......@@ -36,6 +37,7 @@ fi
if [ -n "$MODL_POSTGRES_DSN" ]; then
export MODL_TEST_DSN="$MODL_POSTGRES_DSN"
export MODL_TEST_DIALECT="postgres"
echo "Testing PostgreSQL"
go test $@
else
echo "Skipping PostgreSQL, \$MODL_POSTGRES_DSN=$MODL_POSTGRES_DSN"
......@@ -47,6 +49,7 @@ fi
if [ -n "$MODL_SQLITE_DSN" ]; then
export MODL_TEST_DSN="$MODL_SQLITE_DSN"
export MODL_TEST_DIALECT="sqlite"
echo "Testing SQLite"
go test $@
else
echo "Skipping SQLite, \$MODL_SQLITE_DSN=$MODL_SQLITE_DSN"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment