|
| 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 | + ] |
0 commit comments