Просмотр исходного кода

refactor tag migrations and add changelog fields to tag

John Anderson 7 лет назад
Родитель
Сommit
b9d11aa4ca

+ 1 - 1
netbox/circuits/migrations/0015_custom_tag_models.py

@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
 
     dependencies = [
         ('circuits', '0014_circuittermination_description'),
-        ('extras', '0018_rename_tag_tables'),
+        ('extras', '0017_tag_taggeditem'),
     ]
 
     operations = [

+ 1 - 1
netbox/dcim/migrations/0070_custom_tag_models.py

@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
 
     dependencies = [
         ('dcim', '0069_deprecate_nullablecharfield'),
-        ('extras', '0018_rename_tag_tables'),
+        ('extras', '0017_tag_taggeditem'),
     ]
 
     operations = [

+ 1 - 8
netbox/extras/migrations/0017_tag_taggeditem.py

@@ -12,7 +12,7 @@ class Migration(migrations.Migration):
         ('extras', '0016_exporttemplate_add_cable'),
     ]
 
-    state_operations = [
+    operations = [
         migrations.CreateModel(
             name='Tag',
             fields=[
@@ -41,10 +41,3 @@ class Migration(migrations.Migration):
             index_together={('content_type', 'object_id')},
         ),
     ]
-
-    operations = [
-        migrations.SeparateDatabaseAndState(
-            database_operations=None,
-            state_operations=state_operations
-        )
-    ]

+ 0 - 53
netbox/extras/migrations/0018_rename_tag_tables.py

@@ -1,53 +0,0 @@
-# Generated by Django 2.1.4 on 2019-02-20 06:59
-
-from django.db import migrations
-
-
-class AppTaggitAlterModelTable(migrations.AlterModelTable):
-    """
-    A special subclass of AlterModelTable which hardcodes the app_label to 'taggit'
-
-    This is needed because the migration deals with models which belong to the taggit
-    app, however because taggit is a 3rd party app, we cannot create our own migrations
-    there.
-    """
-
-    def state_forwards(self, app_label, state):
-        super().state_forwards('taggit', state)
-
-    def database_forwards(self, app_label, schema_editor, from_state, to_state):
-        super().database_forwards('taggit', schema_editor, from_state, to_state)
-
-    def database_backwards(self, app_label, schema_editor, from_state, to_state):
-        super().database_backwards('taggit', schema_editor, from_state, to_state)
-
-    def reduce(self, operation, app_label=None):
-        if app_label:
-            app_label = 'taggit'
-        super().reduce(operation, app_label=app_label)
-
-
-class Migration(migrations.Migration):
-    """
-    Rename the tables from taggit_* to extras_*
-
-    Note that while we change the database state, we are not deleting the django
-    model state for the taggit models. Doing so would result in makemigrations
-    recreating them.
-    """
-
-    dependencies = [
-        ('taggit', '0001_initial'),
-        ('extras', '0017_tag_taggeditem'),
-    ]
-
-    operations = [
-        AppTaggitAlterModelTable(
-            name='Tag',
-            table='extras_tag'
-        ),
-        AppTaggitAlterModelTable(
-            name='TaggedItem',
-            table='extras_taggeditem'
-        ),
-    ]

+ 65 - 0
netbox/extras/migrations/0018_tag_data.py

@@ -0,0 +1,65 @@
+# Generated by Django 2.1.4 on 2019-02-20 06:56
+
+from django.db import migrations, models
+import django.db.models.deletion
+import utilities.fields
+
+
+def copy_tags(apps, schema_editor):
+    """
+    Copy data from taggit_tag to extras_tag
+    """
+    TaggitTag = apps.get_model('taggit', 'Tag')
+    ExtrasTag = apps.get_model('extras', 'Tag')
+
+    tags_values = TaggitTag.objects.all().values('id', 'name', 'slug')
+    tags = [ExtrasTag(**tag) for tag in tags_values]
+    ExtrasTag.objects.bulk_create(tags)
+
+
+def copy_taggeditems(apps, schema_editor):
+    """
+    Copy data from taggit_taggeditem to extras_taggeditem
+    """
+    TaggitTaggedItem = apps.get_model('taggit', 'TaggedItem')
+    ExtrasTaggedItem = apps.get_model('extras', 'TaggedItem')
+
+    tagged_items_values = TaggitTaggedItem.objects.all().values('id', 'object_id', 'content_type_id', 'tag_id')
+    tagged_items = [ExtrasTaggedItem(**tagged_item) for tagged_item in tagged_items_values]
+    ExtrasTaggedItem.objects.bulk_create(tagged_items)
+
+
+def delete_taggit_taggeditems(apps, schema_editor):
+    """
+    Delete all TaggedItem instances from taggit_taggeditem
+    """
+    TaggitTaggedItem = apps.get_model('taggit', 'TaggedItem')
+    TaggitTaggedItem.objects.all().delete()
+
+
+def delete_taggit_tags(apps, schema_editor):
+    """
+    Delete all Tag instances from taggit_tag
+    """
+    TaggitTag = apps.get_model('taggit', 'Tag')
+    TaggitTag.objects.all().delete()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extras', '0017_tag_taggeditem'),
+        ('circuits', '0015_custom_tag_models'),
+        ('dcim', '0070_custom_tag_models'),
+        ('ipam', '0025_custom_tag_models'),
+        ('secrets', '0006_custom_tag_models'),
+        ('tenancy', '0006_custom_tag_models'),
+        ('virtualization', '0009_custom_tag_models'),
+    ]
+
+    operations = [
+        migrations.RunPython(copy_tags),
+        migrations.RunPython(copy_taggeditems),
+        migrations.RunPython(delete_taggit_taggeditems),
+        migrations.RunPython(delete_taggit_tags),
+    ]

+ 11 - 7
netbox/extras/migrations/0019_add_color_comments_to_tag.py → netbox/extras/migrations/0019_add_color_comments_changelog_to_tag.py

@@ -7,13 +7,7 @@ import utilities.fields
 class Migration(migrations.Migration):
 
     dependencies = [
-        ('extras', '0018_rename_tag_tables'),
-        ('circuits', '0015_custom_tag_models'),
-        ('dcim', '0070_custom_tag_models'),
-        ('ipam', '0025_custom_tag_models'),
-        ('secrets', '0006_custom_tag_models'),
-        ('tenancy', '0006_custom_tag_models'),
-        ('virtualization', '0009_custom_tag_models'),
+        ('extras', '0018_tag_data'),
     ]
 
     operations = [
@@ -27,4 +21,14 @@ class Migration(migrations.Migration):
             name='comments',
             field=models.TextField(blank=True, default=''),
         ),
+        migrations.AddField(
+            model_name='tag',
+            name='created',
+            field=models.DateField(auto_now_add=True, null=True),
+        ),
+        migrations.AddField(
+            model_name='tag',
+            name='last_updated',
+            field=models.DateTimeField(auto_now=True, null=True),
+        ),
     ]

+ 7 - 2
netbox/extras/models.py

@@ -868,8 +868,8 @@ class ObjectChange(models.Model):
 # Tags
 #
 
-
-class Tag(TagBase):
+from utilities.models import ChangeLoggedModel
+class Tag(TagBase, ChangeLoggedModel):
     color = ColorField(
         default='9e9e9e'
     )
@@ -885,3 +885,8 @@ class TaggedItem(GenericTaggedItemBase):
         related_name="%(app_label)s_%(class)s_items",
         on_delete=models.CASCADE
     )
