class UnittestDB(object): """create DataBase for Unittest """ def __init__(self): """ initialize""" self._metadata = None self.loader = SchemaLoader() def create_from(self, input): """create database schema from input """ return self.loader.create_from(input) def column_count(self, metadata): """ count total columns of this schema to verify schema is correctly created """ c_count = 0 for table in metadata.sorted_tables: # name = table.name # for column in table.c: # c_count = c_count + 1 c_count += sum(1 for _ in table.c) return c_count def load_from_file(self, filename): """create metadata from schema file""" return self.loader.load_from_file(filename) def fill_table(self, table, idx): """fill table with our test data(number)""" # print "fill_table %s %d"%( table.name,idx) insert_clause = table.insert() c_names = [col.name for col in table.c] insert_dict = {} for col_name in c_names: if table.c[col_name].primary_key: insert_dict[col_name] = idx elif table.c[col_name].foreign_keys: insert_dict[col_name] = idx else: insert_dict[col_name] = idx + 100 # _LOGGER.debug("insert %s %s" % (table.name, insert_dict)) insert_clause.execute(insert_dict) def get_graph(self, sorted_tables, table_index, graph): """ get graph represents of schema graph is in adjacent table """ index = 0 for table in sorted_tables: name = table.name c_names = [col.name for col in table.c] for col_name in c_names: if table.c[col_name].foreign_keys: for fk_table in table.c[col_name].foreign_keys: fk_name = fk_table.target_fullname fk_tbname = fk_name.split('.')[0] # 1. fk to him self if fk_tbname == name: continue # 2. duplicate fk to a table if index in graph[table_index[fk_tbname]]: continue graph[table_index[fk_tbname]].append(index) index = index + 1 # print "graph is ", graph def fill_tables(self, metadata, row_cnt): """ fill table by row lines test data do it in topo sequence of foreignkey links otherwise insert could failed. """ sorted_tables = metadata.sorted_tables sorted_tb = {} table_index = {} graph = [] count = 0 for table in sorted_tables: sorted_tb[table.name] = table table_index[table.name] = count count = count + 1 graph.append([]) # print " ", table.name self.get_graph(sorted_tables, table_index, graph) # self.get_graph(sorted_tables, sorted_tb, table_index, graph) # for index in range(0, len(graph)): # for idx in graph[index]: # print "%s -> %s" %( sorted_tables[index].name,\ # sorted_tables[idx].name) sequence = topo_sort(graph) # for index in sequence: # print index, " ", sorted_tables[index].name # print "%s = %s"%(len(sorted_tables),len(sequence)) for idx in range(0, row_cnt): for index in sequence: self.fill_table(sorted_tables[index], idx+1) def load_with_fake_data(self, filename, dbname): """load with fake data""" udb = UnittestDB() if filename.endswith("yaml"): metadata = udb.load_from_file(filename) elif filename.endswith("sql"): metadata = udb.read_from_oracle(filename) metadata.bind = dbname try: metadata.drop_all() except: pass metadata.create_all(checkfirst=False) udb.fill_tables(metadata, 10) return metadata def read_from_oracle(self, filename = "oracle.sql"): """read from oracle""" return self.loader.read_from_oracle(filename)
def __init__(self): """ initialize""" self._metadata = None self.loader = SchemaLoader()
def main(): """main """ usage = "usage: %prog -m mapfile \n" usage += " -v --validate_mapfile=<mapfile> --source=<database link>\n" usage += " --find_key=<table|table.column> --find_column=<key> \n" usage += " --find_table=<key> --list_key --list_column --list_entity" parser = OptionParser(usage=usage, version="%prog 1.0") parser.add_option("-m", "--mapfile", action="store", type="string", dest="mapfile", help="input registration yaml map file") parser.add_option("-v", "--validate", action="store_true", dest="validate", help="perform validation for the mapping") parser.add_option("-f", "--validate_mapfile", action="store", type="string", dest="vmapfile", help="input validate mapfile") parser.add_option("-s", "--source", action="store", type="string", dest="source", help="input validate database source link") parser.add_option("-k", "--find_key", action="store", type="string", dest="find_key", help="input table/column to get corresponding key ") parser.add_option("-c", "--find_column", action="store", type="string", dest="find_column", help="input key to get table[.column] ") parser.add_option("-t", "--find_table", action="store", type="string", dest="find_table", help="input key to get only table name ") parser.add_option("-e", "--list_key", action="store_true", dest="list_key", help="list all keys ") parser.add_option("-o", "--list_column", action="store_true", dest="list_column", help="list all tables/columns") parser.add_option("-i", "--list_entity", action="store_true", dest="list_entity", help="list all entity ") mapper = Mapper() mapfile = os.path.join(os.path.dirname(sys.argv[0]), 'map.yaml') validate = False column = None key = None list_key = False list_column = False list_entity = False vmap = None sloader = SchemaLoader() dbmanager = None source = None (options, args) = parser.parse_args() if options.mapfile: mapfile = options.mapfile if options.validate: validate = options.validate if options.vmapfile: vmap = options.vmapfile elif options.source: source = options.source else: parser.error("options -v need either mapfile or source") if options.find_key: column = options.find_key if options.find_column: key = options.find_column if options.find_table: key = options.find_table if options.list_key: list_key = options.list_key if options.list_column: list_column = options.list_column if options.list_entity: list_entity = options.list_entity mapper.load_mapfile(mapfile) if validate: if vmap: tables = sloader.load_from_file(vmap).sorted_tables if mapper.validate_map(tables): print "Validate is OK" else: dbmanager = DBManager() dbmanager.connect(source) dbaliase = dbmanager.get_alias(source) tables = dbmanager.load_tables(dbaliase) if mapper.validate_map(tables): print "Validate is OK" if column: mapper.pprint(mapper.get_key(column)) if key: if options.find_column: mapper.pprint(mapper.get_column(key)) if options.find_table: mapper.pprint(mapper.get_table(key)) if list_key: mapper.pprint(mapper.list_key()) if list_column: mapper.pprint(mapper.list_column()) if list_entity: mapper.pprint(mapper.list_entity())