Skip to content

Commit

Permalink
Merge pull request #113 from nautobot/u/gas-squash-migrations
Browse files Browse the repository at this point in the history
Squash migrations / fix migrations for Nautobot 2.0
  • Loading branch information
scetron authored Oct 16, 2023
2 parents 417396d + 969640c commit 475c7a7
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 13 deletions.
4 changes: 0 additions & 4 deletions nautobot_device_onboarding/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ class Migration(migrations.Migration):
("dcim", "0004_initial_part_4"),
]

run_before = [
("dcim", "0028_alter_device_and_rack_role_add_new_role"),
]

operations = [
migrations.CreateModel(
name="OnboardingTask",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Generated by Django 3.2.21 on 2023-10-13 16:17

import uuid

from django.db import migrations, models
import django.db.models.deletion

import nautobot_device_onboarding.models


class Migration(migrations.Migration):
initial = True

replaces = [
("nautobot_device_onboarding", "0001_initial"),
("nautobot_device_onboarding", "0004_migrate_to_extras_role_part_1"),
("nautobot_device_onboarding", "0004_migrate_to_extras_role_part_2"),
("nautobot_device_onboarding", "0004_migrate_to_extras_role_part_3"),
("nautobot_device_onboarding", "0005_migrate_site_to_location_part_1"),
("nautobot_device_onboarding", "0005_migrate_site_to_location_part_2"),
("nautobot_device_onboarding", "0005_migrate_site_to_location_part_3"),
("nautobot_device_onboarding", "0006_update_model_fields_part_1"),
("nautobot_device_onboarding", "0006_update_model_fields_part_2"),
("nautobot_device_onboarding", "0006_update_model_fields_part_3"),
]

dependencies = [
("dcim", "0049_remove_slugs_and_change_device_primary_ip_fields"),
("extras", "0098_rename_data_jobresult_result"),
]

operations = [
migrations.CreateModel(
name="OnboardingDevice",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True
),
),
("enabled", models.BooleanField(default=True)),
("device", models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to="dcim.device")),
],
options={
"abstract": False,
},
),
migrations.CreateModel(
name="OnboardingTask",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True
),
),
("created", models.DateTimeField(auto_now_add=True, null=True)),
("last_updated", models.DateTimeField(auto_now=True, null=True)),
("ip_address", models.CharField(max_length=255)),
("device_type", models.CharField(blank=True, default="", max_length=255)),
("status", models.CharField(blank=True, default="", max_length=255)),
("failed_reason", models.CharField(blank=True, default="", max_length=255)),
("message", models.CharField(blank=True, max_length=511)),
("port", models.PositiveSmallIntegerField(default=22)),
("timeout", models.PositiveSmallIntegerField(default=30)),
(
"created_device",
models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to="dcim.device"
),
),
(
"location",
models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to="dcim.location"
),
),
(
"platform",
models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to="dcim.platform"
),
),
(
"role",
nautobot_device_onboarding.models.DeviceLimitedRoleField(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="onboarding_tasks",
to="extras.role",
),
),
],
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Migration(migrations.Migration):
dependencies = [
("nautobot_device_onboarding", "0003_onboardingtask_label"),
("nautobot_device_onboarding", "0001_initial"),
("dcim", "0029_device_and_rack_roles_data_migrations"),
]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.db import migrations
from django.db import connection, migrations


class Migration(migrations.Migration):
dependencies = [
("nautobot_device_onboarding", "0004_migrate_to_extras_role_part_2"),
]

run_before = [
nautobot_run_before = [
("dcim", "0031_remove_device_role_and_rack_role"),
]

Expand All @@ -21,3 +21,18 @@ class Migration(migrations.Migration):
new_name="role",
),
]

def __init__(self, name, app_label):
"""
Override the default __init__ method to conditionally set the run_before attribute based on whether
the first squashed migration has already been applied. This is necessary to ensure that the depdendency
graph generated by Django does not fail due to a circular dependency.
"""
super().__init__(name, app_label)
if self.nautobot_run_before:
recorder = migrations.recorder.MigrationRecorder(connection)
applied_migrations = recorder.applied_migrations()
if ("nautobot_device_onboarding", "0001_initial") in applied_migrations:
for migration in self.nautobot_run_before:
if migration not in applied_migrations:
self.run_before.append(migration)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.db import migrations
from django.db import connection, migrations


class Migration(migrations.Migration):
dependencies = [
("nautobot_device_onboarding", "0005_migrate_site_to_location_part_2"),
]

run_before = [
nautobot_run_before = [
("dcim", "0040_remove_region_and_site"),
]

Expand All @@ -16,3 +16,18 @@ class Migration(migrations.Migration):
name="site",
)
]

def __init__(self, name, app_label):
"""
Override the default __init__ method to conditionally set the run_before attribute based on whether
the first squashed migration has already been applied. This is necessary to ensure that the depdendency
graph generated by Django does not fail due to a circular dependency.
"""
super().__init__(name, app_label)
if self.nautobot_run_before:
recorder = migrations.recorder.MigrationRecorder(connection)
applied_migrations = recorder.applied_migrations()
if ("nautobot_device_onboarding", "0001_initial") in applied_migrations:
for migration in self.nautobot_run_before:
if migration not in applied_migrations:
self.run_before.append(migration)
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ class Migration(migrations.Migration):
name="status",
field=models.CharField(blank=True, default="", max_length=255),
),
migrations.AlterUniqueTogether(
name="onboardingtask",
unique_together={("label", "ip_address")},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("nautobot_device_onboarding", "0003_onboardingtask_label"),
# This migration does not actually depend on the below migration but it is added here to
# prevent Django from failing to migrate due to multiple leaf nodes in the migration graph
("nautobot_device_onboarding", "0007_alter_onboardingtask_ip_address"),
]

operations = [
migrations.AlterModelOptions(
name="onboardingtask",
options={
"ordering": ("label",),
},
),
migrations.AlterUniqueTogether(
name="onboardingtask",
unique_together={("label", "ip_address")},
),
]

0 comments on commit 475c7a7

Please sign in to comment.