Exemple #1
0
    def from_directory(cls, dir, **options):
        if not os.path.exists(dir):
            raise AttributeError("Directory '{0}' does not exit.".format(dir))

        migration_kwargs, import_kwargs = cls._clean_options(options)
        selected_models = migration_kwargs.pop('models')
        selected_serializer = import_kwargs.pop('serializer')

        found_models = []
        files_and_serializers = {}
        for file_name in os.listdir(dir):
            matchobj = FILE_NAME_RE.search(file_name)
            if matchobj is None:
                continue

            app_config = apps.get_app_config(matchobj.group(1))
            model = app_config.get_model(matchobj.group(2))
            if selected_models and model not in selected_models:
                continue

            serializer = (selected_serializer
                          or serializers.get_serializer(matchobj.group(3))())

            found_models.append(model)
            files_and_serializers[model] = (file_name, serializer)

        model_list = sort_dependencies(found_models)
        for model in model_list:
            migration = ModelMigration(model, **migration_kwargs)
            file_name, serializer = files_and_serializers[model]
            file_path = os.path.join(dir, file_name)
            with serializer.open_to_load(file_path) as file:
                migration.import_data(file, serializer, **import_kwargs)
Exemple #2
0
    def test_file(self):
        source_path = os.path.join(MIGRATIONS_DIR, 'backup')
        output = StringIO()
        call_command(
            'import_models',
            file=source_path,
            plan='direct',
            natural=True,
            stdout=output,
        )
        self.assertIn('Entries were successfully imported.', output.getvalue())

        source_path = os.path.join(MIGRATIONS_DIR, 'author.json')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration = ModelMigration(Author)
        result = migration.export_data(None, JsonSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())

        source_path = os.path.join(MIGRATIONS_DIR, 'category.json')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration = ModelMigration(Category)
        result = migration.export_data(None, JsonSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())

        source_path = os.path.join(MIGRATIONS_DIR, 'post.json')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration = ModelMigration(Post)
        result = migration.export_data(None, JsonSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())
Exemple #3
0
    def test_numeric_model(self):
        migration = ModelMigration(NumericModel)
        self.assertEqual(
            [repr(fld) for fld in migration.fields],
            ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
             '<yepes.contrib.datamigrations.fields.IntegerField: integer>',
             '<yepes.contrib.datamigrations.fields.IntegerField: integer_as_string>',
             '<yepes.contrib.datamigrations.fields.FloatField: float>',
             '<yepes.contrib.datamigrations.fields.FloatField: float_as_string>',
             '<yepes.contrib.datamigrations.fields.NumberField: decimal>',
             '<yepes.contrib.datamigrations.fields.NumberField: decimal_as_string>'],
        )
        self.assertEqual(migration.fields_to_export, migration.fields)
        self.assertEqual(migration.fields_to_import, migration.fields)
        self.assertEqual(
            repr(migration.primary_key),
            '<yepes.contrib.datamigrations.fields.IntegerField: id>',
        )
        self.assertIsNone(migration.natural_foreign_keys)
        self.assertTrue(migration.can_create)
        self.assertTrue(migration.can_export)
        self.assertTrue(migration.can_import)
        self.assertTrue(migration.can_update)
        self.assertFalse(migration.requires_model_instances)

        migration = ModelMigration(
            NumericModel,
            use_natural_primary_keys=True,
            use_natural_foreign_keys=True,
        )
        self.assertEqual(
            [repr(fld) for fld in migration.fields],
            ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
             '<yepes.contrib.datamigrations.fields.IntegerField: integer>',
             '<yepes.contrib.datamigrations.fields.IntegerField: integer_as_string>',
             '<yepes.contrib.datamigrations.fields.FloatField: float>',
             '<yepes.contrib.datamigrations.fields.FloatField: float_as_string>',
             '<yepes.contrib.datamigrations.fields.NumberField: decimal>',
             '<yepes.contrib.datamigrations.fields.NumberField: decimal_as_string>'],
        )
        self.assertEqual(migration.fields_to_export, migration.fields)
        self.assertEqual(migration.fields_to_import, migration.fields)
        self.assertEqual(
            repr(migration.primary_key),
            '<yepes.contrib.datamigrations.fields.IntegerField: id>',
        )
        self.assertIsNone(migration.natural_foreign_keys)
        self.assertTrue(migration.can_create)
        self.assertTrue(migration.can_export)
        self.assertTrue(migration.can_import)
        self.assertTrue(migration.can_update)
        self.assertFalse(migration.requires_model_instances)
