Skip to content

Commit 57600c8

Browse files
committed
ACTUALLY fix the bug this time
1 parent e2acfc0 commit 57600c8

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,18 @@ PostgresDB.prototype.commit = function (collection, id, op, snapshot, options, c
6060
* Casting is required as postgres thinks that collection and doc_id are
6161
* not varchar
6262
*/
63-
// ZW: We also should have the first op version be 0, not 1, to match the
64-
// reference MemoryDB implementation. Catching up outdated clients who
65-
// reconnect may be buggy otherwise.
63+
// ZW: We also should have the first op version be 0 (the actual value
64+
// of op.v) instead of 1, in order to match the reference MemoryDB
65+
// implementation. Catching up outdated clients who reconnect may be
66+
// buggy otherwise.
6667
const query = {
6768
name: "sdb-commit-op-and-snap",
6869
text: `WITH snapshot_id AS (
6970
INSERT INTO snapshots (collection, doc_id, doc_type, version, data)
70-
SELECT $1::varchar collection, $2::varchar doc_id, $4 doc_type, $3 v, $5 d
71-
WHERE $3 = (
72-
SELECT version+1 v
71+
SELECT $1::varchar collection, $2::varchar doc_id,
72+
$3 snap_type, $4 snap_v, $5 snap_data
73+
WHERE $4 = (
74+
SELECT version+1 snap_v
7375
FROM snapshots
7476
WHERE collection = $1 AND doc_id = $2
7577
FOR UPDATE
@@ -79,24 +81,28 @@ PostgresDB.prototype.commit = function (collection, id, op, snapshot, options, c
7981
WHERE collection = $1 AND doc_id = $2
8082
FOR UPDATE
8183
)
82-
ON CONFLICT (collection, doc_id) DO UPDATE SET version = $3, data = $5, doc_type = $4
84+
ON CONFLICT (collection, doc_id) DO
85+
UPDATE SET doc_type = $3, version = $4, data = $5
8386
RETURNING version
8487
)
8588
INSERT INTO ops (collection, doc_id, version, operation)
86-
SELECT $1::varchar collection, $2::varchar doc_id, $3 v, $6 operation
89+
SELECT $1::varchar collection, $2::varchar doc_id,
90+
$6 op_v, $7 op
8791
WHERE (
88-
$3 = (
92+
$6 = (
8993
SELECT max(version)+1
9094
FROM ops
9195
WHERE collection = $1 AND doc_id = $2
9296
) OR NOT EXISTS (
93-
SELECT 0
97+
SELECT 1
9498
FROM ops
9599
WHERE collection = $1 AND doc_id = $2
96100
)
97101
) AND EXISTS (SELECT 1 FROM snapshot_id)
98102
RETURNING version`,
99-
values: [collection, id, snapshot.v, snapshot.type, snapshot.data, op]
103+
values: [
104+
collection, id, snapshot.type, snapshot.v, snapshot.data, op.v, op
105+
]
100106
};
101107
client.query(query, (err, res) => {
102108
if (err) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sharedb-postgres",
3-
"version": "3.0.0-zw.3",
3+
"version": "3.0.1-zw",
44
"description": "PostgreSQL adapter for ShareDB",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)