here we go

This commit is contained in:
pacnpal
2024-10-31 22:32:01 +00:00
parent 71272e36a6
commit 3cbda93094
68 changed files with 3114 additions and 1485 deletions

View File

@@ -0,0 +1,17 @@
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('cities_light', '0001_initial'),
('parks', '0006_update_location_fields_to_cities_light'),
]
operations = [
migrations.AlterField(
model_name='historicalpark',
name='city',
field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='cities_light.city'),
),
]

View File

@@ -0,0 +1,43 @@
from django.db import migrations
from django.db.models import Q
def fix_historical_park_data(apps, schema_editor):
HistoricalPark = apps.get_model('parks', 'HistoricalPark')
Park = apps.get_model('parks', 'Park')
Country = apps.get_model('cities_light', 'Country')
# Get a default country (create one if none exists)
default_country = Country.objects.first()
if not default_country:
default_country = Country.objects.create(name='Unknown')
# Fix all historical records with null country
historical_records = HistoricalPark.objects.filter(
Q(country__isnull=True) | Q(location__isnull=True)
)
for record in historical_records:
try:
# Try to get the current park's country
park = Park.objects.get(id=record.id)
record.country = park.country
record.location = park.location or f"{park.country.name}"
except Park.DoesNotExist:
# If park doesn't exist, use default country
record.country = default_country
record.location = default_country.name
record.save()
def reverse_func(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
('parks', '0007_fix_historical_park_city_null'),
('cities_light', '0001_initial'),
]
operations = [
migrations.RunPython(fix_historical_park_data, reverse_func),
]

View File

@@ -0,0 +1,33 @@
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('cities_light', '0001_initial'),
('parks', '0008_fix_historical_park_data'),
]
operations = [
migrations.AlterField(
model_name='historicalpark',
name='city',
field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='cities_light.city'),
),
migrations.AlterField(
model_name='historicalpark',
name='region',
field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='cities_light.region'),
),
migrations.AlterField(
model_name='historicalpark',
name='location',
field=models.CharField(max_length=255, default=''),
preserve_default=False,
),
migrations.AlterField(
model_name='historicalpark',
name='country',
field=models.ForeignKey(db_constraint=False, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='cities_light.country'),
),
]

View File

@@ -0,0 +1,34 @@
# Generated by Django 5.1.2 on 2024-10-31 20:15
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("cities_light", "0011_alter_city_country_alter_city_region_and_more"),
("parks", "0009_fix_historical_park_fields"),
]
operations = [
migrations.AlterField(
model_name="historicalpark",
name="country",
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="cities_light.country",
),
),
migrations.AlterField(
model_name="park",
name="country",
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT, to="cities_light.country"
),
),
]

View File

@@ -0,0 +1,72 @@
from django.db import migrations, models
from django.db.models import Q
import django.db.models.deletion
def fix_historical_records(apps, schema_editor):
HistoricalPark = apps.get_model('parks', 'HistoricalPark')
Park = apps.get_model('parks', 'Park')
Country = apps.get_model('cities_light', 'Country')
# Get or create a default country
default_country = Country.objects.first()
if not default_country:
default_country = Country.objects.create(name='Unknown')
# Update all historical records with null values
for record in HistoricalPark.objects.filter(Q(country__isnull=True) | Q(location__isnull=True)):
try:
park = Park.objects.get(id=record.id)
record.country = park.country
record.location = park.location
except Park.DoesNotExist:
record.country = default_country
record.location = default_country.name
record.save()
class Migration(migrations.Migration):
atomic = False # Allow long-running operations
dependencies = [
('parks', '0009_fix_historical_park_fields'),
]
operations = [
# First, make sure all fields allow null temporarily
migrations.AlterField(
model_name='historicalpark',
name='country',
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.country'
),
),
migrations.AlterField(
model_name='historicalpark',
name='location',
field=models.CharField(max_length=255, null=True, blank=True),
),
# Fix the data
migrations.RunPython(fix_historical_records),
# Now make the fields non-nullable
migrations.AlterField(
model_name='historicalpark',
name='country',
field=models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.country'
),
),
migrations.AlterField(
model_name='historicalpark',
name='location',
field=models.CharField(max_length=255),
),
]

View File

@@ -0,0 +1,52 @@
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('cities_light', '0001_initial'),
('parks', '0010_fix_historical_records'),
]
operations = [
migrations.AlterField(
model_name='historicalpark',
name='city',
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.city'
),
),
migrations.AlterField(
model_name='historicalpark',
name='region',
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.region'
),
),
migrations.AlterField(
model_name='historicalpark',
name='country',
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.country'
),
),
migrations.AlterField(
model_name='historicalpark',
name='location',
field=models.CharField(max_length=255, blank=True, null=True),
),
]

View File

@@ -0,0 +1,13 @@
# Generated by Django 5.1.2 on 2024-10-31 20:17
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("parks", "0010_alter_historicalpark_country_alter_park_country"),
("parks", "0010_fix_historical_records"),
]
operations = []

View File

@@ -0,0 +1,13 @@
# Generated by Django 5.1.2 on 2024-10-31 20:35
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("parks", "0011_alter_historicalpark_fields"),
("parks", "0011_merge_20241031_1617"),
]
operations = []

View File

