25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

58 lines
1.5 KiB

  1. """
  2. Sync sql files (that contain triggers, stored procs etc) into the database
  3. calling sync will walk through all .sql files in the modules file structure
  4. and execute them if they are not executed or their timestamps have changed
  5. All sql timestamps will be saved in a table '__sql_timestamps'
  6. """
  7. # modules path
  8. import webnotes
  9. import webnotes.defs
  10. def get_sql_files():
  11. """
  12. Returns list of .sql files from
  13. """
  14. import os
  15. ret = []
  16. for walk_tuple in os.walk(webnotes.defs.modules_path):
  17. if os.path.split(walk_tuple[0])[-1]=='doctype':
  18. for sql_file in filter(lambda x: x.endswith('.sql'), walk_tuple[2]):
  19. ret.append[os.path.join(walk_tuple[0], sql_file)]
  20. return ret
  21. def run_sql_file(fn):
  22. """
  23. Checks if timestamp matches, if not runs it
  24. """
  25. from webnotes.modules import ModuleFile
  26. mf = ModuleFile(fn)
  27. if mf.is_new():
  28. webnotes.conn.sql(mf.read())
  29. mf.update()
  30. def get_sql_timestamp(fn):
  31. """
  32. Returns the last updated timestamp of the sql file
  33. from the __sql_timestamps table. If the table does not
  34. exist, it will create it
  35. """
  36. try:
  37. ts = webnotes.conn.sql("select tstamp from __sql_timestamp where file_name=%s", fn)
  38. if ts:
  39. return ts[0][0]
  40. except Exception, e:
  41. if e.args[0]==1147:
  42. # create the table
  43. webnotes.conn.commit()
  44. webnotes.conn.sql("""
  45. create table __sql_timestamp (
  46. file_name varchar(320) primary key,
  47. tstamp varchar(40))""")
  48. webnotes.conn.begin()
  49. else:
  50. raise e
  51. def update_sql_timestamp(fn, ts):
  52. pass