def _build_schema(self): # pragma: no cover schema = qvarn.schema_from_prototype( self._prototype, resource_type=self._item_type) for subpath, subproto in self._subitem_prototypes.get_all(): schema += qvarn.schema_from_prototype( subproto, resource_type=self._item_type, subpath=subpath) return schema
def _build_schema(self): # pragma: no cover schema = qvarn.schema_from_prototype(self._prototype, resource_type=self._item_type) for subpath, subproto in self._subitem_prototypes.get_all(): schema += qvarn.schema_from_prototype( subproto, resource_type=self._item_type, subpath=subpath) return schema
def create_tables_for_resource_type(transaction, resource_type, prototype_list): '''Create database tables for a resource type. This creates all the tables for one resource type, given a list of prototypes and additional information. The list of prototypes is a list of tuples (prototype, kwargs), where kwargs are given to schema_from_prototype (which gives them to qvarn.table_name). However, the resource type is given as a separate argument, so that it does not need to be repeated for each kwargs. For example: resource_type = u'foo' prototype_list = [ (prototype, {}), (photo_prototype, {u'subpath': u'photo'}), (qvarn.listener_prototype, {u'auxtable': u'listener'}), (qvarn.notification_prototype, {u'auxtable': u'notification'}), ] create_tables_for_resource_type( transaction, resource_type, prototype_list) ''' for prototype, kwargs in prototype_list: schema = qvarn.schema_from_prototype(prototype, resource_type=resource_type, **kwargs) create_tables_from_schema(transaction, schema)
def create_tables_for_resource_type( transaction, resource_type, prototype_list): # pragma: no cover '''Create database tables for a resource type. This creates all the tables for one resource type, given a list of prototypes and additional information. The list of prototypes is a list of tuples (prototype, kwargs), where kwargs are given to schema_from_prototype (which gives them to qvarn.table_name). However, the resource type is given as a separate argument, so that it does not need to be repeated for each kwargs. For example: resource_type = u'foo' prototype_list = [ (prototype, {}), (photo_prototype, {u'subpath': u'photo'}), (qvarn.listener_prototype, {u'auxtable': u'listener'}), (qvarn.notification_prototype, {u'auxtable': u'notification'}), ] create_tables_for_resource_type( transaction, resource_type, prototype_list) ''' for prototype, kwargs in prototype_list: schema = qvarn.schema_from_prototype( prototype, resource_type=resource_type, **kwargs) create_tables_from_schema(transaction, schema)
def test_gives_correct_schema_from_prototype_with_dict_list(self): prototype = { u'type': u'', u'id': u'', u'vehicle': [ { u'vehicle_type': u'', u'owners': [u''], }, ], } schema = qvarn.schema_from_prototype(prototype, resource_type=u'foo') self.assertEqual( sorted(schema), sorted([ (u'foo', u'type', unicode), (u'foo', u'id', unicode), (u'foo_vehicle', u'id', unicode), (u'foo_vehicle', u'list_pos', int), (u'foo_vehicle', u'vehicle_type', unicode), (u'foo_vehicle_owners', u'id', unicode), (u'foo_vehicle_owners', u'dict_list_pos', int), (u'foo_vehicle_owners', u'list_pos', int), (u'foo_vehicle_owners', u'owners', unicode), ]))
def test_gives_correct_schema_from_prototype_with_dict_list(self): prototype = { u'type': u'', u'id': u'', u'vehicle': [ { u'vehicle_type': u'', u'owners': [u''], }, ], } schema = qvarn.schema_from_prototype( prototype, resource_type=u'foo') self.assertEqual( sorted(schema), sorted([ (u'foo', u'type', unicode), (u'foo', u'id', unicode), (u'foo_vehicle', u'id', unicode), (u'foo_vehicle', u'list_pos', int), (u'foo_vehicle', u'vehicle_type', unicode), (u'foo_vehicle_owners', u'id', unicode), (u'foo_vehicle_owners', u'dict_list_pos', int), (u'foo_vehicle_owners', u'list_pos', int), (u'foo_vehicle_owners', u'owners', unicode), ]))
def _create_tables(self, transaction, prototype_list, table_names): delta_prototype_list = [] for prototype, kwargs in prototype_list: schema = qvarn.schema_from_prototype( prototype, resource_type=self._resource_type, **kwargs) if any(x[0] in table_names for x in schema): delta_prototype_list.append((prototype, kwargs)) qvarn.create_tables_for_resource_type(transaction, self._resource_type, delta_prototype_list)
def _make_table_dict_from_version(self, version): tables = {} for prototype, kwargs in version.prototype_list: schema = qvarn.schema_from_prototype( prototype, resource_type=self._resource_type, **kwargs) for table_name, column_name, _ in schema: if table_name not in tables: tables[table_name] = [] tables[table_name].append(column_name) return tables
def test_gives_correct_schema_from_prototype_for_subresource(self): prototype = { u'foo': u'', } schema = qvarn.schema_from_prototype(prototype, resource_type='big', subpath=u'secret') table_name = qvarn.table_name(resource_type=u'big', subpath=u'secret') self.assertEqual( sorted(schema), sorted([ (table_name, u'id', unicode), (table_name, u'foo', unicode), ]))
def test_gives_correct_schema_from_prototype_for_subresource(self): prototype = { u'foo': u'', } schema = qvarn.schema_from_prototype( prototype, resource_type='big', subpath=u'secret') table_name = qvarn.table_name( resource_type=u'big', subpath=u'secret') self.assertEqual( sorted(schema), sorted([ (table_name, u'id', unicode), (table_name, u'foo', unicode), ]))
def test_gives_correct_schema_from_prototype_with_simple_fields(self): prototype = { u'type': u'', u'name': u'', u'age': 0, } schema = qvarn.schema_from_prototype(prototype, resource_type=u'foo') self.assertEqual( sorted(schema), sorted([ (u'foo', u'type', unicode), (u'foo', u'id', unicode), (u'foo', u'name', unicode), (u'foo', u'age', int), ]))
def test_gives_correct_schema_from_prototype_with_simple_fields(self): prototype = { u'type': u'', u'name': u'', u'age': 0, } schema = qvarn.schema_from_prototype( prototype, resource_type=u'foo') self.assertEqual( sorted(schema), sorted([ (u'foo', u'type', unicode), (u'foo', u'id', unicode), (u'foo', u'name', unicode), (u'foo', u'age', int), ]))
def test_gives_correct_schema_from_prototype_with_string_list(self): prototype = { u'type': u'', u'id': u'', u'strings': [u''], } schema = qvarn.schema_from_prototype(prototype, resource_type=u'foo') self.assertEqual( sorted(schema), sorted([ (u'foo', u'type', unicode), (u'foo', u'id', unicode), (u'foo_strings', u'id', unicode), (u'foo_strings', u'list_pos', int), (u'foo_strings', u'strings', unicode), ]))
def test_gives_correct_schema_from_prototype_with_string_list(self): prototype = { u'type': u'', u'id': u'', u'strings': [u''], } schema = qvarn.schema_from_prototype( prototype, resource_type=u'foo') self.assertEqual( sorted(schema), sorted([ (u'foo', u'type', unicode), (u'foo', u'id', unicode), (u'foo_strings', u'id', unicode), (u'foo_strings', u'list_pos', int), (u'foo_strings', u'strings', unicode), ]))
def _prepare_version(self, transaction, version, tables): # Collect what is missing. create_tables = collections.defaultdict(dict) create_columns = collections.defaultdict(dict) change_columns = collections.defaultdict(dict) for prototype, kwargs in version.prototype_list: schema = qvarn.schema_from_prototype( prototype, resource_type=self._resource_type, **kwargs) for table_name, column_name, column_type in schema: # Create table if table_name not in tables: create_tables[table_name][column_name] = column_type # Add column elif column_name not in tables[table_name]: create_columns[table_name][column_name] = column_type # Alter column elif column_type != tables[table_name][column_name]: change_columns[table_name][column_name] = ( tables[table_name][column_name], column_type, ) # Create missing tables. for table_name in create_tables: transaction.create_table(table_name, create_tables[table_name]) tables[table_name] = dict(create_tables[table_name]) # Create missing columns. for table_name, columns in create_columns.items(): for column_name, column_type in columns.items(): transaction.add_column(table_name, column_name, column_type) tables[table_name][column_name] = column_type # Change column types. for table_name, columns in change_columns.items(): for column_name, (old, new) in columns.items(): transaction.alter_column(table_name, column_name, old, new) tables[table_name][column_name] = new
def test_gives_correct_schema_from_prototype_with_inner_dict_list(self): prototype = { u'type': u'', u'id': u'', u'vehicle': [ { u'vehicle_type': u'', u'owners': [ { u'owner_names': [u''], u'owned_from_year': 0, }, ], }, ], } schema = qvarn.schema_from_prototype(prototype, resource_type=u'foo') self.maxDiff = None self.assertEqual( sorted(schema), sorted([ (u'foo', u'type', six.text_type), (u'foo', u'id', six.text_type), (u'foo_vehicle', u'id', six.text_type), (u'foo_vehicle', u'list_pos', int), (u'foo_vehicle', u'vehicle_type', six.text_type), (u'foo_vehicle_owners', u'id', six.text_type), (u'foo_vehicle_owners', u'dict_list_pos', int), (u'foo_vehicle_owners', u'list_pos', int), (u'foo_vehicle_owners', u'owned_from_year', int), (u'foo_vehicle_owners_owner_names', u'id', six.text_type), (u'foo_vehicle_owners_owner_names', u'dict_list_pos', int), (u'foo_vehicle_owners_owner_names', u'list_pos', int), (u'foo_vehicle_owners_owner_names', u'str_list_pos', int), (u'foo_vehicle_owners_owner_names', u'owner_names', six.text_type), ]))
def test_raises_error_for_empty_prototype(self): with self.assertRaises(Exception): qvarn.schema_from_prototype({})