def _create_table(self, mapping): """Create a table for the given mapping.""" model_name = f"{mapping['table']}Model" mapper_kwargs = {} self.models[mapping["table"]] = type(model_name, (object, ), {}) t = create_table(mapping, self.metadata) if "RecordTypeId" in mapping["fields"]: # We're using Record Type Mapping support. mapping["record_type_table"] = mapping["sf_object"] + "_rt_mapping" # If multiple mappings point to the same table, don't recreate the table if mapping["record_type_table"] not in self.models: self._create_record_type_table(mapping["record_type_table"]) if not mapping["oid_as_pk"]: mapping["sf_id_table"] = mapping["table"] + "_sf_id" # If multiple mappings point to the same table, don't recreate the table if mapping["sf_id_table"] not in self.models: sf_id_model_name = f"{mapping['sf_id_table']}Model" self.models[mapping["sf_id_table"]] = type( sf_id_model_name, (object, ), {}) sf_id_fields = [ Column("id", Integer(), primary_key=True, autoincrement=True), Column("sf_id", Unicode(24)), ] id_t = Table(mapping["sf_id_table"], self.metadata, *sf_id_fields) mapper(self.models[mapping["sf_id_table"]], id_t) mapper(self.models[mapping["table"]], t, **mapper_kwargs)
def _create_table(self, mapping): """Create a table for the given mapping.""" model_name = f"{mapping.table}Model" mapper_kwargs = {} self.models[mapping.table] = type(model_name, (object, ), {}) t = create_table(mapping, self.metadata) if "RecordTypeId" in mapping.fields: # We're using Record Type Mapping support. # If multiple mappings point to the same table, don't recreate the table if mapping.get_source_record_type_table() not in self.models: self._create_record_type_table( mapping.get_source_record_type_table()) if not mapping.get_oid_as_pk(): # If multiple mappings point to the same table, don't recreate the table if mapping.get_sf_id_table() not in self.models: sf_id_model_name = f"{mapping.get_sf_id_table()}Model" self.models[mapping.get_sf_id_table()] = type( sf_id_model_name, (object, ), {}) sf_id_fields = [ Column("id", Integer(), primary_key=True, autoincrement=True), Column("sf_id", Unicode(24)), ] id_t = Table(mapping.get_sf_id_table(), self.metadata, *sf_id_fields) mapper(self.models[mapping.get_sf_id_table()], id_t) mapper(self.models[mapping.table], t, **mapper_kwargs)
def test_create_table_modern_id_mapping(self): mapping_file = os.path.join(os.path.dirname(__file__), "mapping_v2.yml") content = parse_from_yaml(mapping_file) account_mapping = content["Insert Contacts"] with temporary_dir() as d: tmp_db_path = os.path.join(d, "temp.db") engine, metadata = create_db_file(tmp_db_path) t = create_table(account_mapping, metadata) assert t.name == "contacts" assert isinstance(t.columns["id"].type, Integer) assert isinstance(t.columns["first_name"].type, Unicode) assert isinstance(t.columns["last_name"].type, Unicode) assert isinstance(t.columns["email"].type, Unicode)
def test_create_table_legacy_oid_mapping(self): mapping_file = os.path.join(os.path.dirname(__file__), "mapping_v1.yml") with open(mapping_file, "r") as fh: content = yaml.safe_load(fh) account_mapping = content["Insert Contacts"] with temporary_dir() as d: tmp_db_path = os.path.join(d, "temp.db") engine, metadata = create_db_file(tmp_db_path) t = create_table(account_mapping, metadata) assert t.name == "contacts" assert isinstance(t.columns["sf_id"].type, Unicode) assert isinstance(t.columns["first_name"].type, Unicode) assert isinstance(t.columns["last_name"].type, Unicode) assert isinstance(t.columns["email"].type, Unicode)
def _create_table(self, mapping): model_name = "{}Model".format(mapping["table"]) mapper_kwargs = {} self.models[mapping["table"]] = type(model_name, (object,), {}) t = create_table(mapping, self.metadata) if not mapping["oid_as_pk"]: mapping["sf_id_table"] = mapping["table"] + "_sf_id" # If multiple mappings point to the same table, don't recreate the table if mapping["sf_id_table"] not in self.models: sf_id_model_name = "{}Model".format(mapping["sf_id_table"]) self.models[mapping["sf_id_table"]] = type( sf_id_model_name, (object,), {} ) sf_id_fields = [ Column("id", Integer(), primary_key=True, autoincrement=True), Column("sf_id", Unicode(24)), ] id_t = Table(mapping["sf_id_table"], self.metadata, *sf_id_fields) mapper(self.models[mapping["sf_id_table"]], id_t) mapper(self.models[mapping["table"]], t, **mapper_kwargs)