-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: M2M dataset-organization, table-published_by,data_cleaned_by
- Loading branch information
Showing
9 changed files
with
267 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
backend/apps/api/v1/migrations/0039_dataset_organizations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 4.2.10 on 2024-05-10 15:30 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
def migrate_organization_to_organizations(apps, schema_editor): | ||
Dataset = apps.get_model('v1', 'Dataset') | ||
for dataset in Dataset.objects.all(): | ||
if dataset.organization: | ||
dataset.organizations.add(dataset.organization) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('v1', '0038_rename_level_area_administrative_level'), | ||
] | ||
|
||
operations = [ | ||
# Add new ManyToMany field | ||
migrations.AddField( | ||
model_name='dataset', | ||
name='organizations', | ||
field=models.ManyToManyField( | ||
related_name='datasets', | ||
to='v1.organization', | ||
verbose_name='Organizations', | ||
help_text='Organizations associated with this dataset', | ||
), | ||
), | ||
# Run data migration | ||
migrations.RunPython( | ||
migrate_organization_to_organizations, | ||
reverse_code=migrations.RunPython.noop | ||
), | ||
# Remove old ForeignKey field | ||
migrations.RemoveField( | ||
model_name='dataset', | ||
name='organization', | ||
), | ||
] |
78 changes: 78 additions & 0 deletions
78
backend/apps/api/v1/migrations/0040_table_publishers_data_cleaners.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 4.2.10 on 2024-05-10 16:00 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
def migrate_publishers_and_cleaners(apps, schema_editor): | ||
"""Migrate existing ForeignKey relationships to ManyToMany""" | ||
Table = apps.get_model('v1', 'Table') | ||
for table in Table.objects.all(): | ||
# Store old ForeignKey values | ||
old_publisher = getattr(table, 'published_by_old', None) | ||
old_cleaner = getattr(table, 'data_cleaned_by_old', None) | ||
|
||
# Add to new M2M fields if they existed | ||
if old_publisher: | ||
table.published_by.add(old_publisher) | ||
if old_cleaner: | ||
table.data_cleaned_by.add(old_cleaner) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('v1', '0039_dataset_organizations'), | ||
] | ||
|
||
operations = [ | ||
# Rename old fields temporarily | ||
migrations.RenameField( | ||
model_name='table', | ||
old_name='published_by', | ||
new_name='published_by_old', | ||
), | ||
migrations.RenameField( | ||
model_name='table', | ||
old_name='data_cleaned_by', | ||
new_name='data_cleaned_by_old', | ||
), | ||
# Add new M2M fields | ||
migrations.AddField( | ||
model_name='table', | ||
name='published_by', | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name='tables_published', | ||
to=settings.AUTH_USER_MODEL, | ||
verbose_name='Published by', | ||
help_text='People who published the table', | ||
), | ||
), | ||
migrations.AddField( | ||
model_name='table', | ||
name='data_cleaned_by', | ||
field=models.ManyToManyField( | ||
blank=True, | ||
related_name='tables_cleaned', | ||
to=settings.AUTH_USER_MODEL, | ||
verbose_name='Data cleaned by', | ||
help_text='People who cleaned the data', | ||
), | ||
), | ||
# Run data migration | ||
migrations.RunPython( | ||
migrate_publishers_and_cleaners, | ||
reverse_code=migrations.RunPython.noop | ||
), | ||
# Remove old fields | ||
migrations.RemoveField( | ||
model_name='table', | ||
name='published_by_old', | ||
), | ||
migrations.RemoveField( | ||
model_name='table', | ||
name='data_cleaned_by_old', | ||
), | ||
] |
Oops, something went wrong.