Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.58 KB | None | 0 0
  1. """
  2. Fichero         diffdbpg.py
  3. Funcionalidad   Comparar dos bases de datos en postgres para detectar diferencias de estructura de datos
  4. Autor           Alexander Olivares
  5. Email           olivaresa@cantv.net
  6. Fecha           30/05/2007
  7. """
  8.  
  9. """
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  23. """
  24.  
  25. import sys
  26. import string
  27. from pyPgSQL import PgSQL
  28. from pyPgSQL import libpq
  29.  
  30. #Base de datos 1 que se presume mas actualizada
  31. db1="ad_metro2"            # nombre de base de datos
  32. user1="postgres"             # usuario de base de datos
  33. password1="zidane"         # password de base de datos
  34. host1="localhost"       # host base de datos
  35.  
  36. #Base de datos 2 que se presume desactualizada
  37. db2="ad_metro"          # nombre de base de datos
  38. user2="postgres"             # usuario de base de datos
  39. password2="zidane"         # password de base de datos
  40. host2="localhost"       # host base de datos
  41.  
  42. conex1= None
  43. conex2= None
  44.  
  45. #Consulta para abstraer datos sobre las base de datos
  46. query1 = "select table_schema as esquema,table_name as tabla,column_name as columna,data_type as tipo from information_schema.columns where table_catalog='" + db1 + "' and table_schema<>'pg_catalog' and  table_schema<>'information_schema' order by table_schema,table_name limit 20 OFFSET 0"
  47. query2 = "select table_schema as esquema,table_name as tabla,column_name as columna,data_type as tipo from information_schema.columns where table_catalog='" + db2 + "' and table_schema<>'pg_catalog' and  table_schema<>'information_schema' order by table_schema,table_name limit 20 OFFSET 0"
  48.  
  49. #funcion para hacer la coneccion a base de datos
  50. def conectar(db,user,password,host):
  51.         return PgSQL.connect(database=db,user=user,password=password,host=host)
  52.  
  53. try:
  54.     conex1 = conectar(db1,user1,password1,host1)
  55.     conex2 = conectar(db2,user2,password2,host2)
  56. except Exception, error:
  57.     print error
  58.     sys.exit()
  59.  
  60.  
  61. #verifica si esta conectado a ambas bases de datos
  62. if conex1 and conex2:
  63.         esquema=""
  64.         tabla=""
  65.         diferencias=0
  66.        
  67.         cr1 = conex1.cursor()
  68.         cr1.execute(query1)
  69.         cr2 = conex2.cursor()
  70.         cr2.execute(query2)
  71.        
  72.         matriz1 = cr1.fetchall()
  73.         matriz2 = cr2.fetchall()
  74.        
  75.         print
  76.         print "Comparando base de datos '%s' en el host '%s' con base de datos '%s' en el host '%s'" %(db1,host1,db2,host2)
  77.         print
  78.         print "Diferencias:"
  79.         print
  80.  
  81.         for record1 in matriz1:
  82.                 dato=None
  83.                 for record2 in matriz2:
  84.                         #print "r1=", record1, " - r2=", record2
  85.                         if cmp(record1,record2) == 0:
  86.                                
  87.                                 query = "SELECT a.conname, a.conkey FROM pg_constraint a, pg_class b WHERE a.conrelid = b.oid and a.contype = 'p' and b.relname = '"+record1.tabla+"'"
  88.                                 cr1.execute(query)
  89.                                 result = cr1.fetchall()
  90.                                 for fila in result:
  91.                                         tid = fila[0]
  92.                                         idpos = fila[1]
  93.  
  94.                                 query = "select column_name as columna from information_schema.columns where table_catalog='"+db1+"' and table_schema<>'pg_catalog' and  table_schema<>'information_schema' order by table_schema, table_name"
  95.                                 cr1.execute(query)
  96.                                 result = cr1.fetchall()
  97.                                 for fila in result:
  98.                                         campo = fila
  99. ##########                      #HERE IS THE PROBLEM
  100.                                 qbase1 = "SELECT * FROM " + record1.tabla
  101.                                 cr1.execute(qbase1)
  102.                                 result = cr1.fetchall()
  103.  
  104.                                 for fila in result:
  105.                                         qbase2 = "SELECT * FROM " + record2.tabla + " a WHERE a." + tid + " = %s" % fila[idpos[0]-1]
  106.                                         cr2.execute(qbase2)
  107.                                         result2 = cr2.fetchall()
  108.                                         for fila2 in result2:
  109.                                                 for ii in range(0,len(fila2)):
  110.                                                         if fila[ii] <> fila2[ii]:
  111.                                                                 print "Diferencia Columna: %s" % idpos[0]
  112.                                                                 print "Valor en la base de datos " + db1 +": %s" % fila[ii]
  113.                                                                 print "Valor en la base de datos " + db2 +": %s" % fila2[ii]
  114.                                                 break
  115.                                
  116.                                 dato=1
  117.                                 break
  118.                 if(dato==None):
  119.                         #dic = {record1.esquema : {record1.tabla : [record1.columna,record1.tipo] } }
  120.                         if esquema <> record1.esquema:
  121.                                 esquema = record1.esquema
  122.                                 print "+ %20s" % (string.ljust(esquema,20))
  123.                         if tabla <> record1.tabla:
  124.                                 tabla = record1.tabla
  125.                                 print "%20s - %20s" % ("",string.ljust(tabla,20))
  126.                        
  127.                         print "%60s * %30s ,%15s" % ("",string.ljust(record1.columna,30),string.ljust(record1.tipo,15))
  128.                         diferencias += 1
  129.  
  130.         print "%s diferencias encontradas" % diferencias
  131.         print "Leyenda:"
  132.         print "+ = Esquemas"
  133.         print "- = Tablas"
  134.         print "* = Campos"
  135.         print
  136.         print
  137.         print "Corriendo en sistema operativo %s" % sys.platform
  138.  
  139.         cr1.close()
  140.         cr2.close()
  141.         conex1.close()
  142.         conex2.close()
  143.  
  144. else:
  145.         print "No se pudo conectar a las bases de datos...!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement