From 403e3a25f89ba9f2efb9a8c43e4e585f4af2ac67 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 8 Feb 2011 15:57:14 -0800 Subject: [PATCH] ovsdb-tool: New command "needs-conversion". --- ovsdb/ovsdb-tool.1.in | 7 +++++++ ovsdb/ovsdb-tool.c | 15 +++++++++++++++ ovsdb/ovsdb.c | 17 +++++++++++++++++ ovsdb/ovsdb.h | 3 +++ tests/ovsdb-tool.at | 22 ++++++++++++++++++++++ 5 files changed, 64 insertions(+) diff --git a/ovsdb/ovsdb-tool.1.in b/ovsdb/ovsdb-tool.1.in index 79bd2a6a..7f346095 100644 --- a/ovsdb/ovsdb-tool.1.in +++ b/ovsdb/ovsdb-tool.1.in @@ -19,6 +19,8 @@ ovsdb\-tool \- Open vSwitch database management utility \fBovsdb\-tool \fR[\fIoptions\fR] \fBconvert\fI db schema \fR[\fItarget\fR] .br +\fBovsdb\-tool \fR[\fIoptions\fR] \fBneeds\-conversion\fI db schema\fR +.br \fBovsdb\-tool \fR[\fIoptions\fR] \fBdb\-version\fI db\fR .br \fBovsdb\-tool \fR[\fIoptions\fR] \fBschema\-version\fI schema\fR @@ -76,6 +78,11 @@ ignored. Columns that exist in \fIschema\fR but not in \fIdb\fR are set to their default values. All of \fIschema\fR's constraints apply in full. . +.IP "\fBneeds\-conversion\fI db schema\fR" +Reads the schema embedded in \fIdb\fR and the standalone schema in +\fIschema\fR and compares them. If the schemas are the same, prints +\fBno\fR on stdout; if they differ, print \fByes\fR. +. .IP "\fBdb\-version\fI db\fR" .IQ "\fBschema\-version\fI schema\fR" Prints the version number in the schema embedded within the database diff --git a/ovsdb/ovsdb-tool.c b/ovsdb/ovsdb-tool.c index 3730e672..4f55b6ad 100644 --- a/ovsdb/ovsdb-tool.c +++ b/ovsdb/ovsdb-tool.c @@ -243,6 +243,20 @@ do_convert(int argc OVS_UNUSED, char *argv[]) ovsdb_schema_destroy(new_schema); } +static void +do_needs_conversion(int argc OVS_UNUSED, char *argv[]) +{ + const char *db_file_name = argv[1]; + const char *schema_file_name = argv[2]; + struct ovsdb_schema *schema1, *schema2; + + check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema1)); + check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema2)); + puts(ovsdb_schema_equal(schema1, schema2) ? "no" : "yes"); + ovsdb_schema_destroy(schema1); + ovsdb_schema_destroy(schema2); +} + static void do_db_version(int argc OVS_UNUSED, char *argv[]) { @@ -458,6 +472,7 @@ static const struct command all_commands[] = { { "create", 2, 2, do_create }, { "compact", 1, 2, do_compact }, { "convert", 2, 3, do_convert }, + { "needs-conversion", 2, 2, do_needs_conversion }, { "db-version", 1, 1, do_db_version }, { "db-cksum", 1, 1, do_db_cksum }, { "schema-version", 1, 1, do_schema_version }, diff --git a/ovsdb/ovsdb.c b/ovsdb/ovsdb.c index 46d06a0f..2a54a7b9 100644 --- a/ovsdb/ovsdb.c +++ b/ovsdb/ovsdb.c @@ -235,6 +235,23 @@ ovsdb_schema_to_json(const struct ovsdb_schema *schema) return json; } + +/* Returns true if 'a' and 'b' specify equivalent schemas, false if they + * differ. */ +bool +ovsdb_schema_equal(const struct ovsdb_schema *a, + const struct ovsdb_schema *b) +{ + /* This implementation is simple, stupid, and slow, but I doubt that it + * will ever require much maintenance. */ + struct json *ja = ovsdb_schema_to_json(a); + struct json *jb = ovsdb_schema_to_json(b); + bool equals = json_equal(ja, jb); + json_destroy(ja); + json_destroy(jb); + + return equals; +} static void ovsdb_set_ref_table(const struct shash *tables, diff --git a/ovsdb/ovsdb.h b/ovsdb/ovsdb.h index ae743bbc..834ff1a9 100644 --- a/ovsdb/ovsdb.h +++ b/ovsdb/ovsdb.h @@ -47,6 +47,9 @@ struct ovsdb_error *ovsdb_schema_from_json(struct json *, struct ovsdb_schema **) WARN_UNUSED_RESULT; struct json *ovsdb_schema_to_json(const struct ovsdb_schema *); + +bool ovsdb_schema_equal(const struct ovsdb_schema *, + const struct ovsdb_schema *); /* Database. */ struct ovsdb { diff --git a/tests/ovsdb-tool.at b/tests/ovsdb-tool.at index 0f11668e..989159dd 100644 --- a/tests/ovsdb-tool.at +++ b/tests/ovsdb-tool.at @@ -306,3 +306,25 @@ AT_CHECK([ovsdb-tool create db schema], [0], [], [ignore]) AT_CHECK([ovsdb-tool db-cksum db], [0], [12345678 9 ]) AT_CLEANUP + +AT_SETUP([ovsdb-tool needs-conversion (no conversion needed)]) +AT_KEYWORDS([ovsdb file positive]) +AT_DATA([schema], [ORDINAL_SCHEMA +]) +touch .db.~lock~ +AT_CHECK([ovsdb-tool create db schema], [0], [], [ignore]) +AT_CHECK([ovsdb-tool needs-conversion db schema], [0], [no +]) +AT_CLEANUP + +AT_SETUP([ovsdb-tool needs-conversion (conversion needed)]) +AT_KEYWORDS([ovsdb file positive]) +AT_DATA([schema], [ORDINAL_SCHEMA +]) +touch .db.~lock~ +AT_CHECK([ovsdb-tool create db schema], [0], [], [ignore]) +sed 's/5\.1\.3/5.1.4/' < schema > schema2 +AT_CHECK([diff schema schema2], [1], [ignore]) +AT_CHECK([ovsdb-tool needs-conversion db schema2], [0], [yes +]) +AT_CLEANUP -- 2.30.2