Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/server/db/pg.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,31 @@ module.exports = PgDb = (options) ->
callback? error.message

@create = (docName, docData, callback) ->
sql = """
INSERT INTO #{snapshot_table} ("doc", "v", "snapshot", "meta", "type", "created_at")
VALUES ($1, $2, ($3)::jsonb, $4, $5, now() at time zone 'UTC')
# Temporarily guard against improper constraints allowing
# multiple snapshots to be inserted for a given docName
check_snapshots_sql = """
SELECT count("doc") FROM #{snapshot_table}
WHERE "doc" = $1
"""
values = [docName, docData.v, JSON.stringify(docData.snapshot), docData.meta, docData.type]
client.query sql, values, (error, result) ->
if !error?
callback?()
else if error.toString().match "duplicate key value violates unique constraint"

client.query check_snapshots_sql, [docName], (error, result) ->
if !error? and result.rows.length > 0 and result.rows[0].count > 0
callback? "Document already exists"
else
else if error?
callback? error?.message
else
sql = """
INSERT INTO #{snapshot_table} ("doc", "v", "snapshot", "meta", "type", "created_at")
VALUES ($1, $2, ($3)::jsonb, $4, $5, now() at time zone 'UTC')
"""
values = [docName, docData.v, JSON.stringify(docData.snapshot), docData.meta, docData.type]
client.query sql, values, (error, result) ->
if !error?
callback?()
else if error.toString().match "duplicate key value violates unique constraint"
callback? "Document already exists"
else
callback? error?.message

@delete = (docName, dbMeta, callback) ->
sql = """
Expand Down