Skip to content

Commit

Permalink
Allowing to specify schema for tables (apache#330)
Browse files Browse the repository at this point in the history
* Allowing to specify schema for tables

* Installed postgres and tested/fixed
  • Loading branch information
mistercrunch committed Apr 14, 2016
1 parent 0a94b36 commit 5cadd67
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""add schema to table model
Revision ID: bb51420eaf83
Revises: 867bf4f117f9
Create Date: 2016-04-11 22:41:06.185955
"""

# revision identifiers, used by Alembic.
revision = 'bb51420eaf83'
down_revision = '867bf4f117f9'

from alembic import op
import sqlalchemy as sa


def upgrade():
op.add_column('tables', sa.Column('schema', sa.String(length=256), nullable=True))


def downgrade():
op.drop_column('tables', 'schema')
18 changes: 12 additions & 6 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,12 @@ def get_extra(self):
logging.error(e)
return extra

def get_table(self, table_name):
def get_table(self, table_name, schema=None):
extra = self.get_extra()
meta = MetaData(**extra.get('metadata_params', {}))
return Table(
table_name, meta,
schema=schema or None,
autoload=True,
autoload_with=self.get_sqla_engine())

Expand Down Expand Up @@ -417,6 +418,7 @@ class SqlaTable(Model, Queryable, AuditMixinNullable):
'Database', backref='tables', foreign_keys=[database_id])
offset = Column(Integer, default=0)
cache_timeout = Column(Integer)
schema = Column(String(256))

baselink = "tablemodelview"

Expand Down Expand Up @@ -592,7 +594,11 @@ def query( # sqla

select_exprs += metrics_exprs
qry = select(select_exprs)
from_clause = table(self.table_name)

tbl = table(self.table_name)
if self.schema:
tbl.schema = self.schema

if not columns:
qry = qry.group_by(*groupby_exprs)

Expand Down Expand Up @@ -625,7 +631,7 @@ def query( # sqla

if timeseries_limit and groupby:
subq = select(inner_select_exprs)
subq = subq.select_from(table(self.table_name))
subq = subq.select_from(tbl)
subq = subq.where(and_(*(where_clause_and + inner_time_filter)))
subq = subq.group_by(*inner_groupby_exprs)
subq = subq.order_by(desc(main_metric_expr))
Expand All @@ -635,9 +641,9 @@ def query( # sqla
on_clause.append(
groupby_exprs[i] == column("__" + gb))

from_clause = from_clause.join(subq.alias(), and_(*on_clause))
tbl = tbl.join(subq.alias(), and_(*on_clause))

qry = qry.select_from(from_clause)
qry = qry.select_from(tbl)

engine = self.database.get_sqla_engine()
sql = "{}".format(
Expand All @@ -653,7 +659,7 @@ def query( # sqla
def fetch_metadata(self):
"""Fetches the metadata for the table and merges it in"""
try:
table = self.database.get_table(self.table_name)
table = self.database.get_table(self.table_name, schema=self.schema)
except Exception as e:
flash(str(e))
flash(
Expand Down
8 changes: 6 additions & 2 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,18 @@ class TableModelView(CaravelModelView, DeleteMixin): # noqa
'table_link', 'database', 'sql_link', 'is_featured',
'changed_by_', 'changed_on']
add_columns = [
'table_name', 'database', 'default_endpoint', 'offset', 'cache_timeout']
'table_name', 'database', 'schema',
'default_endpoint', 'offset', 'cache_timeout']
edit_columns = [
'table_name', 'is_featured', 'database', 'description', 'owner',
'table_name', 'is_featured', 'database', 'schema', 'description', 'owner',
'main_dttm_col', 'default_endpoint', 'offset', 'cache_timeout']
related_views = [TableColumnInlineView, SqlMetricInlineView]
base_order = ('changed_on', 'desc')
description_columns = {
'offset': "Timezone offset (in hours) for this datasource",
'schema': (
"Schema, as used only in some databases like Postgres, Redshift "
"and DB2"),
'description': Markup(
"Supports <a href='https://daringfireball.net/projects/markdown/'>"
"markdown</a>"),
Expand Down
1 change: 1 addition & 0 deletions dev-reqs.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
coveralls
nose
sphinx
sphinx_bootstrap_theme
sphinxcontrib.youtube

0 comments on commit 5cadd67

Please sign in to comment.