Skip to content

Commit

Permalink
Allow limiting of models to include in an execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Gouline committed Apr 14, 2020
1 parent ae00965 commit 39eb44d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
18 changes: 14 additions & 4 deletions dbtmetabase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from .dbt import DbtReader
from .metabase import MetabaseClient

__version__ = '0.3.0'
__version__ = '0.4.0'

def export(dbt_path: str,
mb_host: str, mb_user: str, mb_password: str,
database: str, schema: str,
mb_https = True, sync = True, sync_timeout = 30):
mb_https = True, sync = True, sync_timeout = 30,
includes = [], excludes = []):
"""Exports models from dbt to Metabase.
Arguments:
Expand All @@ -23,10 +24,15 @@ def export(dbt_path: str,
mb_https {bool} -- Use HTTPS to connect to Metabase instead of HTTP. (default: {True})
sync {bool} -- Synchronize Metabase database before export. (default: {True})
sync_timeout {int} -- Synchronization timeout in seconds. (default: {30})
includes {list} -- Model names to limit processing to. (default: {[]})
excludes {list} -- Model names to exclude. (default: {[]})
"""

mbc = MetabaseClient(mb_host, mb_user, mb_password, mb_https)
models = DbtReader(dbt_path).read_models()
models = DbtReader(dbt_path).read_models(
includes=includes,
excludes=excludes
)

if sync:
if not mbc.sync_and_wait(database, schema, models, sync_timeout):
Expand All @@ -53,6 +59,8 @@ def main(args: list = None):
parser.add_argument('--schema', metavar='SCHEMA', required=True, help="target schema name")
parser.add_argument('--sync', metavar='ENABLE', type=bool, default=True, help="synchronize Metabase database before export")
parser.add_argument('--sync_timeout', metavar='SECS', type=int, default=30, help="synchronization timeout (in secs)")
parser.add_argument('--includes', metavar='MODELS', nargs='*', default=[], help="model names to limit processing to")
parser.add_argument('--excludes', metavar='MODELS', nargs='*', default=[], help="model names to exclude")
parsed = parser.parse_args(args=args)

if parsed.command == 'export':
Expand All @@ -65,5 +73,7 @@ def main(args: list = None):
database=parsed.database,
schema=parsed.schema,
sync=parsed.sync,
sync_timeout=parsed.sync_timeout
sync_timeout=parsed.sync_timeout,
includes=parsed.includes,
excludes=parsed.excludes
)
10 changes: 8 additions & 2 deletions dbtmetabase/dbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ def __init__(self, project_path: str):

self.project_path = project_path

def read_models(self) -> list:
def read_models(self, includes = [], excludes = []) -> list:
"""Reads dbt models in Metabase-friendly format.
Keyword Arguments:
includes {list} -- Model names to limit processing to. (default: {[]})
excludes {list} -- Model names to exclude. (default: {[]})
Returns:
list -- List of dbt models in Metabase-friendly format.
"""
Expand All @@ -28,7 +32,9 @@ def read_models(self) -> list:
with open(path, 'r') as stream:
schema = yaml.safe_load(stream)
for model in schema.get('models', []):
mb_models.append(self.read_model(model))
name = model['name']
if (not includes or name in includes) and (name not in excludes):
mb_models.append(self.read_model(model))

return mb_models

Expand Down

0 comments on commit 39eb44d

Please sign in to comment.