def tearDownClass(cls): for config in DataSourceConfiguration.all(): config.delete() delete_all_report_configs() for domain in Domain.get_all(): domain.delete() super(ReportConfigurationDbTest, cls).tearDownClass()
def bootstrap(self, configs=None): # sets up the initial stuff if configs is None: configs = DataSourceConfiguration.all() self.tables = [IndicatorSqlAdapter(self.get_sql_engine(), config) for config in configs] self.bootstrapped = True
def tearDownClass(cls): for config in DataSourceConfiguration.all(): config.delete() delete_all_report_configs() for domain in Domain.get_all(): domain.delete() super(ReportConfigurationDbTest, cls).tearDownClass()
def handle(self, **options): data_sources = list(DataSourceConfiguration.all()) data_sources.extend(list(StaticDataSourceConfiguration.all())) engine_ids = self._get_engine_ids(data_sources, options.get('engine_id')) tables_to_remove_by_engine = defaultdict(list) for engine_id in engine_ids: engine = connection_manager.get_engine(engine_id) with engine.begin() as connection: migration_context = get_migration_context( connection, include_object=_include_object) raw_diffs = compare_metadata(migration_context, metadata) diffs = reformat_alembic_diffs(raw_diffs) tables_to_remove_by_engine[engine_id] = [ diff.table_name for diff in diffs if diff.type == 'remove_table' ] for engine_id, tablenames in tables_to_remove_by_engine.items(): engine = connection_manager.get_engine(engine_id) for tablename in tablenames: with engine.begin() as connection: try: result = connection.execute( 'SELECT COUNT(*), MAX(inserted_at) FROM "{tablename}"' .format(tablename=tablename)) except Exception: print(tablename, "no inserted_at column, probably not UCR") else: print(tablename, result.fetchone())
def handle(self, **options): data_sources = list(DataSourceConfiguration.all()) data_sources.extend(list(StaticDataSourceConfiguration.all())) engine_ids = self._get_engine_ids(data_sources, options.get('engine_id')) tables_to_remove_by_engine = defaultdict(list) for engine_id in engine_ids: engine = connection_manager.get_engine(engine_id) with engine.begin() as connection: migration_context = get_migration_context(connection, include_object=_include_object) raw_diffs = compare_metadata(migration_context, metadata) diffs = reformat_alembic_diffs(raw_diffs) tables_to_remove_by_engine[engine_id] = [ diff.table_name for diff in diffs if diff.type == 'remove_table' ] for engine_id, tablenames in tables_to_remove_by_engine.items(): engine = connection_manager.get_engine(engine_id) for tablename in tablenames: with engine.begin() as connection: try: result = connection.execute( 'SELECT COUNT(*), MAX(inserted_at) FROM "{tablename}"'.format(tablename=tablename) ) except Exception: print(tablename, "no inserted_at column, probably not UCR") else: print(tablename, result.fetchone())
def handle(self, **options): data_sources = list(DataSourceConfiguration.all()) data_sources.extend(list(StaticDataSourceConfiguration.all())) tables_by_engine_id = self._get_tables_by_engine_id( data_sources, options.get('engine_id')) tables_to_remove_by_engine = defaultdict(list) for engine_id, expected_tables in tables_by_engine_id.items(): engine = connection_manager.get_engine(engine_id) with engine.begin() as connection: # Using string formatting rather than execute with %s syntax # is acceptable here because the strings we're inserting are static # and only templated for DRYness results = connection.execute(f""" SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE' AND ( table_name LIKE '{UCR_TABLE_PREFIX}%%' OR table_name LIKE '{LEGACY_UCR_TABLE_PREFIX}%%' ); """).fetchall() tables_in_db = {r[0] for r in results} tables_to_remove_by_engine[ engine_id] = tables_in_db - expected_tables for engine_id, tablenames in tables_to_remove_by_engine.items(): print("\nTables no longer referenced in database: {}:\n".format( engine_id)) engine = connection_manager.get_engine(engine_id) if not tablenames: print("\t No tables to prune") continue for tablename in tablenames: with engine.begin() as connection: try: result = connection.execute( f'SELECT COUNT(*), MAX(inserted_at) FROM "{tablename}"' ) except Exception: print( f"\t{tablename}: no inserted_at column, probably not UCR" ) else: row_count, idle_since = result.fetchone() if row_count == 0: print(f"\t{tablename}: {row_count} rows") if options['drop_empty_tables']: connection.execute(f'DROP TABLE "{tablename}"') print(f'\t^-- deleted {tablename}') else: print( f"\t{tablename}: {row_count} rows, idle since {idle_since}" )
def setUpClass(cls): super(DataSourceConfigurationDbTest, cls).setUpClass() # TODO - handle cleanup appropriately so this isn't needed for data_source_config in DataSourceConfiguration.all(): data_source_config.delete() DataSourceConfiguration(domain='foo', table_id='foo1', referenced_doc_type='XFormInstance').save() DataSourceConfiguration(domain='foo', table_id='foo2', referenced_doc_type='XFormInstance').save() DataSourceConfiguration(domain='bar', table_id='bar1', referenced_doc_type='XFormInstance').save()
def setUpClass(cls): super(DataSourceConfigurationDbTest, cls).setUpClass() # TODO - handle cleanup appropriately so this isn't needed for data_source_config in DataSourceConfiguration.all(): data_source_config.delete() DataSourceConfiguration(domain='foo', table_id='foo1', referenced_doc_type='XFormInstance').save() DataSourceConfiguration(domain='foo', table_id='foo2', referenced_doc_type='XFormInstance').save() DataSourceConfiguration(domain='bar', table_id='bar1', referenced_doc_type='XFormInstance').save()
def _data_sources_by_engine_id(): by_engine_id = defaultdict(list) for ds in StaticDataSourceConfiguration.all(): ds_engine_id = ds['engine_id'] by_engine_id[ds_engine_id].append(ds) for ds in DataSourceConfiguration.all(): ds_engine_id = ds['engine_id'] by_engine_id[ds_engine_id].append(ds) return by_engine_id
def _data_sources_by_engine_id(): by_engine_id = defaultdict(list) for ds in StaticDataSourceConfiguration.all(): ds_engine_id = ds['engine_id'] by_engine_id[ds_engine_id].append(ds) for ds in DataSourceConfiguration.all(): ds_engine_id = ds['engine_id'] by_engine_id[ds_engine_id].append(ds) return by_engine_id
def handle(self, **options): data_sources = list(DataSourceConfiguration.all()) data_sources.extend(list(StaticDataSourceConfiguration.all())) tables_by_engine_id = self._get_tables_by_engine_id( data_sources, options.get('engine_id')) tables_to_remove_by_engine = defaultdict(list) for engine_id, expected_tables in tables_by_engine_id.items(): engine = connection_manager.get_engine(engine_id) with engine.begin() as connection: results = connection.execute(""" SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE' AND ( table_name LIKE '{}%%' OR table_name LIKE '{}%%' ); """.format(UCR_TABLE_PREFIX, LEGACY_UCR_TABLE_PREFIX)).fetchall() tables_in_db = {r[0] for r in results} tables_to_remove_by_engine[ engine_id] = tables_in_db - expected_tables for engine_id, tablenames in tables_to_remove_by_engine.items(): print("\nTables no longer referenced in database: {}:\n".format( engine_id)) engine = connection_manager.get_engine(engine_id) if not tablenames: print("\t No tables to prune") continue for tablename in tablenames: if options['show_counts']: with engine.begin() as connection: try: result = connection.execute( 'SELECT COUNT(*), MAX(inserted_at) FROM "{tablename}"' .format(tablename=tablename)) except Exception: print( "\t{}: no inserted_at column, probably not UCR" .format(tablename)) else: print("\t{}: {}".format(tablename, result.fetchone())) else: print("\t{}".format(tablename))
def handle(self, **options): data_sources = list(DataSourceConfiguration.all()) data_sources.extend(list(StaticDataSourceConfiguration.all())) tables_by_engine_id = self._get_tables_by_engine_id(data_sources, options.get('engine_id')) tables_to_remove_by_engine = defaultdict(list) for engine_id, expected_tables in tables_by_engine_id.items(): engine = connection_manager.get_engine(engine_id) with engine.begin() as connection: results = connection.execute(""" SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE' AND ( table_name LIKE '{}%%' OR table_name LIKE '{}%%' ); """.format(UCR_TABLE_PREFIX, LEGACY_UCR_TABLE_PREFIX)).fetchall() tables_in_db = {r[0] for r in results} tables_to_remove_by_engine[engine_id] = tables_in_db - expected_tables for engine_id, tablenames in tables_to_remove_by_engine.items(): print("\nTables no longer referenced in database: {}:\n".format(engine_id)) engine = connection_manager.get_engine(engine_id) if not tablenames: print("\t No tables to prune") continue for tablename in tablenames: if options['show_counts']: with engine.begin() as connection: try: result = connection.execute( 'SELECT COUNT(*), MAX(inserted_at) FROM "{tablename}"'.format(tablename=tablename) ) except Exception: print("\t{}: no inserted_at column, probably not UCR".format(tablename)) else: print("\t{}: {}".foramt(tablename, result.fetchone())) else: print("\t{}".format(tablename))
def get_all_configs(self): return filter(lambda config: not config.is_deactivated, DataSourceConfiguration.all())
def tearDownClass(cls): for config in DataSourceConfiguration.all(): config.delete() delete_all_report_configs() super(ReportTranslationTest, cls).tearDownClass()
def tearDownClass(cls): for config in DataSourceConfiguration.all(): config.delete() super(DataSourceConfigurationDbTest, cls).tearDownClass()
def tearDownClass(cls): cls.app.delete() for config in DataSourceConfiguration.all(): config.delete() delete_all_report_configs() super(ReportBuilderDBTest, cls).tearDownClass()
def tearDownClass(cls): for config in DataSourceConfiguration.all(): config.delete() super(DataSourceConfigurationDbTest, cls).tearDownClass()
def _delete_everything(cls): delete_all_cases() for config in DataSourceConfiguration.all(): config.delete() for config in ReportConfiguration.all(): config.delete()
def get_all_configs(self): return DataSourceConfiguration.all()
def tearDownClass(cls): for config in DataSourceConfiguration.all(): config.delete() delete_all_report_configs() super(ReportTranslationTest, cls).tearDownClass()
def tearDownClass(cls): for config in DataSourceConfiguration.all(): config.delete() delete_all_report_configs()
def _delete_everything(cls): delete_all_cases() for config in DataSourceConfiguration.all(): config.delete() delete_all_report_configs()
def tearDownClass(cls): cls.app.delete() for config in DataSourceConfiguration.all(): config.delete() delete_all_report_configs()
def get_data_sources(self): return filter(lambda config: not config.is_deactivated, DataSourceConfiguration.all())
def test_get_all(self): self.assertEqual(3, len(list(DataSourceConfiguration.all())))
def tearDownClass(cls): for config in DataSourceConfiguration.all(): config.delete()
def get_data_sources(self): return [ config for config in DataSourceConfiguration.all() if not config.is_deactivated ]
def tearDownClass(cls): cls.app.delete() for config in DataSourceConfiguration.all(): config.delete() for config in ReportConfiguration.all(): config.delete()
def test_get_all(self): self.assertEqual(3, len(list(DataSourceConfiguration.all())))
def tearDown(self): delete_all_report_configs() for config in DataSourceConfiguration.all(): config.delete() super().tearDown()
def tearDownClass(cls): for config in DataSourceConfiguration.all(): config.delete() for config in ReportConfiguration.all(): config.delete()
def get_all_configs(self): return DataSourceConfiguration.all()