Skip to content

Commit 5cb68c8

Browse files
Fixes #321: Replicate extras_tag_object_types table for branches (#322)
1 parent b4b4fa2 commit 5cb68c8

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from django.db import connection, migrations
2+
3+
from netbox.plugins import get_plugin_config
4+
from netbox_branching.choices import BranchStatusChoices
5+
6+
7+
def copy_table(apps, schema_editor):
8+
"""
9+
Create a copy of the extras_tag_object_types table in each active branch.
10+
"""
11+
Branch = apps.get_model('netbox_branching', 'Branch')
12+
13+
table = 'extras_tag_object_types'
14+
schema_prefix = get_plugin_config('netbox_branching', 'schema_prefix')
15+
16+
with connection.cursor() as cursor:
17+
main_table = f'public.{table}'
18+
19+
for branch in Branch.objects.filter(status=BranchStatusChoices.READY):
20+
print(f'\n Copying {table} for branch {branch.name} ({branch.schema_id})... ', end='')
21+
schema_name = f'{schema_prefix}{branch.schema_id}'
22+
schema_table = f'{schema_name}.{table}'
23+
24+
# Abort if the table already exists (somehow)
25+
cursor.execute(
26+
f"SELECT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname='{schema_name}' AND tablename='{table}')"
27+
)
28+
if cursor.fetchone()[0]:
29+
print('Skipping; table already exists.', end='')
30+
continue
31+
32+
# Copy the extras_tag_object_types table to the branch schema
33+
cursor.execute(f"CREATE TABLE {schema_table} ( LIKE {main_table} INCLUDING INDEXES )")
34+
35+
# Copy table data
36+
cursor.execute(f"INSERT INTO {schema_table} SELECT * FROM {main_table}")
37+
38+
# Set the default value for the ID column to the sequence associated with the source table
39+
cursor.execute(
40+
f"ALTER TABLE {schema_table} ALTER COLUMN id SET DEFAULT nextval('extras_tag_object_types_id_seq')"
41+
)
42+
43+
# Rename indexes
44+
cursor.execute(
45+
f"ALTER INDEX {schema_name}.extras_tag_object_types_objecttype_id_idx "
46+
f"RENAME TO extras_tag_object_types_contenttype_id_c1b220c3"
47+
)
48+
cursor.execute(
49+
f"ALTER INDEX {schema_name}.extras_tag_object_types_tag_id_idx "
50+
f"RENAME TO extras_tag_object_types_tag_id_2e1aab29"
51+
)
52+
cursor.execute(
53+
f"ALTER INDEX {schema_name}.extras_tag_object_types_tag_id_objecttype_id_key "
54+
f"RENAME TO extras_tag_object_types_tag_id_contenttype_id_2ff9910c_uniq"
55+
)
56+
57+
print('Success.', end='')
58+
59+
print('\n ', end='') # Padding for final "OK"
60+
61+
62+
class Migration(migrations.Migration):
63+
64+
dependencies = [
65+
('netbox_branching', '0005_branch_applied_migrations'),
66+
]
67+
68+
operations = [
69+
migrations.RunPython(
70+
code=copy_table,
71+
reverse_code=migrations.RunPython.noop
72+
),
73+
]

netbox_branching/utilities.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,12 @@ def get_tables_to_replicate():
144144

145145
# Capture any M2M fields which reference other replicated models
146146
for m2m_field in model._meta.local_many_to_many:
147-
if m2m_field.related_model in branch_aware_models:
148-
if hasattr(m2m_field, 'through'):
149-
# Field is actually a manager
150-
m2m_table = m2m_field.through._meta.db_table
151-
else:
152-
m2m_table = m2m_field._get_m2m_db_table(model._meta)
153-
tables.add(m2m_table)
147+
if hasattr(m2m_field, 'through'):
148+
# Field is actually a manager
149+
m2m_table = m2m_field.through._meta.db_table
150+
else:
151+
m2m_table = m2m_field._get_m2m_db_table(model._meta)
152+
tables.add(m2m_table)
154153

155154
return sorted(tables)
156155

0 commit comments

Comments
 (0)