Exemple #4
0
    def test_file(self):
        migration = ModelMigration(Alphabet)
        source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        SingleImportFacade.from_file_path(
            source_path,
            model='datamigrations_facades_tests.Alphabet',
            serializer='csv',
        )
        result = migration.export_data(serializer=CsvSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())
Exemple #5
0
    def test_boolean_migration(self):
        migration = ModelMigration(BooleanModel)
        self.assertEqual(
            [repr(fld) for fld in migration.fields],
            ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
             '<yepes.contrib.datamigrations.fields.BooleanField: boolean>',
             '<yepes.contrib.datamigrations.fields.BooleanField: boolean_as_string>',
             '<yepes.contrib.datamigrations.fields.BooleanField: null_boolean>',
             '<yepes.contrib.datamigrations.fields.BooleanField: null_boolean_as_string>'],
        )
        self.assertEqual(migration.fields_to_export, migration.fields)
        self.assertEqual(migration.fields_to_import, migration.fields)
        self.assertEqual(
            repr(migration.primary_key),
            '<yepes.contrib.datamigrations.fields.IntegerField: id>',
        )
        self.assertIsNone(migration.natural_foreign_keys)
        self.assertTrue(migration.can_create)
        self.assertTrue(migration.can_export)
        self.assertTrue(migration.can_import)
        self.assertTrue(migration.can_update)
        self.assertFalse(migration.requires_model_instances)

        migration = ModelMigration(
            BooleanModel,
            use_natural_primary_keys=True,
            use_natural_foreign_keys=True,
        )
        self.assertEqual(
            [repr(fld) for fld in migration.fields],
            ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
             '<yepes.contrib.datamigrations.fields.BooleanField: boolean>',
             '<yepes.contrib.datamigrations.fields.BooleanField: boolean_as_string>',
             '<yepes.contrib.datamigrations.fields.BooleanField: null_boolean>',
             '<yepes.contrib.datamigrations.fields.BooleanField: null_boolean_as_string>'],
        )
        self.assertEqual(migration.fields_to_export, migration.fields)
        self.assertEqual(migration.fields_to_import, migration.fields)
        self.assertEqual(
            repr(migration.primary_key),
            '<yepes.contrib.datamigrations.fields.IntegerField: id>',
        )
        self.assertIsNone(migration.natural_foreign_keys)
        self.assertTrue(migration.can_create)
        self.assertTrue(migration.can_export)
        self.assertTrue(migration.can_import)
        self.assertTrue(migration.can_update)
        self.assertFalse(migration.requires_model_instances)
Exemple #6
0
    def test_no_file(self):
        migration = ModelMigration(Alphabet)
        source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration.import_data(source, CsvSerializer, DirectPlan)
        output = StringIO()
        call_command(
            'export_model',
            'datamigrations_commands_tests.Alphabet',
            format='csv',
            stdout=output,
        )
        result = output.getvalue()
        self.assertEqual(source.splitlines(), result.splitlines())
