From 939c89d3688eb685f6d3dfd215a689d452e6a84f Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sun, 24 May 2026 10:39:47 -0400 Subject: [PATCH] [birds] Fix reset sequence for ebird imports --- .../migrations/0084_fix_ebird_sequence.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 vrobbler/apps/scrobbles/migrations/0084_fix_ebird_sequence.py diff --git a/vrobbler/apps/scrobbles/migrations/0084_fix_ebird_sequence.py b/vrobbler/apps/scrobbles/migrations/0084_fix_ebird_sequence.py new file mode 100644 index 0000000..fe6d027 --- /dev/null +++ b/vrobbler/apps/scrobbles/migrations/0084_fix_ebird_sequence.py @@ -0,0 +1,30 @@ +from django.db import migrations + + +def reset_sequence(apps, schema_editor): + from django.db import connection + + with connection.cursor() as cursor: + vendor = connection.vendor + if vendor == "postgresql": + cursor.execute( + "SELECT setval('scrobbles_ebirdcsvimport_id_seq', " + "COALESCE((SELECT MAX(id) + 1 FROM scrobbles_ebirdcsvimport), 1), " + "false)" + ) + elif vendor == "sqlite": + cursor.execute( + "UPDATE sqlite_sequence SET seq = (" + "SELECT COALESCE(MAX(id), 0) FROM scrobbles_ebirdcsvimport" + ") WHERE name = 'scrobbles_ebirdcsvimport'" + ) + + +class Migration(migrations.Migration): + dependencies = [ + ("scrobbles", "0083_migrate_birding_data"), + ] + + operations = [ + migrations.RunPython(reset_sequence, migrations.RunPython.noop), + ]