Skip to content

Commit

Permalink
Fix bug to support sqlalchemy v1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jvasquezrojas committed Sep 12, 2024
1 parent f753913 commit 6f70a48
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ def __init__(
**kw: Any,
) -> None:
if self.__table_prefix__ != "":
kw.update(prefixes=kw.get("prefixes", []) + [self.__table_prefix__])
super().__init__(name, metadata, *args, **kw)
prefixes = kw.get("prefixes", []) + [self.__table_prefix__]
kw.update(prefixes=prefixes)
if kw["alternative_initializer"] and hasattr(super(), "_init"):
super()._init(name, metadata, *args, **kw)
else:
super().__init__(name, metadata, *args, **kw)

if not kw.get("autoload_with", False):
self._validate_table()

Expand Down
15 changes: 14 additions & 1 deletion src/snowflake/sqlalchemy/sql/custom_schema/dynamic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import typing
from typing import Any

from sqlalchemy.exc import ArgumentError
from sqlalchemy.sql.schema import MetaData, SchemaItem

from snowflake.sqlalchemy.custom_commands import NoneType
Expand Down Expand Up @@ -49,6 +50,16 @@ def __init__(
return
super().__init__(name, metadata, *args, **kw)

def _init(
self,
name: str,
metadata: MetaData,
*args: SchemaItem,
**kw: Any,
) -> None:
kw["alternative_initializer"] = True
super().__init__(name, metadata, *args, **kw)

def _validate_table(self):
missing_attributes = []
if self.target_lag is NoneType:
Expand All @@ -58,7 +69,7 @@ def _validate_table(self):
if self.as_query is NoneType:
missing_attributes.append("AsQuery")
if missing_attributes:
raise exc.ArgumentError(
raise ArgumentError(
"DYNAMIC TABLE must have the following arguments: %s"
% ", ".join(missing_attributes)
)
Expand All @@ -70,5 +81,7 @@ def __repr__(self) -> str:
+ [repr(self.metadata)]
+ [repr(x) for x in self.columns]
+ [repr(self.target_lag)]
+ [repr(self.warehouse)]
+ [repr(self.as_query)]
+ [f"{k}={repr(getattr(self, k))}" for k in ["schema"]]
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import typing
from typing import Any, Optional

import sqlalchemy
from sqlalchemy.sql import Selectable
from sqlalchemy.sql.schema import Column, MetaData, SchemaItem
from sqlalchemy.util import NoneType
Expand Down Expand Up @@ -53,7 +52,7 @@ def __has_defined_columns(self, items: typing.List[SchemaItem]) -> bool:
def __create_columns_from_selectable(
self, selectable: Selectable
) -> Optional[typing.List[Column]]:
if not isinstance(selectable, sqlalchemy.Selectable):
if not isinstance(selectable, Selectable):
return
columns: typing.List[Column] = []
for _, c in selectable.exported_columns.items():
Expand Down

0 comments on commit 6f70a48

Please sign in to comment.