@@ -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;
0 commit comments