aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMasaya Tojo <masaya@tojo.tokyo>2023-03-12 16:45:59 +0900
committerMasaya Tojo <masaya@tojo.tokyo>2023-03-12 23:38:12 +0900
commit40c84f86e9d7326bb0020c9210e0689179b1102c (patch)
tree6c884c18704fa726f1784fd6884048897b16d743
parent1a838c115140bdf0f35054351153fb21f2a228da (diff)
Add function to add new item.
-rw-r--r--deploy/insert_item.sql25
-rw-r--r--revert/insert_item.sql7
-rw-r--r--sqitch.plan1
-rw-r--r--verify/insert_item.sql31
4 files changed, 64 insertions, 0 deletions
diff --git a/deploy/insert_item.sql b/deploy/insert_item.sql
new file mode 100644
index 0000000..9f4e050
--- /dev/null
+++ b/deploy/insert_item.sql
@@ -0,0 +1,25 @@
+-- Deploy kakeibo:insert_item to pg
+-- requires: schema
+-- requires: items
+
+BEGIN;
+
+CREATE OR REPLACE FUNCTION kakeibo.insert_item(
+ p_transaction_id INTEGER,
+ p_category TEXT,
+ p_subcategory TEXT,
+ p_amount INTEGER,
+ p_note TEXT
+) RETURNS INTEGER AS $$
+ DECLARE
+ inserted_id INTEGER;
+ BEGIN
+ INSERT INTO kakeibo.items (transaction_id, category, subcategory, amount, note)
+ VALUES (p_transaction_id, p_category, p_subcategory, p_amount, p_note)
+ RETURNING id INTO inserted_id;
+
+ RETURN inserted_id;
+ END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+COMMIT;
diff --git a/revert/insert_item.sql b/revert/insert_item.sql
new file mode 100644
index 0000000..40417f0
--- /dev/null
+++ b/revert/insert_item.sql
@@ -0,0 +1,7 @@
+-- Revert kakeibo:insert_item from pg
+
+BEGIN;
+
+DROP FUNCTION kakeibo.insert_item;
+
+COMMIT;
diff --git a/sqitch.plan b/sqitch.plan
index 6e2ba7b..fd1bbb4 100644
--- a/sqitch.plan
+++ b/sqitch.plan
@@ -6,3 +6,4 @@ schema 2023-03-11T15:56:41Z Masaya Tojo <masaya@tojo.tokyo> # Add schema for use
transactions [schema] 2023-03-11T16:50:25Z Masaya Tojo <masaya@tojo.tokyo> # Add table for tracking transactions.
items [schema transactions] 2023-03-12T04:48:30Z Masaya Tojo <masaya@tojo.tokyo> # Add table for tracking items.
insert_transaction [schema transactions] 2023-03-12T07:06:15Z Masaya Tojo <masaya@tojo.tokyo> # Add function to create new transaction.
+insert_item [schema items] 2023-03-12T07:25:23Z Masaya Tojo <masaya@tojo.tokyo> # Add function to add new item.
diff --git a/verify/insert_item.sql b/verify/insert_item.sql
new file mode 100644
index 0000000..f1ce212
--- /dev/null
+++ b/verify/insert_item.sql
@@ -0,0 +1,31 @@
+-- Verify kakeibo:insert_item on pg
+
+BEGIN;
+
+SELECT has_function_privilege('kakeibo.insert_item(INTEGER, TEXT, TEXT, INTEGER, TEXT)', 'EXECUTE');
+
+SET search_path = kakeibo;
+
+DO $$
+ DECLARE
+ v_transaction_id INTEGER;
+ v_item_id INTEGER;
+ BEGIN
+ SELECT insert_transaction(0, '2023-03-15', 'テスト') INTO v_transaction_id;
+ SELECT insert_item(v_transaction_id, 'カテ', 'サブカテ', 298, 'メモ') INTO v_item_id;
+
+ PERFORM *
+ FROM items
+ WHERE id = v_item_id
+ AND transaction_id = v_transaction_id
+ AND category = 'カテ'
+ AND subcategory = 'サブカテ'
+ AND amount = 298
+ AND note = 'メモ';
+ IF NOT FOUND THEN
+ RAISE EXCEPTION 'Inserted data is not found.';
+ END IF;
+ END
+$$;
+
+ROLLBACK;