def test_add_field(self): self.create_table() field = {'name': 'counter', 'type': 'INTEGER'} sql.add_field(TEST_PGA_PATH, self.tablename, field) schema = pga.get_schema(self.tablename) self.assertEqual(schema[2]['name'], 'counter') self.assertEqual(schema[2]['type'], 'INTEGER')
def test_cant_add_existing_field(self): self.create_table() field = { 'name': 'name', 'type': 'TEXT' } with self.assertRaises(OperationalError): sql.add_field(TEST_PGA_PATH, self.tablename, field)
def test_add_field(self): self.create_table() field = { 'name': 'counter', 'type': 'INTEGER' } sql.add_field(TEST_PGA_PATH, self.tablename, field) schema = pga.get_schema(self.tablename) self.assertEqual(schema[2]['name'], 'counter') self.assertEqual(schema[2]['type'], 'INTEGER')
def migrate(table, schema): 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: migrated_fields.append(field['name']) sql.add_field(PGA_DB, table, field) else: create_table(table, schema) return migrated_fields
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(PGA_DB, table, field) else: create_table(table, schema) return migrated_fields