diff --git a/data/scripts/migrator/usage_mysql.py b/data/scripts/migrator/usage_mysql.py new file mode 100755 index 000000000..1401f1897 --- /dev/null +++ b/data/scripts/migrator/usage_mysql.py @@ -0,0 +1,40 @@ +#!/usr/bin/python + +# depends: +# ^ mysql (debian: python-mysql.connector) + +host = '127.0.0.1' +port = 3306 +database = 'cgrates' +user = 'root' +password = 'CGRateS.org' + +import mysql.connector + +config = { + 'user': user, + 'password': password, + 'host': host, + 'port': port, + 'database': database, +} + +print('Connecting to MySQL...') +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)') + +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') + +print('Deleting old column...') +cursor.execute('ALTER TABLEX cdrs DROP COLUMN usage_old') + +print('Closing MySQL connection...') +cnx.close() + diff --git a/data/scripts/migrator/usage_postgres.py b/data/scripts/migrator/usage_postgres.py new file mode 100644 index 000000000..621321f8a --- /dev/null +++ b/data/scripts/migrator/usage_postgres.py @@ -0,0 +1,40 @@ +#!/usr/bin/python + +# depends: +# ^ psycopg2 (debian: python-psycopg2) + +host = '127.0.0.1' +port = 5432 +database = 'cgrates' +user = 'cgrates' +password = 'CGRateS.org' + +import psycopg2 + +print('Connecting to PostgreSQL...') +cnx = psycopg2.connect( + host=host, + port=port, + dbname=database, + user=user, + password=password + ) +cursor = cnx.cursor() + +print('Renaming old column...') +cursor.execute('ALTER TABLE cdrs RENAME COLUMN usage to usage_old') + +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') + +print('Deleting old column...') +cursor.execute('ALTER TABLE cdrs DROP COLUMN usage_old') + +print('Commiting...') +cnx.commit() + +print('Closing PostgreSQL connection...') +cnx.close()