def test_add_field(self): self.create_table() field = {'name': 'counter', 'type': 'INTEGER'} sql.add_field(TEST_PGA_PATH, self.tablename, field) _schema = schema.get_schema(self.tablename) self.assertEqual(_schema[2]['name'], 'counter') self.assertEqual(_schema[2]['type'], 'INTEGER')
def migrate(table, schema): """Compare a database table with the reference model and make necessary changes This is very basic and only the needed features have been implemented (adding columns) Args: table (str): Name of the table to migrate schema (dict): Reference schema for the table Returns: list: The list of column names that have been added """ existing_schema = get_schema(table) migrated_fields = [] if existing_schema: columns = [col["name"] for col in existing_schema] for field in schema: if field["name"] not in columns: logger.info("Migrating %s field %s", table, field["name"]) migrated_fields.append(field["name"]) sql.add_field(settings.PGA_DB, table, field) else: create_table(table, schema) return migrated_fields
def test_cant_add_existing_field(self): self.create_table() field = { 'name': 'name', 'type': 'TEXT' } with self.assertRaises(OperationalError): sql.add_field(settings.PGA_DB, self.tablename, field)
def test_cant_add_existing_field(self): self.create_table() with self.assertRaises(OperationalError): field = {'name': 'name', 'type': 'TEXT'} sql.add_field(TEST_PGA_PATH, self.tablename, field)