Skip to content

Commit 10e7f0a

Browse files
Update setup.R
1 parent da85e56 commit 10e7f0a

1 file changed

Lines changed: 60 additions & 47 deletions

File tree

tests/testthat/setup.R

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -112,58 +112,71 @@ dropAllTablesFromSchema <- function(connectionDetails, schema) {
112112
on.exit(DatabaseConnector::disconnect(con))
113113
dbms <- connectionDetails$dbms
114114

115-
# Get table names using the same connection
116-
if (dbms %in% c("postgresql", "redshift", "sql server")) {
117-
tables <- DBI::dbGetQuery(
118-
con,
119-
paste0(
120-
"select table_name from information_schema.tables where table_schema = '",
121-
schema,
122-
"'"
123-
)
124-
)[[1]]
125-
} else if (dbms == "oracle") {
126-
query <- paste0(
127-
"select table_name from all_tables where owner = '",
128-
toupper(schema),
129-
"' and tablespace_name = 'USERS'"
130-
)
131-
tables <- DBI::dbGetQuery(con, query)[[1]]
132-
}
133-
134-
if (length(tables) == 0) {
135-
return(invisible(NULL)) # No tables to drop
136-
}
137-
138-
# Now drop all tables with proper CASCADE handling
139115
if (dbms %in% c("postgresql", "redshift")) {
140-
# PostgreSQL/Redshift: Disable constraints and drop all tables
116+
# PostgreSQL/Redshift: Use DROP SCHEMA CASCADE - most reliable approach
117+
# This drops the schema and recreates it empty
141118
tryCatch({
142-
# Set constraints to deferred mode
143-
DBI::dbExecute(con, "SET CONSTRAINTS ALL DEFERRED")
144-
# Drop all tables with CASCADE
145-
for (tableName in tables) {
146-
DBI::dbExecute(con, paste0('DROP TABLE IF EXISTS "', schema, '"."', tableName, '" CASCADE'))
147-
}
119+
# Drop the schema and all its objects
120+
DBI::dbExecute(con, paste0('DROP SCHEMA IF EXISTS "', schema, '" CASCADE'))
121+
# Recreate the empty schema
122+
DBI::dbExecute(con, paste0('CREATE SCHEMA IF NOT EXISTS "', schema, '"'))
148123
}, error = function(e) {
149-
# Silent fallback - the for loop above still attempts drops
150-
invisible(NULL)
124+
# Fallback: try to list and drop tables individually
125+
tryCatch({
126+
tables <- DBI::dbGetQuery(
127+
con,
128+
paste0(
129+
"SELECT table_name FROM information_schema.tables ",
130+
"WHERE lower(table_schema) = lower('", schema, "')"
131+
)
132+
)[[1]]
133+
134+
if (length(tables) > 0 && !is.null(tables)) {
135+
for (tableName in tables) {
136+
tryCatch({
137+
DBI::dbExecute(con, paste0('DROP TABLE IF EXISTS "', schema, '"."', tableName, '" CASCADE'))
138+
}, error = function(e2) invisible(NULL))
139+
}
140+
}
141+
}, error = function(e2) invisible(NULL))
151142
})
152143
} else if (dbms == "sql server") {
153-
# SQL Server: drop each table
154-
for (tableName in tables) {
155-
tryCatch(
156-
DBI::dbExecute(con, paste0('DROP TABLE IF EXISTS [', schema, '].[', tableName, ']')),
157-
error = function(e2) invisible(NULL)
158-
)
159-
}
144+
# SQL Server: List and drop each table
145+
tryCatch({
146+
tables <- DBI::dbGetQuery(
147+
con,
148+
paste0(
149+
"SELECT table_name FROM information_schema.tables ",
150+
"WHERE table_schema = '", schema, "'"
151+
)
152+
)[[1]]
153+
154+
if (length(tables) > 0 && !is.null(tables)) {
155+
for (tableName in tables) {
156+
tryCatch({
157+
DBI::dbExecute(con, paste0('DROP TABLE IF EXISTS [', schema, '].[', tableName, ']'))
158+
}, error = function(e2) invisible(NULL))
159+
}
160+
}
161+
}, error = function(e2) invisible(NULL))
160162
} else if (dbms == "oracle") {
161-
# Oracle: drop each table with CASCADE CONSTRAINTS
162-
for (tableName in tables) {
163-
tryCatch(
164-
DBI::dbExecute(con, paste0('DROP TABLE "', tableName, '" CASCADE CONSTRAINTS')),
165-
error = function(e2) invisible(NULL)
166-
)
167-
}
163+
# Oracle: List and drop each table
164+
tryCatch({
165+
tables <- DBI::dbGetQuery(
166+
con,
167+
paste0(
168+
"SELECT table_name FROM all_tables ",
169+
"WHERE owner = '", toupper(schema), "'"
170+
)
171+
)[[1]]
172+
173+
if (length(tables) > 0 && !is.null(tables)) {
174+
for (tableName in tables) {
175+
tryCatch({
176+
DBI::dbExecute(con, paste0('DROP TABLE "', tableName, '" CASCADE CONSTRAINTS'))
177+
}, error = function(e2) invisible(NULL))
178+
}
179+
}
180+
}, error = function(e2) invisible(NULL))
168181
}
169182
}

0 commit comments

Comments
 (0)