Refactorize.

This commit is contained in:
Albert Cervera i Areny 2017-01-20 01:01:29 +01:00
parent 9aad9cbee0
commit 39d86ce5ef
2 changed files with 138 additions and 145 deletions

View File

@ -27,31 +27,20 @@ to_uninstall:
- project_unittest
- stock_delivery_note_jreport
to_install:
- account_invoice_tree
- asset_owner
- asset_shipment_work
- contract_revenue
- project
- project_asset
- project_certification
- project_invoice_milestone
- project_management
- project_management_with_sales
- project_management_with_purchase
- project_management_with_shipment_work
- project_menu_tree_remove
- project_invoice
- project_revenue
- project_product
- project_shipment_work
- project_state_by_buttons
- purchase_request
- sale_tree
#to_install:
before:
# Rename table name - 3.6 migration
- tables: account_financial_statement
- UPDATE ir_model_data SET module = 'aeat_303' WHERE module = 'aeat_303_es';
- UPDATE ir_model_data SET module = 'aeat_349' WHERE module = 'aeat_349_es';
- UPDATE ir_translation SET module = 'aeat_303' WHERE module = 'aeat_303_es';
- UPDATE ir_translation SET module = 'aeat_349' WHERE module = 'aeat_349_es';
- DELETE from ir_ui_view where module = '';
- DELETE from ir_ui_view where data = '' and module = 'account_payment_type';
- UPDATE ir_ui_view SET type = NULL WHERE inherit IS NOT NULL AND type IS NOT NULL;
- DELETE FROM ir_model_data WHERE model='stock.location' AND fs_id LIKE '%transit%'
- INSERT INTO stock_location (name, code, type, create_uid, create_date, "right", "left") VALUES ('Migration Drop Shipment', 'DROPMIGRATION', 'drop', 0, '2016-10-04 00:00:00', 0, 0);
- comment: # Rename table name - 3.6 migration
tables: account_financial_statement
query:
ALTER TABLE account_financial_statement_report_line_account
RENAME TO account_financial_statement_rep_lin_acco
@ -60,8 +49,8 @@ before:
ALTER TABLE account_financial_statement_report_line_account_id_seq
RENAME TO account_financial_statement_rep_lin_acco_id_seq
# [SQL]: Change tax sign for credit note (must be run before update):
- tables: account_tax_template
- comment: # [SQL]: Change tax sign for credit note (must be run before update)
tables: account_tax_template
query:
UPDATE account_tax_template
SET
@ -74,50 +63,54 @@ before:
credit_note_base_sign = credit_note_base_sign * -1,
credit_note_tax_sign = credit_note_tax_sign * -1;
# [SQL]: Fix amount second currency with:
- tables: account_move_line
- comment: # [SQL]: Fix amount second currency with:
tables: account_move_line
query:
UPDATE account_move_line
SET
amount_second_currency = (amount_second_currency * -1)
WHERE amount_second_currency IS NOT NULL
AND SIGN(amount_second_currency) != SIGN(debit - credit)
- DELETE FROM ir_model_data WHERE model='stock.location' AND fs_id LIKE '%transit%'
- tables: shipment_work
query: ALTER TABLE shipment_work RENAME COLUMN project TO work_project
- fields: sale_sale.project
query: ALTER TABLE sale_sale RENAME COLUMN project TO work_project
- UPDATE ir_model_data SET module = 'aeat_303' WHERE module = 'aeat_303_es';
- UPDATE ir_model_data SET module = 'aeat_349' WHERE module = 'aeat_349_es';
- UPDATE ir_translation SET module = 'aeat_303' WHERE module = 'aeat_303_es';
- UPDATE ir_translation SET module = 'aeat_349' WHERE module = 'aeat_349_es';
- DELETE from ir_ui_view where module = '';
- DELETE from ir_ui_view where data = '' and module = 'account_payment_type';
- UPDATE ir_ui_view SET type = NULL WHERE inherit IS NOT NULL AND type IS NOT NULL;
after:
- DELETE FROM ir_translation WHERE module = 'account_es';
- DELETE FROM ir_translation WHERE module = 'account_es_pyme';
# Mark all categories as accounting type
- UPDATE product_category SET accounting = True;
# DROP category COLUMN
- ALTER TABLE product_template DROP COLUMN category;
# Change login's length
- ALTER TABLE res_user_login_attempt ALTER COLUMN login TYPE character varying(512);
# Set reconciliation date
- UPDATE account_move_reconciliation SET date=create_date WHERE date IS NULL;
- ALTER TABLE account_move_reconciliation ALTER COLUMN date SET NOT NULL;
- UPDATE contract SET first_invoice_date=start_period_date WHERE
first_invoice_date IS NULL AND state = 'cancelled';
- ALTER TABLE asset ALTER COLUMN company SET NOT NULL;
- ALTER TABLE timesheet_line ALTER COLUMN duration SET NOT NULL;
- ALTER TABLE stock_shipment_in_return ALTER COLUMN supplier SET NOT NULL;
- ALTER TABLE contract ALTER COLUMN first_invoice_date SET NOT NULL;
- UPDATE asset SET company = 1 WHERE company IS NULL;
- comment: # Change login's length
query: ALTER TABLE res_user_login_attempt ALTER COLUMN login TYPE character varying(512);
- comment: # Mark all categories as accounting type
tables: product_category
query: UPDATE product_category SET accounting = True;
- comment: # DROP category COLUMN
tables: product_template
query: ALTER TABLE product_template DROP COLUMN category;
- comment: # Set reconciliation date
tables: account_move_reconciliation
query: UPDATE account_move_reconciliation SET date=create_date WHERE date IS NULL;
- tables: account_move_reconciliation
query: ALTER TABLE account_move_reconciliation ALTER COLUMN date SET NOT NULL;
- tables: contract
query: UPDATE contract SET first_invoice_date=start_period_date WHERE first_invoice_date IS NULL AND state = 'cancelled';
- tables: asset
query: ALTER TABLE asset ALTER COLUMN company SET NOT NULL;
- tables: timesheet_line
query: ALTER TABLE timesheet_line ALTER COLUMN duration SET NOT NULL;
- tables: stock_shipment_in_return
query: ALTER TABLE stock_shipment_in_return ALTER COLUMN supplier SET NOT NULL;
- tables: contract
query: ALTER TABLE contract ALTER COLUMN first_invoice_date SET NOT NULL;
- tables: asset
query: UPDATE asset SET company = 1 WHERE company IS NULL;
- script: convert_domain_rules.py
- tables: work_project
script: migration_project_product.py
- tables: project_work
script: purchase_work.py
- tables: shipment_work
script: migration_shipment_work.py
- tables: account_invoice_milestone
script: milestone_migration.py

