def _get_distinct_values(data_source_configuration, column_config, expansion_limit=10): """ Return a tuple. The first item is a list of distinct values in the given ReportColumn no longer than expansion_limit. The second is a boolean which is True if the number of distinct values in the column is greater than the limit. :param data_source_configuration: :param column_config: :param expansion_limit: :return: """ too_many_values = False session = Session() try: connection = session.connection() table = get_indicator_table(data_source_configuration) if not table.exists(bind=connection): return [], False column = table.c[column_config.field] query = sqlalchemy.select([column], limit=expansion_limit + 1).distinct() result = connection.execute(query).fetchall() distinct_values = [x[0] for x in result] if len(distinct_values) > expansion_limit: distinct_values = distinct_values[:expansion_limit] too_many_values = True except: session.rollback() raise finally: session.close() return distinct_values, too_many_values
def _get_all_tables(): session = Session() try: connection = session.connection() tables = [sql.get_indicator_table(config) for config in userreports_models.DataSourceConfiguration.all()] return [t for t in tables if t.exists(bind=connection)] except: session.rollback() raise finally: session.close()
def _get_all_tables(): session = Session() try: connection = session.connection() tables = [ sql.get_indicator_table(config) for config in userreports_models.DataSourceConfiguration.all() ] return [t for t in tables if t.exists(bind=connection)] except: session.rollback() raise finally: session.close()
def _get_distinct_values(data_source_configuration, column_config, expansion_limit=DEFAULT_MAXIMUM_EXPANSION): """ Return a tuple. The first item is a list of distinct values in the given ExpandedColumn no longer than expansion_limit. The second is a boolean which is True if the number of distinct values in the column is greater than the limit. :param data_source_configuration: :param column_config: :param expansion_limit: :return: """ from corehq.apps.userreports.sql.adapter import get_indicator_table too_many_values = False session = Session() try: connection = session.connection() table = get_indicator_table(data_source_configuration) if not table.exists(bind=connection): return [], False if column_config.field not in table.c: raise ColumnNotFoundError(_( 'The column "{}" does not exist in the report source! ' 'Please double check your report configuration.').format(column_config.field) ) column = table.c[column_config.field] query = sqlalchemy.select([column], limit=expansion_limit + 1).distinct() result = connection.execute(query).fetchall() distinct_values = [x[0] for x in result] if len(distinct_values) > expansion_limit: distinct_values = distinct_values[:expansion_limit] too_many_values = True except: session.rollback() raise finally: session.close() return distinct_values, too_many_values
def _alter_tables_helper(get_tables_func, column_checker_func, column_alter_func): _sync_couch() tables = get_tables_func() session = Session() try: connection = session.connection() ctx = alembic.migration.MigrationContext.configure(connection) op = alembic.operations.Operations(ctx) for table in tables: logger.info("Checking table {}".format(table.name)) for column in table.columns: if column_checker_func(column): logger.info("Altering {}".format(column)) column_alter_func(op, table, column) else: logger.info("Skipping {}".format(column)) session.commit() except: session.rollback() raise finally: session.close()
def _alter_tables_helper(get_tables_func, column_checker_func, column_alter_func): _sync_couch() tables = get_tables_func() session = Session() try: connection = session.connection() ctx = alembic.migration.MigrationContext.configure(connection) op = alembic.operations.Operations(ctx) for table in tables: logger.info("Checking table {}".format(table.name)) for column in table.columns: if column_checker_func(column): logger.info("Altering {}".format(column)) column_alter_func(op, table, column) else: logger.info("Skipping {}".format(column)) session.commit() except: session.rollback() raise finally: session.close()