-
Notifications
You must be signed in to change notification settings - Fork 12
Add Amazon Redshift connector (#2) #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import psycopg2 | ||
| import pandas as pd | ||
|
|
||
| def connect(host: str, port: int, database: str, user: str, password: str): | ||
| conn = psycopg2.connect( | ||
| host=host, | ||
| port=port, | ||
| database=database, | ||
| user=user, | ||
| password=password | ||
| ) | ||
| return conn | ||
|
|
||
| def list_tables(host: str, port: int, database: str, user: str, password: str) -> list: | ||
| conn = connect(host, port, database, user, password) | ||
| cursor = conn.cursor() | ||
| cursor.execute(""" | ||
| SELECT table_name | ||
| FROM information_schema.tables | ||
| WHERE table_schema = 'public' | ||
| ORDER BY table_name | ||
| """) | ||
| tables = [row[0] for row in cursor.fetchall()] | ||
| cursor.close() | ||
| conn.close() | ||
| return tables | ||
|
|
||
| def fetch_table(host: str, port: int, database: str, user: str, password: str, table: str, limit: int = 1000) -> pd.DataFrame: | ||
| conn = connect(host, port, database, user, password) | ||
| cursor = conn.cursor() | ||
|
|
||
| try: | ||
| # Validate table exists to prevent SQL injection | ||
| valid_tables = list_tables(host, port, database, user, password) | ||
| if table not in valid_tables: | ||
| raise ValueError(f"Table '{table}' not found in database. Available tables: {', '.join(valid_tables)}") | ||
|
|
||
| cursor.execute(f"SELECT * FROM {table} LIMIT {limit}") | ||
| columns = [desc[0] for desc in cursor.description] | ||
| rows = cursor.fetchall() | ||
| return pd.DataFrame(rows, columns=columns) | ||
| finally: | ||
| cursor.close() | ||
| conn.close() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,6 +128,75 @@ def test_demo_csv_has_rows(self): | |
| assert len(df) > 0, "Demo CSV is empty" | ||
|
|
||
|
|
||
| class FakeRedshiftCursor: | ||
|
|
||
| def __init__(self): | ||
| self.description = None | ||
| self._rows = [] | ||
| self.executed_query = None | ||
|
|
||
| def execute(self, query): | ||
| self.executed_query = query | ||
| if "information_schema.tables" in query: | ||
| self._rows = [("customers",), ("transactions",)] | ||
| self.description = [("table_name",)] | ||
| else: | ||
| self._rows = [ | ||
| ("TXN001", "Product A", 10.00), | ||
| ("TXN002", "Product B", 15.00), | ||
| ] | ||
| self.description = [("transaction_id",), ("product_name",), ("unit_price",)] | ||
|
|
||
| def fetchall(self): | ||
| return self._rows | ||
|
|
||
| def close(self): | ||
| pass | ||
|
|
||
|
|
||
| class FakeRedshiftConnection: | ||
|
|
||
| def __init__(self): | ||
| self._cursor = FakeRedshiftCursor() | ||
|
|
||
| def cursor(self): | ||
| return self._cursor | ||
|
|
||
| def close(self): | ||
| pass | ||
|
|
||
|
|
||
| class TestRedshiftConnector: | ||
|
|
||
| @pytest.fixture | ||
| def fake_connection(self, monkeypatch): | ||
| from src.connectors import redshift | ||
| conn = FakeRedshiftConnection() | ||
| monkeypatch.setattr(redshift.psycopg2, "connect", lambda **kwargs: conn) | ||
| return conn | ||
|
|
||
| def test_list_tables(self, fake_connection): | ||
| from src.connectors.redshift import list_tables | ||
| tables = list_tables("host", 5439, "dev", "awsuser", "password") | ||
| assert tables == ["customers", "transactions"] | ||
|
|
||
| def test_fetch_table(self, fake_connection): | ||
| from src.connectors.redshift import fetch_table | ||
| df = fetch_table("host", 5439, "dev", "awsuser", "password", "transactions") | ||
| assert len(df) == 2 | ||
| assert list(df.columns) == ["transaction_id", "product_name", "unit_price"] | ||
|
|
||
| def test_fetch_table_applies_limit(self, fake_connection): | ||
| from src.connectors.redshift import fetch_table | ||
| fetch_table("host", 5439, "dev", "awsuser", "password", "transactions", limit=500) | ||
| assert "LIMIT 500" in fake_connection.cursor().executed_query | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this test in 087c44c — it passes along with the rest of the suite. |
||
| def test_fetch_table_validates_table_name(self, fake_connection): | ||
| from src.connectors.redshift import fetch_table | ||
| with pytest.raises(ValueError, match="not found"): | ||
| fetch_table("host", 5439, "dev", "awsuser", "password", "nonexistent_table") | ||
|
|
||
|
|
||
| class TestPDFExists: | ||
|
|
||
| def test_demo_pdf_exists(self): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
` think we can use following code as its Doesn't work if user wants to query views or temporary tables (though they'd need to be in the whitelist query) as well now
actually it will resolve postgres and mysql issue also.
`def fetch_table(host: str, port: int, database: str, user: str, password: str, table: str, limit: int = 1000) -> pd.DataFrame:
"""Fetch a table from Redshift with SQL injection protection."""
conn = connect(host, port, database, user, password)
cursor = conn.cursor()
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied in 087c44c.
fetch_tablenow validates the table name againstlist_tables()before querying and raises aValueErrorlisting the available tables if it's not found. Cursor/connection cleanup is now handled in afinallyblock. The same pattern would also apply to the Postgres and MySQL connectors — happy to address those in a follow-up PR.