Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9a21934
node internal ID: added DB migrations
SteRiccio Feb 17, 2026
7f1a961
node internal ID: added DB migrations
SteRiccio Feb 17, 2026
cd67494
updated migrations
SteRiccio Feb 18, 2026
53b9e8d
improved node i_id update
SteRiccio Feb 22, 2026
d308b83
fixed constraints update
SteRiccio Feb 23, 2026
ac81c23
db migrations: added filter by record_uuid
SteRiccio Feb 24, 2026
f4a3ef0
Merge branch 'master' into feat/node-iid
mergify[bot] Feb 26, 2026
8340e0a
Merge branch 'master' into feat/node-iid
mergify[bot] Feb 26, 2026
c694c2f
Merge branch 'master' into feat/node-iid
mergify[bot] Feb 26, 2026
a02be0b
Merge branch 'master' into feat/node-iid
mergify[bot] Feb 26, 2026
10ad793
Merge branch 'master' into feat/node-iid
mergify[bot] Feb 26, 2026
a8c8442
Merge branch 'master' into feat/node-iid
mergify[bot] Mar 3, 2026
2c5502b
Merge branch 'master' into feat/node-iid
mergify[bot] Mar 6, 2026
d7b6149
added record validation migration
SteRiccio Mar 10, 2026
443bdb3
Merge branch 'master' into feat/node-iid
mergify[bot] Mar 19, 2026
39667c8
Merge branch 'master' into feat/node-iid
mergify[bot] Mar 19, 2026
9c8a431
Merge branch 'master' into feat/node-iid
mergify[bot] Mar 23, 2026
4c7a49a
Merge branch 'master' into feat/node-iid
mergify[bot] Mar 24, 2026
bd0a1cb
Merge branch 'master' into feat/node-iid
mergify[bot] Mar 24, 2026
42bc605
Merge branch 'master' into feat/node-iid
mergify[bot] Mar 24, 2026
f09b247
Merge branch 'master' into feat/node-iid
mergify[bot] Mar 25, 2026
c45d350
Merge branch 'master' into feat/node-iid
mergify[bot] Apr 8, 2026
f03ac52
Merge branch 'master' into feat/node-iid
mergify[bot] Apr 8, 2026
0fa0fe1
Merge branch 'master' into feat/node-iid
mergify[bot] Apr 23, 2026
8bfb423
Merge branch 'master' into feat/node-iid
mergify[bot] Apr 26, 2026
a461ede
Merge branch 'master' into feat/node-iid
mergify[bot] Apr 28, 2026
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict'

var dbm
var type
var seed
var fs = require('fs')
var path = require('path')
var Promise

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate
type = dbm.dataType
seed = seedLink
Promise = options.Promise
}

exports.up = function (db) {
var filePath = path.join(__dirname, 'sqls', '20260217085512-alter-table-node-add-iid-up.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
console.log('received data: ' + data)

resolve(data)
})
}).then(function (data) {
return db.runSql(data)
})
}

exports.down = function (db) {
var filePath = path.join(__dirname, 'sqls', '20260217085512-alter-table-node-add-iid-down.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
console.log('received data: ' + data)

resolve(data)
})
}).then(function (data) {
return db.runSql(data)
})
}

exports._meta = {
version: 1,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Replace with your SQL commands */
Comment thread
SteRiccio marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
ALTER TABLE node
ADD COLUMN i_id INTEGER;

ALTER TABLE node
ADD COLUMN p_i_id INTEGER;

-- Populate the i_id column with the row number for each record_uuid
WITH ranked AS (
SELECT id,
ROW_NUMBER() OVER (PARTITION BY record_uuid ORDER BY id) AS i_id
FROM node
)
UPDATE node
SET i_id = ranked.i_id
FROM ranked
WHERE node.id = ranked.id;

ALTER TABLE node
ALTER COLUMN i_id SET NOT NULL;

-- Populate the p_i_id column by joining the node table with itself based on the parent-child relationship
UPDATE node AS child
SET p_i_id = parent.i_id
FROM node AS parent
WHERE child.parent_uuid = parent.uuid
AND child.record_uuid = parent.record_uuid;

-- Replace meta.h UUID array with i_id array, preserving order
UPDATE node AS n
SET meta = jsonb_set(
meta,
'{h}',
COALESCE(
(
SELECT jsonb_agg(p.i_id ORDER BY elems.ordinality)
FROM jsonb_array_elements_text(n.meta->'h') WITH ORDINALITY AS elems(uuid_text, ordinality)
JOIN node AS p ON p.uuid = elems.uuid_text::uuid
Comment thread
SteRiccio marked this conversation as resolved.
Outdated
),
'[]'::jsonb
)
)
WHERE n.meta ? 'h';

-- Update activity_log content to use i_id/p_i_id and drop meta.h
UPDATE activity_log AS al
SET content = updated.content
FROM (
SELECT al.id,
jsonb_set(
jsonb_set(
jsonb_set(
(
CASE
WHEN al.content ? 'meta'
THEN jsonb_set(al.content, '{meta}', (al.content->'meta') - 'h')
ELSE al.content
END
) - 'uuid' - 'parentUuid',
'{iId}',
to_jsonb(n.i_id),
true
),
'{pIId}',
to_jsonb(n.p_i_id),
true
),
'{recordUuid}',
to_jsonb(n.record_uuid),
true
) AS content
FROM activity_log AS al
JOIN node AS n ON n.uuid = (al.content->>'uuid')::uuid
WHERE al.type IN ('nodeCreate', 'nodeDelete', 'nodeValueUpdate')
Comment thread
SteRiccio marked this conversation as resolved.
Outdated
) AS updated
WHERE al.id = updated.id;

DROP VIEW IF EXISTS activity_log_user_aggregate;
CREATE VIEW activity_log_user_aggregate AS
SELECT
DISTINCT ON (
date_created::date,
user_uuid,
type,
content_uuid,
content_key,
content_record_uuid,
content_i_id
)
id,
date_created,
user_uuid,
type,
(content->>'uuid')::uuid as content_uuid,
content->>'key' as content_key,
-- node related fields
(content->>'recordUuid')::uuid as content_record_uuid,
(content->>'iId')::integer as content_i_id,
content
FROM
activity_log
WHERE
NOT system
ORDER BY
date_created::date DESC,
user_uuid,
type,
content_uuid,
content_key,
content_record_uuid,
content_i_id,
date_created DESC;

Comment thread
SteRiccio marked this conversation as resolved.
Outdated
-- Add a unique constraint on the combination of record_uuid and i_id
Comment thread
SteRiccio marked this conversation as resolved.
Outdated
ALTER TABLE node
DROP CONSTRAINT IF EXISTS node_pkey;

-- Add a composite primary key on record_uuid and i_id
ALTER TABLE node
ADD PRIMARY KEY (record_uuid, i_id);
Comment thread
SteRiccio marked this conversation as resolved.
Outdated

-- Create an index on the combination of record_uuid and i_id for faster lookups
CREATE INDEX IF NOT EXISTS node_record_uuid_i_id_idx
ON node (record_uuid, i_id);
Comment thread
SteRiccio marked this conversation as resolved.
Loading