fix(db): Remove - from table names, which was a bad idea

This commit is contained in:
Ben Gotow 2016-04-04 17:44:45 -07:00
parent 85dfe82c6d
commit cf4b99aba4
10 changed files with 30 additions and 29 deletions

View file

@ -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

View file

@ -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"

View file

@ -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])

View file

@ -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')

View file

@ -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
```

View file

@ -8,6 +8,7 @@ class Event extends Model
@attributes: _.extend {}, Model.attributes,
'calendarId': Attributes.String
queryable: true
modelKey: 'calendarId'
jsonKey: 'calendar_id'

View file

@ -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)',

View file

@ -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('.')

View file

@ -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)

View file

@ -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