diff --git a/postgres.go b/postgres.go index 83c37e1f..4b124bd2 100644 --- a/postgres.go +++ b/postgres.go @@ -76,13 +76,13 @@ func (s *postgres) Quote(key string) string { func (s *postgres) HasTable(scope *Scope, tableName string) bool { var count int - scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = ? AND table_type = 'BASE TABLE'", tableName).Row().Scan(&count) + scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_schema = current_schema AND table_name = ? AND (table_type = 'BASE TABLE' OR table_type = 'TABLE')", tableName).Row().Scan(&count) return count > 0 } func (s *postgres) HasColumn(scope *Scope, tableName string, columnName string) bool { var count int - scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_name = ? AND column_name = ?", tableName, columnName).Row().Scan(&count) + scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_schema = current_schema AND table_name = ? AND column_name = ?", tableName, columnName).Row().Scan(&count) return count > 0 } @@ -91,8 +91,19 @@ func (s *postgres) RemoveIndex(scope *Scope, indexName string) { } func (s *postgres) HasIndex(scope *Scope, tableName string, indexName string) bool { + // Determine whehter the pg_indexes talbe is + detect := []int{} //make([]int, 2) // If detect[0] == 1 then we have postgres, else if detect[1] == 1 then we have detectationDB. + 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 count int - scope.NewDB().Raw("SELECT count(*) FROM pg_indexes WHERE tablename = ? AND indexname = ?", tableName, indexName).Row().Scan(&count) + var indexQuery string + if detect[0] == 1 { // Then we have postgres. + indexQuery = "SELECT count(*) FROM pg_indexes WHERE tablename = ? AND indexname = ?" + } else if detect[1] == 1 { // Then we have FoundationDB. + indexQuery = "SELECT count(*) FROM INFORMATION_SCHEMA.indexes WHERE table_schema = current_schema AND table_name = ? AND index_name = ?" + } else { + panic("unable to query for indexes, unrecognized database schema layout") + } + scope.NewDB().Raw(indexQuery, tableName, indexName).Row().Scan(&count) return count > 0 }