Skip to content

Commit

Permalink
🚧 WIP: Start work on migrations for local datastores
Browse files Browse the repository at this point in the history
  • Loading branch information
kierandrewett committed Dec 26, 2023
1 parent dc36857 commit 7e2321c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 13 deletions.
49 changes: 45 additions & 4 deletions components/favicons/FaviconsDatastore.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,65 @@ const { Datastore } = ChromeUtils.importESModule(
);

export class FaviconsDatastore extends Datastore {
/**
* The version of this datastore
*/
static VERSION = 2;

constructor() {
super("favicons", {
version: 1,
tables: {
favicons: {
id: "INTEGER PRIMARY KEY",
data: "TEXT NOT NULL",
page_url: "TEXT NOT NULL"
data: "TEXT",
size: "INTEGER",
page_url: "TEXT"
}
}
});
}

/**
* Migrates the database
* @param {number} localVersion
*/
#migrateDatabase(localVersion) {
if (localVersion < this.version) {
if (localVersion <= 1) {
this.sql(`
ALTER TABLE favicons
DROP COLUMN url,
ADD data TEXT,
ADD size INTEGER;`);
}
}
}

/**
* Get a stored favicon using its ID
* @param {string} id
*/
getByID(id) {
return this.sql(
`
SELECT * FROM favicons
WHERE id = :id
`,
{ id }
);
}

/**
* Get a stored favicon using a URL
* @param {string} url
*/
getForURL(url) {
return this._conn;
return this.sql(
`
SELECT * FROM favicons
WHERE page_url = :page_url
`,
{ page_url: url }
);
}
}
44 changes: 35 additions & 9 deletions components/storage/Datastore.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ const MAX_ATTEMPTS = 5;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

export class Datastore {
static VERSION = null;

/**
* Creates a new datastore
* @param {string} name
* @param {{ version: number; tables: Record<string, Record<string, string>> }} schema
* @param {{ tables: Record<string, Record<string, string>> }} schema
*/
constructor(name, schema) {
this.#name = name;
this.#version = schema.version;
this.#schema = schema;
}

Expand All @@ -35,11 +36,6 @@ export class Datastore {
*/
#name = null;

/**
* The current version of this datastore
*/
#version = null;

/**
* The schema of this datastore
* @type {{ tables: Record<string, Record<string, string>> }}
Expand All @@ -60,6 +56,13 @@ export class Datastore {
prefix: "Datastore.sys.mjs"
});

/**
* THe version of this datastore
*/
get version() {
return /** @type {typeof Datastore} */ (this.constructor).VERSION;
}

/**
* The path to the browser_storage directory in the profile dir
*/
Expand Down Expand Up @@ -116,6 +119,29 @@ export class Datastore {
await this._conn.execute(sql);
}

/**
* Executes an SQL statement with optional params
* @param {string} statement
* @param {Record<string, any>} [params]
* @returns
*/
async sql(statement, params) {
const rows = await this._conn.executeCached(statement.trim(), params);

console.log(rows);

return rows.map((r) => {
let result = {};

for (const column of Object.keys(this.#schema.tables[this.#name])) {
console.log(column);
result[column] = r.getResultByName(column);
}

return result;
});
}

/**
* Initialises the database and its tables
*/
Expand Down Expand Up @@ -191,15 +217,15 @@ export class Datastore {
// have any data, so we should init the database

await this.#initDatabase();
} else if (databaseVersion < this.#version) {
} else if (databaseVersion < this.version) {
// If the version of the database on the user's computer
// is less than what the browser is currently on,
// begin the migration process for the database.

await this.#migrateDatabase(databaseVersion);
}

await this._conn.setSchemaVersion(this.#version);
await this._conn.setSchemaVersion(this.version);
} catch (e) {
await this._conn.close();

Expand Down

0 comments on commit 7e2321c

Please sign in to comment.