Exemple #7
0
    def test_labels(self):
        migration = ModelMigration(Category)
        source_path = os.path.join(MIGRATIONS_DIR, 'category.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        result_path = os.path.join(self.temp_dir, 'backup')
        MultipleExportFacade.to_file_path(
            result_path,
            models=['datamigrations_facades_tests.Category'],
        )
        with open(result_path, 'r') as result_file:
            result = result_file.read()

        self.assertEqual(1, result.count('Type: MODEL;'))
        self.assertIn('Name: datamigrations_facades_tests.category;', result)
Exemple #8
0
    def test_datetime_model(self):
        migration = ModelMigration(DateTimeModel)
        self.assertEqual(
            [repr(fld) for fld in migration.fields],
            ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
             '<yepes.contrib.datamigrations.fields.DateField: date>',
             '<yepes.contrib.datamigrations.fields.DateTimeField: datetime>',
             '<yepes.contrib.datamigrations.fields.TimeField: time>'],
        )
        self.assertEqual(migration.fields_to_export, migration.fields)
        self.assertEqual(migration.fields_to_import, migration.fields)
        self.assertEqual(
            repr(migration.primary_key),
            '<yepes.contrib.datamigrations.fields.IntegerField: id>',
        )
        self.assertIsNone(migration.natural_foreign_keys)
        self.assertTrue(migration.can_create)
        self.assertTrue(migration.can_export)
        self.assertTrue(migration.can_import)
        self.assertTrue(migration.can_update)
        self.assertFalse(migration.requires_model_instances)

        migration = ModelMigration(
            DateTimeModel,
            use_natural_primary_keys=True,
            use_natural_foreign_keys=True,
        )
        self.assertEqual(
            [repr(fld) for fld in migration.fields],
            ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
             '<yepes.contrib.datamigrations.fields.DateField: date>',
             '<yepes.contrib.datamigrations.fields.DateTimeField: datetime>',
             '<yepes.contrib.datamigrations.fields.TimeField: time>'],
        )
        self.assertEqual(migration.fields_to_export, migration.fields)
        self.assertEqual(migration.fields_to_import, migration.fields)
        self.assertEqual(
            repr(migration.primary_key),
            '<yepes.contrib.datamigrations.fields.IntegerField: id>',
        )
        self.assertIsNone(migration.natural_foreign_keys)
        self.assertTrue(migration.can_create)
        self.assertTrue(migration.can_export)
        self.assertTrue(migration.can_import)
        self.assertTrue(migration.can_update)
        self.assertFalse(migration.requires_model_instances)
Exemple #9
0
    def test_file(self):
        migration = ModelMigration(Alphabet)
        source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
        result_path = os.path.join(self.temp_dir, 'alphabet.csv')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration.import_data(source, CsvSerializer, DirectPlan)
        SingleExportFacade.to_file_path(
            result_path,
            model='datamigrations_facades_tests.Alphabet',
            serializer='csv',
        )
        with open(result_path, 'r') as result_file:
            result = result_file.read()

        self.assertEqual(source.splitlines(), result.splitlines())
Exemple #10
0
    def test_file(self):
        migration = ModelMigration(Alphabet)
        source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        output = StringIO()
        call_command(
            'import_model',
            'datamigrations_commands_tests.Alphabet',
            file=source_path,
            format='csv',
            stdout=output,
        )
        result = migration.export_data(serializer=CsvSerializer)

        self.assertEqual(source.splitlines(), result.splitlines())
        self.assertIn('Entries were successfully imported.', output.getvalue())
Exemple #11
0
    def to_directory(cls, dir, **options):
        if not os.path.exists(dir):
            os.makedirs(dir)

        migration_kwargs, export_kwargs = cls._clean_options(options)

        model_list = sort_dependencies(migration_kwargs.pop('models'))
        model_list.reverse()

        serializer = export_kwargs.pop('serializer')
        for model in model_list:
            migration = ModelMigration(model, **migration_kwargs)
            migration_serializer = migration.get_serializer(serializer)
            file_name = '{0}.{1}.{2}'.format(model._meta.app_label,
                                             model._meta.model_name,
                                             migration_serializer.name)

            file_path = os.path.join(dir, file_name)
            with migration_serializer.open_to_dump(file_path) as file:
                migration.export_data(file, migration_serializer,
                                      **export_kwargs)
