Commits

Andrew Davison committed 7cf1d98

Added a `clear()` method to the `RecordStore` interface, which not only deletes all data but also removes all structure (using DROP TABLE, etc.), to clear the way for an upgrade. In future, this would be better done via migrations (in Django 1.7 or using South). Fixes #189.

  • Participants
  • Parent commits 0a3bc5f

Comments (0)

Files changed (5)

File sumatra/commands.py

     project.sumatra_version = sumatra.__version__
     project.save()
     # upgrade the record store
+    project.record_store.clear()
     filename = "%s/records_export.json" % backup_dir
     if os.path.exists(filename):
         f = open(filename)

File sumatra/projects.py

         smt_dir = os.path.split(_get_project_file(self.path))[0]
         backup_dir = smt_dir + "_backup_%s" % datetime.now().strftime(TIMESTAMP_FORMAT)
         shutil.copytree(smt_dir, backup_dir)
+        # TODO: also backup the record store, if it is not contained in .smt ?
         return backup_dir
 
     def change_record_store(self, new_store):

File sumatra/recordstore/django_store/__init__.py

 
 
 from sumatra.recordstore.base import RecordStore
-from sumatra.recordstore import serialization
 import django.conf as django_conf
 from django.core import management
 import os
                 db_file = db['NAME']
                 if not os.path.exists(os.path.dirname(db_file)):
                     os.makedirs(os.path.dirname(db_file))
-                if not os.path.exists(db_file):
-                    management.call_command('syncdb', database=label, verbosity=0)
-                    print("Created Django record store using SQLite")
+                management.call_command('syncdb', database=label, verbosity=0)
+                print("Created Django record store using SQLite")
             else:
                 management.call_command('syncdb', database=label, verbosity=0)
                 print("Created Django record store using PostgreSQL")
         global db_config
         settings = django_conf.settings
         settings._wrapped = None
-        assert settings.configured == False
+        assert settings.configured is False
         db_config = DjangoConfiguration()
         if db_file:
             self.__init__(db_file)
         management.call_command('flush', database=self._db_label,
                                 interactive=False, verbosity=0)
 
+    def clear(self):
+        """
+        Drop all Sumatra-related tables from the database.
+
+        WARNING: this will delete all data. Make sure you have a backup first.
+        """
+        #management.call_command('sqlclear', 'django_store', database=self._db_label)  # this produces coloured output, need no_color option from Django 1.7
+        cmds = ["BEGIN;"] + ['DROP TABLE "django_store_{}";'.format(x)
+                             for x in ("record", "record_output_data", "record_input_data", "record_dependencies",
+                                       "record_platforms", "platforminformation", "datakey", "datastore", "launchmode",
+                                       "parameterset", "repository", "dependency", "executable", "project")] + ["COMMIT;"]
+        from django.db import connection
+        cur = connection.cursor()
+        for cmd in cmds:
+            cur.execute(cmd)
+
     def _dump(self, indent=2):
         """
         Dump the database contents to a JSON-encoded string

File sumatra/recordstore/http_store.py

 :license: CeCILL, see LICENSE for details.
 """
 
+from warnings import warn
 from sumatra.recordstore.base import RecordStore, RecordStoreAccessError
 from sumatra.recordstore import serialization
 import httplib2
         if not self.has_project(project_name):
             self.create_project(project_name)
         super(HttpRecordStore, self).sync(other, project_name)
+
+    def clear(self):
+        warn("Cannot clear a remote record store directly. Contact the record store administrator")

File sumatra/recordstore/shelve_store.py

 :license: CeCILL, see LICENSE for details.
 """
 
+import os
 from sumatra.recordstore.base import RecordStore
 from sumatra.recordstore import serialization
 import shelve
                 most_recent_timestamp = record.timestamp
                 most_recent = record.label
         return most_recent
+
+    def clear(self):
+        os.remove(self._shelf_name)