@@ -0,0 +1,67 @@
from django.db import migrations
def fix_null_locations(apps, schema_editor):
Park = apps.get_model('parks', 'Park')
Country = apps.get_model('cities_light', 'Country')
Region = apps.get_model('cities_light', 'Region')
City = apps.get_model('cities_light', 'City')
# Get or create default locations
default_country = Country.objects.first()
if not default_country:
default_country = Country.objects.create(
name='Unknown',
name_ascii='Unknown',
slug='unknown',
geoname_id=0,
alternate_names='',
search_names='Unknown'
)
default_region = Region.objects.filter(country=default_country).first()
if not default_region:
default_region = Region.objects.create(
name='Unknown',
name_ascii='Unknown',
slug='unknown',
geoname_id=0,
alternate_names='',
country=default_country,
display_name='Unknown',
search_names='Unknown'
)
default_city = City.objects.filter(region=default_region).first()
if not default_city:
default_city = City.objects.create(
name='Unknown',
name_ascii='Unknown',
slug='unknown',
geoname_id=0,
alternate_names='',
region=default_region,
country=default_country,
display_name='Unknown',
search_names='Unknown',
latitude=0,
longitude=0,
population=0
)
# Update parks with null locations
for park in Park.objects.filter(country__isnull=True):
park.country = default_country
park.region = default_region
park.city = default_city
park.location = 'Unknown, Unknown, Unknown'
park.save()
class Migration(migrations.Migration):
dependencies = [
('parks', '0012_merge_20241031_1635'),
('cities_light', '0001_initial'),
]
operations = [
migrations.RunPython(fix_null_locations, reverse_code=migrations.RunPython.noop),
]

View File

@@ -0,0 +1,52 @@
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('cities_light', '0001_initial'),
('parks', '0013_fix_null_locations'),
]
operations = [
migrations.AlterField(
model_name='historicalpark',
name='city',
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.city'
),
),
migrations.AlterField(
model_name='historicalpark',
name='region',
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.region'
),
),
migrations.AlterField(
model_name='historicalpark',
name='country',
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.country'
),
),
migrations.AlterField(
model_name='historicalpark',
name='location',
field=models.CharField(max_length=255, blank=True, null=True),
),
]

View File

@@ -0,0 +1,16 @@
from django.db import migrations
def fix_historical_records(apps, schema_editor):
HistoricalPark = apps.get_model('parks', 'HistoricalPark')
# Update any historical records that might have issues
HistoricalPark.objects.filter(city__isnull=True).update(city=None)
class Migration(migrations.Migration):
dependencies = [
('parks', '0014_alter_location_fields'),
]
operations = [
migrations.RunPython(fix_historical_records, migrations.RunPython.noop),
]

View File

@@ -0,0 +1,22 @@
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('cities_light', '0011_alter_city_country_alter_city_region_and_more'),
('parks', '0015_fix_historical_park_city_constraint'),
]
operations = [
migrations.AlterField(
model_name='historicalpark',
name='city',
field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='cities_light.city'),
),
migrations.AlterField(
model_name='historicalpark',
name='region',
field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='cities_light.region'),
),
]

View File

@@ -0,0 +1,22 @@
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('parks', '0016_alter_historicalpark_city_nullable'),
]
operations = [
migrations.RunSQL(
# Make the city column nullable
sql='ALTER TABLE parks_historicalpark ALTER COLUMN city DROP NOT NULL;',
# Reverse operation if needed
reverse_sql='ALTER TABLE parks_historicalpark ALTER COLUMN city SET NOT NULL;'
),
migrations.RunSQL(
# Make the city_id column nullable
sql='ALTER TABLE parks_historicalpark ALTER COLUMN city_id DROP NOT NULL;',
# Reverse operation if needed
reverse_sql='ALTER TABLE parks_historicalpark ALTER COLUMN city_id SET NOT NULL;'
),
]

View File

@@ -0,0 +1,48 @@
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('cities_light', '0011_alter_city_country_alter_city_region_and_more'),
('parks', '0017_fix_historicalpark_city_column'),
]
operations = [
migrations.AlterField(
model_name='historicalpark',
name='country',
field=models.ForeignKey(
blank=False,
db_constraint=False,
null=False,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.country'
),
),
migrations.AlterField(
model_name='historicalpark',
name='region',
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.region'
),
),
migrations.AlterField(
model_name='historicalpark',
name='city',
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name='+',
to='cities_light.city'
),
),
]

View File

@@ -0,0 +1,15 @@
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('parks', '0018_fix_historicalpark_location_fields'),
]
operations = [
migrations.RunSQL(
# Make region_id nullable
sql='ALTER TABLE parks_historicalpark ALTER COLUMN region_id DROP NOT NULL;',
reverse_sql='ALTER TABLE parks_historicalpark ALTER COLUMN region_id SET NOT NULL;'
),
]

View File

@@ -0,0 +1,16 @@
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('parks', '0019_fix_historicalpark_region_constraint'),
]
operations = [
migrations.RunSQL(
# Remove the redundant city text column
sql='ALTER TABLE parks_historicalpark DROP COLUMN IF EXISTS city;',
# Recreate the column if needed (reverse migration)
reverse_sql='ALTER TABLE parks_historicalpark ADD COLUMN city character varying(255);'
),
]