Reworked broken db-type query to ensure accurate detection.

This commit is contained in:
Jay Taylor 2015-03-17 11:19:51 -07:00
parent a1f1d1e42a
commit 49bb1e19c2

View File

@ -91,14 +91,15 @@ func (s *postgres) RemoveIndex(scope *Scope, indexName string) {
} }
func (s *postgres) HasIndex(scope *Scope, tableName string, indexName string) bool { func (s *postgres) HasIndex(scope *Scope, tableName string, indexName string) bool {
// Determine whehter the pg_indexes talbe is // Determine whether we're using a postgres db or foundation.
detect := []int{} //make([]int, 2) // If detect[0] == 1 then we have postgres, else if detect[1] == 1 then we have detectationDB. var isPg int
scope.NewDB().Raw(`SELECT count(*) "detect" FROM INFORMATION_SCHEMA.tables WHERE table_name = 'pg_indexes' UNION SELECT count(*) "detect" FROM INFORMATION_SCHEMA.tables WHERE table_name = 'indexes'`).Pluck("detect", &detect) var isFdb int
scope.NewDB().Raw(`SELECT (SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = 'pg_indexes') "pg", (SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = 'indexes') "fdb"`).Row().Scan(&isPg, &isFdb)
var count int var count int
var indexQuery string var indexQuery string
if detect[0] == 1 { // Then we have postgres. if isPg == 1 { // Then we have postgres.
indexQuery = "SELECT count(*) FROM pg_indexes WHERE tablename = ? AND indexname = ?" indexQuery = "SELECT count(*) FROM pg_indexes WHERE tablename = ? AND indexname = ?"
} else if detect[1] == 1 { // Then we have FoundationDB. } else if isFdb == 1 { // Then we have FoundationDB.
indexQuery = "SELECT count(*) FROM INFORMATION_SCHEMA.indexes WHERE table_schema = current_schema AND table_name = ? AND index_name = ?" indexQuery = "SELECT count(*) FROM INFORMATION_SCHEMA.indexes WHERE table_schema = current_schema AND table_name = ? AND index_name = ?"
} else { } else {
panic("unable to query for indexes, unrecognized database schema layout") panic("unable to query for indexes, unrecognized database schema layout")