def _export_metadata(source, db_list, output_file, options): """Export metadata from the specified list of databases. This private method retrieves the objects metadata for each database listed in the form of CREATE (SQL) statements or in a tabular form (GRID, TAB, CSV, VERTICAL) to the specified file. This private method does not check permissions. source[in] Server instance. db_list[in] List of databases to export. output_file[in] Output file to store the metadata information. options[in] Dictionary containing the options for the export: (skip_tables, skip_views, skip_triggers, skip_procs, skip_funcs, skip_events, skip_grants, skip_create, skip_data, no_header, display, format, debug, exclude_names, exclude_patterns) """ frmt = options.get("format", "sql") no_headers = options.get("no_headers", False) column_type = options.get("display", "brief") quiet = options.get("quiet", False) skip_create = options.get("skip_create", False) skip_tables = options.get("skip_tables", False) skip_views = options.get("skip_views", False) skip_triggers = options.get("skip_triggers", False) skip_procs = options.get("skip_procs", False) skip_funcs = options.get("skip_funcs", False) skip_events = options.get("skip_events", False) skip_grants = options.get("skip_grants", False) sql_mode = source.select_variable("SQL_MODE") for db_name in db_list: # Get a Database class instance db = Database(source, db_name, options) # Export database metadata if not quiet: output_file.write("# Exporting metadata from {0}\n".format( db.q_db_name)) # Perform the extraction if frmt == "sql": db.init() if not skip_create: output_file.write("DROP DATABASE IF EXISTS {0};\n".format( db.q_db_name)) output_file.write("CREATE DATABASE {0};\n".format( db.q_db_name)) output_file.write("USE {0};\n".format(db.q_db_name)) for dbobj in db.get_next_object(): if dbobj[0] == "GRANT" and not skip_grants: if not quiet: output_file.write("# Grant:\n") if dbobj[1][3]: create_str = "GRANT {0} ON {1}.{2} TO {3};\n".format( dbobj[1][1], db.q_db_name, quote_with_backticks(dbobj[1][3], sql_mode), dbobj[1][0]) else: create_str = "GRANT {0} ON {1}.* TO {2};\n".format( dbobj[1][1], db.q_db_name, dbobj[1][0]) output_file.write(create_str) else: if not quiet: output_file.write("# {0}: {1}.{2}\n".format( dbobj[0], db.q_db_name, quote_with_backticks(dbobj[1][0], sql_mode))) if (dbobj[0] == "PROCEDURE" and not skip_procs) or \ (dbobj[0] == "FUNCTION" and not skip_funcs) or \ (dbobj[0] == "EVENT" and not skip_events) or \ (dbobj[0] == "TRIGGER" and not skip_triggers): output_file.write("DELIMITER ||\n") output_file.write("{0};\n".format( db.get_create_statement(db.db_name, dbobj[1][0], dbobj[0]))) if (dbobj[0] == "PROCEDURE" and not skip_procs) or \ (dbobj[0] == "FUNCTION" and not skip_funcs) or \ (dbobj[0] == "EVENT" and not skip_events) or \ (dbobj[0] == "TRIGGER" and not skip_triggers): output_file.write("||\n") output_file.write("DELIMITER ;\n") else: objects = [] if not skip_tables: objects.append("TABLE") if not skip_funcs: objects.append("FUNCTION") if not skip_procs: objects.append("PROCEDURE") if not skip_views: objects.append("VIEW") if not skip_triggers: objects.append("TRIGGER") if not skip_events: objects.append("EVENT") if not skip_grants: objects.append("GRANT") for obj_type in objects: output_file.write("# {0}S in {1}:".format( obj_type, db.q_db_name)) if frmt in ('grid', 'vertical'): rows = db.get_db_objects(obj_type, column_type, True) else: rows = db.get_db_objects(obj_type, column_type, True, True) if len(rows[1]) < 1: output_file.write(" (none found)\n") else: output_file.write("\n") # Cannot use print_list here because we must manipulate # the behavior of format_tabular_list. list_options = {} if frmt == "vertical": format_vertical_list(output_file, rows[0], rows[1]) elif frmt == "tab": list_options['print_header'] = not no_headers list_options['separator'] = '\t' format_tabular_list(output_file, rows[0], rows[1], list_options) elif frmt == "csv": list_options['print_header'] = not no_headers list_options['separator'] = ',' format_tabular_list(output_file, rows[0], rows[1], list_options) else: # default to table format format_tabular_list(output_file, rows[0], rows[1]) if not quiet: output_file.write("#...done.\n")
def export_metadata(source, src_val, db_list, options): """Produce rows to be used to recreate objects in a database. This method retrieves the objects for each database listed in the form of CREATE (SQL) statements or in a tabular form to the file specified. The valid values for the format parameter are SQL, CSV, TSV, VERTICAL, or GRID. source[in] Server instance src_val[in] a dictionary containing connection information for the source including: (user, password, host, port, socket) options[in] a dictionary containing the options for the copy: (skip_tables, skip_views, skip_triggers, skip_procs, skip_funcs, skip_events, skip_grants, skip_create, skip_data, no_header, display, format, debug, exclude_names, exclude_patterns) Returns bool True = success, False = error """ from mysql.utilities.common.database import Database from mysql.utilities.common.format import format_tabular_list from mysql.utilities.common.format import format_vertical_list format = options.get("format", "sql") no_headers = options.get("no_headers", False) column_type = options.get("display", "brief") skip_create = options.get("skip_create", False) quiet = options.get("quiet", False) skip_tables = options.get("skip_tables", False) skip_views = options.get("skip_views", False) skip_triggers = options.get("skip_triggers", False) skip_procs = options.get("skip_procs", False) skip_funcs = options.get("skip_funcs", False) skip_events = options.get("skip_events", False) skip_grants = options.get("skip_grants", False) if options.get("all", False): rows = source.get_all_databases() for row in rows: db_list.append(row[0]) # Check user permissions on source for all databases for db_name in db_list: source_db = Database(source, db_name) # Make a dictionary of the options access_options = { 'skip_views' : skip_views, 'skip_procs' : skip_procs, 'skip_funcs' : skip_funcs, 'skip_grants' : skip_grants, 'skip_events' : skip_events, } source_db.check_read_access(src_val["user"], src_val["host"], access_options) for db_name in db_list: # Get a Database class instance db = Database(source, db_name, options) # Error is source database does not exist if not db.exists(): raise UtilDBError("Source database does not exist - %s" % db_name, -1, db_name) if not quiet: print "# Exporting metadata from %s" % db_name # Perform the extraction if format == "sql": db.init() # quote database name with backticks q_db_name = quote_with_backticks(db_name) if not skip_create: print "DROP DATABASE IF EXISTS %s;" % q_db_name print "CREATE DATABASE %s;" % q_db_name print "USE %s;" % q_db_name for dbobj in db.get_next_object(): if dbobj[0] == "GRANT" and not skip_grants: if not quiet: print "# Grant:" if dbobj[1][3]: create_str = "GRANT %s ON %s.%s TO %s;" % \ (dbobj[1][1], q_db_name, quote_with_backticks(dbobj[1][3]), dbobj[1][0]) else: create_str = "GRANT %s ON %s.* TO %s;" % \ (dbobj[1][1], q_db_name, dbobj[1][0]) if create_str.find("%"): create_str = re.sub("%", "%%", create_str) print create_str else: if not quiet: print "# %s: %s.%s" % (dbobj[0], db_name, dbobj[1][0]) if (dbobj[0] == "PROCEDURE" and not skip_procs) or \ (dbobj[0] == "FUNCTION" and not skip_funcs) or \ (dbobj[0] == "EVENT" and not skip_events) or \ (dbobj[0] == "TRIGGER" and not skip_triggers): print "DELIMITER ||" print "%s;" % db.get_create_statement(db_name, dbobj[1][0], dbobj[0]) if (dbobj[0] == "PROCEDURE" and not skip_procs) or \ (dbobj[0] == "FUNCTION" and not skip_funcs) or \ (dbobj[0] == "EVENT" and not skip_events) or \ (dbobj[0] == "TRIGGER" and not skip_triggers): print "||" print "DELIMITER ;" else: objects = [] if not skip_tables: objects.append("TABLE") if not skip_views: objects.append("VIEW") if not skip_triggers: objects.append("TRIGGER") if not skip_procs: objects.append("PROCEDURE") if not skip_funcs: objects.append("FUNCTION") if not skip_events: objects.append("EVENT") if not skip_grants: objects.append("GRANT") for obj_type in objects: sys.stdout.write("# %sS in %s:" % (obj_type, db_name)) if format in ('grid', 'vertical'): rows = db.get_db_objects(obj_type, column_type, True) else: rows = db.get_db_objects(obj_type, column_type, True, True) if len(rows[1]) < 1: print " (none found)" else: print # Cannot use print_list here becasue we must manipulate # the behavior of format_tabular_list list_options = {} if format == "vertical": format_vertical_list(sys.stdout, rows[0], rows[1]) elif format == "tab": list_options['print_header'] = not no_headers list_options['separator'] = '\t' format_tabular_list(sys.stdout, rows[0], rows[1], list_options) elif format == "csv": list_options['print_header'] = not no_headers list_options['separator'] = ',' format_tabular_list(sys.stdout, rows[0], rows[1], list_options) else: # default to table format format_tabular_list(sys.stdout, rows[0], rows[1]) if not quiet: print "#...done." return True
def _export_metadata(source, db_list, output_file, options): """Export metadata from the specified list of databases. This private method retrieves the objects metadata for each database listed in the form of CREATE (SQL) statements or in a tabular form (GRID, TAB, CSV, VERTICAL) to the specified file. This private method does not check permissions. source[in] Server instance. db_list[in] List of databases to export. output_file[in] Output file to store the metadata information. options[in] Dictionary containing the options for the export: (skip_tables, skip_views, skip_triggers, skip_procs, skip_funcs, skip_events, skip_grants, skip_create, skip_data, no_header, display, format, debug, exclude_names, exclude_patterns) """ frmt = options.get("format", "sql") no_headers = options.get("no_headers", False) column_type = options.get("display", "brief") quiet = options.get("quiet", False) skip_create = options.get("skip_create", False) skip_tables = options.get("skip_tables", False) skip_views = options.get("skip_views", False) skip_triggers = options.get("skip_triggers", False) skip_procs = options.get("skip_procs", False) skip_funcs = options.get("skip_funcs", False) skip_events = options.get("skip_events", False) skip_grants = options.get("skip_grants", False) for db_name in db_list: # Get a Database class instance db = Database(source, db_name, options) # Export database metadata if not quiet: output_file.write( "# Exporting metadata from {0}\n".format(db.db_name) ) # Perform the extraction if frmt == "sql": db.init() if not skip_create: output_file.write( "DROP DATABASE IF EXISTS {0};\n".format(db.q_db_name) ) output_file.write( "CREATE DATABASE {0};\n".format(db.q_db_name) ) output_file.write("USE {0};\n".format(db.q_db_name)) for dbobj in db.get_next_object(): if dbobj[0] == "GRANT" and not skip_grants: if not quiet: output_file.write("# Grant:\n") if dbobj[1][3]: create_str = "GRANT {0} ON {1}.{2} TO {3};\n".format( dbobj[1][1], db.q_db_name, quote_with_backticks(dbobj[1][3]), dbobj[1][0] ) else: create_str = "GRANT {0} ON {1}.* TO {2};\n".format( dbobj[1][1], db.q_db_name, dbobj[1][0] ) output_file.write(create_str) else: if not quiet: output_file.write( "# {0}: {1}.{2}\n".format(dbobj[0], db.db_name, dbobj[1][0]) ) if (dbobj[0] == "PROCEDURE" and not skip_procs) or \ (dbobj[0] == "FUNCTION" and not skip_funcs) or \ (dbobj[0] == "EVENT" and not skip_events) or \ (dbobj[0] == "TRIGGER" and not skip_triggers): output_file.write("DELIMITER ||\n") output_file.write("{0};\n".format( db.get_create_statement(db.db_name, dbobj[1][0], dbobj[0]) )) if (dbobj[0] == "PROCEDURE" and not skip_procs) or \ (dbobj[0] == "FUNCTION" and not skip_funcs) or \ (dbobj[0] == "EVENT" and not skip_events) or \ (dbobj[0] == "TRIGGER" and not skip_triggers): output_file.write("||\n") output_file.write("DELIMITER ;\n") else: objects = [] if not skip_tables: objects.append("TABLE") if not skip_funcs: objects.append("FUNCTION") if not skip_procs: objects.append("PROCEDURE") if not skip_views: objects.append("VIEW") if not skip_triggers: objects.append("TRIGGER") if not skip_events: objects.append("EVENT") if not skip_grants: objects.append("GRANT") for obj_type in objects: output_file.write( "# {0}S in {1}:".format(obj_type, db.db_name) ) if frmt in ('grid', 'vertical'): rows = db.get_db_objects(obj_type, column_type, True) else: rows = db.get_db_objects(obj_type, column_type, True, True) if len(rows[1]) < 1: output_file.write(" (none found)\n") else: output_file.write("\n") # Cannot use print_list here because we must manipulate # the behavior of format_tabular_list. list_options = {} if frmt == "vertical": format_vertical_list(output_file, rows[0], rows[1]) elif frmt == "tab": list_options['print_header'] = not no_headers list_options['separator'] = '\t' format_tabular_list(output_file, rows[0], rows[1], list_options) elif frmt == "csv": list_options['print_header'] = not no_headers list_options['separator'] = ',' format_tabular_list(output_file, rows[0], rows[1], list_options) else: # default to table format format_tabular_list(output_file, rows[0], rows[1]) if not quiet: output_file.write("#...done.\n")
def export_metadata(source, src_val, db_list, options): """Produce rows to be used to recreate objects in a database. This method retrieves the objects for each database listed in the form of CREATE (SQL) statements or in a tabular form to the file specified. The valid values for the format parameter are SQL, CSV, TSV, VERTICAL, or GRID. source[in] Server instance src_val[in] a dictionary containing connection information for the source including: (user, password, host, port, socket) options[in] a dictionary containing the options for the copy: (skip_tables, skip_views, skip_triggers, skip_procs, skip_funcs, skip_events, skip_grants, skip_create, skip_data, no_header, display, format, debug, exclude_names, exclude_patterns) Returns bool True = success, False = error """ from mysql.utilities.common.database import Database from mysql.utilities.common.format import format_tabular_list from mysql.utilities.common.format import format_vertical_list format = options.get("format", "sql") no_headers = options.get("no_headers", False) column_type = options.get("display", "brief") skip_create = options.get("skip_create", False) quiet = options.get("quiet", False) skip_tables = options.get("skip_tables", False) skip_views = options.get("skip_views", False) skip_triggers = options.get("skip_triggers", False) skip_procs = options.get("skip_procs", False) skip_funcs = options.get("skip_funcs", False) skip_events = options.get("skip_events", False) skip_grants = options.get("skip_grants", False) if options.get("all", False): rows = source.get_all_databases() for row in rows: db_list.append(row[0]) # Check user permissions on source for all databases for db_name in db_list: source_db = Database(source, db_name) # Make a dictionary of the options access_options = { 'skip_views': skip_views, 'skip_procs': skip_procs, 'skip_funcs': skip_funcs, 'skip_grants': skip_grants, 'skip_events': skip_events, } source_db.check_read_access(src_val["user"], src_val["host"], access_options) for db_name in db_list: # Get a Database class instance db = Database(source, db_name, options) # Error is source database does not exist if not db.exists(): raise UtilDBError("Source database does not exist - %s" % db_name, -1, db_name) if not quiet: print "# Exporting metadata from %s" % db_name # Perform the extraction if format == "sql": db.init() # quote database name with backticks q_db_name = quote_with_backticks(db_name) if not skip_create: print "DROP DATABASE IF EXISTS %s;" % q_db_name print "CREATE DATABASE %s;" % q_db_name print "USE %s;" % q_db_name for dbobj in db.get_next_object(): if dbobj[0] == "GRANT" and not skip_grants: if not quiet: print "# Grant:" if dbobj[1][3]: create_str = "GRANT %s ON %s.%s TO %s;" % \ (dbobj[1][1], q_db_name, quote_with_backticks(dbobj[1][3]), dbobj[1][0]) else: create_str = "GRANT %s ON %s.* TO %s;" % \ (dbobj[1][1], q_db_name, dbobj[1][0]) if create_str.find("%"): create_str = re.sub("%", "%%", create_str) print create_str else: if not quiet: print "# %s: %s.%s" % (dbobj[0], db_name, dbobj[1][0]) if (dbobj[0] == "PROCEDURE" and not skip_procs) or \ (dbobj[0] == "FUNCTION" and not skip_funcs) or \ (dbobj[0] == "EVENT" and not skip_events) or \ (dbobj[0] == "TRIGGER" and not skip_triggers): print "DELIMITER ||" print "%s;" % db.get_create_statement( db_name, dbobj[1][0], dbobj[0]) if (dbobj[0] == "PROCEDURE" and not skip_procs) or \ (dbobj[0] == "FUNCTION" and not skip_funcs) or \ (dbobj[0] == "EVENT" and not skip_events) or \ (dbobj[0] == "TRIGGER" and not skip_triggers): print "||" print "DELIMITER ;" else: objects = [] if not skip_tables: objects.append("TABLE") if not skip_views: objects.append("VIEW") if not skip_triggers: objects.append("TRIGGER") if not skip_procs: objects.append("PROCEDURE") if not skip_funcs: objects.append("FUNCTION") if not skip_events: objects.append("EVENT") if not skip_grants: objects.append("GRANT") for obj_type in objects: sys.stdout.write("# %sS in %s:" % (obj_type, db_name)) if format in ('grid', 'vertical'): rows = db.get_db_objects(obj_type, column_type, True) else: rows = db.get_db_objects(obj_type, column_type, True, True) if len(rows[1]) < 1: print " (none found)" else: print # Cannot use print_list here becasue we must manipulate # the behavior of format_tabular_list list_options = {} if format == "vertical": format_vertical_list(sys.stdout, rows[0], rows[1]) elif format == "tab": list_options['print_header'] = not no_headers list_options['separator'] = '\t' format_tabular_list(sys.stdout, rows[0], rows[1], list_options) elif format == "csv": list_options['print_header'] = not no_headers list_options['separator'] = ',' format_tabular_list(sys.stdout, rows[0], rows[1], list_options) else: # default to table format format_tabular_list(sys.stdout, rows[0], rows[1]) if not quiet: print "#...done." return True