def post_sql_on_import( self ): if self.args.postsql: db_srv = self.dest_db db_x = DriverMysql( self.dest_db ) print '' print 'Running Post SQL' print ' %s' % self.args.postsql try: db_x.execute( self.args.postsql ) except _mysql_exceptions.ProgrammingError: print 'Error With Post SQL'
def setup_download_specific_tables( self ): if self.args.set: if self.args.set not in pulldown_set: custom_script_sets = enterprise_sets().get( self.args.set ) if not custom_script_sets: print 'Unknown pulldown set requested: %s' % self.args.set sys.exit() self.downloads = custom_script_sets return for database, tables in pulldown_set[self.args.set].iteritems(): if len( tables ) > 0: self.downloads.update( { database : {'tables' : tables } } ) elif not self.args.database and '.' in self.args.tables: if ' ' in self.args.tables: unfiltered_tables = self.args.tables.split(' ') elif ',' in self.args.tables: unfiltered_tables = self.args.tables.split(',') else: unfiltered_tables = [ self.args.tables ] for t in unfiltered_tables: db = t.split('.')[0] tb = t.split('.')[1] if db in self.downloads: self.downloads[db]['tables'].append( tb ) else: self.downloads.update( { db : {'tables' : [ tb ] } } ) return else: database = self.args.database if ',' in database: _databases = database.split(',') else: _databases = [ self.args.database ] for database in _databases: if self.args.tables: tables = self.args.tables if ',' in tables: tables = tables.replace( ' ', '' ).split( ',' ) elif ' ' in self.args.tables: tables = tables.split(' ') else: tables = [ tables ] else: qry = """SHOW TABLES IN %s;""" % database db = DriverMysql( self.source_db ) result = db.execute( qry ) tables = [] for table in result: if table[0][-5:] != '_view': tables.append( table[0] ) if self.args.verbosity: print 'Downloading from schema "%s" tables %s' % ( database, ','.join( tables ) ) self.downloads.update( { database : { 'tables' : tables } } )
def get_download_size( self, source_or_dest ): if source_or_dest == 'source': db_srv = self.source_db else: db_srv = self.dest_db db_x = DriverMysql( db_srv ) totals = 0 for database, info in self.downloads.iteritems(): table_sql = '' for t in info['tables']: table_sql += '"%s",' % t table_sql = table_sql[:-1] if table_sql != '': qry = """SELECT table_name AS "Table", ( data_length + index_length ) as "bytes" FROM information_schema.TABLES WHERE table_schema = "%s" AND table_name IN( %s );""" %( database, table_sql ) for size in db_x.execute( qry, True ): totals = totals + int( size['bytes'] ) megs = float( totals / 1024 ) / 1024 gigs = megs / 1024 gigs = round( gigs, 3 ) if self.args.verbosity: print 'Download Size %s Gigs' % str( gigs ) rows = 0 for database, info in self.downloads.iteritems(): for table in info['tables']: qry = """SELECT count(*) AS c FROM `%s`.`%s`;""" % ( database, table ) count = db_x.execute( qry, True )[0]['c'] rows = rows + count data = { 'gigs' : gigs, 'rows' : rows } return data
def verify_download( self, source_or_dest ): if source_or_dest == 'source': db_srv = self.source_db else: db_srv = self.dest_db db_x = DriverMysql( db_srv ) print 'Source Database: ', self.source_db['info'] print 'Destination Database: ', self.dest_db['info'] print ' ' print 'Downloads ' for database, info in self.downloads.iteritems(): qry = "SHOW TABLES IN `%s`; " % database tables = db_x.execute( qry ) full_table_list = [] for table in tables: full_table_list.append( table[0] ) if len( info['tables'] ) == 0: download_tables = full_table_list else: download_tables = info['tables'] for dl_table in download_tables: if dl_table not in full_table_list: print 'ERROR: %s is missing from the source database!' % dl_table sys.exit() print database for table in info['tables']: print ' ', table if self.args.skip_verify == False: if self.args.structure == False: self.download_size = self.get_download_size( 'source' ) print ' ' print 'Download Size: %s Gigs' % self.download_size['gigs'] print 'Download Rows: %s ' % format( self.download_size['rows'], ",d") print 'Continue?' print ' ' verification = raw_input('--> ') if verification not in ( 'y', 'yes'): print 'Exiting' sys.exit()