You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

80 lines
2.6 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. """
  23. Sync sql files (that contain triggers, stored procs etc) into the database
  24. calling sync will walk through all .sql files in the modules file structure
  25. and execute them if they are not executed or their timestamps have changed
  26. All sql timestamps will be saved in a table '__sql_timestamps'
  27. """
  28. # modules path
  29. import webnotes
  30. import webnotes.defs
  31. def get_sql_files():
  32. """
  33. Returns list of .sql files from
  34. """
  35. import os
  36. ret = []
  37. for walk_tuple in os.walk(webnotes.defs.modules_path):
  38. if os.path.split(walk_tuple[0])[-1]=='doctype':
  39. for sql_file in filter(lambda x: x.endswith('.sql'), walk_tuple[2]):
  40. ret.append[os.path.join(walk_tuple[0], sql_file)]
  41. return ret
  42. def run_sql_file(fn):
  43. """
  44. Checks if timestamp matches, if not runs it
  45. """
  46. from webnotes.modules import ModuleFile
  47. mf = ModuleFile(fn)
  48. if mf.is_new():
  49. webnotes.conn.sql(mf.read())
  50. mf.update()
  51. def get_sql_timestamp(fn):
  52. """
  53. Returns the last updated timestamp of the sql file
  54. from the __sql_timestamps table. If the table does not
  55. exist, it will create it
  56. """
  57. try:
  58. ts = webnotes.conn.sql("select tstamp from __sql_timestamp where file_name=%s", fn)
  59. if ts:
  60. return ts[0][0]
  61. except Exception, e:
  62. if e.args[0]==1147:
  63. # create the table
  64. webnotes.conn.commit()
  65. webnotes.conn.sql("""
  66. create table __sql_timestamp (
  67. file_name varchar(320) primary key,
  68. tstamp varchar(40))""")
  69. webnotes.conn.begin()
  70. else:
  71. raise e
  72. def update_sql_timestamp(fn, ts):
  73. pass