Skip to content

Commit

Permalink
fix bug with UUID field #52
Browse files Browse the repository at this point in the history
  • Loading branch information
toluaina committed Nov 5, 2020
1 parent 68d282b commit 09527d6
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 22 deletions.
31 changes: 29 additions & 2 deletions pgsync/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def model(self, table, schema):
if name not in self.models:
if schema not in self.__metadata:
metadata = sa.MetaData(schema=schema)
metadata.reflect(self.__engine)
metadata.reflect(self.__engine, views=True)
self.__metadata[schema] = metadata
metadata = self.__metadata[schema]
if name not in metadata.tables:
Expand Down Expand Up @@ -348,10 +348,12 @@ def create_triggers(self, database, schema, tables=None, drop=False):
self.drop_triggers(database, schema)
self.execute(CREATE_TRIGGER_TEMPLATE)
queries = []
insp = sa.engine.reflection.Inspector.from_engine(self.engine)
views = insp.get_view_names(schema)
for table in self.tables(schema):
pairs = table.split('.')
schema, table = pairs[0], pairs[1]
if tables and table not in tables:
if (tables and table not in tables) or (table in views):
continue
logger.debug(f'Creating trigger on table: {schema}.{table}')
for name, for_each, tg_op in [
Expand Down Expand Up @@ -497,6 +499,7 @@ def parse_value(self, type_, value):
'character',
'character varying',
'text',
'uuid',
'varchar',
):
value = value.lstrip("'").rstrip("'")
Expand Down Expand Up @@ -806,6 +809,30 @@ def drop_extension(database, extension, echo=False):
logger.debug(f'Dropped extension: {extension}')


def create_materialized_view(database, view, query, echo=False):
"""Create a materialized database view."""
logger.debug(f'Creating materialized view: {view} with {query}')
engine = pg_engine(database=database, echo=echo)
pg_execute(engine, f'CREATE MATERIALIZED VIEW {view} AS {query}')
logger.debug(f'Created materialized view: {view}')


def refresh_materialized_view(database, view, echo=False):
"""Refresh a materialized database view."""
logger.debug(f'Refreshing materialized view: {view}')
engine = pg_engine(database=database, echo=echo)
pg_execute(engine, f'REFRESH MATERIALIZED VIEW {view}')
logger.debug(f'Refreshed materialized view: {view}')


def drop_materialized_view(database, view, echo=False):
"""Drop a materialized database view."""
logger.debug(f'Drop materialized view: {view}')
engine = pg_engine(database=database, echo=echo)
pg_execute(engine, f'DROP MATERIALIZED VIEW IF EXISTS {view}')
logger.debug(f'Dropped materialized view: {view}')


def compiled_query(query, label=None):
"""Compile an SQLAlchemy query with an optional label."""
query = str(
Expand Down
3 changes: 2 additions & 1 deletion pgsync/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'children',
'columns',
'label',
'primary_key',
'relationship',
'schema',
'table',
Expand Down Expand Up @@ -106,5 +107,5 @@
r'table\s(?P<schema>\w+).\"?(?P<table>\w+)\"?:\s(?P<tg_op>[A-Z]+):'
)
LOGICAL_SLOT_SUFFIX = re.compile(
"\s(?P<key>\w+)\[(?P<type>[\w\s]+)\]:(?P<value>[\w\']+)"
"\s(?P<key>\w+)\[(?P<type>[\w\s]+)\]:(?P<value>[\w\'\"\-]+)"
)
5 changes: 5 additions & 0 deletions pgsync/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def __init__(self, *args, **kwargs): # noqa: C901

self.table_columns = self.model.columns.keys()
self._primary_keys = get_primary_keys(self.model)

if not self._primary_keys:
self._primary_keys = kwargs.get('primary_key')

