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

Make PyCDS schema name agnostic #44

Merged
merged 15 commits into from
Jan 22, 2020
Merged
Prev Previous commit
Next Next commit
Make view definition respect schema
  • Loading branch information
rod-glover committed Jan 16, 2020
commit bbebd7c327cdb085096fb47291bbdabe18211353
20 changes: 14 additions & 6 deletions pycds/view_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import re
from sqlalchemy.ext import compiler
from sqlalchemy.schema import DDLElement
from sqlalchemy import Table, Column
from sqlalchemy.sql import table
from sqlalchemy.ext.declarative import declared_attr

Expand Down Expand Up @@ -93,9 +94,9 @@ class Thing(Base, ViewMixin):

@declared_attr
def __table__(cls):
t = table(cls.viewname())
t = Table(cls.base_viewname(), cls.metadata)
for c in cls.__selectable__.c:
c._make_proxy(t)
t.append_column(Column(c.name, c.type, primary_key=c.primary_key))
return t

@declared_attr
Expand All @@ -107,17 +108,24 @@ def __mapper_args__(cls):
return {}

@classmethod
def viewname(cls):
def base_viewname(cls):
try:
return cls.__viewname__
except AttributeError:
return snake_case(cls.__name__)

@classmethod
def qualfied_viewname(cls):
prefix = '' if cls.metadata.schema is None \
else cls.metadata.schema + '.'
return prefix + cls.base_viewname()

@classmethod
def create(cls, sesh):
return sesh.execute(CreateView(cls.viewname(), cls.__selectable__))
return sesh.execute(CreateView(
cls.qualfied_viewname(), cls.__selectable__)
)

@classmethod
def drop(cls, sesh):
return sesh.execute(DropView(cls.viewname()))

return sesh.execute(DropView(cls.qualfied_viewname()))
10 changes: 10 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,13 @@ def action(sesh):

with_schema_name(sesh, schema_name, action)


# Test helpers

def get_items_in_schema(sesh, item_type, schema_name=get_schema_name()):
r = sesh.execute(f'''
SELECT table_name
FROM information_schema.{item_type}
WHERE table_schema = '{schema_name}';
''')
return {x[0] for x in r.fetchall()}
10 changes: 10 additions & 0 deletions tests/view_helpers/test_materialized_view_helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
from ..helpers import get_items_in_schema
from .content import \
Thing, SimpleThingMatview, ThingWithDescriptionMatview, ThingCountMatview


def test_schema_content(matview_sesh):
assert get_items_in_schema(matview_sesh, 'tables') >= {
'things', 'descriptions',
'simple_thing_matview_mv', 'thing_with_description_matview_mv',
'thing_count_matview_mv'
}


def test_viewname(schema_name):
assert SimpleThingMatview.base_viewname() == 'simple_thing_matview_mv'
assert SimpleThingMatview.qualfied_viewname() == \
f'{schema_name}.simple_thing_matview_mv'


def test_simple_view(matview_sesh):
sesh = matview_sesh

Expand Down
23 changes: 20 additions & 3 deletions tests/view_helpers/test_view_helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
from ..helpers import get_items_in_schema
from .content import \
Thing, SimpleThingView, ThingWithDescriptionView, ThingCountView


def test_viewname():
assert SimpleThingView.viewname() == 'simple_thing_view'
def test_schema_content(view_sesh):
assert get_items_in_schema(view_sesh, 'tables') >= {
'things', 'descriptions'
}
# Wrong
# assert get_items_in_schema(view_sesh, 'views', schema_name='public') >= {
# 'simple_thing_view', 'thing_with_description_view',
# 'thing_count_view'
# }
assert get_items_in_schema(view_sesh, 'views') >= {
'simple_thing_view', 'thing_with_description_view',
'thing_count_view'
}


def test_viewname(schema_name):
assert SimpleThingView.base_viewname() == 'simple_thing_view'
assert SimpleThingView.qualfied_viewname() == \
f'{schema_name}.simple_thing_view'


def test_simple_view(view_sesh):
Expand All @@ -18,7 +36,6 @@ def test_simple_view(view_sesh):

def test_complex_view(view_sesh):
sesh = view_sesh

things = sesh.query(ThingWithDescriptionView)
assert [t.desc for t in things.order_by(ThingWithDescriptionView.id)] \
== ['alpha', 'beta', 'gamma', 'beta', 'alpha']
Expand Down