mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 17:26:10 -06:00
Add migration file
This commit is contained in:
parent
2970fde873
commit
fc5366a94a
78
netbox/circuits/migrations/0042_provideraccount_and_more.py
Normal file
78
netbox/circuits/migrations/0042_provideraccount_and_more.py
Normal file
@ -0,0 +1,78 @@
|
||||
# Generated by Django 4.1.4 on 2023-03-14 16:02
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import taggit.managers
|
||||
import utilities.json
|
||||
|
||||
|
||||
#
|
||||
# Migrate Account in Provider model to separate account model
|
||||
#
|
||||
def create_provideraccounts_from_providers(apps, schema_editor):
|
||||
Provider = apps.get_model('circuits', 'Provider')
|
||||
ProviderAccount = apps.get_model('circuits', 'ProviderAccount')
|
||||
|
||||
for provider in Provider.objects.all():
|
||||
if provider.account is not None:
|
||||
provideraccount = ProviderAccount.objects.create(
|
||||
name=f'{provider.name} {provider.account}',
|
||||
account=provider.account,
|
||||
provider=provider,
|
||||
)
|
||||
|
||||
|
||||
#
|
||||
# Unmigrate ProviderAccount to Provider model
|
||||
#
|
||||
def revert_provideraccounts_from_providers(apps, schema_editor):
|
||||
ProviderAccount = apps.get_model('circuits', 'ProviderAccount')
|
||||
provideraccounts = ProviderAccount.objects.all().orderby('pk')
|
||||
for provideraccount in provideraccounts:
|
||||
if provideraccounts.filter(provider=provideraccount.provider)[0] == provideraccount:
|
||||
provideraccount.provider.account = provideraccount.account
|
||||
provideraccount.provider.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('extras', '0084_staging'),
|
||||
('circuits', '0041_standardize_description_comments'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ProviderAccount',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)),
|
||||
('created', models.DateTimeField(auto_now_add=True, null=True)),
|
||||
('last_updated', models.DateTimeField(auto_now=True, null=True)),
|
||||
('custom_field_data', models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder)),
|
||||
('description', models.CharField(blank=True, max_length=200)),
|
||||
('comments', models.TextField(blank=True)),
|
||||
('name', models.CharField(max_length=100)),
|
||||
('account', models.CharField(blank=True, max_length=30)),
|
||||
('provider', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='accounts', to='circuits.provider')),
|
||||
('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
|
||||
],
|
||||
options={
|
||||
'ordering': ('provider', 'name'),
|
||||
},
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='provideraccount',
|
||||
constraint=models.UniqueConstraint(fields=('provider', 'name'), name='circuits_provideraccount_unique_provider_name'),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='provideraccount',
|
||||
constraint=models.UniqueConstraint(fields=('provider', 'account'), name='circuits_provideraccount_unique_provider_account'),
|
||||
),
|
||||
migrations.RunPython(
|
||||
create_provideraccounts_from_providers, revert_provideraccounts_from_providers
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='provider',
|
||||
name='account',
|
||||
),
|
||||
]
|
Loading…
Reference in New Issue
Block a user