Bug 1431149 - Add an optional schema name argument to `OpenedConnection#{get, set}SchemaVersion`. r=mak
authorKit Cambridge <kit@yakshaving.ninja>
Wed, 17 Jan 2018 09:13:47 -0800
changeset 451699 9e512081bde6bb4ccdb5be14743d9fe36f8a2ddc
parent 451698 f8230d6dd54f00b3439e5a587499255e29f9ebd9
child 451700 677af3c4c901b9b7e3b2ae1054cfcc6bed1b3aa8
push id8560
push userryanvm@gmail.com
push dateFri, 19 Jan 2018 16:34:00 +0000
treeherdermozilla-beta@4c9965a3b8a0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1431149
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1431149 - Add an optional schema name argument to `OpenedConnection#{get, set}SchemaVersion`. r=mak MozReview-Commit-ID: 8Uk1Vuqc1lc
toolkit/modules/Sqlite.jsm
toolkit/modules/tests/xpcshell/test_sqlite.js
--- a/toolkit/modules/Sqlite.jsm
+++ b/toolkit/modules/Sqlite.jsm
@@ -1166,34 +1166,34 @@ OpenedConnection.prototype = Object.free
 
   /**
    * The integer schema version of the database.
    *
    * This is 0 if not schema version has been set.
    *
    * @return Promise<int>
    */
-  getSchemaVersion() {
-    return this.execute("PRAGMA user_version").then(
+  getSchemaVersion(schemaName = "main") {
+    return this.execute(`PRAGMA ${schemaName}.user_version`).then(
       function onSuccess(result) {
         if (result == null) {
           return 0;
         }
         return JSON.stringify(result[0].getInt32(0));
       }
     );
   },
 
-  setSchemaVersion(value) {
+  setSchemaVersion(value, schemaName = "main") {
     if (!Number.isInteger(value)) {
       // Guarding against accidental SQLi
       throw new TypeError("Schema version must be an integer. Got " + value);
     }
     this._connectionData.ensureOpen();
-    return this.execute("PRAGMA user_version = " + value);
+    return this.execute(`PRAGMA ${schemaName}.user_version = ${value}`);
   },
 
   /**
    * Close the database connection.
    *
    * This must be performed when you are finished with the database.
    *
    * Closing the database connection has the side effect of forcefully
--- a/toolkit/modules/tests/xpcshell/test_sqlite.js
+++ b/toolkit/modules/tests/xpcshell/test_sqlite.js
@@ -127,16 +127,35 @@ add_task(async function test_schema_vers
       success = true;
     }
     Assert.ok(success);
 
     version = await db.getSchemaVersion();
     Assert.equal(version, 14);
   }
 
+  await db.execute("ATTACH :memory AS attached");
+
+  let attachedVersion = await db.getSchemaVersion("attached");
+  Assert.equal(attachedVersion, 0,
+    "Should return 0 for initial attached schema version");
+
+  await db.setSchemaVersion(3, "attached");
+  attachedVersion = await db.getSchemaVersion("attached");
+  Assert.equal(attachedVersion, 3, "Should set attached schema version");
+
+  version = await db.getSchemaVersion();
+  Assert.equal(version, 14,
+    "Setting attached schema version should not change main schema version");
+
+  await db.setSchemaVersion(15);
+  attachedVersion = await db.getSchemaVersion("attached");
+  Assert.equal(attachedVersion, 3,
+    "Setting main schema version should not change attached schema version");
+
   await db.close();
 });
 
 add_task(async function test_simple_insert() {
   let c = await getDummyDatabase("simple_insert");
 
   let result = await c.execute("INSERT INTO dirs VALUES (NULL, 'foo')");
   Assert.ok(Array.isArray(result));