def migrate_model(app_label_models): """ Send all models obect from the models in the list of tuple to the queue migration :param app_label_models: A list of tuple, each tuple has app_label as key and a list of model_name as value ex : [('base',['person', 'tutor', 'offer']),('dissertation',['offer_proposition', 'adviser'])] """ if hasattr(settings, 'QUEUES'): print('Queue Name : {}'.format( settings.QUEUES.get('QUEUES_NAME').get('MIGRATIONS_TO_PRODUCE'))) print('Models : ') for app_label, model_names in app_label_models: print(' App label : {}'.format(app_label)) for model_name in model_names: print(' Model : {}'.format(model_name)) try: Model = apps.get_model(app_label=app_label, model_name=model_name) except LookupError: print(' Model {} does not exists'.format(model_name)) continue objects = Model.objects.all() print(' Count of objects to send : {}'.format( str(len(objects)))) for entity in objects: try: ser_obj = serialize(entity) queue_sender.send_message( settings.QUEUES.get('QUEUES_NAME').get( 'MIGRATIONS_TO_PRODUCE'), wrap_serialization(ser_obj)) except (ChannelClosed, ConnectionClosed): print('QueueServer is not installed or not launched') else: print('You have to configure queues to use migration script!')
def test_persist_case_insert_new_one(self, mock_make_upsert): structure_serialized = serialize(self.model_with_user, to_delete=False) persist(structure_serialized) mock_make_upsert.assert_called_once_with( structure_serialized['fields'], SerializableModel, ModelWithUser )
def test_make_upsert_case_insert(self): obj = ModelWithUser(user='******', name='With User') structure_serialized = serialize(obj, to_delete=False) pk = _make_upsert(structure_serialized['fields'], SerializableModel, ModelWithUser) self.assertTrue(pk) self.assertTrue( ModelWithUser.objects.filter(pk=pk, user='******', name='With User').exists())
def test_persist_case_update_existing(self, mock_make_upsert): # Save instance in order to have UUID on database self.model_with_user.save() structure_serialized = serialize(self.model_with_user, to_delete=False) persist(structure_serialized) mock_make_upsert.assert_called_once_with( structure_serialized['fields'], SerializableModel, ModelWithUser, instance=self.model_with_user)
def test_persist_case_update_existing(self, mock_make_upsert): # Save instance in order to have UUID on database self.model_with_user.save() structure_serialized = serialize(self.model_with_user, to_delete=False) persist(structure_serialized) mock_make_upsert.assert_called_once_with( structure_serialized['fields'], SerializableModel, ModelWithUser, instance=self.model_with_user )
def test_make_upsert_case_insert(self): obj = ModelWithUser(user='******', name='With User') structure_serialized = serialize(obj, to_delete=False) pk = _make_upsert( structure_serialized['fields'], SerializableModel, ModelWithUser ) self.assertTrue(pk) self.assertTrue( ModelWithUser.objects.filter(pk=pk, user='******', name='With User').exists() )
def test_make_upsert_case_update(self): obj = ModelWithUser(user='******', name='With User') obj.save() structure_serialized = serialize(obj, to_delete=False) structure_serialized['fields']['name'] = "Update name" _make_upsert(structure_serialized['fields'], SerializableModel, ModelWithUser, instance=obj) obj.refresh_from_db() self.assertTrue(obj.pk) self.assertEqual(obj.name, "Update name")
def test_make_upsert_case_update(self): obj = ModelWithUser(user='******', name='With User') obj.save() structure_serialized = serialize(obj, to_delete=False) structure_serialized['fields']['name'] = "Update name" _make_upsert( structure_serialized['fields'], SerializableModel, ModelWithUser, instance=obj ) obj.refresh_from_db() self.assertTrue(obj.pk) self.assertEqual(obj.name, "Update name")
def test_persist_case_insert_new_one(self, mock_make_upsert): structure_serialized = serialize(self.model_with_user, to_delete=False) persist(structure_serialized) mock_make_upsert.assert_called_once_with( structure_serialized['fields'], SerializableModel, ModelWithUser)