Skip to content

Fix escaping sequence names in health check #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/postgres_mcp/database_health/sequence_health_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ def _parse_sequence_name(self, default_value: str) -> tuple[str, str]:
# Remove nextval and cast parts
clean_value = default_value.replace("nextval('", "").replace("'::regclass)", "")
clean_value = clean_value.replace("('", "").replace("'::text)", "")
# We're going to feed these to sql.Identifier, so we need to remove quotes, or else
# they will be double quoted.
clean_value = clean_value.replace('"', "")

# Split into schema and sequence
parts = clean_value.split(".")
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/database_health/test_database_health_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def setup_test_tables(sql_driver):
conn_pool = await pool_wrapper.pool_connect()
async with conn_pool.connection() as conn:
# Drop existing tables if they exist
await conn.execute("DROP TABLE IF EXISTS test_orders")
await conn.execute("DROP TABLE IF EXISTS \"TestOrders\"")
await conn.execute("DROP TABLE IF EXISTS test_customers")
await conn.execute("DROP SEQUENCE IF EXISTS test_seq")

Expand All @@ -42,7 +42,7 @@ async def setup_test_tables(sql_driver):

await conn.execute(
"""
CREATE TABLE test_orders (
CREATE TABLE "TestOrders" (
id SERIAL PRIMARY KEY,
customer_id INTEGER REFERENCES test_customers(id),
total DECIMAL NOT NULL CHECK (total >= 0),
Expand All @@ -55,23 +55,23 @@ async def setup_test_tables(sql_driver):
# Create some indexes to test index health
await conn.execute(
"""
CREATE INDEX idx_orders_customer ON test_orders(customer_id)
CREATE INDEX idx_orders_customer ON "TestOrders"(customer_id)
"""
)
await conn.execute(
"""
CREATE INDEX idx_orders_status ON test_orders(status)
CREATE INDEX idx_orders_status ON "TestOrders"(status)
"""
)
await conn.execute(
"""
CREATE INDEX idx_orders_created ON test_orders(created_at)
CREATE INDEX idx_orders_created ON "TestOrders"(created_at)
"""
)
# Create a duplicate index to test duplicate index detection
await conn.execute(
"""
CREATE INDEX idx_orders_customer_dup ON test_orders(customer_id)
CREATE INDEX idx_orders_customer_dup ON "TestOrders"(customer_id)
"""
)

Expand All @@ -88,7 +88,7 @@ async def setup_test_tables(sql_driver):

await conn.execute(
"""
INSERT INTO test_orders (customer_id, total, status)
INSERT INTO "TestOrders" (customer_id, total, status)
SELECT
(random() * 99)::int + 1, -- Changed to ensure IDs are between 1 and 100
(random() * 1000)::decimal,
Expand All @@ -103,15 +103,15 @@ async def setup_test_tables(sql_driver):

# Run ANALYZE to update statistics
await conn.execute("ANALYZE test_customers")
await conn.execute("ANALYZE test_orders")
await conn.execute("ANALYZE \"TestOrders\"")


async def cleanup_test_tables(sql_driver):
pool_wrapper = sql_driver.connect()
conn_pool = await pool_wrapper.pool_connect()
try:
async with conn_pool.connection() as conn:
await conn.execute("DROP TABLE IF EXISTS test_orders")
await conn.execute("DROP TABLE IF EXISTS \"TestOrders\"")
await conn.execute("DROP TABLE IF EXISTS test_customers")
await conn.execute("DROP SEQUENCE IF EXISTS test_seq")
finally:
Expand Down