@@ -165,48 +165,30 @@ async def test_execute_postgresql_medium_risk_unsafe_mode(self, initialized_cont
165
165
safety_manager = initialized_container_integration .safety_manager
166
166
safety_manager .set_safety_mode (ClientType .DATABASE , SafetyMode .UNSAFE )
167
167
168
- # Clean up any existing migrations with this name to avoid unique constraint violations
169
- cleanup_query = """
170
- DELETE FROM supabase_migrations.schema_migrations
171
- WHERE name LIKE '%create_public_test_values%';
172
- """
173
- from supabase_mcp .services .database .sql .models import QueryValidationResults
174
-
175
- validation_result = QueryValidationResults (
176
- statements = [],
177
- highest_risk_level = OperationRiskLevel .MEDIUM ,
178
- original_query = cleanup_query ,
179
- )
180
- await postgres_client .execute_query_async (validation_result , readonly = False )
168
+ # Generate a unique table name for this test run to avoid migration conflicts
169
+ unique_suffix = str (uuid .uuid4 ()).replace ("-" , "" )[:8 ]
170
+ test_table_name = f"test_values_{ unique_suffix } "
171
+ migration_name_pattern = f"create_public_{ test_table_name } "
181
172
182
173
# Store migration names created during this test for cleanup
183
174
test_migration_names = []
184
175
185
176
try :
186
- # First create a test table if it doesn't exist
187
- await query_manager .handle_query (
188
- "CREATE TABLE IF NOT EXISTS public.test_values (id SERIAL PRIMARY KEY, value TEXT);"
189
- )
190
- # Store migration name pattern for cleanup
191
- test_migration_names .append ("create_public_test_values" )
192
-
193
- create_table_query = """
194
- CREATE TABLE IF NOT EXISTS public.test_values (
177
+ # First create a test table if it doesn't exist with a unique name
178
+ create_table_query = f"""
179
+ CREATE TABLE IF NOT EXISTS public.{ test_table_name } (
195
180
id SERIAL PRIMARY KEY,
196
181
value TEXT
197
182
);
198
183
"""
199
184
200
- # This might require confirmation since it's a DDL operation
201
- try :
202
- await query_manager .handle_query (create_table_query )
203
- except ConfirmationRequiredError :
204
- # We can't confirm in tests, so we'll skip this part
205
- pass
185
+ await query_manager .handle_query (create_table_query )
186
+ # Store migration name pattern for cleanup
187
+ test_migration_names .append (migration_name_pattern )
206
188
207
189
# Now test a MEDIUM risk operation (INSERT)
208
- medium_risk_query = """
209
- INSERT INTO public.test_values (value) VALUES ('test_value');
190
+ medium_risk_query = f """
191
+ INSERT INTO public.{ test_table_name } (value) VALUES ('test_value');
210
192
"""
211
193
212
194
# This should NOT raise an error in UNSAFE mode
@@ -243,6 +225,13 @@ async def test_execute_postgresql_medium_risk_unsafe_mode(self, initialized_cont
243
225
except Exception as e :
244
226
print (f"Failed to clean up test migrations: { e } " )
245
227
228
+ # Drop the test table
229
+ try :
230
+ drop_table_query = f"DROP TABLE IF EXISTS public.{ test_table_name } ;"
231
+ await query_manager .handle_query (drop_table_query )
232
+ except Exception as e :
233
+ print (f"Failed to drop test table: { e } " )
234
+
246
235
# Reset safety mode
247
236
safety_manager .set_safety_mode (ClientType .DATABASE , SafetyMode .SAFE )
248
237
0 commit comments