Skip to content
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

fix: postgres errors #80

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion meilisync/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def mapping_data(self, fields_mapping: Optional[dict] = None):
if fields_mapping is not None and k in fields_mapping:
real_k = fields_mapping[k] or k
data[real_k] = v
else:
elif fields_mapping is None:
data[k] = v
return data or self.data
27 changes: 21 additions & 6 deletions meilisync/source/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
from meilisync.source import Source


class CustomDictRow(psycopg2.extras.RealDictRow):
def __getitem__(self, key):
try:
return super().__getitem__(key)
except KeyError as exc:
if isinstance(key, int):
return super().__getitem__(list(self.keys())[key])
raise exc


class CustomDictCursor(psycopg2.extras.RealDictCursor):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
kwargs["row_factory"] = CustomDictRow
self.row_factory = CustomDictRow


class Postgres(Source):
type = SourceType.postgres
slot = "meilisync"
Expand All @@ -33,9 +50,7 @@ def __init__(
else:
self.cursor.execute("SELECT pg_current_wal_lsn()")
self.start_lsn = self.cursor.fetchone()[0]
self.conn_dict = psycopg2.connect(
**self.kwargs, cursor_factory=psycopg2.extras.RealDictCursor
)
self.conn_dict = psycopg2.connect(**self.kwargs, cursor_factory=CustomDictCursor)

async def get_current_progress(self):
sql = "SELECT pg_current_wal_lsn()"
Expand Down Expand Up @@ -81,9 +96,9 @@ def _consumer(self, msg: ReplicationMessage):
table = change.get("table")
if table not in self.tables:
return
columnnames = change.get("columnnames")
columnvalues = change.get("columnvalues")
columntypes = change.get("columntypes")
columnnames = change.get("columnnames", [])
columnvalues = change.get("columnvalues", [])
columntypes = change.get("columntypes", [])

for i in range(len(columntypes)):
if columntypes[i] == "json":
Expand Down