db.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from pathlib import Path
  2. import typer
  3. import config
  4. import database
  5. app = typer.Typer()
  6. cfg = config.Config()
  7. @app.command()
  8. def create(config_file: str):
  9. config_file = cfg.system_dir + f"\\SQL\\config\\{config_file}.json"
  10. database.create(config_file)
  11. @app.command()
  12. def compare(config_file: str):
  13. config_file = cfg.system_dir + f"\\SQL\\config\\{config_file}.json"
  14. database.compare(config_file)
  15. @app.command()
  16. def run(config_file: str, increment: str = "1", max: int = 5):
  17. """
  18. Executes a database operation using a specified configuration file.
  19. Args:
  20. config_file (str): The name of the configuration file (without extension) to use, located in the SQL config directory.
  21. increment (str, optional): Determines whether to increment; treated as True if "1", otherwise False. Defaults to "1".
  22. max (int, optional): The maximum number of operations to perform. Defaults to 5.
  23. Returns:
  24. None
  25. """
  26. config_file = cfg.system_dir + f"\\SQL\\config\\{config_file}.json"
  27. database.run(config_file, increment == "1", max)
  28. bcp_log()
  29. @app.command()
  30. def run_folder(folder: str):
  31. folder_list = [
  32. Path(folder),
  33. Path(cfg.system_dir + "\\SQL\\exec\\" + folder),
  34. Path(cfg.system_dir + "\\Export\\SQL\\" + folder),
  35. ]
  36. for f in folder_list:
  37. if f.exists() and f.is_dir():
  38. folder = str(f.resolve())
  39. break
  40. print(folder)
  41. for sql_file in Path(folder).glob("*.sql"):
  42. print(f"call sqlexec2.bat {sql_file}")
  43. @app.command()
  44. def schema():
  45. database.schema()
  46. @app.command()
  47. def bcp_log():
  48. logs_dir = cfg.system_dir + "\\SQL\\logs"
  49. if not Path(logs_dir).exists():
  50. logs_dir = cfg.system_dir + "\\SQL\\temp"
  51. output_file = cfg.log_dir + "\\bcp.csv.log"
  52. database.bcp_log(logs_dir, output_file)
  53. if __name__ == "__main__":
  54. app()