Commit 0b8bfba3 authored by Julien Muchembled's avatar Julien Muchembled

storage: do not recreate tables when clearing database at exit

parent 1745d6ac
...@@ -81,7 +81,7 @@ class Application(object): ...@@ -81,7 +81,7 @@ class Application(object):
# ready is True when operational and got all informations # ready is True when operational and got all informations
self.ready = False self.ready = False
self.dm.setup(reset=config.getReset()) self.dm.setup(self, reset=config.getReset())
self.loadConfiguration() self.loadConfiguration()
# force node uuid from command line argument, for testing purpose only # force node uuid from command line argument, for testing purpose only
...@@ -400,6 +400,7 @@ class Application(object): ...@@ -400,6 +400,7 @@ class Application(object):
except PrimaryFailure: except PrimaryFailure:
pass pass
# clear database to avoid polluting the cluster at restart # clear database to avoid polluting the cluster at restart
self.dm.setup(reset=erase) if erase:
self.dm.erase()
logging.info("Application has been asked to shut down") logging.info("Application has been asked to shut down")
sys.exit() sys.exit()
...@@ -34,17 +34,21 @@ class DatabaseManager(object): ...@@ -34,17 +34,21 @@ class DatabaseManager(object):
"""Called during instanciation, to process database parameter.""" """Called during instanciation, to process database parameter."""
pass pass
def setup(self, reset = 0): def setup(self, app, reset=0):
"""Set up a database """Set up a database, discarding existing data first if reset is True
"""
if reset:
self.erase()
self._setup(app)
def _setup(self, app):
"""To be overriden by the backend to set up a database
It must recover self._uncommitted_data from temporary object table. It must recover self._uncommitted_data from temporary object table.
_uncommitted_data is a dict containing refcounts to data of _uncommitted_data is a dict containing refcounts to data of
write-locked objects, except in case of undo, where the refcount is write-locked objects, except in case of undo, where the refcount is
increased later, when the object is read-locked. increased later, when the object is read-locked.
Keys are data ids and values are number of references. Keys are data ids and values are number of references.
If reset is true, existing data must be discarded and
self._uncommitted_data must be an empty dict.
""" """
raise NotImplementedError raise NotImplementedError
......
...@@ -135,13 +135,13 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -135,13 +135,13 @@ class MySQLDatabaseManager(DatabaseManager):
"""Escape special characters in a string.""" """Escape special characters in a string."""
return self.conn.escape_string(s) return self.conn.escape_string(s)
def setup(self, reset = 0): def erase(self):
self.query(
"DROP TABLE IF EXISTS config, pt, trans, obj, data, ttrans, tobj")
def _setup(self, app):
self._config.clear() self._config.clear()
q = self.query q = self.query
if reset:
q('DROP TABLE IF EXISTS config, pt, trans, obj, data, ttrans, tobj')
# The table "config" stores configuration parameters which affect the # The table "config" stores configuration parameters which affect the
# persistent data. # persistent data.
q("""CREATE TABLE IF NOT EXISTS config ( q("""CREATE TABLE IF NOT EXISTS config (
......
...@@ -101,14 +101,13 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -101,14 +101,13 @@ class SQLiteDatabaseManager(DatabaseManager):
else: else:
query = property(lambda self: self.conn.execute) query = property(lambda self: self.conn.execute)
def setup(self, reset = 0): def erase(self):
for t in 'config', 'pt', 'trans', 'obj', 'data', 'ttrans', 'tobj':
self.query('DROP TABLE IF EXISTS ' + t)
def _setup(self, app):
self._config.clear() self._config.clear()
q = self.query q = self.query
if reset:
for t in 'config', 'pt', 'trans', 'obj', 'data', 'ttrans', 'tobj':
q('DROP TABLE IF EXISTS ' + t)
# The table "config" stores configuration parameters which affect the # The table "config" stores configuration parameters which affect the
# persistent data. # persistent data.
q("""CREATE TABLE IF NOT EXISTS config ( q("""CREATE TABLE IF NOT EXISTS config (
......
...@@ -53,7 +53,7 @@ class StorageDBTests(NeoUnitTestBase): ...@@ -53,7 +53,7 @@ class StorageDBTests(NeoUnitTestBase):
self._db = db = self.getDB(reset) self._db = db = self.getDB(reset)
else: else:
if reset: if reset:
db.setup(reset) db.setup(None, reset)
else: else:
try: try:
n = db.getNumPartitions() n = db.getNumPartitions()
......
...@@ -31,7 +31,7 @@ class StorageMySQSLdbTests(StorageDBTests): ...@@ -31,7 +31,7 @@ class StorageMySQSLdbTests(StorageDBTests):
# db manager # db manager
database = '%s@%s' % (NEO_SQL_USER, NEO_SQL_DATABASE) database = '%s@%s' % (NEO_SQL_USER, NEO_SQL_DATABASE)
db = MySQLDatabaseManager(database, 0) db = MySQLDatabaseManager(database, 0)
db.setup(reset) db.setup(None, reset)
return db return db
def test_MySQLDatabaseManagerInit(self): def test_MySQLDatabaseManagerInit(self):
......
...@@ -22,7 +22,7 @@ class StorageSQLiteTests(StorageDBTests): ...@@ -22,7 +22,7 @@ class StorageSQLiteTests(StorageDBTests):
def getDB(self, reset=0): def getDB(self, reset=0):
db = SQLiteDatabaseManager(':memory:', 0) db = SQLiteDatabaseManager(':memory:', 0)
db.setup(reset) db.setup(None, reset)
return db return db
del StorageDBTests del StorageDBTests
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment