Skip to content

Commit

Permalink
feat(ModelV2): store slug when inserting into the v2_rules view
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Sep 4, 2024
1 parent 508694d commit 18e440f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
46 changes: 46 additions & 0 deletions db/functions/v2_rules_insert_v02.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
CREATE OR REPLACE FUNCTION v2_rules_insert() RETURNS trigger LANGUAGE plpgsql AS
$func$
DECLARE result_id uuid;
BEGIN
INSERT INTO "rules" (
"ref_id",
"slug",
"title",
"severity",
"description",
"rationale",
"created_at",
"updated_at",
"remediation_available",
"benchmark_id",
"upstream",
"precedence",
"rule_group_id",
"value_checks",
"identifier"
) VALUES (
NEW."ref_id",
LOWER(REGEXP_REPLACE(NEW."ref_id", '\.', '-', 'g')),
NEW."title",
NEW."severity",
NEW."description",
NEW."rationale",
NEW."created_at",
NEW."updated_at",
NEW."remediation_available",
NEW."security_guide_id",
NEW."upstream",
NEW."precedence",
NEW."rule_group_id",
NEW."value_checks",
NEW."identifier"
) RETURNING "id" INTO "result_id";

-- Insert a new rule reference record separately
INSERT INTO "rule_references_containers" ("rule_references", "rule_id", "created_at", "updated_at")
SELECT NEW."references", "result_id", NOW(), NOW();

NEW."id" := "result_id";
RETURN NEW;
END
$func$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class UpdateFunctionV2RulesInsertToVersion2 < ActiveRecord::Migration[7.1]
def change
drop_trigger :v2_rules_insert, on: :v2_rules
update_function :v2_rules_insert, version: 2, revert_to_version: 1
create_trigger :v2_rules_insert, on: :v2_rules
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_09_03_073330) do
ActiveRecord::Schema[7.1].define(version: 2024_09_04_141834) do
create_schema "inventory"

# These are extensions that must be enabled in order to support this database
Expand Down Expand Up @@ -530,6 +530,7 @@
BEGIN
INSERT INTO "rules" (
"ref_id",
"slug",
"title",
"severity",
"description",
Expand All @@ -545,6 +546,7 @@
"identifier"
) VALUES (
NEW."ref_id",
LOWER(REGEXP_REPLACE(NEW."ref_id", '\.', '-', 'g')),
NEW."title",
NEW."severity",
NEW."description",
Expand Down

0 comments on commit 18e440f

Please sign in to comment.