@@ -0,0 +1,1489 @@
+import json%0A%0Aimport argparse%0A%0Afrom django.core.management.base import BaseCommand%0Afrom django.db import connection%0Afrom django.db import transaction%0A%0Aclass Command(BaseCommand):%0A    def add_arguments(self, parser):%0A        parser.add_argument('out_file', type=argparse.FileType('w'), help='Output file for json map of source ids')%0A        parser.add_argument('work_ids', nargs='*', type=int, help='The id(s) of the creative works to migrate')%0A%0A    def handle(self, out_file, work_ids, *args, **options):%0A%0A        query = '''%0A        SELECT DISTINCT acw.id, rd.provider_doc_id FROM%0A          share_rawdata AS rd%0A          JOIN share_normalizeddata AS nd ON rd.id = nd.raw_id%0A          JOIN share_changeset AS cs ON nd.id = cs.normalized_data_id%0A          JOIN share_change AS c ON cs.id = c.change_set_id%0A          JOIN share_abstractcreativework AS acw ON c.id = acw.change_id%0A          WHERE acw.id IN (%7B%7D) AND acw.title != '' AND c.change-%3E%3E'title' = acw.title%0A          ORDER BY acw.id;%0A        '''.format(','.join(%5B'%25s'%5D * len(work_ids)))%0A%0A        with connection.cursor() as c:%0A            c.execute(query, work_ids)%0A%0A            source_ids = %7B%7D%0A            data = c.fetchone()%0A            while data:%0A                (id, source_id) = data%0A                try:%0A                    source_ids%5Bid%5D.append(source_id)%0A                except KeyError:%0A                    source_ids%5Bid%5D = %5Bsource_id%5D%0A                data = c.fetchone()%0A%0A        json.dump(source_ids, out_file, indent=4)%0A

Add getsourceids management command.