Skip to content

Commit

Permalink
Merge pull request #23 from acdh-oeaw/reset-sql-id-sequence
Browse files Browse the repository at this point in the history
Reset root object and relation ID sequences
  • Loading branch information
gythaogg authored May 17, 2024
2 parents c3b875f + 96ba880 commit 206379a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions apis_ontology/management/commands/reset-id-sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.core.management.base import BaseCommand
from django.db import connection


class Command(BaseCommand):
help = "Reset ID sequences to max values for all models"

def handle(self, *args, **options):
cursor = connection.cursor()
# Replace "app_model" with your actual app name and model name
# # BEGIN;
# SELECT setval(pg_get_serial_sequence('"apis_ontology_work_collection"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "apis_ontology_work_collection";
# SELECT setval(pg_get_serial_sequence('"apis_ontology_versionwork"','history_id'), coalesce(max("history_id"), 1), max("history_id") IS NOT null) FROM "apis_ontology_versionwork";
# SELECT setval(pg_get_serial_sequence('"apis_ontology_versionwork_collection"','m2m_history_id'), coalesce(max("m2m_history_id"), 1), max("m2m_history_id") IS NOT null) FROM "apis_ontology_versionwork_collection";
# SELECT setval(pg_get_serial_sequence('"apis_ontology_instance_collection"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "apis_ontology_instance_collection";
# SELECT setval(pg_get_serial_sequence('"apis_ontology_versioninstance"','history_id'), coalesce(max("history_id"), 1), max("history_id") IS NOT null) FROM "apis_ontology_versioninstance";
# SELECT setval(pg_get_serial_sequence('"apis_ontology_versioninstance_collection"','m2m_history_id'), coalesce(max("m2m_history_id"), 1), max("m2m_history_id") IS NOT null) FROM "apis_ontology_versioninstance_collection";
# SELECT setval(pg_get_serial_sequence('"apis_ontology_zoteroentry"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "apis_ontology_zoteroentry";
# COMMIT;

cursor.execute(
"""SELECT setval(pg_get_serial_sequence('"apis_metainfo_rootobject"','id'),
coalesce(max("id"), 1), max("id") IS NOT null)
FROM "apis_metainfo_rootobject";
"""
)
cursor.execute(
"""SELECT setval(pg_get_serial_sequence('"relations_relation"','id'),
coalesce(max("id"), 1), max("id") IS NOT null)
FROM "relations_relation";
"""
)
self.stdout.write(self.style.SUCCESS("ID sequences have been reset."))

0 comments on commit 206379a

Please sign in to comment.