Upgrade gopkg.in/testfixtures.v2 (#4999)
This commit is contained in:
parent
b8d048fa0d
commit
dba955be7c
13 changed files with 708 additions and 171 deletions
6
Gopkg.lock
generated
6
Gopkg.lock
generated
|
@ -885,12 +885,12 @@
|
||||||
version = "v2.3.2"
|
version = "v2.3.2"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
digest = "1:e2144032dcf8e856fb733151391669dc2d32d79ebb69e3c4151efa605f5e8a01"
|
digest = "1:9c541fc507676a69ea8aaed1af53278a5241d26ce0f192c993fec2ac5b78f795"
|
||||||
name = "gopkg.in/testfixtures.v2"
|
name = "gopkg.in/testfixtures.v2"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
pruneopts = "NUT"
|
pruneopts = "NUT"
|
||||||
revision = "b9ef14dc461bf934d8df2dfc6f1f456be5664cca"
|
revision = "fa3fb89109b0b31957a5430cef3e93e535de362b"
|
||||||
version = "v2.0.0"
|
version = "v2.5.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
digest = "1:ad6f94355d292690137613735965bd3688844880fdab90eccf66321910344942"
|
digest = "1:ad6f94355d292690137613735965bd3688844880fdab90eccf66321910344942"
|
||||||
|
|
28
vendor/gopkg.in/testfixtures.v2/deprecated.go
generated
vendored
28
vendor/gopkg.in/testfixtures.v2/deprecated.go
generated
vendored
|
@ -5,22 +5,38 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
DataBaseHelper Helper // Deprecated: Use Helper instead
|
// DataBaseHelper is the helper interface
|
||||||
|
// Deprecated: Use Helper instead
|
||||||
|
DataBaseHelper Helper
|
||||||
|
|
||||||
PostgreSQLHelper struct { // Deprecated: Use PostgreSQL{} instead
|
// PostgreSQLHelper is the PostgreSQL helper
|
||||||
|
// Deprecated: Use PostgreSQL{} instead
|
||||||
|
PostgreSQLHelper struct {
|
||||||
PostgreSQL
|
PostgreSQL
|
||||||
UseAlterConstraint bool
|
UseAlterConstraint bool
|
||||||
}
|
}
|
||||||
MySQLHelper struct { // Deprecated: Use MySQL{} instead
|
|
||||||
|
// MySQLHelper is the MySQL helper
|
||||||
|
// Deprecated: Use MySQL{} instead
|
||||||
|
MySQLHelper struct {
|
||||||
MySQL
|
MySQL
|
||||||
}
|
}
|
||||||
SQLiteHelper struct { // Deprecated: Use SQLite{} instead
|
|
||||||
|
// SQLiteHelper is the SQLite helper
|
||||||
|
// Deprecated: Use SQLite{} instead
|
||||||
|
SQLiteHelper struct {
|
||||||
SQLite
|
SQLite
|
||||||
}
|
}
|
||||||
SQLServerHelper struct { // Deprecated: Use SQLServer{} instead
|
|
||||||
|
// SQLServerHelper is the SQLServer helper
|
||||||
|
// Deprecated: Use SQLServer{} instead
|
||||||
|
SQLServerHelper struct {
|
||||||
SQLServer
|
SQLServer
|
||||||
}
|
}
|
||||||
OracleHelper struct { // Deprecated: Use Oracle{} instead
|
|
||||||
|
// OracleHelper is the Oracle helper
|
||||||
|
// Deprecated: Use Oracle{} instead
|
||||||
|
OracleHelper struct {
|
||||||
Oracle
|
Oracle
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
41
vendor/gopkg.in/testfixtures.v2/errors.go
generated
vendored
Normal file
41
vendor/gopkg.in/testfixtures.v2/errors.go
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package testfixtures
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrWrongCastNotAMap is returned when a map is not a map[interface{}]interface{}
|
||||||
|
ErrWrongCastNotAMap = errors.New("Could not cast record: not a map[interface{}]interface{}")
|
||||||
|
|
||||||
|
// ErrFileIsNotSliceOrMap is returned the the fixture file is not a slice or map.
|
||||||
|
ErrFileIsNotSliceOrMap = errors.New("The fixture file is not a slice or map")
|
||||||
|
|
||||||
|
// ErrKeyIsNotString is returned when a record is not of type string
|
||||||
|
ErrKeyIsNotString = errors.New("Record map key is not string")
|
||||||
|
|
||||||
|
// ErrNotTestDatabase is returned when the database name doesn't contains "test"
|
||||||
|
ErrNotTestDatabase = errors.New(`Loading aborted because the database name does not contains "test"`)
|
||||||
|
)
|
||||||
|
|
||||||
|
// InsertError will be returned if any error happens on database while
|
||||||
|
// inserting the record
|
||||||
|
type InsertError struct {
|
||||||
|
Err error
|
||||||
|
File string
|
||||||
|
Index int
|
||||||
|
SQL string
|
||||||
|
Params []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *InsertError) Error() string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"testfixtures: error inserting record: %v, on file: %s, index: %d, sql: %s, params: %v",
|
||||||
|
e.Err,
|
||||||
|
e.File,
|
||||||
|
e.Index,
|
||||||
|
e.SQL,
|
||||||
|
e.Params,
|
||||||
|
)
|
||||||
|
}
|
110
vendor/gopkg.in/testfixtures.v2/generate.go
generated
vendored
Normal file
110
vendor/gopkg.in/testfixtures.v2/generate.go
generated
vendored
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
package testfixtures
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TableInfo is settings for generating a fixture for table.
|
||||||
|
type TableInfo struct {
|
||||||
|
Name string // Table name
|
||||||
|
Where string // A condition for extracting records. If this value is empty, extracts all records.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ti *TableInfo) whereClause() string {
|
||||||
|
if ti.Where == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return fmt.Sprintf(" WHERE %s", ti.Where)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateFixtures generates fixtures for the current contents of a database, and saves
|
||||||
|
// them to the specified directory
|
||||||
|
func GenerateFixtures(db *sql.DB, helper Helper, dir string) error {
|
||||||
|
tables, err := helper.tableNames(db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, table := range tables {
|
||||||
|
filename := path.Join(dir, table+".yml")
|
||||||
|
if err := generateFixturesForTable(db, helper, &TableInfo{Name: table}, filename); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateFixturesForTables generates fixtures for the current contents of specified tables in a database, and saves
|
||||||
|
// them to the specified directory
|
||||||
|
func GenerateFixturesForTables(db *sql.DB, tables []*TableInfo, helper Helper, dir string) error {
|
||||||
|
for _, table := range tables {
|
||||||
|
filename := path.Join(dir, table.Name+".yml")
|
||||||
|
if err := generateFixturesForTable(db, helper, table, filename); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateFixturesForTable(db *sql.DB, h Helper, table *TableInfo, filename string) error {
|
||||||
|
query := fmt.Sprintf("SELECT * FROM %s%s", h.quoteKeyword(table.Name), table.whereClause())
|
||||||
|
rows, err := db.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
columns, err := rows.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fixtures := make([]interface{}, 0, 10)
|
||||||
|
for rows.Next() {
|
||||||
|
entries := make([]interface{}, len(columns))
|
||||||
|
entryPtrs := make([]interface{}, len(entries))
|
||||||
|
for i := range entries {
|
||||||
|
entryPtrs[i] = &entries[i]
|
||||||
|
}
|
||||||
|
if err := rows.Scan(entryPtrs...); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
entryMap := make(map[string]interface{}, len(entries))
|
||||||
|
for i, column := range columns {
|
||||||
|
entryMap[column] = convertValue(entries[i])
|
||||||
|
}
|
||||||
|
fixtures = append(fixtures, entryMap)
|
||||||
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
marshaled, err := yaml.Marshal(fixtures)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = f.Write(marshaled)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertValue(value interface{}) interface{} {
|
||||||
|
switch v := value.(type) {
|
||||||
|
case []byte:
|
||||||
|
if utf8.Valid(v) {
|
||||||
|
return string(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
43
vendor/gopkg.in/testfixtures.v2/helper.go
generated
vendored
43
vendor/gopkg.in/testfixtures.v2/helper.go
generated
vendored
|
@ -18,21 +18,56 @@ type Helper interface {
|
||||||
init(*sql.DB) error
|
init(*sql.DB) error
|
||||||
disableReferentialIntegrity(*sql.DB, loadFunction) error
|
disableReferentialIntegrity(*sql.DB, loadFunction) error
|
||||||
paramType() int
|
paramType() int
|
||||||
databaseName(*sql.DB) string
|
databaseName(queryable) (string, error)
|
||||||
|
tableNames(queryable) ([]string, error)
|
||||||
|
isTableModified(queryable, string) (bool, error)
|
||||||
|
afterLoad(queryable) error
|
||||||
quoteKeyword(string) string
|
quoteKeyword(string) string
|
||||||
whileInsertOnTable(*sql.Tx, string, func() error) error
|
whileInsertOnTable(*sql.Tx, string, func() error) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type queryable interface {
|
||||||
|
Exec(string, ...interface{}) (sql.Result, error)
|
||||||
|
Query(string, ...interface{}) (*sql.Rows, error)
|
||||||
|
QueryRow(string, ...interface{}) *sql.Row
|
||||||
|
}
|
||||||
|
|
||||||
|
// batchSplitter is an interface with method which returns byte slice for
|
||||||
|
// splitting SQL batches. This need to split sql statements and run its
|
||||||
|
// separately.
|
||||||
|
//
|
||||||
|
// For Microsoft SQL Server batch splitter is "GO". For details see
|
||||||
|
// https://docs.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go
|
||||||
|
type batchSplitter interface {
|
||||||
|
splitter() []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ Helper = &MySQL{}
|
||||||
|
_ Helper = &PostgreSQL{}
|
||||||
|
_ Helper = &SQLite{}
|
||||||
|
_ Helper = &Oracle{}
|
||||||
|
_ Helper = &SQLServer{}
|
||||||
|
)
|
||||||
|
|
||||||
type baseHelper struct{}
|
type baseHelper struct{}
|
||||||
|
|
||||||
func (*baseHelper) init(_ *sql.DB) error {
|
func (baseHelper) init(_ *sql.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*baseHelper) quoteKeyword(str string) string {
|
func (baseHelper) quoteKeyword(str string) string {
|
||||||
return fmt.Sprintf(`"%s"`, str)
|
return fmt.Sprintf(`"%s"`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*baseHelper) whileInsertOnTable(_ *sql.Tx, _ string, fn func() error) error {
|
func (baseHelper) whileInsertOnTable(_ *sql.Tx, _ string, fn func() error) error {
|
||||||
return fn()
|
return fn()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (baseHelper) isTableModified(_ queryable, _ string) (bool, error) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (baseHelper) afterLoad(_ queryable) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
44
vendor/gopkg.in/testfixtures.v2/json.go
generated
vendored
Normal file
44
vendor/gopkg.in/testfixtures.v2/json.go
generated
vendored
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package testfixtures
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ driver.Valuer = jsonArray{}
|
||||||
|
_ driver.Valuer = jsonMap{}
|
||||||
|
)
|
||||||
|
|
||||||
|
type jsonArray []interface{}
|
||||||
|
|
||||||
|
func (a jsonArray) Value() (driver.Value, error) {
|
||||||
|
return json.Marshal(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsonMap map[string]interface{}
|
||||||
|
|
||||||
|
func (m jsonMap) Value() (driver.Value, error) {
|
||||||
|
return json.Marshal(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Go refuses to convert map[interface{}]interface{} to JSON because JSON only support string keys
|
||||||
|
// So it's necessary to recursively convert all map[interface]interface{} to map[string]interface{}
|
||||||
|
func recursiveToJSON(v interface{}) (r interface{}) {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case []interface{}:
|
||||||
|
for i, e := range v {
|
||||||
|
v[i] = recursiveToJSON(e)
|
||||||
|
}
|
||||||
|
r = jsonArray(v)
|
||||||
|
case map[interface{}]interface{}:
|
||||||
|
newMap := make(map[string]interface{}, len(v))
|
||||||
|
for k, e := range v {
|
||||||
|
newMap[k.(string)] = recursiveToJSON(e)
|
||||||
|
}
|
||||||
|
r = jsonMap(newMap)
|
||||||
|
default:
|
||||||
|
r = v
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
101
vendor/gopkg.in/testfixtures.v2/mysql.go
generated
vendored
101
vendor/gopkg.in/testfixtures.v2/mysql.go
generated
vendored
|
@ -8,6 +8,18 @@ import (
|
||||||
// MySQL is the MySQL helper for this package
|
// MySQL is the MySQL helper for this package
|
||||||
type MySQL struct {
|
type MySQL struct {
|
||||||
baseHelper
|
baseHelper
|
||||||
|
tables []string
|
||||||
|
tablesChecksum map[string]int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *MySQL) init(db *sql.DB) error {
|
||||||
|
var err error
|
||||||
|
h.tables, err = h.tableNames(db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*MySQL) paramType() int {
|
func (*MySQL) paramType() int {
|
||||||
|
@ -18,28 +30,105 @@ func (*MySQL) quoteKeyword(str string) string {
|
||||||
return fmt.Sprintf("`%s`", str)
|
return fmt.Sprintf("`%s`", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*MySQL) databaseName(db *sql.DB) (dbName string) {
|
func (*MySQL) databaseName(q queryable) (string, error) {
|
||||||
db.QueryRow("SELECT DATABASE()").Scan(&dbName)
|
var dbName string
|
||||||
return
|
err := q.QueryRow("SELECT DATABASE()").Scan(&dbName)
|
||||||
|
return dbName, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MySQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) error {
|
func (h *MySQL) tableNames(q queryable) ([]string, error) {
|
||||||
|
query := `
|
||||||
|
SELECT table_name
|
||||||
|
FROM information_schema.tables
|
||||||
|
WHERE table_schema = ?
|
||||||
|
AND table_type = 'BASE TABLE';
|
||||||
|
`
|
||||||
|
dbName, err := h.databaseName(q)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := q.Query(query, dbName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var tables []string
|
||||||
|
for rows.Next() {
|
||||||
|
var table string
|
||||||
|
if err = rows.Scan(&table); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tables = append(tables, table)
|
||||||
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tables, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *MySQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
|
||||||
// re-enable after load
|
// re-enable after load
|
||||||
defer db.Exec("SET FOREIGN_KEY_CHECKS = 1")
|
defer func() {
|
||||||
|
if _, err2 := db.Exec("SET FOREIGN_KEY_CHECKS = 1"); err2 != nil && err == nil {
|
||||||
|
err = err2
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
if _, err = tx.Exec("SET FOREIGN_KEY_CHECKS = 0"); err != nil {
|
if _, err = tx.Exec("SET FOREIGN_KEY_CHECKS = 0"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = loadFn(tx); err != nil {
|
if err = loadFn(tx); err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *MySQL) isTableModified(q queryable, tableName string) (bool, error) {
|
||||||
|
checksum, err := h.getChecksum(q, tableName)
|
||||||
|
if err != nil {
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
|
||||||
|
oldChecksum := h.tablesChecksum[tableName]
|
||||||
|
|
||||||
|
return oldChecksum == 0 || checksum != oldChecksum, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *MySQL) afterLoad(q queryable) error {
|
||||||
|
if h.tablesChecksum != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
h.tablesChecksum = make(map[string]int64, len(h.tables))
|
||||||
|
for _, t := range h.tables {
|
||||||
|
checksum, err := h.getChecksum(q, t)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
h.tablesChecksum[t] = checksum
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *MySQL) getChecksum(q queryable, tableName string) (int64, error) {
|
||||||
|
sql := fmt.Sprintf("CHECKSUM TABLE %s", h.quoteKeyword(tableName))
|
||||||
|
var (
|
||||||
|
table string
|
||||||
|
checksum int64
|
||||||
|
)
|
||||||
|
if err := q.QueryRow(sql).Scan(&table, &checksum); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return checksum, nil
|
||||||
|
}
|
||||||
|
|
83
vendor/gopkg.in/testfixtures.v2/oracle.go
generated
vendored
83
vendor/gopkg.in/testfixtures.v2/oracle.go
generated
vendored
|
@ -43,54 +43,90 @@ func (*Oracle) quoteKeyword(str string) string {
|
||||||
return fmt.Sprintf("\"%s\"", strings.ToUpper(str))
|
return fmt.Sprintf("\"%s\"", strings.ToUpper(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*Oracle) databaseName(db *sql.DB) (dbName string) {
|
func (*Oracle) databaseName(q queryable) (string, error) {
|
||||||
db.QueryRow("SELECT user FROM DUAL").Scan(&dbName)
|
var dbName string
|
||||||
return
|
err := q.QueryRow("SELECT user FROM DUAL").Scan(&dbName)
|
||||||
|
return dbName, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*Oracle) getEnabledConstraints(db *sql.DB) ([]oracleConstraint, error) {
|
func (*Oracle) tableNames(q queryable) ([]string, error) {
|
||||||
constraints := make([]oracleConstraint, 0)
|
query := `
|
||||||
rows, err := db.Query(`
|
SELECT TABLE_NAME
|
||||||
SELECT table_name, constraint_name
|
FROM USER_TABLES
|
||||||
FROM user_constraints
|
`
|
||||||
WHERE constraint_type = 'R'
|
rows, err := q.Query(query)
|
||||||
AND status = 'ENABLED'
|
|
||||||
`)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
|
var tables []string
|
||||||
|
for rows.Next() {
|
||||||
|
var table string
|
||||||
|
if err = rows.Scan(&table); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tables = append(tables, table)
|
||||||
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tables, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*Oracle) getEnabledConstraints(q queryable) ([]oracleConstraint, error) {
|
||||||
|
var constraints []oracleConstraint
|
||||||
|
rows, err := q.Query(`
|
||||||
|
SELECT table_name, constraint_name
|
||||||
|
FROM user_constraints
|
||||||
|
WHERE constraint_type = 'R'
|
||||||
|
AND status = 'ENABLED'
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var constraint oracleConstraint
|
var constraint oracleConstraint
|
||||||
rows.Scan(&constraint.tableName, &constraint.constraintName)
|
rows.Scan(&constraint.tableName, &constraint.constraintName)
|
||||||
constraints = append(constraints, constraint)
|
constraints = append(constraints, constraint)
|
||||||
}
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return constraints, nil
|
return constraints, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*Oracle) getSequences(db *sql.DB) ([]string, error) {
|
func (*Oracle) getSequences(q queryable) ([]string, error) {
|
||||||
sequences := make([]string, 0)
|
var sequences []string
|
||||||
rows, err := db.Query("SELECT sequence_name FROM user_sequences")
|
rows, err := q.Query("SELECT sequence_name FROM user_sequences")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var sequence string
|
var sequence string
|
||||||
rows.Scan(&sequence)
|
if err = rows.Scan(&sequence); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
sequences = append(sequences, sequence)
|
sequences = append(sequences, sequence)
|
||||||
}
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return sequences, nil
|
return sequences, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Oracle) resetSequences(db *sql.DB) error {
|
func (h *Oracle) resetSequences(q queryable) error {
|
||||||
for _, sequence := range h.sequences {
|
for _, sequence := range h.sequences {
|
||||||
_, err := db.Exec(fmt.Sprintf("DROP SEQUENCE %s", h.quoteKeyword(sequence)))
|
_, err := q.Exec(fmt.Sprintf("DROP SEQUENCE %s", h.quoteKeyword(sequence)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = db.Exec(fmt.Sprintf("CREATE SEQUENCE %s START WITH %d", h.quoteKeyword(sequence), resetSequencesTo))
|
_, err = q.Exec(fmt.Sprintf("CREATE SEQUENCE %s START WITH %d", h.quoteKeyword(sequence), resetSequencesTo))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -98,11 +134,14 @@ func (h *Oracle) resetSequences(db *sql.DB) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Oracle) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) error {
|
func (h *Oracle) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
|
||||||
// re-enable after load
|
// re-enable after load
|
||||||
defer func() {
|
defer func() {
|
||||||
for _, c := range h.enabledConstraints {
|
for _, c := range h.enabledConstraints {
|
||||||
db.Exec(fmt.Sprintf("ALTER TABLE %s ENABLE CONSTRAINT %s", h.quoteKeyword(c.tableName), h.quoteKeyword(c.constraintName)))
|
_, err2 := db.Exec(fmt.Sprintf("ALTER TABLE %s ENABLE CONSTRAINT %s", h.quoteKeyword(c.tableName), h.quoteKeyword(c.constraintName)))
|
||||||
|
if err2 != nil && err == nil {
|
||||||
|
err = err2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -118,9 +157,9 @@ func (h *Oracle) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) er
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
if err = loadFn(tx); err != nil {
|
if err = loadFn(tx); err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
150
vendor/gopkg.in/testfixtures.v2/postgresql.go
generated
vendored
150
vendor/gopkg.in/testfixtures.v2/postgresql.go
generated
vendored
|
@ -3,6 +3,7 @@ package testfixtures
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PostgreSQL is the PG helper for this package
|
// PostgreSQL is the PG helper for this package
|
||||||
|
@ -18,6 +19,7 @@ type PostgreSQL struct {
|
||||||
tables []string
|
tables []string
|
||||||
sequences []string
|
sequences []string
|
||||||
nonDeferrableConstraints []pgConstraint
|
nonDeferrableConstraints []pgConstraint
|
||||||
|
tablesChecksum map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type pgConstraint struct {
|
type pgConstraint struct {
|
||||||
|
@ -28,7 +30,7 @@ type pgConstraint struct {
|
||||||
func (h *PostgreSQL) init(db *sql.DB) error {
|
func (h *PostgreSQL) init(db *sql.DB) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
h.tables, err = h.getTables(db)
|
h.tables, err = h.tableNames(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -50,44 +52,57 @@ func (*PostgreSQL) paramType() int {
|
||||||
return paramTypeDollar
|
return paramTypeDollar
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*PostgreSQL) databaseName(db *sql.DB) (dbName string) {
|
func (*PostgreSQL) databaseName(q queryable) (string, error) {
|
||||||
db.QueryRow("SELECT current_database()").Scan(&dbName)
|
var dbName string
|
||||||
return
|
err := q.QueryRow("SELECT current_database()").Scan(&dbName)
|
||||||
|
return dbName, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *PostgreSQL) getTables(db *sql.DB) ([]string, error) {
|
func (h *PostgreSQL) tableNames(q queryable) ([]string, error) {
|
||||||
var tables []string
|
var tables []string
|
||||||
|
|
||||||
sql := `
|
sql := `
|
||||||
SELECT table_name
|
SELECT pg_namespace.nspname || '.' || pg_class.relname
|
||||||
FROM information_schema.tables
|
FROM pg_class
|
||||||
WHERE table_schema = 'public'
|
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
|
||||||
AND table_type = 'BASE TABLE';
|
WHERE pg_class.relkind = 'r'
|
||||||
`
|
AND pg_namespace.nspname NOT IN ('pg_catalog', 'information_schema')
|
||||||
rows, err := db.Query(sql)
|
AND pg_namespace.nspname NOT LIKE 'pg_toast%';
|
||||||
|
`
|
||||||
|
rows, err := q.Query(sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var table string
|
var table string
|
||||||
rows.Scan(&table)
|
if err = rows.Scan(&table); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
tables = append(tables, table)
|
tables = append(tables, table)
|
||||||
}
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return tables, nil
|
return tables, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *PostgreSQL) getSequences(db *sql.DB) ([]string, error) {
|
func (h *PostgreSQL) getSequences(q queryable) ([]string, error) {
|
||||||
var sequences []string
|
const sql = `
|
||||||
|
SELECT pg_namespace.nspname || '.' || pg_class.relname AS sequence_name
|
||||||
|
FROM pg_class
|
||||||
|
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
|
||||||
|
WHERE pg_class.relkind = 'S'
|
||||||
|
`
|
||||||
|
|
||||||
sql := "SELECT relname FROM pg_class WHERE relkind = 'S'"
|
rows, err := q.Query(sql)
|
||||||
rows, err := db.Query(sql)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
|
var sequences []string
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var sequence string
|
var sequence string
|
||||||
if err = rows.Scan(&sequence); err != nil {
|
if err = rows.Scan(&sequence); err != nil {
|
||||||
|
@ -95,18 +110,22 @@ func (h *PostgreSQL) getSequences(db *sql.DB) ([]string, error) {
|
||||||
}
|
}
|
||||||
sequences = append(sequences, sequence)
|
sequences = append(sequences, sequence)
|
||||||
}
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return sequences, nil
|
return sequences, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*PostgreSQL) getNonDeferrableConstraints(db *sql.DB) ([]pgConstraint, error) {
|
func (*PostgreSQL) getNonDeferrableConstraints(q queryable) ([]pgConstraint, error) {
|
||||||
var constraints []pgConstraint
|
var constraints []pgConstraint
|
||||||
|
|
||||||
sql := `
|
sql := `
|
||||||
SELECT table_name, constraint_name
|
SELECT table_schema || '.' || table_name, constraint_name
|
||||||
FROM information_schema.table_constraints
|
FROM information_schema.table_constraints
|
||||||
WHERE constraint_type = 'FOREIGN KEY'
|
WHERE constraint_type = 'FOREIGN KEY'
|
||||||
AND is_deferrable = 'NO'`
|
AND is_deferrable = 'NO'
|
||||||
rows, err := db.Query(sql)
|
`
|
||||||
|
rows, err := q.Query(sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -114,23 +133,27 @@ WHERE constraint_type = 'FOREIGN KEY'
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var constraint pgConstraint
|
var constraint pgConstraint
|
||||||
err = rows.Scan(&constraint.tableName, &constraint.constraintName)
|
if err = rows.Scan(&constraint.tableName, &constraint.constraintName); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
constraints = append(constraints, constraint)
|
constraints = append(constraints, constraint)
|
||||||
}
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return constraints, nil
|
return constraints, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *PostgreSQL) disableTriggers(db *sql.DB, loadFn loadFunction) error {
|
func (h *PostgreSQL) disableTriggers(db *sql.DB, loadFn loadFunction) (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
// re-enable triggers after load
|
// re-enable triggers after load
|
||||||
var sql string
|
var sql string
|
||||||
for _, table := range h.tables {
|
for _, table := range h.tables {
|
||||||
sql += fmt.Sprintf("ALTER TABLE %s ENABLE TRIGGER ALL;", h.quoteKeyword(table))
|
sql += fmt.Sprintf("ALTER TABLE %s ENABLE TRIGGER ALL;", h.quoteKeyword(table))
|
||||||
}
|
}
|
||||||
db.Exec(sql)
|
if _, err2 := db.Exec(sql); err2 != nil && err == nil {
|
||||||
|
err = err2
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
|
@ -154,14 +177,16 @@ func (h *PostgreSQL) disableTriggers(db *sql.DB, loadFn loadFunction) error {
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *PostgreSQL) makeConstraintsDeferrable(db *sql.DB, loadFn loadFunction) error {
|
func (h *PostgreSQL) makeConstraintsDeferrable(db *sql.DB, loadFn loadFunction) (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
// ensure constraint being not deferrable again after load
|
// ensure constraint being not deferrable again after load
|
||||||
var sql string
|
var sql string
|
||||||
for _, constraint := range h.nonDeferrableConstraints {
|
for _, constraint := range h.nonDeferrableConstraints {
|
||||||
sql += fmt.Sprintf("ALTER TABLE %s ALTER CONSTRAINT %s NOT DEFERRABLE;", h.quoteKeyword(constraint.tableName), h.quoteKeyword(constraint.constraintName))
|
sql += fmt.Sprintf("ALTER TABLE %s ALTER CONSTRAINT %s NOT DEFERRABLE;", h.quoteKeyword(constraint.tableName), h.quoteKeyword(constraint.constraintName))
|
||||||
}
|
}
|
||||||
db.Exec(sql)
|
if _, err2 := db.Exec(sql); err2 != nil && err == nil {
|
||||||
|
err = err2
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var sql string
|
var sql string
|
||||||
|
@ -176,28 +201,31 @@ func (h *PostgreSQL) makeConstraintsDeferrable(db *sql.DB, loadFn loadFunction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
if _, err = tx.Exec("SET CONSTRAINTS ALL DEFERRED"); err != nil {
|
if _, err = tx.Exec("SET CONSTRAINTS ALL DEFERRED"); err != nil {
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = loadFn(tx); err != nil {
|
if err = loadFn(tx); err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *PostgreSQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) error {
|
func (h *PostgreSQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
|
||||||
// ensure sequences being reset after load
|
// ensure sequences being reset after load
|
||||||
defer h.resetSequences(db)
|
defer func() {
|
||||||
|
if err2 := h.resetSequences(db); err2 != nil && err == nil {
|
||||||
|
err = err2
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if h.UseAlterConstraint {
|
if h.UseAlterConstraint {
|
||||||
return h.makeConstraintsDeferrable(db, loadFn)
|
return h.makeConstraintsDeferrable(db, loadFn)
|
||||||
} else {
|
|
||||||
return h.disableTriggers(db, loadFn)
|
|
||||||
}
|
}
|
||||||
|
return h.disableTriggers(db, loadFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *PostgreSQL) resetSequences(db *sql.DB) error {
|
func (h *PostgreSQL) resetSequences(db *sql.DB) error {
|
||||||
|
@ -209,3 +237,53 @@ func (h *PostgreSQL) resetSequences(db *sql.DB) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *PostgreSQL) isTableModified(q queryable, tableName string) (bool, error) {
|
||||||
|
checksum, err := h.getChecksum(q, tableName)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
oldChecksum := h.tablesChecksum[tableName]
|
||||||
|
|
||||||
|
return oldChecksum == "" || checksum != oldChecksum, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *PostgreSQL) afterLoad(q queryable) error {
|
||||||
|
if h.tablesChecksum != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
h.tablesChecksum = make(map[string]string, len(h.tables))
|
||||||
|
for _, t := range h.tables {
|
||||||
|
checksum, err := h.getChecksum(q, t)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
h.tablesChecksum[t] = checksum
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *PostgreSQL) getChecksum(q queryable, tableName string) (string, error) {
|
||||||
|
sqlStr := fmt.Sprintf(`
|
||||||
|
SELECT md5(CAST((array_agg(t.*)) AS TEXT))
|
||||||
|
FROM %s AS t
|
||||||
|
`,
|
||||||
|
h.quoteKeyword(tableName),
|
||||||
|
)
|
||||||
|
|
||||||
|
var checksum sql.NullString
|
||||||
|
if err := q.QueryRow(sqlStr).Scan(&checksum); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return checksum.String, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*PostgreSQL) quoteKeyword(s string) string {
|
||||||
|
parts := strings.Split(s, ".")
|
||||||
|
for i, p := range parts {
|
||||||
|
parts[i] = fmt.Sprintf(`"%s"`, p)
|
||||||
|
}
|
||||||
|
return strings.Join(parts, ".")
|
||||||
|
}
|
||||||
|
|
54
vendor/gopkg.in/testfixtures.v2/sqlite.go
generated
vendored
54
vendor/gopkg.in/testfixtures.v2/sqlite.go
generated
vendored
|
@ -14,23 +14,59 @@ func (*SQLite) paramType() int {
|
||||||
return paramTypeQuestion
|
return paramTypeQuestion
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*SQLite) databaseName(db *sql.DB) (dbName string) {
|
func (*SQLite) databaseName(q queryable) (string, error) {
|
||||||
var seq int
|
var seq int
|
||||||
var main string
|
var main, dbName string
|
||||||
db.QueryRow("PRAGMA database_list").Scan(&seq, &main, &dbName)
|
err := q.QueryRow("PRAGMA database_list").Scan(&seq, &main, &dbName)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
dbName = filepath.Base(dbName)
|
dbName = filepath.Base(dbName)
|
||||||
return
|
return dbName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*SQLite) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) error {
|
func (*SQLite) tableNames(q queryable) ([]string, error) {
|
||||||
|
query := `
|
||||||
|
SELECT name
|
||||||
|
FROM sqlite_master
|
||||||
|
WHERE type = 'table';
|
||||||
|
`
|
||||||
|
rows, err := q.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var tables []string
|
||||||
|
for rows.Next() {
|
||||||
|
var table string
|
||||||
|
if err = rows.Scan(&table); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tables = append(tables, table)
|
||||||
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return tables, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*SQLite) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
|
||||||
|
defer func() {
|
||||||
|
if _, err2 := db.Exec("PRAGMA defer_foreign_keys = OFF"); err2 != nil && err == nil {
|
||||||
|
err = err2
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if _, err = db.Exec("PRAGMA defer_foreign_keys = ON"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
if _, err = tx.Exec("PRAGMA defer_foreign_keys = ON"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = loadFn(tx); err != nil {
|
if err = loadFn(tx); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
75
vendor/gopkg.in/testfixtures.v2/sqlserver.go
generated
vendored
75
vendor/gopkg.in/testfixtures.v2/sqlserver.go
generated
vendored
|
@ -3,6 +3,7 @@ package testfixtures
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SQLServer is the helper for SQL Server for this package.
|
// SQLServer is the helper for SQL Server for this package.
|
||||||
|
@ -16,7 +17,7 @@ type SQLServer struct {
|
||||||
func (h *SQLServer) init(db *sql.DB) error {
|
func (h *SQLServer) init(db *sql.DB) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
h.tables, err = h.getTables(db)
|
h.tables, err = h.tableNames(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -28,46 +29,62 @@ func (*SQLServer) paramType() int {
|
||||||
return paramTypeQuestion
|
return paramTypeQuestion
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*SQLServer) quoteKeyword(str string) string {
|
func (*SQLServer) quoteKeyword(s string) string {
|
||||||
return fmt.Sprintf("[%s]", str)
|
parts := strings.Split(s, ".")
|
||||||
|
for i, p := range parts {
|
||||||
|
parts[i] = fmt.Sprintf(`[%s]`, p)
|
||||||
|
}
|
||||||
|
return strings.Join(parts, ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*SQLServer) databaseName(db *sql.DB) (dbname string) {
|
func (*SQLServer) databaseName(q queryable) (string, error) {
|
||||||
db.QueryRow("SELECT DB_NAME()").Scan(&dbname)
|
var dbName string
|
||||||
return
|
err := q.QueryRow("SELECT DB_NAME()").Scan(&dbName)
|
||||||
|
return dbName, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*SQLServer) getTables(db *sql.DB) ([]string, error) {
|
func (*SQLServer) tableNames(q queryable) ([]string, error) {
|
||||||
rows, err := db.Query("SELECT table_name FROM information_schema.tables")
|
rows, err := q.Query("SELECT table_schema + '.' + table_name FROM information_schema.tables")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tables := make([]string, 0)
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
|
var tables []string
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var table string
|
var table string
|
||||||
rows.Scan(&table)
|
if err = rows.Scan(&table); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
tables = append(tables, table)
|
tables = append(tables, table)
|
||||||
}
|
}
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return tables, nil
|
return tables, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*SQLServer) tableHasIdentityColumn(tx *sql.Tx, tableName string) bool {
|
func (h *SQLServer) tableHasIdentityColumn(q queryable, tableName string) bool {
|
||||||
sql := `
|
sql := `
|
||||||
SELECT COUNT(*)
|
SELECT COUNT(*)
|
||||||
FROM SYS.IDENTITY_COLUMNS
|
FROM SYS.IDENTITY_COLUMNS
|
||||||
WHERE OBJECT_NAME(OBJECT_ID) = ?
|
WHERE OBJECT_ID = OBJECT_ID(?)
|
||||||
`
|
`
|
||||||
var count int
|
var count int
|
||||||
tx.QueryRow(sql, tableName).Scan(&count)
|
q.QueryRow(sql, h.quoteKeyword(tableName)).Scan(&count)
|
||||||
return count > 0
|
return count > 0
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SQLServer) whileInsertOnTable(tx *sql.Tx, tableName string, fn func() error) error {
|
func (h *SQLServer) whileInsertOnTable(tx *sql.Tx, tableName string, fn func() error) (err error) {
|
||||||
if h.tableHasIdentityColumn(tx, tableName) {
|
if h.tableHasIdentityColumn(tx, tableName) {
|
||||||
defer tx.Exec(fmt.Sprintf("SET IDENTITY_INSERT %s OFF", h.quoteKeyword(tableName)))
|
defer func() {
|
||||||
|
_, err2 := tx.Exec(fmt.Sprintf("SET IDENTITY_INSERT %s OFF", h.quoteKeyword(tableName)))
|
||||||
|
if err2 != nil && err == nil {
|
||||||
|
err = err2
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
_, err := tx.Exec(fmt.Sprintf("SET IDENTITY_INSERT %s ON", h.quoteKeyword(tableName)))
|
_, err := tx.Exec(fmt.Sprintf("SET IDENTITY_INSERT %s ON", h.quoteKeyword(tableName)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -76,19 +93,19 @@ func (h *SQLServer) whileInsertOnTable(tx *sql.Tx, tableName string, fn func() e
|
||||||
return fn()
|
return fn()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SQLServer) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) error {
|
func (h *SQLServer) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
|
||||||
// ensure the triggers are re-enable after all
|
// ensure the triggers are re-enable after all
|
||||||
defer func() {
|
defer func() {
|
||||||
sql := ""
|
var sql string
|
||||||
for _, table := range h.tables {
|
for _, table := range h.tables {
|
||||||
sql += fmt.Sprintf("ALTER TABLE %s WITH CHECK CHECK CONSTRAINT ALL;", h.quoteKeyword(table))
|
sql += fmt.Sprintf("ALTER TABLE %s WITH CHECK CHECK CONSTRAINT ALL;", h.quoteKeyword(table))
|
||||||
}
|
}
|
||||||
if _, err := db.Exec(sql); err != nil {
|
if _, err2 := db.Exec(sql); err2 != nil && err == nil {
|
||||||
fmt.Printf("Error on re-enabling constraints: %v\n", err)
|
err = err2
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
sql := ""
|
var sql string
|
||||||
for _, table := range h.tables {
|
for _, table := range h.tables {
|
||||||
sql += fmt.Sprintf("ALTER TABLE %s NOCHECK CONSTRAINT ALL;", h.quoteKeyword(table))
|
sql += fmt.Sprintf("ALTER TABLE %s NOCHECK CONSTRAINT ALL;", h.quoteKeyword(table))
|
||||||
}
|
}
|
||||||
|
@ -100,11 +117,19 @@ func (h *SQLServer) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
if err = loadFn(tx); err != nil {
|
if err = loadFn(tx); err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// splitter is a batchSplitter interface implementation. We need it for
|
||||||
|
// SQL Server because commands like a `CREATE SCHEMA...` and a `CREATE TABLE...`
|
||||||
|
// could not be executed in the same batch.
|
||||||
|
// See https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms175502(v=sql.105)#rules-for-using-batches
|
||||||
|
func (*SQLServer) splitter() []byte {
|
||||||
|
return []byte("GO\n")
|
||||||
|
}
|
||||||
|
|
90
vendor/gopkg.in/testfixtures.v2/testfixtures.go
generated
vendored
90
vendor/gopkg.in/testfixtures.v2/testfixtures.go
generated
vendored
|
@ -2,7 +2,6 @@ package testfixtures
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path"
|
"path"
|
||||||
|
@ -33,22 +32,10 @@ type insertSQL struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrWrongCastNotAMap is returned when a map is not a map[interface{}]interface{}
|
|
||||||
ErrWrongCastNotAMap = errors.New("Could not cast record: not a map[interface{}]interface{}")
|
|
||||||
|
|
||||||
// ErrFileIsNotSliceOrMap is returned the the fixture file is not a slice or map.
|
|
||||||
ErrFileIsNotSliceOrMap = errors.New("The fixture file is not a slice or map")
|
|
||||||
|
|
||||||
// ErrKeyIsNotString is returned when a record is not of type string
|
|
||||||
ErrKeyIsNotString = errors.New("Record map key is not string")
|
|
||||||
|
|
||||||
// ErrNotTestDatabase is returned when the database name doesn't contains "test"
|
|
||||||
ErrNotTestDatabase = errors.New(`Loading aborted because the database name does not contains "test"`)
|
|
||||||
|
|
||||||
dbnameRegexp = regexp.MustCompile("(?i)test")
|
dbnameRegexp = regexp.MustCompile("(?i)test")
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewFolder craetes a context for all fixtures in a given folder into the database:
|
// NewFolder creates a context for all fixtures in a given folder into the database:
|
||||||
// NewFolder(db, &PostgreSQL{}, "my/fixtures/folder")
|
// NewFolder(db, &PostgreSQL{}, "my/fixtures/folder")
|
||||||
func NewFolder(db *sql.DB, helper Helper, folderName string) (*Context, error) {
|
func NewFolder(db *sql.DB, helper Helper, folderName string) (*Context, error) {
|
||||||
fixtures, err := fixturesFromFolder(folderName)
|
fixtures, err := fixturesFromFolder(folderName)
|
||||||
|
@ -64,7 +51,7 @@ func NewFolder(db *sql.DB, helper Helper, folderName string) (*Context, error) {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFiles craetes a context for all specified fixtures files into database:
|
// NewFiles creates a context for all specified fixtures files into database:
|
||||||
// NewFiles(db, &PostgreSQL{},
|
// NewFiles(db, &PostgreSQL{},
|
||||||
// "fixtures/customers.yml",
|
// "fixtures/customers.yml",
|
||||||
// "fixtures/orders.yml"
|
// "fixtures/orders.yml"
|
||||||
|
@ -102,27 +89,55 @@ func newContext(db *sql.DB, helper Helper, fixtures []*fixtureFile) (*Context, e
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DetectTestDatabase returns nil if databaseName matches regexp
|
||||||
|
// if err := fixtures.DetectTestDatabase(); err != nil {
|
||||||
|
// log.Fatal(err)
|
||||||
|
// }
|
||||||
|
func (c *Context) DetectTestDatabase() error {
|
||||||
|
dbName, err := c.helper.databaseName(c.db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !dbnameRegexp.MatchString(dbName) {
|
||||||
|
return ErrNotTestDatabase
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Load wipes and after load all fixtures in the database.
|
// Load wipes and after load all fixtures in the database.
|
||||||
// if err := fixtures.Load(); err != nil {
|
// if err := fixtures.Load(); err != nil {
|
||||||
// log.Fatal(err)
|
// log.Fatal(err)
|
||||||
// }
|
// }
|
||||||
func (c *Context) Load() error {
|
func (c *Context) Load() error {
|
||||||
if !skipDatabaseNameCheck {
|
if !skipDatabaseNameCheck {
|
||||||
if !dbnameRegexp.MatchString(c.helper.databaseName(c.db)) {
|
if err := c.DetectTestDatabase(); err != nil {
|
||||||
return ErrNotTestDatabase
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := c.helper.disableReferentialIntegrity(c.db, func(tx *sql.Tx) error {
|
err := c.helper.disableReferentialIntegrity(c.db, func(tx *sql.Tx) error {
|
||||||
for _, file := range c.fixturesFiles {
|
for _, file := range c.fixturesFiles {
|
||||||
|
modified, err := c.helper.isTableModified(tx, file.fileNameWithoutExtension())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !modified {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if err := file.delete(tx, c.helper); err != nil {
|
if err := file.delete(tx, c.helper); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err := c.helper.whileInsertOnTable(tx, file.fileNameWithoutExtension(), func() error {
|
err = c.helper.whileInsertOnTable(tx, file.fileNameWithoutExtension(), func() error {
|
||||||
for _, i := range file.insertSQLs {
|
for j, i := range file.insertSQLs {
|
||||||
if _, err := tx.Exec(i.sql, i.params...); err != nil {
|
if _, err := tx.Exec(i.sql, i.params...); err != nil {
|
||||||
return err
|
return &InsertError{
|
||||||
|
Err: err,
|
||||||
|
File: file.fileName,
|
||||||
|
Index: j,
|
||||||
|
SQL: i.sql,
|
||||||
|
Params: i.params,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -133,7 +148,10 @@ func (c *Context) Load() error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.helper.afterLoad(c.db)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) buildInsertSQLs() error {
|
func (c *Context) buildInsertSQLs() error {
|
||||||
|
@ -204,25 +222,33 @@ func (f *fixtureFile) buildInsertSQL(h Helper, record map[interface{}]interface{
|
||||||
|
|
||||||
sqlColumns = append(sqlColumns, h.quoteKeyword(keyStr))
|
sqlColumns = append(sqlColumns, h.quoteKeyword(keyStr))
|
||||||
|
|
||||||
|
// if string, try convert to SQL or time
|
||||||
|
// if map or array, convert to json
|
||||||
|
switch v := value.(type) {
|
||||||
|
case string:
|
||||||
|
if strings.HasPrefix(v, "RAW=") {
|
||||||
|
sqlValues = append(sqlValues, strings.TrimPrefix(v, "RAW="))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if t, err := tryStrToDate(v); err == nil {
|
||||||
|
value = t
|
||||||
|
}
|
||||||
|
case []interface{}, map[interface{}]interface{}:
|
||||||
|
value = recursiveToJSON(v)
|
||||||
|
}
|
||||||
|
|
||||||
switch h.paramType() {
|
switch h.paramType() {
|
||||||
case paramTypeDollar:
|
case paramTypeDollar:
|
||||||
sqlValues = append(sqlValues, fmt.Sprintf("$%d", i))
|
sqlValues = append(sqlValues, fmt.Sprintf("$%d", i))
|
||||||
case paramTypeQuestion:
|
case paramTypeQuestion:
|
||||||
sqlValues = append(sqlValues, "?")
|
sqlValues = append(sqlValues, "?")
|
||||||
case paramTypeColon:
|
case paramTypeColon:
|
||||||
switch {
|
sqlValues = append(sqlValues, fmt.Sprintf(":%d", i))
|
||||||
case isDateTime(value):
|
|
||||||
sqlValues = append(sqlValues, fmt.Sprintf("to_date(:%d, 'YYYY-MM-DD HH24:MI:SS')", i))
|
|
||||||
case isDate(value):
|
|
||||||
sqlValues = append(sqlValues, fmt.Sprintf("to_date(:%d, 'YYYY-MM-DD')", i))
|
|
||||||
case isTime(value):
|
|
||||||
sqlValues = append(sqlValues, fmt.Sprintf("to_date(:%d, 'HH24:MI:SS')", i))
|
|
||||||
default:
|
|
||||||
sqlValues = append(sqlValues, fmt.Sprintf(":%d", i))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
i++
|
|
||||||
values = append(values, value)
|
values = append(values, value)
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlStr = fmt.Sprintf(
|
sqlStr = fmt.Sprintf(
|
||||||
|
|
54
vendor/gopkg.in/testfixtures.v2/time.go
generated
vendored
54
vendor/gopkg.in/testfixtures.v2/time.go
generated
vendored
|
@ -1,36 +1,34 @@
|
||||||
package testfixtures
|
package testfixtures
|
||||||
|
|
||||||
import "regexp"
|
import (
|
||||||
|
"errors"
|
||||||
var (
|
"time"
|
||||||
regexpDate = regexp.MustCompile("\\d\\d\\d\\d-\\d\\d-\\d\\d")
|
|
||||||
regexpDateTime = regexp.MustCompile("\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d")
|
|
||||||
regexpTime = regexp.MustCompile("\\d\\d:\\d\\d:\\d\\d")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func isDate(value interface{}) bool {
|
var timeFormats = []string{
|
||||||
str, isStr := value.(string)
|
"2006-01-02",
|
||||||
if !isStr {
|
"2006-01-02 15:04",
|
||||||
return false
|
"2006-01-02 15:04:05",
|
||||||
}
|
"20060102",
|
||||||
|
"20060102 15:04",
|
||||||
return regexpDate.MatchString(str)
|
"20060102 15:04:05",
|
||||||
|
"02/01/2006",
|
||||||
|
"02/01/2006 15:04",
|
||||||
|
"02/01/2006 15:04:05",
|
||||||
|
"2006-01-02T15:04-07:00",
|
||||||
|
"2006-01-02T15:04:05-07:00",
|
||||||
}
|
}
|
||||||
|
|
||||||
func isDateTime(value interface{}) bool {
|
// ErrCouldNotConvertToTime is returns when a string is not a reconizable time format
|
||||||
str, isStr := value.(string)
|
var ErrCouldNotConvertToTime = errors.New("Could not convert string to time")
|
||||||
if !isStr {
|
|
||||||
return false
|
func tryStrToDate(s string) (time.Time, error) {
|
||||||
|
for _, f := range timeFormats {
|
||||||
|
t, err := time.ParseInLocation(f, s, time.Local)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return t, nil
|
||||||
}
|
}
|
||||||
|
return time.Time{}, ErrCouldNotConvertToTime
|
||||||
return regexpDateTime.MatchString(str)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isTime(value interface{}) bool {
|
|
||||||
str, isStr := value.(string)
|
|
||||||
if !isStr {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return regexpTime.MatchString(str)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue