Cleaned up migrations

This commit is contained in:
Jeremy Stretch 2018-10-31 11:52:43 -04:00
parent 470aabe1d7
commit f2f9ff5cbd
2 changed files with 59 additions and 60 deletions

View File

@ -20,28 +20,28 @@ def circuit_terminations_to_cables(apps, schema_editor):
# Create a new Cable instance from each console connection # Create a new Cable instance from each console connection
print("\n Adding circuit terminations... ", end='', flush=True) print("\n Adding circuit terminations... ", end='', flush=True)
for circuittermination in CircuitTermination.objects.filter(interface__isnull=False): for circuittermination in CircuitTermination.objects.filter(interface__isnull=False):
c = Cable()
# We have to assign all fields manually because we're inside a migration. # Create the new Cable
c.termination_a_type = circuittermination_type cable = Cable.objects.create(
c.termination_a_id = circuittermination.id termination_a_type=circuittermination_type,
c.termination_b_type = interface_type termination_a_id=circuittermination.id,
c.termination_b_id = circuittermination.interface_id termination_b_type=interface_type,
c.connection_status = CONNECTION_STATUS_CONNECTED termination_b_id=circuittermination.interface_id,
c.save() status=CONNECTION_STATUS_CONNECTED
)
# Cache the connected Cable on the CircuitTermination
circuittermination.cable = c
circuittermination.connected_endpoint = circuittermination.interface
circuittermination.connection_status = CONNECTION_STATUS_CONNECTED
circuittermination.save()
# Cache the Cable on its two termination points
CircuitTermination.objects.filter(pk=circuittermination.pk).update(
cable=cable,
connected_endpoint=circuittermination.interface,
connection_status=CONNECTION_STATUS_CONNECTED
)
# Cache the connected Cable on the Interface # Cache the connected Cable on the Interface
interface = circuittermination.interface Interface.objects.filter(pk=circuittermination.interface_id).update(
interface.cable = c cable=cable,
interface._connected_circuittermination = circuittermination _connected_circuittermination=circuittermination,
interface.connection_status = CONNECTION_STATUS_CONNECTED connection_status=CONNECTION_STATUS_CONNECTED
interface.save() )
cable_count = Cable.objects.filter(termination_a_type=circuittermination_type).count() cable_count = Cable.objects.filter(termination_a_type=circuittermination_type).count()
print("{} cables created".format(cable_count)) print("{} cables created".format(cable_count))

View File

@ -19,19 +19,19 @@ def console_connections_to_cables(apps, schema_editor):
# Create a new Cable instance from each console connection # Create a new Cable instance from each console connection
print("\n Adding console connections... ", end='', flush=True) print("\n Adding console connections... ", end='', flush=True)
for consoleport in ConsolePort.objects.filter(connected_endpoint__isnull=False): for consoleport in ConsolePort.objects.filter(connected_endpoint__isnull=False):
c = Cable()
# We have to assign GFK fields manually because we're inside a migration. # Create the new Cable
c.termination_a_type = consoleport_type cable = Cable.objects.create(
c.termination_a_id = consoleport.id termination_a_type=consoleport_type,
c.termination_b_type = consoleserverport_type termination_a_id=consoleport.id,
c.termination_b_id = consoleport.connected_endpoint_id termination_b_type=consoleserverport_type,
c.connection_status = consoleport.connection_status termination_b_id=consoleport.connected_endpoint_id,
c.save() status=consoleport.connection_status
)
# Cache the Cable on its two termination points (replicate Cable.save()) # Cache the Cable on its two termination points
ConsolePort.objects.filter(pk=consoleport.id).update(cable=c) ConsolePort.objects.filter(pk=consoleport.id).update(cable=cable)
ConsoleServerPort.objects.filter(pk=consoleport.connected_endpoint_id).update(cable=c) ConsoleServerPort.objects.filter(pk=consoleport.connected_endpoint_id).update(cable=cable)
cable_count = Cable.objects.filter(termination_a_type=consoleport_type).count() cable_count = Cable.objects.filter(termination_a_type=consoleport_type).count()
print("{} cables created".format(cable_count)) print("{} cables created".format(cable_count))
@ -53,19 +53,19 @@ def power_connections_to_cables(apps, schema_editor):
# Create a new Cable instance from each power connection # Create a new Cable instance from each power connection
print(" Adding power connections... ", end='', flush=True) print(" Adding power connections... ", end='', flush=True)
for powerport in PowerPort.objects.filter(connected_endpoint__isnull=False): for powerport in PowerPort.objects.filter(connected_endpoint__isnull=False):
c = Cable()
# We have to assign GFK fields manually because we're inside a migration. # Create the new Cable
c.termination_a_type = powerport_type cable = Cable.objects.create(
c.termination_a_id = powerport.id termination_a_type=powerport_type,
c.termination_b_type = poweroutlet_type termination_a_id=powerport.id,
c.termination_b_id = powerport.connected_endpoint_id termination_b_type=poweroutlet_type,
c.connection_status = powerport.connection_status termination_b_id=powerport.connected_endpoint_id,
c.save() status=powerport.connection_status
)
# Cache the Cable on its two termination points (replicate Cable.save()) # Cache the Cable on its two termination points
PowerPort.objects.filter(pk=powerport.id).update(cable=c) PowerPort.objects.filter(pk=powerport.id).update(cable=cable)
PowerOutlet.objects.filter(pk=powerport.connected_endpoint_id).update(cable=c) PowerOutlet.objects.filter(pk=powerport.connected_endpoint_id).update(cable=cable)
cable_count = Cable.objects.filter(termination_a_type=powerport_type).count() cable_count = Cable.objects.filter(termination_a_type=powerport_type).count()
print("{} cables created".format(cable_count)) print("{} cables created".format(cable_count))
@ -86,28 +86,27 @@ def interface_connections_to_cables(apps, schema_editor):
# Create a new Cable instance from each InterfaceConnection # Create a new Cable instance from each InterfaceConnection
print(" Adding interface connections... ", end='', flush=True) print(" Adding interface connections... ", end='', flush=True)
for conn in InterfaceConnection.objects.all(): for conn in InterfaceConnection.objects.all():
c = Cable()
# We have to assign all fields manually because we're inside a migration. # Create the new Cable
c.termination_a_type = interface_type cable = Cable.objects.create(
c.termination_a_id = conn.interface_a_id termination_a_type=interface_type,
c.termination_b_type = interface_type termination_a_id=conn.interface_a_id,
c.termination_b_id = conn.interface_b_id termination_b_type=interface_type,
c.connection_status = conn.connection_status termination_b_id=conn.interface_b_id,
c.save() status=conn.connection_status
)
# Cache the connected Cable on each Interface # Cache the connected Cable on each Interface
interface_a = conn.interface_a Interface.objects.filter(pk=conn.interface_a_id).update(
interface_a._connected_interface = conn.interface_b _connected_interface=conn.interface_b,
interface_a.connection_status = conn.connection_status connection_status=conn.connection_status,
interface_a.cable = c cable=cable
interface_a.save() )
Interface.objects.filter(pk=conn.interface_b_id).update(
interface_b = conn.interface_b _connected_interface=conn.interface_a,
interface_b._connected_interface = conn.interface_a connection_status=conn.connection_status,
interface_b.connection_status = conn.connection_status cable=cable
interface_b.cable = c )
interface_b.save()
cable_count = Cable.objects.filter(termination_a_type=interface_type).count() cable_count = Cable.objects.filter(termination_a_type=interface_type).count()
print("{} cables created".format(cable_count)) print("{} cables created".format(cable_count))