From f9916a9596a546c6a7d588baea2a16dd28cbcb96 Mon Sep 17 00:00:00 2001 From: Mark Jacobson <52427991+marksparkza@users.noreply.github.com> Date: Mon, 14 Aug 2023 11:11:53 +0200 Subject: [PATCH] Create once-off migration for linking parent records --- bin/onceoff | 20 ++++++++++++++++++++ migrate/onceoff/link_parent_records.py | 14 ++++++++++++++ 2 files changed, 34 insertions(+) create mode 100755 bin/onceoff create mode 100644 migrate/onceoff/link_parent_records.py diff --git a/bin/onceoff b/bin/onceoff new file mode 100755 index 0000000..acb3997 --- /dev/null +++ b/bin/onceoff @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +import argparse +import pathlib +import sys +from importlib import import_module + +rootdir = pathlib.Path(__file__).parent.parent +sys.path.append(str(rootdir)) + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('module_name') + parser.add_argument('function_name') + + args = parser.parse_args() + onceoff_mod = import_module(f'migrate.onceoff.{args.module_name}') + onceoff_func = getattr(onceoff_mod, args.function_name) + + onceoff_func() diff --git a/migrate/onceoff/link_parent_records.py b/migrate/onceoff/link_parent_records.py new file mode 100644 index 0000000..967a1e7 --- /dev/null +++ b/migrate/onceoff/link_parent_records.py @@ -0,0 +1,14 @@ +from sqlalchemy import select + +from odp.api.routers.record import get_parent_id +from odp.db import Session +from odp.db.models import Record + + +def set_parent_ids(): + for record in Session.execute( + select(Record) + ).scalars().all(): + record.parent_id = get_parent_id(record.metadata_, record.schema_id) + + Session.commit()