ovsdb-tool: New command "needs-conversion".
authorBen Pfaff <blp@nicira.com>
Tue, 8 Feb 2011 23:57:14 +0000 (15:57 -0800)
committerBen Pfaff <blp@nicira.com>
Tue, 15 Feb 2011 20:24:29 +0000 (12:24 -0800)
ovsdb/ovsdb-tool.1.in
ovsdb/ovsdb-tool.c
ovsdb/ovsdb.c
ovsdb/ovsdb.h
tests/ovsdb-tool.at

index 79bd2a6..7f34609 100644 (file)
@@ -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
index 3730e67..4f55b6a 100644 (file)
@@ -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 },
index 46d06a0..2a54a7b 100644 (file)
@@ -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;
+}
 \f
 static void
 ovsdb_set_ref_table(const struct shash *tables,
index ae743bb..834ff1a 100644 (file)
@@ -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 *);
 \f
 /* Database. */
 struct ovsdb {
index 0f11668..989159d 100644 (file)
@@ -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