176
upgrade
View File

@ -1,70 +1,50 @@
#!/usr/bin/env python
import os
import sys
import yaml
import argparse
from urlparse import urlparse
import psycopg2
import subprocess
import ConfigParser
from blessings import Terminal
t = Terminal()
parser = argparse.ArgumentParser(description='Upgrade a Tryton database.')
parser.add_argument('database', nargs=1, help='PostgreSQL database to upgrade')
parser.add_argument('-c', '--config', default='trytond.conf',
help='path to the trytond configuration file')
args = parser.parse_args()
database_name, = args.database
config_file = args.config
#host = args.host
#port = args.port
#user = args.user
#password = args.password
#if args.json and args.yaml:
#print('Cannot use both --json and --yaml parameters.')
#sys.exit(1)
#
#if not args.check and not args.list:
#print('One of --list or check arguments is required.')
#sys.exit(1)
config = yaml.load(open('upgrades/config.yml', 'r').read())
if os.path.exists('config.yml'):
override = yaml.load(open('config.yml', 'r').read())
config['to_install'] += override.get('to_install', [])
to_uninstall = config.get('to_uninstall')
#db = psycopg2.connect(dbname=database, host=host, port=port, user=user,
#password=password)
connection = psycopg2.connect(dbname=database_name)
cursor = connection.cursor()
def get_url():
config = ConfigParser.ConfigParser()
config.read(args.config)
url = urlparse(config.get('database', 'uri'))
return url
def run(*args):
#process = subprocess.Popen(args, stdout=subprocess.PIPE,
# stderr=subprocess.PIPE)
#process.wait()
#stdout = process.stdout.read()
#stderr = process.stderr.read()
print "RUNING: ", args
process = subprocess.Popen(args, stdout=subprocess.PIPE, bufsize=1)
for line in iter(process.stdout.readline, b''):
print line,
print 'RUNING:', ' '.join(args)
summary = []
process = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, bufsize=1)
#for line in iter(process.stdout.readline, b''):
for line in iter(process.stderr.readline, b''):
line = line.strip()
if 'ERROR' in line:
line = t.red(line)
summary.append(line)
elif 'WARNING' in line:
line = t.yellow(line)
summary.append(line)
print line
process.stdout.close()
process.wait()
#return process.returncode, stdout, stderr
if summary:
print t.bold('\nWARNING AND ERROR SUMMARY:')
print '\n'.join(summary)
return process.returncode
def execute(query, *args, **kwargs):
if not args:
args = kwargs
cursor.execute(query, args)
def run_trytond():
to_run = ['trytond/bin/trytond-admin', '-v', '-c', config_file]
to_install = config.get('to_install')
@ -74,16 +54,11 @@ def run_trytond():
to_run.append('--all')
to_run.append('-d')
to_run.append(database_name)
return run(*to_run)
def run_after():
for filename in os.listdir('upgrades/after'):
print 'Running %s...' % filename
run('upgrades/after/' + filename, database_name, args.config)
# INSERT INTO stock_location (name, code, type, create_uid, create_date, "right", "left") VALUES ('Migration Drop Shipment', 'DROPMIGRATION', 'drop', 0, '2016-10-04 00:00:00', 0, 0);
returncode = run(*to_run)
if returncode:
print t.red('Trytond update failed. Upgrade aborted.')
sys.exit(1)
return returncode
def table_exists(table):
execute('SELECT count(*) FROM information_schema.tables '
@ -96,8 +71,8 @@ def field_exists(field):
'WHERE table_name=%s AND column_name=%s', table, field)
return bool(cursor.fetchone()[0])
def uninstall_modules(to_uninstall):
def uninstall_modules():
to_uninstall = config.get('to_uninstall')
module_table = None
for table in ('ir_module_module', 'ir_module'):
if table_exists(table):
@ -130,20 +105,20 @@ def uninstall_modules(to_uninstall):
execute('DELETE FROM ir_ui_view WHERE module NOT IN (SELECT name FROM '
'%s)' % module_table)
def process_queries(queries):
if not queries:
def process_actions(actions):
if not actions:
return
for query in queries:
if isinstance(query, dict):
tables = query.get('tables', '')
fields = query.get('fields', '')
query = query.get('query')
for action in actions:
if isinstance(action, dict):
tables = action.get('tables', '')
fields = action.get('fields', '')
query = action.get('query')
script = action.get('script')
found = True
tables = tables.split()
for table in tables:
if not table_exists(table):
print t.red("TABLE '%s' NOT FOUND" % table)
print "TABLE '%s' NOT FOUND" % table
found = False
break
if not found:
@ -153,37 +128,62 @@ def process_queries(queries):
fields = fields.split()
for field in fields:
if not field_exists(field):
print t.red("FIELD '%s' NOT FOUND" % field)
print "FIELD '%s' NOT FOUND" % field
found = False
break
if not found:
continue
query = query.replace('%', '%%')
print query
execute(query)
else:
query = action
script = None
if query:
query = query.replace('%', '%%')
print query
execute(query)
if script:
run('upgrades/after/' + script, database_name, args.config)
print t.green('Uninstalling modules...')
uninstall_modules(to_uninstall)
parser = argparse.ArgumentParser(description='Upgrade a Tryton database.')
parser.add_argument('database', nargs=1, help='PostgreSQL database to upgrade')
parser.add_argument('-c', '--config', default='trytond.conf',
help='path to the trytond configuration file')
print t.green('Executing queries before update...')
process_queries(config.get('before'))
args = parser.parse_args()
database_name, = args.database
config_file = args.config
url = get_url()
config = yaml.load(open('upgrades/config.yml', 'r').read())
config.setdefault('to_uninstall', [])
config.setdefault('to_install', [])
if os.path.exists('upgrade.yml'):
override = yaml.load(open('upgrade.yml', 'r').read())
config['to_install'] += override.get('to_install', [])
if url.username:
connection = psycopg2.connect(dbname=database_name, host=url.hostname,
port=url.port, user=url.username, password=url.password)
else:
connection = psycopg2.connect(dbname=database_name)
cursor = connection.cursor()
print t.green('\nUninstalling modules...')
uninstall_modules()
print t.green('\nExecuting actions before update...')
process_actions(config.get('before'))
connection.commit()
print t.green('Updating trytond...')
print t.green('\nUpdating trytond...')
run_trytond()
print t.green('\nExecuting actions after update...')
process_actions(config.get('after'))
run_after()
#python ./upgrades/4.0/migration_project_product.py $1 $2
#python ./upgrades/4.0/migration_shipment_work.py $1 $2
#python ./upgrades/4.0/purchase_work.py $1 $2
#python ./upgrades/4.0/milestone_migration.py $1 $2
#python ./upgrades/4.0/convert_domain_rules.py $1 $2
print t.green('Executing queries after update...')
process_queries(config.get('after'))
connection.rollback()
connection.commit()