-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from acdh-oeaw/reset-sql-id-sequence
Reset root object and relation ID sequences
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 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
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.")) |