1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
1 # copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # |
3 # |
4 # This file is part of CubicWeb. |
4 # This file is part of CubicWeb. |
5 # |
5 # |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
805 #with open(backup_stack, 'w') as backup_stack_file: |
805 #with open(backup_stack, 'w') as backup_stack_file: |
806 # import traceback |
806 # import traceback |
807 # traceback.print_stack(file=backup_stack_file) |
807 # traceback.print_stack(file=backup_stack_file) |
808 return backup_file |
808 return backup_file |
809 |
809 |
810 def _new_repo(self, config): |
|
811 repo = super(SQLiteTestDataBaseHandler, self)._new_repo(config) |
|
812 install_sqlite_patch(repo.querier) |
|
813 return repo |
|
814 |
|
815 def _restore_database(self, backup_coordinates, _config): |
810 def _restore_database(self, backup_coordinates, _config): |
816 # remove database file if it exists ? |
811 # remove database file if it exists ? |
817 dbfile = self.absolute_dbfile() |
812 dbfile = self.absolute_dbfile() |
818 self._cleanup_database(dbfile) |
813 self._cleanup_database(dbfile) |
819 shutil.copy(backup_coordinates, dbfile) |
814 shutil.copy(backup_coordinates, dbfile) |
827 init_repository(self.config, interactive=False, |
822 init_repository(self.config, interactive=False, |
828 init_config=self.init_config) |
823 init_config=self.init_config) |
829 |
824 |
830 import atexit |
825 import atexit |
831 atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb) |
826 atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb) |
832 |
|
833 |
|
834 def install_sqlite_patch(querier): |
|
835 """This patch hotfixes the following sqlite bug : |
|
836 - http://www.sqlite.org/cvstrac/tktview?tn=1327,33 |
|
837 (some dates are returned as strings rather thant date objects) |
|
838 """ |
|
839 if hasattr(querier.__class__, '_devtools_sqlite_patched'): |
|
840 return # already monkey patched |
|
841 def wrap_execute(base_execute): |
|
842 def new_execute(*args, **kwargs): |
|
843 rset = base_execute(*args, **kwargs) |
|
844 if rset.description: |
|
845 found_date = False |
|
846 for row, rowdesc in zip(rset, rset.description): |
|
847 for cellindex, (value, vtype) in enumerate(zip(row, rowdesc)): |
|
848 if vtype in ('Date', 'Datetime') and isinstance(value, text_type): |
|
849 found_date = True |
|
850 value = value.rsplit('.', 1)[0] |
|
851 try: |
|
852 row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S') |
|
853 except Exception: |
|
854 row[cellindex] = strptime(value, '%Y-%m-%d') |
|
855 if vtype == 'Time' and isinstance(value, text_type): |
|
856 found_date = True |
|
857 try: |
|
858 row[cellindex] = strptime(value, '%H:%M:%S') |
|
859 except Exception: |
|
860 # DateTime used as Time? |
|
861 row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S') |
|
862 if vtype == 'Interval' and isinstance(value, int): |
|
863 found_date = True |
|
864 row[cellindex] = timedelta(0, value, 0) # XXX value is in number of seconds? |
|
865 if not found_date: |
|
866 break |
|
867 return rset |
|
868 return new_execute |
|
869 querier.__class__.execute = wrap_execute(querier.__class__.execute) |
|
870 querier.__class__._devtools_sqlite_patched = True |
|
871 |
|
872 |
827 |
873 |
828 |
874 HANDLERS = {} |
829 HANDLERS = {} |
875 |
830 |
876 def register_handler(handlerkls, overwrite=False): |
831 def register_handler(handlerkls, overwrite=False): |