Exemple #12
0
    def test_labels(self):
        migration = ModelMigration(Category)
        source_path = os.path.join(MIGRATIONS_DIR, 'category.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        result_path = os.path.join(self.temp_dir, 'backup')
        output = StringIO()
        call_command(
            'export_models',
            'datamigrations_commands_tests.Category',
            file=result_path,
            format='json',
            natural=True,
            stdout=output,
        )
        with open(result_path, 'r') as result_file:
            result = result_file.read()

        self.assertEqual(1, result.count('Type: MODEL;'))
        self.assertIn('Name: datamigrations_commands_tests.category;', result)
Exemple #13
0
 def test_blog_category_model(self):
     migration = ModelMigration(BlogCategoryModel)
     self.assertEqual(
         [repr(fld) for fld in migration.fields],
         ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
          '<yepes.contrib.datamigrations.fields.IntegerField: blog (blog_id)>',
          '<yepes.contrib.datamigrations.fields.TextField: name>',
          '<yepes.contrib.datamigrations.fields.TextField: description>'],
     )
     self.assertEqual(migration.fields_to_export, migration.fields)
     self.assertEqual(migration.fields_to_import, migration.fields)
     self.assertEqual(
         repr(migration.primary_key),
         '<yepes.contrib.datamigrations.fields.IntegerField: id>',
     )
     self.assertIsNone(migration.natural_foreign_keys)
     self.assertTrue(migration.can_create)
     self.assertTrue(migration.can_export)
     self.assertTrue(migration.can_import)
     self.assertTrue(migration.can_update)
     self.assertFalse(migration.requires_model_instances)
Exemple #14
0
 def from_file(cls, file, **options):
     migration_kwargs, import_kwargs = cls._clean_options(options)
     migration = ModelMigration(**migration_kwargs)
     migration.import_data(file, **import_kwargs)
Exemple #15
0
 def to_file(cls, file, **options):
     migration_kwargs, export_kwargs = cls._clean_options(options)
     migration = ModelMigration(**migration_kwargs)
     migration.export_data(file, **export_kwargs)
Exemple #16
0
    def test_filter_migration_fields(self):
        migration = ModelMigration(BlogCategoryModel, fields=[
            'pk',
            'name',
            'description',
        ], exclude=[
            'pk',
        ])
        self.assertEqual(
            [repr(fld) for fld in migration.fields],
            ['<yepes.contrib.datamigrations.fields.TextField: name>',
             '<yepes.contrib.datamigrations.fields.TextField: description>'],
        )
        self.assertEqual(migration.fields_to_export, migration.fields)
        self.assertEqual(migration.fields_to_import, migration.fields)
        self.assertIsNone(migration.primary_key)
        self.assertIsNone(migration.natural_foreign_keys)
        self.assertFalse(migration.can_create)
        self.assertTrue(migration.can_export)
        self.assertFalse(migration.can_import)
        self.assertFalse(migration.can_update)
        self.assertFalse(migration.requires_model_instances)

        migration = ModelMigration(BlogCategoryModel, fields=[
            'pk',
            'name',
            'blog__name',
        ])
        self.assertEqual(
            [repr(fld) for fld in migration.fields],
            ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
             '<yepes.contrib.datamigrations.fields.TextField: name>',
             '<yepes.contrib.datamigrations.fields.TextField: blog__name (blog_id__name)>'],
        )
        self.assertEqual(migration.fields_to_export, migration.fields)
        self.assertEqual(migration.fields_to_import, migration.fields)
        self.assertEqual(
            repr(migration.primary_key),
            '<yepes.contrib.datamigrations.fields.IntegerField: id>',
        )
        self.assertEqual(
            [repr(fld) for fld in migration.natural_foreign_keys],
            ['<yepes.contrib.datamigrations.fields.TextField: blog__name (blog_id__name)>'],
        )
        self.assertTrue(migration.can_create)
        self.assertTrue(migration.can_export)
        self.assertTrue(migration.can_import)
        self.assertTrue(migration.can_update)
        self.assertFalse(migration.requires_model_instances)

        migration = ModelMigration(BlogCategoryModel, fields=[
            'pk',
            'name',
            'description',
            'blog__name',
            'blog__description',
        ])
        self.assertEqual(
            [repr(fld) for fld in migration.fields],
            ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
             '<yepes.contrib.datamigrations.fields.TextField: name>',
             '<yepes.contrib.datamigrations.fields.TextField: description>',
             '<yepes.contrib.datamigrations.fields.TextField: blog__name (blog_id__name)>',
             '<yepes.contrib.datamigrations.fields.TextField: blog__description (blog_id__description)>'],
        )
        self.assertEqual(migration.fields_to_export, migration.fields)
        self.assertEqual(
            [repr(fld) for fld in migration.fields_to_import],
            ['<yepes.contrib.datamigrations.fields.IntegerField: id>',
             '<yepes.contrib.datamigrations.fields.TextField: name>',
             '<yepes.contrib.datamigrations.fields.TextField: description>',
             '<yepes.contrib.datamigrations.fields.TextField: blog__name (blog_id__name)>'],
        )
        self.assertEqual(
            repr(migration.primary_key),
            '<yepes.contrib.datamigrations.fields.IntegerField: id>',
        )
        self.assertEqual(
            [repr(fld) for fld in migration.natural_foreign_keys],
            ['<yepes.contrib.datamigrations.fields.TextField: blog__name (blog_id__name)>'],
        )
        self.assertTrue(migration.can_create)
        self.assertTrue(migration.can_export)
        self.assertTrue(migration.can_import)
        self.assertTrue(migration.can_update)
        self.assertFalse(migration.requires_model_instances)
