Skip to content

Commit

Permalink
Fix various compatibility issues with database containers and less re…
Browse files Browse the repository at this point in the history
…cent Python versions
  • Loading branch information
hunyadi committed Aug 5, 2024
1 parent 9f98563 commit a0200e2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ jobs:
- 1433:1433
# set health checks to wait until Microsoft SQL Server has started
options: >-
--health-cmd "/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -Q 'SELECT 1' -b"
--health-cmd "/opt/mssql-tools18/bin/sqlcmd -S localhost -N -C -U sa -Q 'SELECT @@VERSION' -b"
--health-interval 10s
--health-timeout 5s
--health-retries 5
Expand Down
89 changes: 60 additions & 29 deletions pysqlsync/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,42 +1111,73 @@ async def _get_transformer(
"found lookup table column %s in table %s", column.name, table.name
)

enum_dict: dict[str, int]
values: set[Optional[str]] = set()

if field_type is str:
# a single enumeration value represented as a string
if isinstance(records, Iterable):
for record in records:
values.add(record[index])
elif isinstance(records, AsyncIterable):
async for record in records:
values.add(record[index])
else:
raise TypeError("expected: `Iterable` or `AsyncIterable` of records")
values.discard(None) # do not insert NULL into referenced table
enum_dict = await self._merge_lookup_table(
relation, typing.cast(set[str], values)
return await self._get_enum_transformer(
relation,
records,
generator,
index,
)
return generator.get_enum_transformer(enum_dict)
elif field_type is list or field_type is set:
# a list of enumeration values represented as a list of strings
if isinstance(records, Iterable):
for record in records:
values.update(record[index])
elif isinstance(records, AsyncIterable):
async for record in records:
values.update(record[index])
else:
raise TypeError("expected: `Iterable` or `AsyncIterable` of records")
values.discard(None) # do not insert NULL into referenced table
enum_dict = await self._merge_lookup_table(
relation, typing.cast(set[str], values)
return await self._get_enum_list_transformer(
relation,
records,
generator,
index,
)
return generator.get_enum_list_transformer(enum_dict)
else:
return transformer

async def _get_enum_transformer(
self,
relation: Table,
records: RecordIterable,
generator: BaseGenerator,
index: int,
) -> Optional[Callable[[Any], Any]]:
"Returns a callable function object that looks up an assigned integer value based on the enumeration string value."

# a single enumeration value represented as a string
values: set[Optional[str]] = set()
if isinstance(records, Iterable):
for record in records:
values.add(record[index])
elif isinstance(records, AsyncIterable):
async for record in records:
values.add(record[index])
else:
raise TypeError("expected: `Iterable` or `AsyncIterable` of records")
values.discard(None) # do not insert NULL into referenced table
enum_dict = await self._merge_lookup_table(
relation, typing.cast(set[str], values)
)
return generator.get_enum_transformer(enum_dict)

async def _get_enum_list_transformer(
self,
relation: Table,
records: RecordIterable,
generator: BaseGenerator,
index: int,
) -> Optional[Callable[[Any], Any]]:
"Returns a callable function object that looks up assigned integer values based on a list of enumeration string values."

# a list of enumeration values represented as a list of strings
values: set[Optional[str]] = set()
if isinstance(records, Iterable):
for record in records:
values.update(record[index])
elif isinstance(records, AsyncIterable):
async for record in records:
values.update(record[index])
else:
raise TypeError("expected: `Iterable` or `AsyncIterable` of records")
values.discard(None) # do not insert NULL into referenced table
enum_list_dict = await self._merge_lookup_table(
relation, typing.cast(set[str], values)
)
return generator.get_enum_list_transformer(enum_list_dict)

async def _merge_lookup_table(
self, table: Table, values: set[str]
) -> dict[str, int]:
Expand Down

0 comments on commit a0200e2

Please sign in to comment.