# columns to fetch
self.column_names = [
column for column in kwargs.get(
Expand Down Expand Up @@ -233,6 +237,7 @@ def build(self, root: dict) -> Node:
model=self.base.model(table, schema=schema),
table=table,
schema=schema,
primary_key=root.get('primary_key', []),
label=root.get('label', table),
transform=root.get('transform', {}),
columns=root.get('columns', []),
Expand Down
2 changes: 1 addition & 1 deletion pgsync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def _payloads(self, nodes, index, payloads):

if tg_op == INSERT:

if table in self.tree.nodes:
if table == root_table and table in self.tree.nodes:
for payload in payloads:
payload_data = self._payload_data(payload)
primary_values = [
Expand Down
14 changes: 7 additions & 7 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# pip-compile --output-file=requirements/dev.txt requirements/dev.in
#
attrs==20.2.0 # via pytest
attrs==20.3.0 # via pytest
bleach==3.2.1 # via readme-renderer
bump2version==1.0.1 # via bumpversion
bumpversion==0.6.0 # via -r requirements/base.in, -r requirements/dev.in
Expand All @@ -17,8 +17,8 @@ cython==0.29.21 # via -r requirements/dev.in
docutils==0.16 # via readme-renderer
elasticsearch-dsl==7.3.0 # via -r requirements/base.in
elasticsearch==7.9.1 # via elasticsearch-dsl
environs==8.0.0 # via -r requirements/base.in
faker==4.14.0 # via -r requirements/base.in
environs==9.0.0 # via -r requirements/base.in
faker==4.14.2 # via -r requirements/base.in
flake8-debugger==3.2.1 # via -r requirements/test.in
flake8-docstrings==1.5.0 # via -r requirements/test.in
flake8-isort==4.0.0 # via -r requirements/test.in
Expand All @@ -28,13 +28,13 @@ flake8==3.8.4 # via flake8-debugger, flake8-docstrings, flake8-isort
idna==2.10 # via requests
isort==5.6.4 # via flake8-isort
keyring==21.4.0 # via twine
marshmallow==3.8.0 # via environs
marshmallow==3.9.0 # via environs
mccabe==0.6.1 # via flake8
mock==4.0.2 # via -r requirements/test.in
more-itertools==8.5.0 # via pytest
more-itertools==8.6.0 # via pytest
packaging==20.4 # via bleach, pytest, pytest-sugar
pip-tools==5.3.1 # via -r requirements/dev.in
pkginfo==1.6.0 # via twine
pkginfo==1.6.1 # via twine
pluggy==0.13.1 # via pytest
psycopg2-binary==2.8.6 # via -r requirements/base.in
py==1.9.0 # via pytest
Expand All @@ -49,7 +49,7 @@ pytest-runner==5.2 # via -r requirements/test.in
pytest-sugar==0.9.4 # via -r requirements/test.in
pytest==5.3.5 # via -r requirements/test.in, pytest-cov, pytest-mock, pytest-sugar
python-dateutil==2.8.1 # via elasticsearch-dsl, faker
python-dotenv==0.14.0 # via environs
python-dotenv==0.15.0 # via environs
readme-renderer==28.0 # via twine
redis==3.5.3 # via -r requirements/base.in
requests-toolbelt==0.9.1 # via twine
Expand Down
10 changes: 5 additions & 5 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ certifi==2020.6.20 # via elasticsearch
click==7.1.2 # via -r requirements/base.in
elasticsearch-dsl==7.3.0 # via -r requirements/base.in
elasticsearch==7.9.1 # via elasticsearch-dsl
environs==8.0.0 # via -r requirements/base.in
faker==4.14.0 # via -r requirements/base.in
marshmallow==3.8.0 # via environs
newrelic==5.22.0.151 # via -r requirements/prod.in
environs==9.0.0 # via -r requirements/base.in
faker==4.14.2 # via -r requirements/base.in
marshmallow==3.9.0 # via environs
newrelic==5.22.1.152 # via -r requirements/prod.in
psycopg2-binary==2.8.6 # via -r requirements/base.in
python-dateutil==2.8.1 # via elasticsearch-dsl, faker
python-dotenv==0.14.0 # via environs
python-dotenv==0.15.0 # via environs
redis==3.5.3 # via -r requirements/base.in
six==1.15.0 # via elasticsearch-dsl, python-dateutil
sqlalchemy==1.3.20 # via -r requirements/base.in
Expand Down
12 changes: 6 additions & 6 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
#
# pip-compile --output-file=requirements/test.txt requirements/test.in
#
attrs==20.2.0 # via pytest
attrs==20.3.0 # via pytest
bump2version==1.0.1 # via bumpversion
bumpversion==0.6.0 # via -r requirements/base.in
certifi==2020.6.20 # via elasticsearch
click==7.1.2 # via -r requirements/base.in
coverage==5.3 # via pytest-cov
elasticsearch-dsl==7.3.0 # via -r requirements/base.in
elasticsearch==7.9.1 # via elasticsearch-dsl
environs==8.0.0 # via -r requirements/base.in
faker==4.14.0 # via -r requirements/base.in
environs==9.0.0 # via -r requirements/base.in
faker==4.14.2 # via -r requirements/base.in
flake8-debugger==3.2.1 # via -r requirements/test.in
flake8-docstrings==1.5.0 # via -r requirements/test.in
flake8-isort==4.0.0 # via -r requirements/test.in
flake8-print==3.1.4 # via -r requirements/test.in
flake8-todo==0.7 # via -r requirements/test.in
flake8==3.8.4 # via flake8-debugger, flake8-docstrings, flake8-isort, flake8-print
isort==5.6.4 # via flake8-isort
marshmallow==3.8.0 # via environs
marshmallow==3.9.0 # via environs
mccabe==0.6.1 # via flake8
mock==4.0.2 # via -r requirements/test.in
more-itertools==8.5.0 # via pytest
more-itertools==8.6.0 # via pytest
packaging==20.4 # via pytest, pytest-sugar
pluggy==0.13.1 # via pytest
psycopg2-binary==2.8.6 # via -r requirements/base.in
Expand All @@ -39,7 +39,7 @@ pytest-runner==5.2 # via -r requirements/test.in
pytest-sugar==0.9.4 # via -r requirements/test.in
pytest==5.3.5 # via -r requirements/test.in, pytest-cov, pytest-mock, pytest-sugar
python-dateutil==2.8.1 # via elasticsearch-dsl, faker
python-dotenv==0.14.0 # via environs
python-dotenv==0.15.0 # via environs
redis==3.5.3 # via -r requirements/base.in
six==1.15.0 # via elasticsearch-dsl, flake8-print, packaging, python-dateutil
snowballstemmer==2.0.0 # via pydocstyle
Expand Down

0 comments on commit 09527d6

Please sign in to comment.