public static void copy(IMetaStore from, IMetaStore to, boolean overwrite) throws MetaStoreException { // get all of the namespace in the "from" metastore List<String> namespaces = from.getNamespaces(); for (String namespace : namespaces) { // create the sme namespace in the "to" metastore try { to.createNamespace(namespace); } catch (MetaStoreNamespaceExistsException e) { // already there } // get all of the element types defined in this namespace List<IMetaStoreElementType> elementTypes = from.getElementTypes(namespace); for (IMetaStoreElementType elementType : elementTypes) { // See if it's already there IMetaStoreElementType existingType = to.getElementTypeByName(namespace, elementType.getName()); if (existingType != null) { if (overwrite) { // we looked it up by name, but need to update it by id elementType.setId(existingType.getId()); to.updateElementType(namespace, elementType); } else { elementType = existingType; } } else { // create the elementType in the "to" metastore to.createElementType(namespace, elementType); } // get all of the elements defined for this type List<IMetaStoreElement> elements = from.getElements(namespace, elementType); for (IMetaStoreElement element : elements) { IMetaStoreElement existingElement = to.getElementByName(namespace, elementType, element.getName()); if (existingElement != null) { element.setId(existingElement.getId()); if (overwrite) { to.updateElement(namespace, elementType, existingElement.getId(), element); } } else { to.createElement(namespace, elementType, element); } } } } }
@Test public void testIDMigration() throws Exception { String namespace = "custom"; String stepName = "Step Name"; String elementName = "migration"; String hostName = "Host Name"; String fieldMappings = "Field Mappings"; String sourceFieldName = "Source Field Name"; String targetFieldName = "Target Field Name"; String parameterName = "Parameter Name"; IMetaStore metaStore = new MemoryMetaStore(); MetaStoreFactory<MyMigrationElement> factory = new MetaStoreFactory<MyMigrationElement>(MyMigrationElement.class, metaStore, namespace); if (!metaStore.namespaceExists(namespace)) { metaStore.createNamespace(namespace); } MetaStoreElementType elementTypeAnnotation = MyMigrationElement.class.getAnnotation(MetaStoreElementType.class); // Make sure the element type exists... IMetaStoreElementType elementType = metaStore.getElementTypeByName(namespace, elementTypeAnnotation.name()); if (elementType == null) { elementType = metaStore.newElementType(namespace); elementType.setName(elementTypeAnnotation.name()); elementType.setDescription(elementTypeAnnotation.description()); metaStore.createElementType(namespace, elementType); } // Create an element with the old keys we want to migrate IMetaStoreElement element = metaStore.newElement(); element.setName(elementName); element.setElementType(elementType); element.addChild(metaStore.newAttribute("stepName", stepName)); element.addChild(metaStore.newAttribute("hostname", hostName)); element.addChild(metaStore.newAttribute("fieldMappings", fieldMappings)); element.addChild(metaStore.newAttribute("sourceFieldName", sourceFieldName)); element.addChild(metaStore.newAttribute("targetFieldName", targetFieldName)); element.addChild(metaStore.newAttribute("parameterName", parameterName)); metaStore.createElement(namespace, elementType, element); MyMigrationElement loadedElement = factory.loadElement(elementName); assertNotNull(loadedElement); assertEquals(loadedElement.getStepName(), stepName); assertEquals(loadedElement.getHostname(), hostName); assertEquals(loadedElement.getFieldMappings(), fieldMappings); assertEquals(loadedElement.getSourceFieldName(), sourceFieldName); assertEquals(loadedElement.getTargetFieldName(), targetFieldName); assertEquals(loadedElement.getParameterName(), parameterName); // Test the variation of the step name id element = metaStore.newElement(); element.setName(elementName); element.setElementType(elementType); element.addChild(metaStore.newAttribute("stepname", stepName)); metaStore.createElement(namespace, elementType, element); loadedElement = factory.loadElement(elementName); assertNotNull(loadedElement); assertEquals(loadedElement.getStepname(), stepName); }