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

Commit 3d66325

Browse files
committed
Add Oracle, Redshift
1 parent 49e8a4f commit 3d66325

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

data_diff/sqeleton/databases/oracle.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
TimestampTZ,
1515
FractionalType,
1616
)
17-
from ..abcs.mixins import AbstractMixin_MD5, AbstractMixin_NormalizeValue
18-
from .base import BaseDialect, ThreadedDatabase, import_helper, ConnectError, QueryError
17+
from ..abcs.mixins import AbstractMixin_MD5, AbstractMixin_NormalizeValue, AbstractMixin_Schema
18+
from ..abcs import Compilable
19+
from ..queries import this, table, SKIP
20+
from .base import BaseDialect, ThreadedDatabase, import_helper, ConnectError, QueryError, Mixin_Schema
1921
from .base import TIMESTAMP_PRECISION_POS
2022

2123
SESSION_TIME_ZONE = None # Changed by the tests
@@ -57,8 +59,19 @@ def normalize_number(self, value: str, coltype: FractionalType) -> str:
5759
format_str += "0." + "9" * (coltype.precision - 1) + "0"
5860
return f"to_char({value}, '{format_str}')"
5961

62+
class Mixin_Schema(AbstractMixin_Schema):
63+
def list_tables(self, table_schema: str, like: Compilable = None) -> Compilable:
64+
return (
65+
table('ALL_TABLES')
66+
.where(
67+
this.OWNER == table_schema,
68+
this.TABLE_NAME.like(like) if like is not None else SKIP,
69+
)
70+
.select(table_name = this.TABLE_NAME)
71+
)
72+
6073

61-
class Dialect(BaseDialect):
74+
class Dialect(BaseDialect, Mixin_Schema):
6275
name = "Oracle"
6376
SUPPORTS_PRIMARY_KEY = True
6477
TYPE_CLASSES: Dict[str, type] = {
@@ -73,7 +86,7 @@ class Dialect(BaseDialect):
7386
ROUNDS_ON_PREC_LOSS = True
7487

7588
def quote(self, s: str):
76-
return f"{s}"
89+
return f'"{s}"'
7790

7891
def to_string(self, s: str):
7992
return f"cast({s} as varchar(1024))"
@@ -143,7 +156,7 @@ class Oracle(ThreadedDatabase):
143156
def __init__(self, *, host, database, thread_count, **kw):
144157
self.kwargs = dict(dsn=f"{host}/{database}" if database else host, **kw)
145158

146-
self.default_schema = kw.get("user")
159+
self.default_schema = kw.get("user").upper()
147160

148161
super().__init__(thread_count=thread_count)
149162

@@ -168,5 +181,5 @@ def select_table_schema(self, path: DbPath) -> str:
168181

169182
return (
170183
f"SELECT column_name, data_type, 6 as datetime_precision, data_precision as numeric_precision, data_scale as numeric_scale"
171-
f" FROM ALL_TAB_COLUMNS WHERE table_name = '{table.upper()}' AND owner = '{schema.upper()}'"
184+
f" FROM ALL_TAB_COLUMNS WHERE table_name = '{table}' AND owner = '{schema}'"
172185
)

tests/sqeleton/test_database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
TEST_DATABASES = {
1616
dbs.MySQL,
1717
dbs.PostgreSQL,
18-
# dbs.Oracle,
19-
# dbs.Redshift,
18+
dbs.Oracle,
19+
dbs.Redshift,
2020
dbs.Snowflake,
2121
dbs.DuckDB,
2222
dbs.BigQuery,
@@ -61,7 +61,7 @@ def test_table_list(self):
6161
assert not db.query(q)
6262

6363
db.query(tbl.create())
64-
assert db.query(q, List[str] ) == [name]
64+
self.assertEqual( db.query(q, List[str] ), [name])
6565

6666
db.query( tbl.drop() )
6767
assert not db.query(q)

tests/test_database_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,13 @@ def _create_indexes(conn, table):
602602

603603
try:
604604
if_not_exists = "IF NOT EXISTS" if not isinstance(conn, (db.MySQL, db.Oracle)) else ""
605+
quote = conn.dialect.quote
605606
conn.query(
606-
f"CREATE INDEX {if_not_exists} xa_{table[1:-1]} ON {table} (id, col)",
607+
f"CREATE INDEX {if_not_exists} xa_{table[1:-1]} ON {table} ({quote('id')}, {quote('col')})",
607608
None,
608609
)
609610
conn.query(
610-
f"CREATE INDEX {if_not_exists} xb_{table[1:-1]} ON {table} (id)",
611+
f"CREATE INDEX {if_not_exists} xb_{table[1:-1]} ON {table} ({quote('id')})",
611612
None,
612613
)
613614
except Exception as err:

0 commit comments

Comments
 (0)