Initially made it through full update with new fieldseeker schema
This commit is contained in:
parent
0904f086b2
commit
bd16f69e28
510 changed files with 13617 additions and 14851 deletions
|
|
@ -1,58 +1,97 @@
|
|||
-- Prepared statement for conditional insert with versioning for fieldseeker.fieldscoutinglog
|
||||
-- Only inserts a new version if data has changed
|
||||
|
||||
PREPARE insert_fieldscoutinglog_versioned(bigint, fieldseeker.fieldscoutinglog_fieldscoutingsymbology_enum, uuid, varchar, timestamp, varchar, timestamp, timestamp, varchar, timestamp, varchar) AS
|
||||
WITH
|
||||
-- Get the current latest version of this record
|
||||
latest_version AS (
|
||||
SELECT * FROM fieldseeker.fieldscoutinglog
|
||||
WHERE objectid = $1
|
||||
ORDER BY VERSION DESC
|
||||
LIMIT 1
|
||||
),
|
||||
-- Calculate the next version number
|
||||
next_version AS (
|
||||
SELECT COALESCE(MAX(VERSION) + 1, 1) as version_num
|
||||
FROM fieldseeker.fieldscoutinglog
|
||||
WHERE objectid = $1
|
||||
)
|
||||
-- Perform conditional insert
|
||||
INSERT INTO fieldseeker.fieldscoutinglog (
|
||||
objectid, status, globalid, created_user, created_date, last_edited_user, last_edited_date, creationdate, creator, editdate, editor,
|
||||
VERSION
|
||||
)
|
||||
SELECT
|
||||
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11,
|
||||
v.version_num
|
||||
FROM next_version v
|
||||
WHERE
|
||||
-- Only insert if no record exists yet OR data has changed
|
||||
NOT EXISTS (SELECT 1 FROM latest_version lv WHERE
|
||||
lv.objectid IS NOT DISTINCT FROM $1 AND
|
||||
lv.status IS NOT DISTINCT FROM $2 AND
|
||||
lv.globalid IS NOT DISTINCT FROM $3 AND
|
||||
lv.created_user IS NOT DISTINCT FROM $4 AND
|
||||
lv.created_date IS NOT DISTINCT FROM $5 AND
|
||||
lv.last_edited_user IS NOT DISTINCT FROM $6 AND
|
||||
lv.last_edited_date IS NOT DISTINCT FROM $7 AND
|
||||
lv.creationdate IS NOT DISTINCT FROM $8 AND
|
||||
lv.creator IS NOT DISTINCT FROM $9 AND
|
||||
lv.editdate IS NOT DISTINCT FROM $10 AND
|
||||
lv.editor IS NOT DISTINCT FROM $11
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- Example usage: EXECUTE insert_fieldscoutinglog_versioned(id, value1, value2, ...);
|
||||
|
||||
-- Parameters in order:
|
||||
-- $1: OBJECTID (bigint)
|
||||
-- $2: STATUS (fieldseeker.fieldscoutinglog_fieldscoutingsymbology_enum)
|
||||
-- $3: GlobalID (uuid)
|
||||
-- $4: created_user (varchar)
|
||||
-- $5: created_date (timestamp)
|
||||
-- $6: last_edited_user (varchar)
|
||||
-- $7: last_edited_date (timestamp)
|
||||
-- $8: CreationDate (timestamp)
|
||||
-- $9: Creator (varchar)
|
||||
-- $10: EditDate (timestamp)
|
||||
-- $11: Editor (varchar)
|
||||
-- +goose StatementBegin
|
||||
CREATE OR REPLACE FUNCTION fieldseeker.insert_fieldscoutinglog(
|
||||
p_objectid bigint,
|
||||
|
||||
p_status smallint,
|
||||
p_globalid uuid,
|
||||
p_created_user varchar,
|
||||
p_created_date timestamp,
|
||||
p_last_edited_user varchar,
|
||||
p_last_edited_date timestamp,
|
||||
p_creationdate timestamp,
|
||||
p_creator varchar,
|
||||
p_editdate timestamp,
|
||||
p_editor varchar,
|
||||
p_geometry jsonb,
|
||||
p_geospatial geometry
|
||||
) RETURNS TABLE(row_inserted boolean, version_num integer) AS $$
|
||||
DECLARE
|
||||
v_next_version integer;
|
||||
v_changes_exist boolean;
|
||||
BEGIN
|
||||
-- Check if changes exist
|
||||
SELECT NOT EXISTS (
|
||||
SELECT 1 FROM fieldseeker.fieldscoutinglog lv
|
||||
WHERE lv.objectid = p_objectid
|
||||
|
||||
AND lv.status IS NOT DISTINCT FROM p_status
|
||||
AND lv.globalid IS NOT DISTINCT FROM p_globalid
|
||||
AND lv.created_user IS NOT DISTINCT FROM p_created_user
|
||||
AND lv.created_date IS NOT DISTINCT FROM p_created_date
|
||||
AND lv.last_edited_user IS NOT DISTINCT FROM p_last_edited_user
|
||||
AND lv.last_edited_date IS NOT DISTINCT FROM p_last_edited_date
|
||||
AND lv.creationdate IS NOT DISTINCT FROM p_creationdate
|
||||
AND lv.creator IS NOT DISTINCT FROM p_creator
|
||||
AND lv.editdate IS NOT DISTINCT FROM p_editdate
|
||||
AND lv.editor IS NOT DISTINCT FROM p_editor
|
||||
AND lv.geometry IS NOT DISTINCT FROM p_geometry
|
||||
AND lv.geospatial IS NOT DISTINCT FROM p_geospatial
|
||||
ORDER BY VERSION DESC LIMIT 1
|
||||
) INTO v_changes_exist;
|
||||
|
||||
-- If no changes, return false with current version
|
||||
IF NOT v_changes_exist THEN
|
||||
RETURN QUERY
|
||||
SELECT
|
||||
FALSE AS row_inserted,
|
||||
(SELECT VERSION FROM fieldseeker.fieldscoutinglog
|
||||
WHERE objectid = p_objectid ORDER BY VERSION DESC LIMIT 1) AS version_num;
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
-- Calculate next version
|
||||
SELECT COALESCE(MAX(VERSION) + 1, 1) INTO v_next_version
|
||||
FROM fieldseeker.fieldscoutinglog
|
||||
WHERE objectid = p_objectid;
|
||||
|
||||
-- Insert new version
|
||||
INSERT INTO fieldseeker.fieldscoutinglog (
|
||||
objectid,
|
||||
|
||||
status,
|
||||
globalid,
|
||||
created_user,
|
||||
created_date,
|
||||
last_edited_user,
|
||||
last_edited_date,
|
||||
creationdate,
|
||||
creator,
|
||||
editdate,
|
||||
editor,
|
||||
geometry,
|
||||
geospatial,
|
||||
VERSION
|
||||
) VALUES (
|
||||
p_objectid,
|
||||
|
||||
p_status,
|
||||
p_globalid,
|
||||
p_created_user,
|
||||
p_created_date,
|
||||
p_last_edited_user,
|
||||
p_last_edited_date,
|
||||
p_creationdate,
|
||||
p_creator,
|
||||
p_editdate,
|
||||
p_editor,
|
||||
p_geometry,
|
||||
p_geospatial,
|
||||
v_next_version
|
||||
);
|
||||
|
||||
-- Return success with new version
|
||||
RETURN QUERY SELECT TRUE AS row_inserted, v_next_version AS version_num;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
-- +goose StatementEnd
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue