CREATE VIEW "code_notebook_cell_latest" AS
-- code provenance: `RssdInitSqlNotebook.notebookBusinessLogicViews` (file:///home/baasit/www.surveilr.com/lib/std/lifecycle.sql.ts)
-- Retrieve the latest version of each code_notebook_cell.
-- Notebooks can have multiple versions of cells, where the interpretable_code and other metadata may be updated over time.
-- The latest record is determined by the most recent COALESCE(updated_at, created_at) timestamp.
SELECT
c.code_notebook_cell_id, -- Selects the unique ID of the notebook cell
c.notebook_kernel_id, -- References the kernel associated with this cell
c.notebook_name, -- The name of the notebook containing this cell
c.cell_name, -- The name of the cell within the notebook
c.interpretable_code, -- The latest interpretable code associated with the cell
c.interpretable_code_hash, -- Hash of the latest interpretable code
c.description, -- Description of the cell's purpose or content
c.cell_governance, -- Governance details for the cell (if any)
c.arguments, -- Arguments or parameters related to the cell's execution
c.activity_log, -- Log of activities related to this cell
COALESCE(c.updated_at, c.created_at) AS version_timestamp -- The latest timestamp (updated or created)
FROM (
SELECT
code_notebook_cell_id,
notebook_kernel_id,
notebook_name,
cell_name,
interpretable_code,
interpretable_code_hash,
description,
cell_governance,
arguments,
activity_log,
updated_at,
created_at,
ROW_NUMBER() OVER (
PARTITION BY code_notebook_cell_id
ORDER BY COALESCE(updated_at, created_at) DESC -- Orders by the latest timestamp
) AS rn
FROM
code_notebook_cell
) c WHERE c.rn = 1