@@ -107,35 +107,63 @@ dropAllTablesFromSchema <- function(connectionDetails, schema) {
107107 length(schema ) == 1
108108 )
109109 stopifnot(connectionDetails $ dbms %in% c(" postgresql" , " redshift" , " sql server" , " oracle" ))
110- tableNames <- listTablesInSchema(connectionDetails , schema )
111-
112- if (length(tableNames ) == 0 ) {
113- return (invisible (NULL )) # No tables to drop
114- }
115-
110+
116111 con <- DatabaseConnector :: connect(connectionDetails )
117112 on.exit(DatabaseConnector :: disconnect(con ))
118113 dbms <- connectionDetails $ dbms
119114
120- # For all databases, use DROP TABLE IF EXISTS ... CASCADE
121- # but quote schema and table names properly
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
122139 if (dbms %in% c(" postgresql" , " redshift" )) {
123- # PostgreSQL/Redshift: quote identifiers
124- for (tableName in tableNames ) {
125- sql <- paste0(' DROP TABLE IF EXISTS "' , schema , ' "."' , tableName , ' " CASCADE' )
126- DBI :: dbExecute(con , sql )
127- }
140+ # PostgreSQL/Redshift: Disable constraints and drop all tables
141+ 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+ }
148+ }, error = function (e ) {
149+ # Silent fallback - the for loop above still attempts drops
150+ invisible (NULL )
151+ })
128152 } else if (dbms == " sql server" ) {
129- # SQL Server: use brackets for identifiers
130- for (tableName in tableNames ) {
131- sql <- paste0(' DROP TABLE IF EXISTS [' , schema , ' ].[' , tableName , ' ]' )
132- DBI :: dbExecute(con , sql )
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+ )
133159 }
134160 } else if (dbms == " oracle" ) {
135- # Oracle: drop each table individually
136- for (tableName in tableNames ) {
137- sql <- paste0(' DROP TABLE "' , tableName , ' " CASCADE CONSTRAINTS' )
138- DBI :: dbExecute(con , sql )
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+ )
139167 }
140168 }
141169}
0 commit comments