Skip to content

Commit 7f067a5

Browse files
committed
Maintain backward compatibility.
1 parent 4d49c42 commit 7f067a5

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

cpp/deeplake_pg/table_storage.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,39 @@ void table_storage::load_table_metadata()
200200
return;
201201
}
202202

203+
// Backward compatibility: Check if table_oid column exists
204+
// If not, drop and recreate the table with the correct schema
205+
if (!pg::utils::check_column_exists("pg_deeplake_tables", "table_oid")) {
206+
base::log_warning(base::log_channel::generic,
207+
"Detected old schema for pg_deeplake_tables without table_oid column. "
208+
"Dropping and recreating table to match current schema.");
209+
210+
pg::utils::spi_connector connector;
211+
const char* drop_query = "DROP TABLE IF EXISTS public.pg_deeplake_tables CASCADE";
212+
if (SPI_execute(drop_query, false, 0) != SPI_OK_UTILITY) {
213+
base::log_warning(base::log_channel::generic, "Failed to drop old pg_deeplake_tables table");
214+
}
215+
216+
const char* create_query =
217+
"CREATE TABLE public.pg_deeplake_tables ("
218+
" id SERIAL PRIMARY KEY,"
219+
" table_oid OID NOT NULL UNIQUE,"
220+
" table_name NAME NOT NULL UNIQUE,"
221+
" ds_path TEXT NOT NULL UNIQUE"
222+
")";
223+
if (SPI_execute(create_query, false, 0) != SPI_OK_UTILITY) {
224+
base::log_warning(base::log_channel::generic, "Failed to create new pg_deeplake_tables table");
225+
}
226+
227+
const char* grant_query = "GRANT SELECT, INSERT, UPDATE, DELETE ON public.pg_deeplake_tables TO PUBLIC";
228+
if (SPI_execute(grant_query, false, 0) != SPI_OK_UTILITY) {
229+
base::log_warning(base::log_channel::generic, "Failed to grant permissions on pg_deeplake_tables table");
230+
}
231+
232+
// Table is now empty, so we can return early
233+
return;
234+
}
235+
203236
const char* query = "SELECT table_oid, table_name, ds_path FROM public.pg_deeplake_tables";
204237

205238
pg::utils::spi_connector connector;

cpp/deeplake_pg/utils.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,15 @@ inline bool check_table_exists(const std::string& table_name, const std::string&
663663
return SPI_execute(query.c_str(), true, 0) == SPI_OK_SELECT && SPI_processed > 0;
664664
}
665665

666+
inline bool check_column_exists(const std::string& table_name, const std::string& column_name, const std::string& schema_name = "public")
667+
{
668+
std::string query = fmt::format("SELECT 1 FROM information_schema.columns WHERE "
669+
"table_schema = '{}' AND table_name = '{}' AND column_name = '{}'",
670+
schema_name, table_name, column_name);
671+
spi_connector connector;
672+
return SPI_execute(query.c_str(), true, 0) == SPI_OK_SELECT && SPI_processed > 0;
673+
}
674+
666675
} // namespace utils
667676

668677
} // namespace pg

0 commit comments

Comments
 (0)