-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Generated by Django 3.1.14 on 2024-12-29 16:47 | ||
|
||
from django.db import migrations | ||
from datetime import datetime | ||
|
||
import sentry_sdk | ||
|
||
|
||
def migrate_dates(apps, schema_editor): | ||
Market = apps.get_model("ddcz", "Market") | ||
|
||
for market in Market.objects.all(): | ||
if not market.published_varchar: | ||
sentry_sdk.capture_message( | ||
f"Market entry without date", | ||
level="error", | ||
extras={ | ||
"market_id": market.id, | ||
"invalid_date": market.published_varchar, | ||
}, | ||
) | ||
|
||
try: | ||
parsed_date = datetime.strptime(market.published_varchar, "%-d. %-m. %Y") | ||
except ValueError: | ||
# If both formats fail, report to Sentry | ||
sentry_sdk.capture_message( | ||
f"Failed to parse date in Market record", | ||
level="error", | ||
extras={ | ||
"market_id": market.id, | ||
"invalid_date": market.published_varchar, | ||
}, | ||
) | ||
continue | ||
|
||
market.created = parsed_date | ||
market.save() | ||
|
||
|
||
def reverse_migrate_dates(apps, schema_editor): | ||
Market = apps.get_model("ddcz", "Market") | ||
|
||
for market in Market.objects.all(): | ||
if not market.created: | ||
continue | ||
|
||
# Convert back to the original format with leading zeros | ||
market.published_varchar = market.created.strftime("%d. %m. %Y") | ||
market.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("ddcz", "0004_market_created"), | ||
] | ||
|
||
operations = [migrations.RunPython(migrate_dates, reverse_migrate_dates)] |