Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- db_host_src='localhost'
- db_port_src='5432'
- db_user_src='src'
- db_password_src='src'
- db_host_dest='src'
- db_port_dest='src'
- db_user_dest='src'
- db_password_dest='src'
- db_source='pronova.v110.mylocal.com'
- db_dest='springdomain.v110.mylocal.com'
- import psycopg2
- try:
- conn_src = psycopg2.connect(dbname=db_source, user=db_user_src, host=db_host_src, password=db_password_src)
- cur_src = conn_src.cursor()
- #
- except:
- print ("I am unable to connect to the database %s" %db_source)
- try:
- conn_dest = psycopg2.connect(dbname=db_dest, user=db_user_dest, host=db_host_dest, password=db_password_dest)
- cur_dest = conn_dest.cursor()
- #
- except:
- print ("I am unable to connect to the database %s" %db_dest)
- def update_dest_refeneces_from_src(src_table, dest_table, src_col, dest_col, models={}):
- # get all records from source table
- cur_dest.execute("alter table %s add column IF NOT EXISTS data_set_bkp_col int;" % 'res_partner')
- query_src = "select %s, %s from %s;" %(src_col, 'id', src_table)
- cur_src.execute(query_src)
- result = cur_src.fetchall()
- updated = False
- for row in result:
- #get id from destination DB
- query_dest = "select id from %s where %s='%s';" % (dest_table, dest_col,row[0])
- cur_dest.execute(query_dest)
- record = cur_dest.fetchall()
- for model, column in models.items():
- if not updated:
- cur_dest.execute("alter table %s add column IF NOT EXISTS data_set_bkp_col int;" % model)
- cur_dest.execute("update %s set data_set_bkp_col=%s;" % (model,column))
- updated = True
- #update only if you find the record in destation DB
- if len(record):
- update_query = "update %s set %s='%s' where id='%s'" %(model, column,record[0][0], row[1])
- # print (update_query)
- cur_dest.execute(update_query)
- else:
- print ("No record found for.......................",row, model, column)
- print ("FInished Sync!!!")
- return True
- #update_dest_refeneces_from_src(res_country, res_country, code, code, models = ['res_users', 'res_partners', 'hr_employee'])
- # the above call will match res_country from source DB to res_coiuntry in dest DB with column code
- # it will update models in given models list
- update_dest_refeneces_from_src('res_country', 'res_country', 'code', 'code', models={'res_partner': 'country_id'}) # 'hr_employee': 'country_id
- conn_dest.commit()
- cur_src.close()
- cur_dest.close()
- conn_dest.close()
- conn_src.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement