db_create.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import sqlite3
  2. import pandas as pd
  3. from sqlalchemy import create_engine, inspect
  4. from threading import Thread
  5. csv_file = "CARLO.csv"
  6. clients = {'1': "M und S Fahrzeughandel GmbH" }
  7. client_db = "1"
  8. date_filter = "'2018-01-01'"
  9. source_dsn = { 'user': "sa", 'pass': "Mffu3011#", 'server': "GC-SERVER1\\GLOBALCUBE", 'database': "DE0017" }
  10. source_schema = "dbo"
  11. target_dsn = { 'user': "sa", 'pass': "Mffu3011#", 'server': "GC-SERVER1\\GLOBALCUBE", 'database': "CARLO2" }
  12. target_schema = "import"
  13. #stage_dir = "\\\\gc-server1\Austausch\\stage"
  14. stage_dir = "C:\\GlobalCube\\System\\CARLO\\Export\\stage"
  15. def db_import (select_query, source_db, current_table, target_db, target_schema):
  16. pd.read_sql(select_query, source_db).to_sql(current_table['target'], target_db, schema=target_schema, index=False, if_exists='append')
  17. def conn_string (dsn):
  18. return f"mssql+pyodbc://{dsn['user']}:{dsn['pass']}@{dsn['server']}/{dsn['database']}?driver=SQL+Server+Native+Client+11.0"
  19. def conn_params (dsn):
  20. return f"-S {dsn['server']} -d {dsn['database']} -U {dsn['user']} -P {dsn['pass']}"
  21. df = pd.read_csv(csv_file, sep=";", encoding="ansi")
  22. config = df[df['target'].notnull()]
  23. print(config.head())
  24. source_db = create_engine(conn_string(source_dsn))
  25. source_insp = inspect(source_db)
  26. source_tables = source_insp.get_table_names(schema=source_schema)
  27. source_tables_prefix = set([t.split("$")[0] for t in source_tables if "$" in t])
  28. print(source_tables_prefix)
  29. target_db = create_engine(conn_string(target_dsn))
  30. target_insp = inspect(target_db)
  31. target_tables = target_insp.get_table_names(schema=target_schema)
  32. for index, current_table in config.iterrows():
  33. with open(stage_dir + "\\batch\\" + current_table['target'] + ".bat", "w", encoding="cp850") as f:
  34. f.write("@echo off \n")
  35. f.write("rem ==" + current_table['target'] + "==\n")
  36. if not current_table['target'] in target_tables:
  37. f.write(f"echo Ziel-Tabelle '{current_table['target']}' existiert nicht!\n")
  38. continue
  39. f.write(f"del {stage_dir}\\{current_table['target']}*.* /Q /F >nul 2>nul \n")
  40. f.write(f"sqlcmd.exe {conn_params(target_dsn)} -p -Q \"TRUNCATE TABLE [{target_schema}].[{current_table['target']}]\" \n")
  41. target_insp_cols = target_insp.get_columns(current_table['target'], schema=target_schema)
  42. target_columns_list = [col['name'] for col in target_insp_cols]
  43. target_columns = set(target_columns_list)
  44. for client_db, prefix in clients.items():
  45. source_table = current_table['source'].format(prefix)
  46. if not source_table in source_tables:
  47. f.write(f"echo Quell-Tabelle '{source_table}' existiert nicht!\n")
  48. continue
  49. stage_csv = f"{stage_dir}\\{current_table['target']}_{client_db}.csv"
  50. if not pd.isnull(current_table['query']):
  51. select_query = current_table['query'].format(prefix, date_filter)
  52. else:
  53. select_query = f"SELECT T1.* FROM [{source_schema}].[{source_table}] T1 "
  54. if not pd.isnull(current_table['filter']):
  55. select_query += " WHERE " + current_table['filter'].format("", date_filter)
  56. source_insp_cols = source_insp.get_columns(source_table)
  57. source_columns = set([col['name'] for col in source_insp_cols])
  58. intersect = source_columns.intersection(target_columns)
  59. #print("Auf beiden Seiten: " + ";".join(intersect))
  60. diff1 = source_columns.difference(target_columns)
  61. if len(diff1) > 0:
  62. f.write("rem Nur in Quelle: " + ";".join(diff1) + "\n")
  63. diff2 = target_columns.difference(source_columns)
  64. if not "Client_DB" in diff2:
  65. f.write("echo Spalte 'Client_DB' fehlt!\n")
  66. continue
  67. diff2.remove("Client_DB")
  68. if len(diff2) > 0:
  69. f.write("rem Nur in Ziel: " + ";".join(diff2) + "\n")
  70. #select_columns = "T1.[" + "], T1.[".join(intersect) + "],"
  71. select_columns = ""
  72. for col in target_columns_list:
  73. if col in intersect:
  74. select_columns += "T1.[" + col + "], "
  75. elif col == "Client_DB":
  76. select_columns += "'" + client_db + "' as \\\"Client_DB\\\", "
  77. else:
  78. select_columns += "'' as \\\"" + col + "\\\", "
  79. select_query = select_query.replace("T1.*", select_columns[:-2])
  80. select_query = select_query.replace("%", "%%") # batch-Problem
  81. #print(select_query)
  82. f.write(f"bcp \"{select_query}\" queryout \"{stage_csv}\" {conn_params(source_dsn)} -c -C 65001 -e \"{stage_csv[:-4]}.queryout.log\" > \"{stage_csv[:-4]}.bcp1.log\" \n")
  83. f.write(f"type \"{stage_csv[:-4]}.bcp1.log\" | findstr -v \"1000\" \n")
  84. f.write(f"bcp [{target_schema}].[{current_table['target']}] in \"{stage_csv}\" {conn_params(target_dsn)} -c -C 65001 -e \"{stage_csv[:-4]}.in.log\" > \"{stage_csv[:-4]}.bcp2.log\" \n")
  85. f.write(f"type \"{stage_csv[:-4]}.bcp2.log\" | findstr -v \"1000\" \n")
  86. with open(stage_dir + "\\batch\\_all.bat", "w", encoding="cp850") as f:
  87. f.write("@echo off & cd /d %~dp0 \n")
  88. f.write(f"del {stage_dir}\\*.* /Q /F >nul 2>nul \n\n")
  89. for index, current_table in config.iterrows():
  90. f.write("echo ==" + current_table['target'] + "==\n")
  91. f.write("echo " + current_table['target'] + " >CON \n")
  92. f.write("call " + current_table['target'] + ".bat\n\n")