+
+    class Meta:
+        index_together = (
+            ("content_type", "object_id")
+        )

+ 1 - 1
netbox/ipam/migrations/0025_custom_tag_models.py

@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
 
     dependencies = [
         ('ipam', '0024_vrf_allow_null_rd'),
-        ('extras', '0018_rename_tag_tables'),
+        ('extras', '0017_tag_taggeditem'),
     ]
 
     operations = [

+ 1 - 1
netbox/secrets/migrations/0006_custom_tag_models.py

@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
 
     dependencies = [
         ('secrets', '0005_change_logging'),
-        ('extras', '0018_rename_tag_tables'),
+        ('extras', '0017_tag_taggeditem'),
     ]
 
     operations = [

+ 1 - 1
netbox/tenancy/migrations/0006_custom_tag_models.py

@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
 
     dependencies = [
         ('tenancy', '0005_change_logging'),
-        ('extras', '0018_rename_tag_tables'),
+        ('extras', '0017_tag_taggeditem'),
     ]
 
     operations = [

+ 1 - 1
netbox/virtualization/migrations/0009_custom_tag_models.py

@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
 
     dependencies = [
         ('virtualization', '0008_virtualmachine_local_context_data'),
-        ('extras', '0018_rename_tag_tables'),
+        ('extras', '0017_tag_taggeditem'),
     ]
 
     operations = [