def test_dependency_order(self): # b depends on (a), f: (d, e), d: (b), g: (f) deps = [ TableDeps(None), TableDeps(None), TableDeps(None), TableDeps(None) ] deps[0].deps = [DependencyRecord('b', 'rid', 'a', 'id')] deps[1].deps = [ DependencyRecord('f', 'rid', 'd', 'id'), DependencyRecord('d', 'rid', 'a', 'id') ] deps[2].deps = [ DependencyRecord('d', 'rid', 'b', 'id'), DependencyRecord('b', 'rid', 'a', 'id') ] deps[3].deps = [ DependencyRecord('g', 'rid', 'f', 'id'), DependencyRecord('f', 'rid', 'd', 'id'), DependencyRecord('d', 'rid', 'a', 'id') ] relations = [('b', '', 'a', ''), ('f', '', 'd', ''), ('f', '', 'e', ''), ('d', '', 'b', ''), ('g', '', 'f', '')] deps = TableDeps.sort_deps(deps, relations) dep_names = ''.join(self.deps_to_table_names(deps)) self.assertEqual('gfdb', dep_names)
def test_parse_deps(self): text = '' deps = TableDeps.parse_stored_deps_multiline(text) self.assertEqual(0, len(deps)) text = \ """ pk:[id], document_documentnote.field_value_id -> document_documentfieldvalue.id, document_documentfieldvalue.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_trademarkusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id """ deps = TableDeps.parse_stored_deps_multiline(text) self.assertEqual(2, len(deps)) self.assertEqual(3, len(deps[0].deps)) self.assertEqual('extract_trademarkusage', deps[1].deps[0].own_table) self.assertEqual('text_unit_id', deps[1].deps[0].ref_key) self.assertEqual('document_document', deps[0].deps[-1].ref_table) self.assertEqual('id', deps[0].deps[-1].ref_table_pk)
def build_table_dependences(table_name: str) -> List[TableDeps]: relations = TableDepsBuilder.get_relations() pkeys = TableDepsBuilder.get_all_primary_keys() all_deps = [] # type: List[TableDeps] TableDepsBuilder.find_dependend_tables(relations, all_deps, None, table_name) all_deps = TableDeps.sort_deps(all_deps) for dep in all_deps: dep.own_table_pk = pkeys[dep.deps[0].own_table] return all_deps
def build_table_dependences(table_name: str) -> List[TableDeps]: relations = TableDepsBuilder.get_relations() pkeys = TableDepsBuilder.get_all_primary_keys() all_deps = [] # type: List[TableDeps] TableDepsBuilder.find_dependend_tables(relations, all_deps, None, table_name) # remove longer chains dep_by_own_table = group_by( all_deps, lambda d: d.deps[0].own_table) # type: Dict[str, List[TableDeps]] shortcuts = [] # type: List[TableDeps] for key in dep_by_own_table: opt_deps = TableDeps.remove_duplicates(dep_by_own_table[key]) opt_deps = TableDeps.leave_shortest_chains(opt_deps) shortcuts += opt_deps all_deps = TableDeps.sort_deps(shortcuts, relations) for dep in all_deps: dep.own_table_pk = pkeys[dep.deps[0].own_table] return all_deps
def find_dependend_tables(relations: List[Tuple[str, str, str, str]], all_deps: List[TableDeps], current_dep: TableDeps, table_name: str) -> None: for rel in relations: if rel[2] != table_name: continue dep_record = DependencyRecord(rel[0], rel[1], rel[2], rel[3]) if not current_dep: new_dep = TableDeps(None) new_dep.deps = [dep_record] else: new_dep = TableDeps(current_dep) new_dep.deps.insert(0, dep_record) all_deps.append(new_dep) # max recursion is limited if len(new_dep.deps) < 6: TableDepsBuilder.find_dependend_tables(relations, all_deps, new_dep, dep_record.own_table)
def build_bulk_delete(self, dep_set_text:str='') -> ModelBulkDelete: full_depset = """ pk:[id], document_documentnote.field_value_id -> document_documentfieldvalue.id, document_documentfieldvalue.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_trademarkusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_copyrightusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_citationusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_percentusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_ratiousage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_distanceusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], employee_provision.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], document_documentfieldvalue.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], document_textunitnote.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], document_textunitproperty.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], document_textunitrelation.text_unit_b_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], analyze_textunitclassifiersuggestion.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], analyze_textunitcluster_text_units.textunit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], analyze_textunitsimilarity.text_unit_a_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_partyusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_geoentityusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_dateusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_currencyusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], document_textunittag.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_urlusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_amountusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], employee_employerusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], document_textunitrelation.text_unit_a_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], analyze_textunitclassification.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], analyze_textunitsimilarity.text_unit_b_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_regulationusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_termusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_geoaliasusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_definitionusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_datedurationusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], extract_courtusage.text_unit_id -> document_textunit.id, document_textunit.document_id -> document_document.id pk:[id], fields_documentannotation_tags.documentannotation_id -> fields_documentannotation.id, fields_documentannotation.document_id -> document_document.id pk:[id], employee_provision.employee_id -> employee_employee.id, employee_employee.document_id -> document_document.id pk:[id], document_documentnote.field_value_id -> document_documentfieldvalue.id, document_documentfieldvalue.document_id -> document_document.id pk:[document_id], doc_fields_document_generic_document.document_id -> document_document.id pk:[id], employee_provision.document_id -> document_document.id pk:[id], project_taskqueue_completed_documents.document_id -> document_document.id pk:[id], document_documentrelation.document_b_id -> document_document.id pk:[id], document_textunit.document_id -> document_document.id pk:[id], analyze_documentsimilarity.document_b_id -> document_document.id pk:[document_ptr_id], lease_leasedocument.document_ptr_id -> document_document.id pk:[document_id], doc_fields_com_cred.document_id -> document_document.id pk:[id], imanage_integration_imanagedocument.document_id -> document_document.id pk:[id], fields_classifierdatasetentry.document_id -> document_document.id pk:[id], fields_documentannotation.document_id -> document_document.id pk:[id], employee_employee.document_id -> document_document.id pk:[id], document_documentfieldvalue.document_id -> document_document.id pk:[id], project_taskqueue_documents.document_id -> document_document.id pk:[id], document_documentnote.document_id -> document_document.id pk:[id], document_documentproperty.document_id -> document_document.id pk:[id], document_documentrelation.document_a_id -> document_document.id pk:[id], document_documenttag.document_id -> document_document.id pk:[id], project_taskqueuehistory_documents.document_id -> document_document.id pk:[id], analyze_documentcluster_documents.document_id -> document_document.id pk:[id], analyze_documentsimilarity.document_a_id -> document_document.id """ dep_set_text = dep_set_text or full_depset deps = TableDeps.parse_stored_deps_multiline(dep_set_text) return ModelBulkDelete(deps)