From af93f73f1e6ed75ac60d49b0d5444af3c20bef67 Mon Sep 17 00:00:00 2001 From: olivier Date: Tue, 21 Apr 2009 15:26:12 +0000 Subject: [PATCH] migration: add collections:copy migrator git-svn-id: http://svn.parisson.org/svn/crem@80 3bf09e05-f825-4182-b9bc-eedd7160adf0 --- trunk/import/migration/migrate.py | 2 +- trunk/import/migration/tasks/__init__.py | 1 + trunk/import/migration/tasks/collections.py | 125 ++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 trunk/import/migration/tasks/collections.py diff --git a/trunk/import/migration/migrate.py b/trunk/import/migration/migrate.py index 254bd05..2540f21 100644 --- a/trunk/import/migration/migrate.py +++ b/trunk/import/migration/migrate.py @@ -129,7 +129,7 @@ if __name__ == '__main__': print "Usage: %s [task_name]" % sys.argv[0] print "Tasks:" for task in manager.list_tasks(): - print " %-16s%s" % (task.get_name() + ':', task.__doc__) + print " %-20s%s" % (task.get_name(), task.__doc__) sys.exit(1) diff --git a/trunk/import/migration/tasks/__init__.py b/trunk/import/migration/tasks/__init__.py index 337a207..e531bb5 100644 --- a/trunk/import/migration/tasks/__init__.py +++ b/trunk/import/migration/tasks/__init__.py @@ -36,3 +36,4 @@ import enums import geoethno import ethnic import publishers +import collections diff --git a/trunk/import/migration/tasks/collections.py b/trunk/import/migration/tasks/collections.py new file mode 100644 index 0000000..eade0d8 --- /dev/null +++ b/trunk/import/migration/tasks/collections.py @@ -0,0 +1,125 @@ +# -*- coding: utf-8 -*- +# +# CREM Database migrator + +# Copyright (C) 2009 Samalyse SARL +# Author: Olivier Guilyardi + +# This software is governed by the CeCILL license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL +# license as circulated by CEA, CNRS and INRIA at the following URL +# "http://www.cecill.info". + +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. + +# In this respect, the user's attention is drawn to the risks associated +# with loading, using, modifying and/or developing or reproducing the +# software by the user in light of its specific status of free software, +# that may mean that it is complicated to manipulate, and that also +# therefore means that it is reserved for developers and experienced +# professionals having in-depth computer knowledge. Users are therefore +# encouraged to load and test the software's suitability as regards their +# requirements in conditions enabling the security of their systems and/or +# data to be ensured and, more generally, to use and operate it in the +# same conditions as regards security. + +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL license and that you accept its terms. + +from telemeta.core import * +from api import IDataMigrator +from core import DataMigrator + +class CollectionsMigrator(DataMigrator): + """Perform a preliminary raw copy of the collection table""" + + implements(IDataMigrator) + + flat_map = [ + ('Ref', 'reference'), + ('Cote', 'old_code'), + ('Cote', 'code'), + ('Titre', 'title'), + ('Transcrip_Trad', 'alt_title'), + ('Nb_de_pieces', 'physical_items_num'), + ('Auteur_Compil', 'creator'), + ('Auteur_Notice', 'booklet_author'), + ('Notice', 'booklet_description'), + ('Collecteur', 'collector'), + ('Num_Dans_Collec', 'publisher_serial'), + ('Ref_Biblio', 'external_references'), + ('Commentaire', 'comment'), + ('Autres_Cotes', 'alt_ids'), + ('Duree_approx', 'approx_duration'), + ('Tri DiBm', 'doctype_code'), + ('Travail', 'travail'), + ('CompilFacePlage', 'state'), + ('DeposantCNRS', 'cnrs_contributor'), + ('Fiches', 'items_done'), + ('A informer_07_03_', 'a_informer_07_03') + ] + + enums_map = [ + ('Format', 'physical_format'), + ('Reedition', 'publishing_status'), + #('Editeur', 'publisher'), + #('Collect_Série', 'publisher_collection'), + ('Mode_Acqui', 'acquisition_mode'), + ('Redacteur_Fiche', 'metadata_author'), + ('Saisie_Fiche', 'metadata_writer'), + ('Droit_Utiliser', 'legal_rights'), + ('Terrain_ou_Autr', 'recording_context'), + ('Numerisation', 'ad_conversion') + ] + + def get_name(self): + return "collections:copy" + + def build_flat_assignments(self, map): + assign = [] + for f1, f2 in map: + f2 = '`%s`' % f2 + f1 = '`%s`' % f1 + assign.append((f2, f1)) + + return assign + + def build_enum_assignments(self, src_table, map): + assign = [] + for src_field, target_base in map: + target_field = '`%s_id`' % target_base + if target_base[-1] == 's': + enum_table = target_base + else: + enum_table = target_base + 's' + + subquery = "COALESCE((SELECT id FROM `%s`.`%s` AS e WHERE %s.`%s` = e.value), -1)" % ( + self.target_db_name, enum_table, src_table, src_field) + + assign.append((target_field, subquery)) + + return assign + + def process(self): + + target_fields = [] + src_fields = [] + + flat = self.build_flat_assignments(self.flat_map) + target_fields += [str(a[0]) for a in flat] + src_fields += [str(a[1]) for a in flat] + + enum = self.build_enum_assignments('s', self.enums_map) + target_fields += [str(a[0]) for a in enum] + src_fields += [str(a[1]) for a in enum] + + query = "INSERT INTO %s.media_collections (\n %s\n)\nSELECT \n %s\n FROM %s.Support AS s" % ( + self.target_db_name, ",\n ".join(target_fields), ",\n ".join(src_fields), + self.src_db_name) + + self.target_cursor.execute(query) -- 2.39.5