Exemple #17
0
    def test_file(self):
        migration = ModelMigration(Author)
        source_path = os.path.join(MIGRATIONS_DIR, 'author.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(Category)
        source_path = os.path.join(MIGRATIONS_DIR, 'category.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(Tag)
        source_path = os.path.join(MIGRATIONS_DIR, 'tag.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(Post)
        source_path = os.path.join(MIGRATIONS_DIR, 'post.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(PostTags)
        source_path = os.path.join(MIGRATIONS_DIR, 'post_tags.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        source_path = os.path.join(MIGRATIONS_DIR, 'backup')
        result_path = os.path.join(self.temp_dir, 'backup')
        MultipleExportFacade.to_file_path(
            result_path,
            serializer='json',
            use_natural_keys=True,
        )
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        with open(result_path, 'r') as result_file:
            result = result_file.read()

        self.assertEqual(source.splitlines(), result.splitlines())
Exemple #18
0
    def test_file(self):
        migration = ModelMigration(Author)
        source_path = os.path.join(MIGRATIONS_DIR, 'author.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(Category)
        source_path = os.path.join(MIGRATIONS_DIR, 'category.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(Tag)
        source_path = os.path.join(MIGRATIONS_DIR, 'tag.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(Post)
        source_path = os.path.join(MIGRATIONS_DIR, 'post.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(PostTags)
        source_path = os.path.join(MIGRATIONS_DIR, 'post_tags.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        source_path = os.path.join(MIGRATIONS_DIR, 'backup')
        result_path = os.path.join(self.temp_dir, 'backup')
        output = StringIO()
        call_command(
            'export_models',
            file=result_path,
            format='json',
            natural=True,
            stdout=output,
        )
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        with open(result_path, 'r') as result_file:
            result = result_file.read()

        self.assertEqual(source.splitlines(), result.splitlines())
        self.assertIn('Objects were successfully exported.', output.getvalue())
Exemple #19
0
    def test_file(self):
        source_path = os.path.join(MIGRATIONS_DIR, 'backup')
        MultipleImportFacade.from_file_path(
            source_path,
            use_natural_keys=True,
            plan='direct',
        )
        source_path = os.path.join(MIGRATIONS_DIR, 'author.json')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration = ModelMigration(Author)
        result = migration.export_data(None, JsonSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())

        source_path = os.path.join(MIGRATIONS_DIR, 'category.json')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration = ModelMigration(Category)
        result = migration.export_data(None, JsonSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())

        source_path = os.path.join(MIGRATIONS_DIR, 'tag.json')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration = ModelMigration(Tag)
        result = migration.export_data(None, JsonSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())

        source_path = os.path.join(MIGRATIONS_DIR, 'post.json')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration = ModelMigration(Post)
        result = migration.export_data(None, JsonSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())

        source_path = os.path.join(MIGRATIONS_DIR, 'post_tags.json')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration = ModelMigration(PostTags)
        result = migration.export_data(None, JsonSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())