Skip to content

Commit 141e958

Browse files
Merge pull request #42 from alexander-zuev/fix/migration-test-unique-names
fix(tests): error in duplicate migration names
2 parents 2a07a57 + 89eaa76 commit 141e958

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

tests/test_tools.py

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,48 +165,30 @@ async def test_execute_postgresql_medium_risk_unsafe_mode(self, initialized_cont
165165
safety_manager = initialized_container_integration.safety_manager
166166
safety_manager.set_safety_mode(ClientType.DATABASE, SafetyMode.UNSAFE)
167167

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}"
181172

182173
# Store migration names created during this test for cleanup
183174
test_migration_names = []
184175

185176
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} (
195180
id SERIAL PRIMARY KEY,
196181
value TEXT
197182
);
198183
"""
199184

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)
206188

207189
# 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');
210192
"""
211193

212194
# 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
243225
except Exception as e:
244226
print(f"Failed to clean up test migrations: {e}")
245227

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+
246235
# Reset safety mode
247236
safety_manager.set_safety_mode(ClientType.DATABASE, SafetyMode.SAFE)
248237

0 commit comments

Comments
 (0)