Skip to content

Commit

Permalink
Add cli command for refreshing data samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina Samuel committed Apr 17, 2019
1 parent 82546b0 commit daf9a5a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
22 changes: 22 additions & 0 deletions redash/cli/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from redash import models
from redash.query_runner import (get_configuration_schema_for_query_runner_type,
query_runners)
from redash.tasks import refresh_samples
from redash.utils import json_loads
from redash.utils.configuration import ConfigurationContainer

Expand Down Expand Up @@ -172,6 +173,27 @@ def update_attr(obj, attr, new_value):
setattr(obj, attr, new_value)


@manager.command()
@click.argument('name')
@click.option('--org', 'organization', default='default',
help="The organization the user belongs to (leave blank for "
"'default').")
@click.option('--count', 'num_tables', default=50,
help="number of tables to process data samples for")
def refresh_data_samples(name, num_tables=50, organization='default'):
"""Refresh table samples by data source name."""
try:
org = models.Organization.get_by_slug(organization)
data_source = models.DataSource.query.filter(
models.DataSource.name == name,
models.DataSource.org == org).one()
print("Refreshing samples for data source: {} (id={})".format(name, data_source.id))
refresh_samples(data_source.id, num_tables)
except NoResultFound:
print("Couldn't find data source named: {}".format(name))
exit(1)


@manager.command()
@click.argument('name')
@click.option('--name', 'new_name', default=None,
Expand Down
13 changes: 12 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from click.testing import CliRunner

from tests import BaseTestCase
from redash import utils
from redash.utils.configuration import ConfigurationContainer
from redash.query_runner import query_runners
from redash.cli import manager
from redash.models import DataSource, Group, Organization, User, db
from redash.models import TableMetadata, DataSource, Group, Organization, User, db


class DataSourceCommandTests(BaseTestCase):
Expand Down Expand Up @@ -139,6 +140,16 @@ def test_connection_bad_delete(self):
self.assertIn("Couldn't find", result.output)
self.assertEqual(DataSource.query.count(), 1)

def test_refresh_samples(self):
ds = self.factory.create_data_source(
name='test1', type='sqlite',
options=ConfigurationContainer({"dbpath": "/tmp/test.db"}))
runner = CliRunner()
result = runner.invoke(manager, ['ds', 'refresh_data_samples', 'test1'])
self.assertFalse(result.exception)
self.assertEqual(result.exit_code, 0)
self.assertIn('Refreshing', result.output)

def test_options_edit(self):
self.factory.create_data_source(
name='test1', type='sqlite',
Expand Down

0 comments on commit daf9a5a

Please sign in to comment.