0228_rack_group.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import django.db.models.deletion
  2. import taggit.managers
  3. from django.db import migrations, models
  4. import netbox.models.deletion
  5. import utilities.json
  6. class Migration(migrations.Migration):
  7. dependencies = [
  8. ('dcim', '0227_alter_interface_speed_bigint'),
  9. ('extras', '0134_owner'),
  10. ('users', '0015_owner'),
  11. ]
  12. operations = [
  13. # Rename legacy database objects left over from when the RackGroup model was renamed
  14. # to Location (v2.11) and Rack.group was renamed to Rack.location. Old installations
  15. # retained the original names, which conflict with the new dcim_rackgroup table and
  16. # dcim_rack.group_id column created by this migration. No-op on fresh installs.
  17. migrations.RunSQL(
  18. sql=[
  19. "ALTER INDEX IF EXISTS dcim_rackgroup_pkey RENAME TO dcim_location_pkey",
  20. "ALTER INDEX IF EXISTS dcim_rackgroup_parent_id_cc315105 RENAME TO dcim_location_parent_id_d77f3318",
  21. "ALTER INDEX IF EXISTS dcim_rackgroup_site_id_13520e89 RENAME TO dcim_location_site_id_b55e975f",
  22. "ALTER INDEX IF EXISTS dcim_rackgroup_slug_3f4582a7 RENAME TO dcim_location_slug_352c5472",
  23. "ALTER INDEX IF EXISTS dcim_rackgroup_slug_3f4582a7_like RENAME TO dcim_location_slug_352c5472_like",
  24. "ALTER INDEX IF EXISTS dcim_rackgroup_tree_id_9c2ad6f4 RENAME TO dcim_location_tree_id_5089ef14",
  25. "ALTER SEQUENCE IF EXISTS dcim_rackgroup_id_seq RENAME TO dcim_location_id_seq",
  26. # Rename the legacy index on dcim_rack from when Rack.group was renamed to
  27. # Rack.location. The column was renamed but the index was not, so it still
  28. # carries the old "group_id" name while indexing location_id. Its name
  29. # collides with the new index Django creates for the new Rack.group FK.
  30. "ALTER INDEX IF EXISTS dcim_rack_group_id_44e90ea9 RENAME TO dcim_rack_location_id_5f63ec31",
  31. ],
  32. reverse_sql=migrations.RunSQL.noop,
  33. ),
  34. # PostgreSQL does not support IF EXISTS on RENAME CONSTRAINT, so use a DO block.
  35. # Target names match what a fresh v4.5.9 install produces (Django generates the FK
  36. # constraint name as <table>_<col>_<hash>_fk_<ref_table>_<ref_col>, where the hash is
  37. # md5(table + col)[:8] computed against the new dcim_location table name).
  38. migrations.RunSQL(
  39. sql="""
  40. DO $$
  41. DECLARE
  42. r RECORD;
  43. BEGIN
  44. FOR r IN (
  45. SELECT old_name, new_name FROM (VALUES
  46. ('dcim_rackgroup_level_check', 'dcim_location_level_check'),
  47. ('dcim_rackgroup_lft_check', 'dcim_location_lft_check'),
  48. ('dcim_rackgroup_rght_check', 'dcim_location_rght_check'),
  49. ('dcim_rackgroup_tree_id_check', 'dcim_location_tree_id_check'),
  50. ('dcim_rackgroup_site_id_13520e89_fk',
  51. 'dcim_location_site_id_b55e975f_fk_dcim_site_id')
  52. ) AS m(old_name, new_name)
  53. WHERE EXISTS (
  54. SELECT 1 FROM pg_constraint
  55. WHERE conrelid = to_regclass('dcim_location') AND conname = m.old_name
  56. )
  57. ) LOOP
  58. EXECUTE format('ALTER TABLE dcim_location RENAME CONSTRAINT %I TO %I',
  59. r.old_name, r.new_name);
  60. END LOOP;
  61. END $$;
  62. """,
  63. reverse_sql=migrations.RunSQL.noop,
  64. ),
  65. migrations.CreateModel(
  66. name='RackGroup',
  67. fields=[
  68. ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)),
  69. ('created', models.DateTimeField(auto_now_add=True, null=True)),
  70. ('last_updated', models.DateTimeField(auto_now=True, null=True)),
  71. (
  72. 'custom_field_data',
  73. models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
  74. ),
  75. ('name', models.CharField(max_length=100, unique=True)),
  76. ('slug', models.SlugField(max_length=100, unique=True)),
  77. ('description', models.CharField(blank=True, max_length=200)),
  78. ('comments', models.TextField(blank=True)),
  79. (
  80. 'owner',
  81. models.ForeignKey(
  82. blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='users.owner'
  83. ),
  84. ),
  85. ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
  86. ],
  87. options={
  88. 'verbose_name': 'rack group',
  89. 'verbose_name_plural': 'rack groups',
  90. 'ordering': ('name',),
  91. },
  92. bases=(netbox.models.deletion.DeleteMixin, models.Model),
  93. ),
  94. migrations.AddField(
  95. model_name='rack',
  96. name='group',
  97. field=models.ForeignKey(
  98. blank=True,
  99. null=True,
  100. on_delete=django.db.models.deletion.PROTECT,
  101. related_name='racks',
  102. to='dcim.rackgroup',
  103. ),
  104. ),
  105. ]