def reverse(apps, schema_editor): """ Drop the new geo_version column from all data tables """ session = get_session() inspector = inspect(session.bind) try: for data_table in DATA_TABLES.itervalues(): db_model = data_table.model table = db_model.__table__ # remove the primary key constraint, if any pk = inspector.get_pk_constraint(table.name)['name'] if pk: session.execute("ALTER TABLE %s DROP CONSTRAINT %s" % (table.name, pk)) # drop the new column session.execute("ALTER TABLE %s DROP COLUMN geo_version" % table.name) # add the old pk constraint pk = table.primary_key pk.columns.remove(table.c.geo_version) session.execute(AddConstraint(pk)) session.commit() finally: session.close()
def forwards(apps, schema_editor): """ Ensure all data tables have the new geo_version column, with a default of '' """ session = get_session() inspector = inspect(session.bind) try: for data_table in DATA_TABLES.itervalues(): db_model = data_table.model table = db_model.__table__ cols = [c['name'] for c in inspector.get_columns(table.name)] if 'geo_version' in cols: continue # remove the old primary key constraint, if any pk = inspector.get_pk_constraint(table.name)['name'] if pk: session.execute("ALTER TABLE %s DROP CONSTRAINT %s" % (table.name, pk)) # add the new column session.execute( "ALTER TABLE %s ADD COLUMN geo_version VARCHAR(100) DEFAULT ''" % table.name) # add the correct new constraint session.execute(AddConstraint(table.primary_key)) session.commit() finally: session.close()
def forwards(apps, schema_editor): """ Ensure all data tables have the new geo_version column, with a default of '' """ session = get_session() inspector = inspect(session.bind) try: for data_table in DATA_TABLES.itervalues(): db_model = data_table.model table = db_model.__table__ cols = [c['name'] for c in inspector.get_columns(table.name)] if 'geo_version' in cols: continue # remove the old primary key constraint, if any pk = inspector.get_pk_constraint(table.name)['name'] if pk: session.execute("ALTER TABLE %s DROP CONSTRAINT %s" % (table.name, pk)) # add the new column session.execute("ALTER TABLE %s ADD COLUMN geo_version VARCHAR(100) DEFAULT ''" % table.name) # add the correct new constraint session.execute(AddConstraint(table.primary_key)) session.commit() finally: session.close()
def handle(self, *args, **options): from wazimap.data.tables import FIELD_TABLES, DATA_TABLES for table in FIELD_TABLES.itervalues(): self.upgrade_field_table(table) for table in DATA_TABLES.itervalues(): if not hasattr(table, 'fields'): self.upgrade_simple_table(table)
def get(self, request, *args, **kwargs): return render_json_to_response( [t.as_dict(columns=False) for t in DATA_TABLES.itervalues()])
def setUp(self): self.s = get_session() DATA_TABLES.clear()
def handle(self, *args, **options): self.session = get_session() self.verbosity = options.get('verbosity', 0) self.table_id = options.get('table') self.geo_version = options.get('geo_version') self.store_missing_entries = options.get('store_missing_entries', False) self.dryrun = options.get('dryrun') self.geos = self.get_geos(self.geo_version) self.wc_geos = GeographyYouth.objects.filter(version='2011') self.db_tables = [] self.fields_by_table = {} self.keys_by_table = {} self.missing_keys_by_table = {} self.missing_geos_by_table = {} self.missing_geos_by_simple_table = {} self.simple_tables = {} self.field_tables = {} if self.table_id: table = get_datatable(self.table_id) if type(table) == FieldTable: self.field_tables[table.id] = table else: self.simple_tables[table.id] = table elif ONLY_CHECK_TABLES: for table_id in ONLY_CHECK_TABLES: table = get_datatable(table_id) if type(table) == FieldTable: self.field_tables[table.id] = table else: self.simple_tables[table.id] = table else: self.tables = DATA_TABLES self.field_tables = FIELD_TABLES self.simple_tables = { k: v for k, v in DATA_TABLES.iteritems() if k not in FIELD_TABLES.keys() } for table_id, table in self.field_tables.iteritems(): if table.db_table in self.db_tables: # Multiple field tables can refer to the same underlying db table continue self.db_tables.append(table.db_table) self.stdout.write("Checking table: %s" % (table.id)) self.fields_by_table[table.id] = table.fields self.keys_by_table[table_id] = self.get_table_keys( table, table.fields) rows = self.session.query(table.model).filter( table.model.geo_version == self.geo_version).all() missing_keys = self.get_missing_keys(table, rows, table.fields) if missing_keys: self.missing_keys_by_table[table.id] = missing_keys missing_geos = self.get_missing_geos(table, rows) if missing_geos: self.missing_geos_by_table[table.id] = missing_geos if self.missing_keys_by_table: self.stdout.write("Missing keys for tables:") for table in self.missing_keys_by_table.iterkeys(): self.stdout.write("%s" % (table)) if self.missing_geos_by_table: self.stdout.write("Missing geos for tables:") for table in self.missing_geos_by_table.iterkeys(): self.stdout.write("%s" % table) if self.store_missing_entries: if self.missing_keys_by_table: self.store_missing_keys() self.stdout.write("Missing keys stored in database.") if self.missing_geos_by_table: self.store_missing_geos() self.stdout.write("Missing geos stored in database.") else: self.stdout.write( "Run command with --store-missing-entries to populate missing keys with 0 and missing geos with null" ) for table_id, table in self.simple_tables.iteritems(): self.stdout.write("Checking table: %s" % (table.id)) rows = self.session.query(table.model).filter( table.model.geo_version == self.geo_version).all() missing_geos = self.get_missing_geos(table, rows) if missing_geos: self.missing_geos_by_simple_table[table.id] = missing_geos if self.missing_geos_by_simple_table: self.stdout.write("Missing geos for Simple tables:") for table in self.missing_geos_by_simple_table.iterkeys(): self.stdout.write("%s" % table) if self.store_missing_entries: self.store_simple_table_missing_geos() self.stdout.write( "Missing geos for Simple tables stored in database.") self.session.close()
def get(self, request, *args, **kwargs): return render_json_to_response([t.as_dict(columns=False) for t in DATA_TABLES.itervalues()])