diff --git a/data/scripts/migrator/dbsmerge_mongo.py b/data/scripts/migrator/dbsmerge_mongo.py index ad1194223..f4195d65b 100755 --- a/data/scripts/migrator/dbsmerge_mongo.py +++ b/data/scripts/migrator/dbsmerge_mongo.py @@ -3,8 +3,9 @@ # depends: # ^ pymongo # install via: easy_install pymongo # behaviour: -# ^ the script will "move" the collections if source and target server are the same -# but will "copy" (dump/restore) if source and target servers are different +# ^ the script will "move" the collections if source and target server are +# the same but will "copy" (dump/restore) if source and target servers are +# different import subprocess import sys @@ -33,7 +34,8 @@ ignore_empty_cols = True # Overwrite target collections flag. # Works only if from/to is on same host. -# If from/to hosts are different we use mongorestore which overwrites by default. +# If from/to hosts are different we use mongorestore +# which overwrites by default. drop_target = False dump_folder = 'dump' @@ -41,9 +43,19 @@ dump_folder = 'dump' # same server if from_host == to_host and from_port == to_port: print('Migrating on same server...') - mongo_from_url = 'mongodb://' + from_user + ':' + quote_plus(from_pass) + '@' + from_host + ':' + from_port + '/' + from_auth_db + mongo_from_url = 'mongodb://%s:%s@%s:%s/%s' % ( + from_user, + quote_plus(from_pass), + from_host, + from_port, + from_auth_db + ) if from_pass == '': # disabled auth - mongo_from_url = 'mongodb://' + from_host + ':' + from_port + '/' + from_db + mongo_from_url = 'mongodb://%s:%s/%s' % ( + from_host, + from_port, + from_db + ) client = MongoClient(mongo_from_url) db = client[from_db] @@ -55,15 +67,43 @@ if from_host == to_host and from_port == to_port: i = 0 for col in cols: i += 1 - if not ignore_empty_cols or (ignore_empty_cols and db[col].count() > 0): - print('Moving collection %s (%d of %d)...' % (col, i, len(cols))) + if( + not ignore_empty_cols or + (ignore_empty_cols and db[col].count() > 0) + ): + print( + 'Moving collection %s (%d of %d)...' % ( + col, i, len(cols) + ) + ) try: - client.admin.command(OrderedDict([('renameCollection', from_db + '.' + col), ('to', to_db + '.' + col), ('dropTarget', drop_target)])) + client.admin.command( + OrderedDict( + [ + ( + 'renameCollection', + from_db + '.' + col + ), + ( + 'to', + to_db + '.' + col + ), + ( + 'dropTarget', + drop_target + ) + ] + ) + ) except: e = sys.exc_info()[0] print(e) else: - print('Skipping empty collection %s (%d of %d)...' % (col, i, len(cols))) + print( + 'Skipping empty collection %s (%d of %d)...' % ( + col, i, len(cols) + ) + ) # no collections found else: print('No collections in source database.') diff --git a/data/scripts/migrator/dbsmerge_redis.py b/data/scripts/migrator/dbsmerge_redis.py index be754be3c..55f55c782 100755 --- a/data/scripts/migrator/dbsmerge_redis.py +++ b/data/scripts/migrator/dbsmerge_redis.py @@ -3,8 +3,8 @@ # depends: # ^ redis # install via easy_install redis # asserts: -# ^ destination redis is not password protected when connected from source redis server -# (https://github.com/antirez/redis/pull/2507) +# ^ destination redis is not password protected when connected from source +# redis server (https://github.com/antirez/redis/pull/2507) # behaviour: # ^ the script will not overwrite keys on the destination server/database @@ -23,8 +23,17 @@ to_pass = '' # Not used keymask = '*' timeout = 2000 -from_redis = redis.Redis(host=from_host, port=from_port, password=from_pass, db=from_db) -to_redis = redis.Redis(host=to_host, port=to_port, db=to_db) +from_redis = redis.Redis( + host=from_host, + port=from_port, + password=from_pass, + db=from_db + ) +to_redis = redis.Redis( + host=to_host, + port=to_port, + db=to_db + ) to_keys = to_redis.keys(keymask) from_keys = from_redis.keys(keymask) @@ -50,7 +59,14 @@ if len(from_keys) > 0: i += 1 print('Moving key %s (%d of %d)...' % (key, i, len(from_keys))), try: - from_redis.execute_command('MIGRATE', to_host, to_port, key, to_db, timeout) + from_redis.execute_command( + 'MIGRATE', + to_host, + to_port, + key, + to_db, + timeout + ) except redis.exceptions.ResponseError as e: if 'ERR Target key name is busy' not in str(e): raise e diff --git a/data/scripts/migrator/usage_mysql.py b/data/scripts/migrator/usage_mysql.py index 75e6b334d..d558f6e09 100755 --- a/data/scripts/migrator/usage_mysql.py +++ b/data/scripts/migrator/usage_mysql.py @@ -12,11 +12,11 @@ user = 'root' password = 'CGRateS.org' config = { - 'user': user, - 'password': password, - 'host': host, - 'port': port, - 'database': database, + 'user': user, + 'password': password, + 'host': host, + 'port': port, + 'database': database, } print('Connecting to MySQL...') @@ -24,13 +24,24 @@ cnx = mysql.connector.connect(**config) cursor = cnx.cursor() print('Renaming old column...') -cursor.execute('ALTER TABLE cdrs CHANGE COLUMN `usage` `usage_old` DECIMAL(30,9)') +cursor.execute( + ( + 'ALTER TABLE cdrs' + ' CHANGE COLUMN `usage` `usage_old`' + ' DECIMAL(30,9)' + ) + ) print('Adding new column...') cursor.execute('ALTER TABLE cdrs ADD `usage` DECIMAL(30)') print('Setting new values...') -cursor.execute('UPDATE cdrs SET `usage` = `usage_old` * 1000000000 WHERE usage_old IS NOT NULL') +cursor.execute( + ( + 'UPDATE cdrs SET `usage` = `usage_old` * 1000000000' + ' WHERE usage_old IS NOT NULL' + ) + ) print('Deleting old column...') cursor.execute('ALTER TABLE cdrs DROP COLUMN usage_old') diff --git a/data/scripts/migrator/usage_postgres.py b/data/scripts/migrator/usage_postgres.py index f9aa7816f..e05df5b34 100644 --- a/data/scripts/migrator/usage_postgres.py +++ b/data/scripts/migrator/usage_postgres.py @@ -13,12 +13,12 @@ password = 'CGRateS.org' print('Connecting to PostgreSQL...') cnx = psycopg2.connect( - host=host, - port=port, - dbname=database, - user=user, - password=password - ) + host=host, + port=port, + dbname=database, + user=user, + password=password + ) cursor = cnx.cursor() print('Renaming old column...') @@ -28,7 +28,12 @@ print('Adding new column...') cursor.execute('ALTER TABLE cdrs ADD usage NUMERIC(30)') print('Setting new values...') -cursor.execute('UPDATE cdrs SET usage = usage_old * 1000000000 WHERE usage_old IS NOT NULL') +cursor.execute( + ( + 'UPDATE cdrs SET usage = usage_old * 1000000000' + ' WHERE usage_old IS NOT NULL' + ) + ) print('Deleting old column...') cursor.execute('ALTER TABLE cdrs DROP COLUMN usage_old')