from invenio_rdm_records.records.api import RDMRecord, RDMDraft
from invenio_rdm_records.proxies import current_rdm_records as crr
from invenio_access.permissions import system_identity
from invenio_db import db

# NOTE: specify the latest version number in the "vX" string
drafts = [RDMDraft(rm.data, model=rm) for rm in RDMDraft.model_cls.query.all()]
old_drafts = [d for d in drafts if d and "vX" not in d.get("$schema")]
new_drafts = [d for d in drafts if d and "vX" in d.get("$schema")]
records = [RDMRecord(rm.data, model=rm) for rm in RDMRecord.model_cls.query.all()]
old_records = [r for r in records if "vX" not in r.get("$schema")]
new_records = [r for r in records if "vX" in r.get("$schema")]

# check how many old & new drafts/records there are
print(len(old_drafts))
print(len(new_drafts))
print(len(old_records))
print(len(new_records))

# check which old schemas there are... should only be v4
print({d.get("$schema") for d in old_drafts})

# pop the '$schema' (will be bumped automatically)
# do the same for old_records if there are any (we didn't have any)
for d in old_drafts:
     d.pop("$schema")
     d.commit()
     d.parent.commit()
     crr.records_service.indexer.index(d)

db.session.commit()