Commit ca7acefc authored by Julien Muchembled's avatar Julien Muchembled

storage: pass schema of tables to migration methods

parent 04f6d9c3
...@@ -161,13 +161,13 @@ class DatabaseManager(object): ...@@ -161,13 +161,13 @@ class DatabaseManager(object):
"The database can not be upgraded because you have unfinished" "The database can not be upgraded because you have unfinished"
" transactions. Use an older version of NEO to verify them.") " transactions. Use an older version of NEO to verify them.")
def migrate(self): def migrate(self, *args, **kw):
version = int(self.getConfiguration("version") or 0) version = int(self.getConfiguration("version") or 0)
if self.VERSION < version: if self.VERSION < version:
raise DatabaseFailure("The database can not be downgraded.") raise DatabaseFailure("The database can not be downgraded.")
while version < self.VERSION: while version < self.VERSION:
version += 1 version += 1
getattr(self, '_migrate%s' % version)() getattr(self, '_migrate%s' % version)(*args, **kw)
self.setConfiguration("version", version) self.setConfiguration("version", version)
def doOperation(self, app): def doOperation(self, app):
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from binascii import a2b_hex from binascii import a2b_hex
from collections import OrderedDict
import MySQLdb import MySQLdb
from MySQLdb import DataError, IntegrityError, \ from MySQLdb import DataError, IntegrityError, \
OperationalError, ProgrammingError OperationalError, ProgrammingError
...@@ -176,7 +177,7 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -176,7 +177,7 @@ class MySQLDatabaseManager(DatabaseManager):
if e.args[0] != NO_SUCH_TABLE: if e.args[0] != NO_SUCH_TABLE:
raise raise
def _migrate1(self): def _migrate1(self, _):
self._checkNoUnfinishedTransactions() self._checkNoUnfinishedTransactions()
self.query("DROP TABLE IF EXISTS ttrans") self.query("DROP TABLE IF EXISTS ttrans")
...@@ -184,32 +185,29 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -184,32 +185,29 @@ class MySQLDatabaseManager(DatabaseManager):
self._config.clear() self._config.clear()
q = self.query q = self.query
p = engine = self._engine p = engine = self._engine
schema_dict = OrderedDict()
if self.nonempty("config") is None: # The table "config" stores configuration
# The table "config" stores configuration # parameters which affect the persistent data.
# parameters which affect the persistent data. schema_dict['config'] = """CREATE TABLE %s (
q("""CREATE TABLE config ( name VARBINARY(255) NOT NULL PRIMARY KEY,
name VARBINARY(255) NOT NULL PRIMARY KEY, value VARBINARY(255) NULL
value VARBINARY(255) NULL ) ENGINE=""" + engine
) ENGINE=""" + engine)
self._setConfiguration("version", self.VERSION)
else:
self.migrate()
# The table "pt" stores a partition table. # The table "pt" stores a partition table.
q("""CREATE TABLE IF NOT EXISTS pt ( schema_dict['pt'] = """CREATE TABLE %s (
rid INT UNSIGNED NOT NULL, rid INT UNSIGNED NOT NULL,
nid INT NOT NULL, nid INT NOT NULL,
state TINYINT UNSIGNED NOT NULL, state TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (rid, nid) PRIMARY KEY (rid, nid)
) ENGINE=""" + engine) ) ENGINE=""" + engine
if self._use_partition: if self._use_partition:
p += """ PARTITION BY LIST (`partition`) ( p += """ PARTITION BY LIST (`partition`) (
PARTITION dummy VALUES IN (NULL))""" PARTITION dummy VALUES IN (NULL))"""
# The table "trans" stores information on committed transactions. # The table "trans" stores information on committed transactions.
q("""CREATE TABLE IF NOT EXISTS trans ( schema_dict['trans'] = """CREATE TABLE %s (
`partition` SMALLINT UNSIGNED NOT NULL, `partition` SMALLINT UNSIGNED NOT NULL,
tid BIGINT UNSIGNED NOT NULL, tid BIGINT UNSIGNED NOT NULL,
packed BOOLEAN NOT NULL, packed BOOLEAN NOT NULL,
...@@ -219,10 +217,10 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -219,10 +217,10 @@ class MySQLDatabaseManager(DatabaseManager):
ext BLOB NOT NULL, ext BLOB NOT NULL,
ttid BIGINT UNSIGNED NOT NULL, ttid BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (`partition`, tid) PRIMARY KEY (`partition`, tid)
) ENGINE=""" + p) ) ENGINE=""" + p
# The table "obj" stores committed object metadata. # The table "obj" stores committed object metadata.
q("""CREATE TABLE IF NOT EXISTS obj ( schema_dict['obj'] = """CREATE TABLE %s (
`partition` SMALLINT UNSIGNED NOT NULL, `partition` SMALLINT UNSIGNED NOT NULL,
oid BIGINT UNSIGNED NOT NULL, oid BIGINT UNSIGNED NOT NULL,
tid BIGINT UNSIGNED NOT NULL, tid BIGINT UNSIGNED NOT NULL,
...@@ -231,7 +229,7 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -231,7 +229,7 @@ class MySQLDatabaseManager(DatabaseManager):
PRIMARY KEY (`partition`, tid, oid), PRIMARY KEY (`partition`, tid, oid),
KEY (`partition`, oid, tid), KEY (`partition`, oid, tid),
KEY (data_id) KEY (data_id)
) ENGINE=""" + p) ) ENGINE=""" + p
if engine == "TokuDB": if engine == "TokuDB":
engine += " compression='tokudb_uncompressed'" engine += " compression='tokudb_uncompressed'"
...@@ -239,21 +237,21 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -239,21 +237,21 @@ class MySQLDatabaseManager(DatabaseManager):
# The table "data" stores object data. # The table "data" stores object data.
# We'd like to have partial index on 'hash' column (e.g. hash(4)) # We'd like to have partial index on 'hash' column (e.g. hash(4))
# but 'UNIQUE' constraint would not work as expected. # but 'UNIQUE' constraint would not work as expected.
q("""CREATE TABLE IF NOT EXISTS data ( schema_dict['data'] = """CREATE TABLE %%s (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
hash BINARY(20) NOT NULL, hash BINARY(20) NOT NULL,
compression TINYINT UNSIGNED NULL, compression TINYINT UNSIGNED NULL,
value MEDIUMBLOB NOT NULL%s value MEDIUMBLOB NOT NULL%s
) ENGINE=%s""" % (""", ) ENGINE=%s""" % (""",
UNIQUE (hash, compression)""" if dedup else "", engine)) UNIQUE (hash, compression)""" if dedup else "", engine)
q("""CREATE TABLE IF NOT EXISTS bigdata ( schema_dict['bigdata'] = """CREATE TABLE %s (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
value MEDIUMBLOB NOT NULL value MEDIUMBLOB NOT NULL
) ENGINE=""" + engine) ) ENGINE=""" + engine
# The table "ttrans" stores information on uncommitted transactions. # The table "ttrans" stores information on uncommitted transactions.
q("""CREATE TABLE IF NOT EXISTS ttrans ( schema_dict['ttrans'] = """CREATE TABLE %s (
`partition` SMALLINT UNSIGNED NOT NULL, `partition` SMALLINT UNSIGNED NOT NULL,
tid BIGINT UNSIGNED, tid BIGINT UNSIGNED,
packed BOOLEAN NOT NULL, packed BOOLEAN NOT NULL,
...@@ -262,17 +260,26 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -262,17 +260,26 @@ class MySQLDatabaseManager(DatabaseManager):
description BLOB NOT NULL, description BLOB NOT NULL,
ext BLOB NOT NULL, ext BLOB NOT NULL,
ttid BIGINT UNSIGNED NOT NULL ttid BIGINT UNSIGNED NOT NULL
) ENGINE=""" + engine) ) ENGINE=""" + engine
# The table "tobj" stores uncommitted object metadata. # The table "tobj" stores uncommitted object metadata.
q("""CREATE TABLE IF NOT EXISTS tobj ( schema_dict['tobj'] = """CREATE TABLE %s (
`partition` SMALLINT UNSIGNED NOT NULL, `partition` SMALLINT UNSIGNED NOT NULL,
oid BIGINT UNSIGNED NOT NULL, oid BIGINT UNSIGNED NOT NULL,
tid BIGINT UNSIGNED NOT NULL, tid BIGINT UNSIGNED NOT NULL,
data_id BIGINT UNSIGNED NULL, data_id BIGINT UNSIGNED NULL,
value_tid BIGINT UNSIGNED NULL, value_tid BIGINT UNSIGNED NULL,
PRIMARY KEY (tid, oid) PRIMARY KEY (tid, oid)
) ENGINE=""" + engine) ) ENGINE=""" + engine
if self.nonempty('config') is None:
q(schema_dict.pop('config') % 'config')
self._setConfiguration('version', self.VERSION)
else:
self.migrate(schema_dict)
for table, schema in schema_dict.iteritems():
q(schema % ('IF NOT EXISTS ' + table))
self._uncommitted_data.update(q("SELECT data_id, count(*)" self._uncommitted_data.update(q("SELECT data_id, count(*)"
" FROM tobj WHERE data_id IS NOT NULL GROUP BY data_id")) " FROM tobj WHERE data_id IS NOT NULL GROUP BY data_id"))
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from collections import OrderedDict
import os import os
import sqlite3 import sqlite3
from hashlib import sha1 from hashlib import sha1
...@@ -112,7 +113,7 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -112,7 +113,7 @@ class SQLiteDatabaseManager(DatabaseManager):
if not e.args[0].startswith("no such table:"): if not e.args[0].startswith("no such table:"):
raise raise
def _migrate1(self): def _migrate1(self, *_):
self._checkNoUnfinishedTransactions() self._checkNoUnfinishedTransactions()
self.query("DROP TABLE IF EXISTS ttrans") self.query("DROP TABLE IF EXISTS ttrans")
...@@ -123,27 +124,26 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -123,27 +124,26 @@ class SQLiteDatabaseManager(DatabaseManager):
# relatively slow; unit tests enables the UNSAFE boolean flag. # relatively slow; unit tests enables the UNSAFE boolean flag.
self._config.clear() self._config.clear()
q = self.query q = self.query
schema_dict = OrderedDict()
index_dict = {}
if self.nonempty("config") is None: # The table "config" stores configuration
# The table "config" stores configuration # parameters which affect the persistent data.
# parameters which affect the persistent data. schema_dict['config'] = """CREATE TABLE %s (
q("CREATE TABLE IF NOT EXISTS config (" name TEXT NOT NULL PRIMARY KEY,
" name TEXT NOT NULL PRIMARY KEY," value TEXT)
" value TEXT)") """
self._setConfiguration("version", self.VERSION)
else:
self.migrate()
# The table "pt" stores a partition table. # The table "pt" stores a partition table.
q("""CREATE TABLE IF NOT EXISTS pt ( schema_dict['pt'] = """CREATE TABLE %s (
rid INTEGER NOT NULL, rid INTEGER NOT NULL,
nid INTEGER NOT NULL, nid INTEGER NOT NULL,
state INTEGER NOT NULL, state INTEGER NOT NULL,
PRIMARY KEY (rid, nid)) PRIMARY KEY (rid, nid))
""") """
# The table "trans" stores information on committed transactions. # The table "trans" stores information on committed transactions.
q("""CREATE TABLE IF NOT EXISTS trans ( schema_dict['trans'] = """CREATE TABLE %s (
partition INTEGER NOT NULL, partition INTEGER NOT NULL,
tid INTEGER NOT NULL, tid INTEGER NOT NULL,
packed BOOLEAN NOT NULL, packed BOOLEAN NOT NULL,
...@@ -153,38 +153,34 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -153,38 +153,34 @@ class SQLiteDatabaseManager(DatabaseManager):
ext BLOB NOT NULL, ext BLOB NOT NULL,
ttid INTEGER NOT NULL, ttid INTEGER NOT NULL,
PRIMARY KEY (partition, tid)) PRIMARY KEY (partition, tid))
""") """
# The table "obj" stores committed object metadata. # The table "obj" stores committed object metadata.
q("""CREATE TABLE IF NOT EXISTS obj ( schema_dict['obj'] = """CREATE TABLE %s (
partition INTEGER NOT NULL, partition INTEGER NOT NULL,
oid INTEGER NOT NULL, oid INTEGER NOT NULL,
tid INTEGER NOT NULL, tid INTEGER NOT NULL,
data_id INTEGER, data_id INTEGER,
value_tid INTEGER, value_tid INTEGER,
PRIMARY KEY (partition, tid, oid)) PRIMARY KEY (partition, tid, oid))
""") """
q("""CREATE INDEX IF NOT EXISTS _obj_i1 ON index_dict['obj'] = (
obj(partition, oid, tid) "CREATE INDEX %s ON %s(partition, oid, tid)",
""") "CREATE INDEX %s ON %s(data_id)")
q("""CREATE INDEX IF NOT EXISTS _obj_i2 ON
obj(data_id)
""")
# The table "data" stores object data. # The table "data" stores object data.
q("""CREATE TABLE IF NOT EXISTS data ( schema_dict['data'] = """CREATE TABLE %s (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
hash BLOB NOT NULL, hash BLOB NOT NULL,
compression INTEGER NOT NULL, compression INTEGER NOT NULL,
value BLOB NOT NULL) value BLOB NOT NULL)
""") """
if dedup: if dedup:
q("""CREATE UNIQUE INDEX IF NOT EXISTS _data_i1 ON index_dict['data'] = (
data(hash, compression) "CREATE UNIQUE INDEX %s ON %s(hash, compression)",)
""")
# The table "ttrans" stores information on uncommitted transactions. # The table "ttrans" stores information on uncommitted transactions.
q("""CREATE TABLE IF NOT EXISTS ttrans ( schema_dict['ttrans'] = """CREATE TABLE %s (
partition INTEGER NOT NULL, partition INTEGER NOT NULL,
tid INTEGER, tid INTEGER,
packed BOOLEAN NOT NULL, packed BOOLEAN NOT NULL,
...@@ -193,17 +189,28 @@ class SQLiteDatabaseManager(DatabaseManager): ...@@ -193,17 +189,28 @@ class SQLiteDatabaseManager(DatabaseManager):
description BLOB NOT NULL, description BLOB NOT NULL,
ext BLOB NOT NULL, ext BLOB NOT NULL,
ttid INTEGER NOT NULL) ttid INTEGER NOT NULL)
""") """
# The table "tobj" stores uncommitted object metadata. # The table "tobj" stores uncommitted object metadata.
q("""CREATE TABLE IF NOT EXISTS tobj ( schema_dict['tobj'] = """CREATE TABLE %s (
partition INTEGER NOT NULL, partition INTEGER NOT NULL,
oid INTEGER NOT NULL, oid INTEGER NOT NULL,
tid INTEGER NOT NULL, tid INTEGER NOT NULL,
data_id INTEGER, data_id INTEGER,
value_tid INTEGER, value_tid INTEGER,
PRIMARY KEY (tid, oid)) PRIMARY KEY (tid, oid))
""") """
if self.nonempty('config') is None:
q(schema_dict.pop('config') % 'config')
self._setConfiguration('version', self.VERSION)
else:
self.migrate(schema_dict, index_dict)
for table, schema in schema_dict.iteritems():
q(schema % ('IF NOT EXISTS ' + table))
for i, index in enumerate(index_dict.get(table, ()), 1):
q(index % ('IF NOT EXISTS _%s_i%s' % (table, i), table))
self._uncommitted_data.update(q("SELECT data_id, count(*)" self._uncommitted_data.update(q("SELECT data_id, count(*)"
" FROM tobj WHERE data_id IS NOT NULL GROUP BY data_id")) " FROM tobj WHERE data_id IS NOT NULL GROUP BY data_id"))
......
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