diff --git a/docs/Database.md b/docs/Database.md index 977c76fa1..bc3362dae 100644 --- a/docs/Database.md +++ b/docs/Database.md @@ -131,7 +131,7 @@ DatabaseStore.findAll(Thread).where([Thread.attributes.tags.contains('inbox')]) This is equivalent to writing the following SQL: ```sql -SELECT `Thread`.`data` FROM `Thread` INNER JOIN `Thread-Tag` AS `M1` ON `M1`.`id` = `Thread`.`id` WHERE `M1`.`value` = 'inbox' ORDER BY `Thread`.`last_message_timestamp` DESC +SELECT `Thread`.`data` FROM `Thread` INNER JOIN `ThreadTag` AS `M1` ON `M1`.`id` = `Thread`.`id` WHERE `M1`.`value` = 'inbox' ORDER BY `Thread`.`last_message_timestamp` DESC ``` #### Listening for Changes diff --git a/spec/models/query-spec.coffee b/spec/models/query-spec.coffee index 2039c1edc..426476aa0 100644 --- a/spec/models/query-spec.coffee +++ b/spec/models/query-spec.coffee @@ -150,15 +150,15 @@ describe "ModelQuery", -> @runScenario Thread, builder: (q) -> q.where(Thread.attributes.categories.contains('category-id')).where({id: '1234'}) sql: "SELECT `Thread`.`data` FROM `Thread` \ - INNER JOIN `Thread-Category` AS `M1` ON `M1`.`id` = `Thread`.`id` \ + INNER JOIN `ThreadCategory` AS `M1` ON `M1`.`id` = `Thread`.`id` \ WHERE `M1`.`value` = 'category-id' AND `Thread`.`id` = '1234' \ ORDER BY `Thread`.`last_message_received_timestamp` DESC" @runScenario Thread, builder: (q) -> q.where([Thread.attributes.categories.contains('l-1'), Thread.attributes.categories.contains('l-2')]) sql: "SELECT `Thread`.`data` FROM `Thread` \ - INNER JOIN `Thread-Category` AS `M1` ON `M1`.`id` = `Thread`.`id` \ - INNER JOIN `Thread-Category` AS `M2` ON `M2`.`id` = `Thread`.`id` \ + INNER JOIN `ThreadCategory` AS `M1` ON `M1`.`id` = `Thread`.`id` \ + INNER JOIN `ThreadCategory` AS `M2` ON `M2`.`id` = `Thread`.`id` \ WHERE `M1`.`value` = 'l-1' AND `M2`.`value` = 'l-2' \ ORDER BY `Thread`.`last_message_received_timestamp` DESC" diff --git a/spec/stores/database-setup-query-builder-spec.coffee b/spec/stores/database-setup-query-builder-spec.coffee index 4605113cb..b08d6c893 100644 --- a/spec/stores/database-setup-query-builder-spec.coffee +++ b/spec/stores/database-setup-query-builder-spec.coffee @@ -31,9 +31,9 @@ describe "DatabaseSetupQueryBuilder", -> expected = [ 'CREATE TABLE IF NOT EXISTS `TestModel` (id TEXT PRIMARY KEY,data BLOB,client_id TEXT,server_id TEXT)', 'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_id` ON `TestModel` (`id`)', - 'CREATE TABLE IF NOT EXISTS `TestModel-Category` (id TEXT KEY, `value` TEXT)' - 'CREATE INDEX IF NOT EXISTS `TestModel_Category_id` ON `TestModel-Category` (`id` ASC)' - 'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_Category_val_id` ON `TestModel-Category` (`value` ASC, `id` ASC)', + 'CREATE TABLE IF NOT EXISTS `TestModelCategory` (id TEXT KEY, `value` TEXT)' + 'CREATE INDEX IF NOT EXISTS `TestModel_Category_id` ON `TestModelCategory` (`id` ASC)' + 'CREATE UNIQUE INDEX IF NOT EXISTS `TestModel_Category_val_id` ON `TestModelCategory` (`value` ASC, `id` ASC)', ] for query,i in queries expect(query).toBe(expected[i]) diff --git a/spec/stores/database-transaction-spec.coffee b/spec/stores/database-transaction-spec.coffee index f760e5c79..5b59262fe 100644 --- a/spec/stores/database-transaction-spec.coffee +++ b/spec/stores/database-transaction-spec.coffee @@ -174,7 +174,7 @@ describe "DatabaseTransaction", -> .then => expect(@performed.length).toBe(4) expect(@performed[0].query).toBe("BEGIN IMMEDIATE TRANSACTION") - expect(@performed[2].query).toBe("DELETE FROM `TestModel-Category` WHERE `id` = ?") + expect(@performed[2].query).toBe("DELETE FROM `TestModelCategory` WHERE `id` = ?") expect(@performed[2].values[0]).toBe('1234') expect(@performed[3].query).toBe("COMMIT") @@ -233,10 +233,10 @@ describe "DatabaseTransaction", -> @transaction._writeModels([@m]) it "should delete all association records for the model from join tables", -> - expect(@performed[1].query).toBe('DELETE FROM `TestModel-Category` WHERE `id` IN (\'local-6806434c-b0cd\')') + expect(@performed[1].query).toBe('DELETE FROM `TestModelCategory` WHERE `id` IN (\'local-6806434c-b0cd\')') it "should insert new association records into join tables in a single query", -> - expect(@performed[2].query).toBe('INSERT OR IGNORE INTO `TestModel-Category` (`id`, `value`) VALUES (?,?),(?,?)') + expect(@performed[2].query).toBe('INSERT OR IGNORE INTO `TestModelCategory` (`id`, `value`) VALUES (?,?),(?,?)') expect(@performed[2].values).toEqual(['local-6806434c-b0cd', 'a','local-6806434c-b0cd', 'b']) describe "model collection attributes query building", -> @@ -250,7 +250,7 @@ describe "DatabaseTransaction", -> @transaction._writeModels([@m]) collectionAttributeQueries = _.filter @performed, (i) -> - i.query.indexOf('INSERT OR IGNORE INTO `TestModel-Category`') == 0 + i.query.indexOf('INSERT OR IGNORE INTO `TestModelCategory`') == 0 expect(collectionAttributeQueries.length).toBe(1) expect(collectionAttributeQueries[0].values[399]).toEqual('id-199') @@ -260,7 +260,7 @@ describe "DatabaseTransaction", -> @transaction._writeModels([@m]) collectionAttributeQueries = _.filter @performed, (i) -> - i.query.indexOf('INSERT OR IGNORE INTO `TestModel-Category`') == 0 + i.query.indexOf('INSERT OR IGNORE INTO `TestModelCategory`') == 0 expect(collectionAttributeQueries.length).toBe(2) expect(collectionAttributeQueries[0].values[399]).toEqual('id-199') @@ -271,7 +271,7 @@ describe "DatabaseTransaction", -> @transaction._writeModels([@m]) collectionAttributeQueries = _.filter @performed, (i) -> - i.query.indexOf('INSERT OR IGNORE INTO `TestModel-Category`') == 0 + i.query.indexOf('INSERT OR IGNORE INTO `TestModelCategory`') == 0 expect(collectionAttributeQueries.length).toBe(2) expect(collectionAttributeQueries[0].values[399]).toEqual('id-199') diff --git a/src/flux/attributes/attribute-collection.coffee b/src/flux/attributes/attribute-collection.coffee index 118e8198b..744fcae91 100644 --- a/src/flux/attributes/attribute-collection.coffee +++ b/src/flux/attributes/attribute-collection.coffee @@ -22,7 +22,7 @@ This is equivalent to writing the following SQL: ```sql SELECT `Thread`.`data` FROM `Thread` -INNER JOIN `Thread-Label` AS `M1` ON `M1`.`id` = `Thread`.`id` +INNER JOIN `ThreadLabel` AS `M1` ON `M1`.`id` = `Thread`.`id` WHERE `M1`.`value` = 'inbox' ORDER BY `Thread`.`last_message_received_timestamp` DESC ``` diff --git a/src/flux/models/event.coffee b/src/flux/models/event.coffee index e0f4d523b..9d98a894f 100644 --- a/src/flux/models/event.coffee +++ b/src/flux/models/event.coffee @@ -8,6 +8,7 @@ class Event extends Model @attributes: _.extend {}, Model.attributes, 'calendarId': Attributes.String + queryable: true modelKey: 'calendarId' jsonKey: 'calendar_id' diff --git a/src/flux/models/thread.es6 b/src/flux/models/thread.es6 index 8221b48c4..97143a349 100644 --- a/src/flux/models/thread.es6 +++ b/src/flux/models/thread.es6 @@ -110,8 +110,8 @@ class Thread extends ModelWithMetadata { static additionalSQLiteConfig = { setup: () => [ - 'CREATE TABLE IF NOT EXISTS `Thread-Counts` (`category_id` TEXT PRIMARY KEY, `unread` INTEGER, `total` INTEGER)', - 'CREATE UNIQUE INDEX IF NOT EXISTS ThreadCountsIndex ON `Thread-Counts` (category_id DESC)', + 'CREATE TABLE IF NOT EXISTS `ThreadCounts` (`category_id` TEXT PRIMARY KEY, `unread` INTEGER, `total` INTEGER)', + 'CREATE UNIQUE INDEX IF NOT EXISTS ThreadCountsIndex ON `ThreadCounts` (category_id DESC)', 'CREATE INDEX IF NOT EXISTS ThreadListIndex ON Thread(last_message_received_timestamp DESC, id)', 'CREATE INDEX IF NOT EXISTS ThreadListSentIndex ON Thread(last_message_sent_timestamp DESC, id)', 'CREATE INDEX IF NOT EXISTS ThreadStarIndex ON Thread(account_id, starred)', diff --git a/src/flux/models/utils.coffee b/src/flux/models/utils.coffee index caa799d42..dd1d174bd 100644 --- a/src/flux/models/utils.coffee +++ b/src/flux/models/utils.coffee @@ -175,7 +175,7 @@ Utils = id[0..5] is 'local-' tableNameForJoin: (primaryKlass, secondaryKlass) -> - "#{primaryKlass.name}-#{secondaryKlass.name}" + "#{primaryKlass.name}#{secondaryKlass.name}" imageNamed: (fullname, resourcePath) -> [name, ext] = fullname.split('.') diff --git a/src/flux/stores/database-store.coffee b/src/flux/stores/database-store.coffee index fbc031e24..978d0e39f 100644 --- a/src/flux/stores/database-store.coffee +++ b/src/flux/stores/database-store.coffee @@ -266,7 +266,7 @@ class DatabaseStore extends NylasStore @_db.all "EXPLAIN QUERY PLAN #{query}", values, (err, results=[]) => str = results.map((row) -> row.detail).join('\n') + " for " + query return if str.indexOf("SCAN") is -1 - return if str.indexOf('Thread-Counts') > 0 + return if str.indexOf('ThreadCounts') > 0 return if str.indexOf('ThreadSearch') > 0 @_prettyConsoleLog(str) diff --git a/src/flux/stores/thread-counts-store.coffee b/src/flux/stores/thread-counts-store.coffee index f27d8f644..70908fa6b 100644 --- a/src/flux/stores/thread-counts-store.coffee +++ b/src/flux/stores/thread-counts-store.coffee @@ -13,36 +13,36 @@ Note: SUM(unread) works because unread is represented as an int: 0 or 1. ### ReadCountsQuery = -> - "SELECT * FROM `Thread-Counts`" + "SELECT * FROM `ThreadCounts`" SetCountsQuery = -> """ - REPLACE INTO `Thread-Counts` (category_id, unread, total) + REPLACE INTO `ThreadCounts` (category_id, unread, total) SELECT - `Thread-Category`.`value` as category_id, + `ThreadCategory`.`value` as category_id, SUM(unread) as unread, COUNT(*) as total FROM `Thread` - INNER JOIN `Thread-Category` ON `Thread`.`id` = `Thread-Category`.`id` + INNER JOIN `ThreadCategory` ON `Thread`.`id` = `ThreadCategory`.`id` WHERE `Thread`.in_all_mail = 1 - GROUP BY `Thread-Category`.`value`; + GROUP BY `ThreadCategory`.`value`; """ UpdateCountsQuery = (objectIds, operator) -> objectIdsString = "'" + objectIds.join("','") + "'" """ - REPLACE INTO `Thread-Counts` (category_id, unread, total) + REPLACE INTO `ThreadCounts` (category_id, unread, total) SELECT - `Thread-Category`.`value` as category_id, - COALESCE((SELECT unread FROM `Thread-Counts` WHERE category_id = `Thread-Category`.`value`), 0) #{operator} SUM(unread) as unread, - COALESCE((SELECT total FROM `Thread-Counts` WHERE category_id = `Thread-Category`.`value`), 0) #{operator} COUNT(*) as total + `ThreadCategory`.`value` as category_id, + COALESCE((SELECT unread FROM `ThreadCounts` WHERE category_id = `ThreadCategory`.`value`), 0) #{operator} SUM(unread) as unread, + COALESCE((SELECT total FROM `ThreadCounts` WHERE category_id = `ThreadCategory`.`value`), 0) #{operator} COUNT(*) as total FROM `Thread` - INNER JOIN `Thread-Category` ON `Thread`.`id` = `Thread-Category`.`id` + INNER JOIN `ThreadCategory` ON `Thread`.`id` = `ThreadCategory`.`id` WHERE `Thread`.id IN (#{objectIdsString}) AND `Thread`.in_all_mail = 1 - GROUP BY `Thread-Category`.`value` + GROUP BY `ThreadCategory`.`value` """ class CategoryDatabaseMutationObserver