Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 1bae429

Browse files
committed
Tests: Rewrite create index code to be simpler
1 parent 81e2f47 commit 1bae429

File tree

6 files changed

+12
-31
lines changed

6 files changed

+12
-31
lines changed

data_diff/sqeleton/databases/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def list_tables(self, table_schema: str, like: Compilable = None) -> Compilable:
121121

122122
class BaseDialect(AbstractDialect):
123123
SUPPORTS_PRIMARY_KEY = False
124+
SUPPORTS_INDEXES = False
124125
TYPE_CLASSES: Dict[str, type] = {}
125126

126127
def offset_limit(self, offset: Optional[int] = None, limit: Optional[int] = None):

data_diff/sqeleton/databases/duckdb.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Dialect(BaseDialect, Mixin_Schema):
5858
name = "DuckDB"
5959
ROUNDS_ON_PREC_LOSS = False
6060
SUPPORTS_PRIMARY_KEY = True
61+
SUPPORTS_INDEXES = True
6162

6263
TYPE_CLASSES = {
6364
# Timestamps

data_diff/sqeleton/databases/mysql.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Dialect(BaseDialect, Mixin_Schema):
5151
name = "MySQL"
5252
ROUNDS_ON_PREC_LOSS = True
5353
SUPPORTS_PRIMARY_KEY = True
54+
SUPPORTS_INDEXES = True
5455
TYPE_CLASSES = {
5556
# Dates
5657
"datetime": Datetime,

data_diff/sqeleton/databases/oracle.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def list_tables(self, table_schema: str, like: Compilable = None) -> Compilable:
7474
class Dialect(BaseDialect, Mixin_Schema):
7575
name = "Oracle"
7676
SUPPORTS_PRIMARY_KEY = True
77+
SUPPORTS_INDEXES = True
7778
TYPE_CLASSES: Dict[str, type] = {
7879
"NUMBER": Decimal,
7980
"FLOAT": Float,

data_diff/sqeleton/databases/postgresql.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class PostgresqlDialect(BaseDialect, Mixin_Schema):
5252
name = "PostgreSQL"
5353
ROUNDS_ON_PREC_LOSS = True
5454
SUPPORTS_PRIMARY_KEY = True
55+
SUPPORTS_INDEXES = True
5556

5657
TYPE_CLASSES = {
5758
# Timestamps

tests/test_database_types.py

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -592,37 +592,9 @@ def _insert_to_table(conn, table_path, values, type):
592592
conn.query(commit)
593593

594594

595-
def _create_indexes(conn, table):
596-
# It is unfortunate that Presto doesn't support creating indexes...
597-
# Technically we could create it in the backing Postgres behind the scenes.
598-
if isinstance(
599-
conn, (db.Snowflake, db.Redshift, db.Presto, db.BigQuery, db.Databricks, db.Trino, db.Clickhouse, db.Vertica)
600-
):
601-
return
602-
603-
try:
604-
quote = conn.dialect.quote
605-
conn.query(
606-
f"CREATE INDEX xa_{table[1:-1]} ON {table} ({quote('id')}, {quote('col')})",
607-
None,
608-
)
609-
conn.query(
610-
f"CREATE INDEX xb_{table[1:-1]} ON {table} ({quote('id')})",
611-
None,
612-
)
613-
except Exception as err:
614-
if "Duplicate key name" in str(err): # mysql
615-
pass
616-
elif "such column list already indexed" in str(err): # oracle
617-
pass
618-
elif "name is already used" in str(err): # oracle
619-
pass
620-
else:
621-
raise (err)
622-
623-
624595
def _create_table_with_indexes(conn, table_path, type_):
625-
table_name = ".".join(map(conn.dialect.quote, table_path))
596+
quote = conn.dialect.quote
597+
table_name = ".".join(map(quote, table_path))
626598

627599
tbl = table(
628600
table_path,
@@ -637,7 +609,11 @@ def _create_table_with_indexes(conn, table_path, type_):
637609
else:
638610
conn.query(tbl.create())
639611

640-
_create_indexes(conn, table_name)
612+
if conn.dialect.SUPPORTS_INDEXES:
613+
index_id ,= table_path
614+
conn.query(f"CREATE INDEX xa_{index_id} ON {table_name} ({quote('id')}, {quote('col')})")
615+
conn.query(f"CREATE INDEX xb_{index_id} ON {table_name} ({quote('id')})")
616+
641617
conn.query(commit)
642618

643619

0 commit comments

Comments
 (0)