2
0
mirror of https://github.com/openvswitch/ovs synced 2025-09-02 23:35:27 +00:00

Implement database schema versioning.

As the database schema evolves, it might be useful to have an identifier
for the particular version in use.  This commit adds that feature.
This commit is contained in:
Ben Pfaff
2010-12-27 14:26:47 -08:00
parent 538c6dfab4
commit 8159b984dc
17 changed files with 255 additions and 18 deletions

View File

@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import sys
from ovs.db import error
@@ -21,8 +22,9 @@ from ovs.db import types
class DbSchema(object):
"""Schema for an OVSDB database."""
def __init__(self, name, tables):
def __init__(self, name, version, tables):
self.name = name
self.version = version
self.tables = tables
# Validate that all ref_tables refer to the names of tables
@@ -36,9 +38,16 @@ class DbSchema(object):
def from_json(json):
parser = ovs.db.parser.Parser(json, "database schema")
name = parser.get("name", ['id'])
version = parser.get_optional("version", [unicode])
parser.get_optional("cksum", [unicode])
tablesJson = parser.get("tables", [dict])
parser.finish()
if (version is not None and
not re.match('[0-9]+\.[0-9]+\.[0-9]+$', version)):
raise error.Error("schema version \"%s\" not in format x.y.z"
% version)
tables = {}
for tableName, tableJson in tablesJson.iteritems():
if tableName.startswith('_'):
@@ -48,13 +57,16 @@ class DbSchema(object):
raise error.Error("name must be a valid id", json)
tables[tableName] = TableSchema.from_json(tableJson, tableName)
return DbSchema(name, tables)
return DbSchema(name, version, tables)
def to_json(self):
tables = {}
for table in self.tables.itervalues():
tables[table.name] = table.to_json()
return {"name": self.name, "tables": tables}
json = {"name": self.name, "tables": tables}
if self.version:
json["version"] = self.version
return json
def __check_ref_table(self, column, base, base_name):
if (base and base.type == types.UuidType and base.ref_table and
@@ -64,8 +76,8 @@ class DbSchema(object):
tag="syntax error")
class IdlSchema(DbSchema):
def __init__(self, name, tables, idlPrefix, idlHeader):
DbSchema.__init__(self, name, tables)
def __init__(self, name, version, tables, idlPrefix, idlHeader):
DbSchema.__init__(self, name, version, tables)
self.idlPrefix = idlPrefix
self.idlHeader = idlHeader
@@ -80,7 +92,8 @@ class IdlSchema(DbSchema):
del subjson["idlHeader"]
schema = DbSchema.from_json(subjson)
return IdlSchema(schema.name, schema.tables, idlPrefix, idlHeader)
return IdlSchema(schema.name, schema.version, schema.tables,
idlPrefix, idlHeader)
class TableSchema(object):
def __init__(self, name, columns, mutable=True, max_rows=sys.maxint):