This repository has been archived by the owner on Dec 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from walnuts1018/initDB
Init db
- Loading branch information
Showing
3 changed files
with
184 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
-- 公開タイプの列挙型を定義 | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'public_type') THEN | ||
CREATE TYPE public_type AS ENUM ('private', 'public', 'restricted'); | ||
END IF; | ||
END$$; | ||
|
||
-- ユーザーテーブル | ||
CREATE TABLE IF NOT EXISTS users ( | ||
id BIGSERIAL PRIMARY KEY | ||
); | ||
|
||
-- ユーザーグループテーブル | ||
CREATE TABLE IF NOT EXISTS user_groups ( | ||
id BIGSERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
creator_id BIGINT NOT NULL, | ||
FOREIGN KEY (creator_id) REFERENCES users(id) | ||
); | ||
|
||
-- マネープールテーブル | ||
CREATE TABLE IF NOT EXISTS money_pool ( | ||
id BIGSERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
description TEXT, | ||
type public_type NOT NULL, | ||
owner_id BIGINT NOT NULL, | ||
FOREIGN KEY (owner_id) REFERENCES users(id) | ||
); | ||
|
||
-- マネープロバイダーテーブル | ||
CREATE TABLE IF NOT EXISTS money_provider ( | ||
id BIGSERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
creator_id BIGINT NOT NULL, | ||
balance DECIMAL(19,4) NOT NULL CHECK (balance >= 0), | ||
FOREIGN KEY (creator_id) REFERENCES users(id) | ||
); | ||
|
||
-- 店舗テーブル | ||
CREATE TABLE IF NOT EXISTS store ( | ||
id BIGSERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
creator_id BIGINT NOT NULL, | ||
FOREIGN KEY (creator_id) REFERENCES users(id) | ||
); | ||
|
||
-- 商品テーブル | ||
CREATE TABLE IF NOT EXISTS item ( | ||
id BIGSERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
creator_id BIGINT NOT NULL, | ||
FOREIGN KEY (creator_id) REFERENCES users(id) | ||
); | ||
|
||
-- ラベルテーブル | ||
CREATE TABLE IF NOT EXISTS label ( | ||
id BIGSERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
creator_id BIGINT NOT NULL, | ||
FOREIGN KEY (creator_id) REFERENCES users(id) | ||
); | ||
|
||
-- 取引テーブル | ||
CREATE TABLE IF NOT EXISTS payment ( | ||
id BIGSERIAL PRIMARY KEY, | ||
money_pool_id BIGINT NOT NULL, | ||
date DATE NOT NULL, | ||
title VARCHAR(255) NOT NULL, | ||
amount DECIMAL(19,4) NOT NULL, | ||
description TEXT, | ||
is_planned BOOLEAN NOT NULL, | ||
store_id BIGINT, | ||
FOREIGN KEY (money_pool_id) REFERENCES money_pool(id), | ||
FOREIGN KEY (store_id) REFERENCES store(id) | ||
); | ||
|
||
-- 商品取引テーブル | ||
CREATE TABLE IF NOT EXISTS item_payment ( | ||
payment_id BIGINT NOT NULL, | ||
item_id BIGINT NOT NULL, | ||
quantity BIGINT NOT NULL CHECK (quantity > 0), | ||
PRIMARY KEY (payment_id, item_id), | ||
FOREIGN KEY (payment_id) REFERENCES payment(id), | ||
FOREIGN KEY (item_id) REFERENCES item(id) | ||
); | ||
|
||
-- ユーザーグループ所属テーブル | ||
CREATE TABLE IF NOT EXISTS user_group_membership ( | ||
group_id BIGINT NOT NULL, | ||
user_id BIGINT NOT NULL, | ||
PRIMARY KEY (group_id, user_id), | ||
FOREIGN KEY (group_id) REFERENCES user_groups(id), | ||
FOREIGN KEY (user_id) REFERENCES users(id) | ||
); | ||
|
||
-- 限定公開範囲テーブル | ||
CREATE TABLE IF NOT EXISTS restricted_publication_scope ( | ||
pool_id BIGINT NOT NULL, | ||
group_id BIGINT NOT NULL, | ||
PRIMARY KEY (pool_id, group_id), | ||
FOREIGN KEY (pool_id) REFERENCES money_pool(id) ON UPDATE CASCADE ON DELETE CASCADE, | ||
FOREIGN KEY (group_id) REFERENCES user_groups(id) | ||
); | ||
|
||
-- 制約:限定公開範囲のプールIDに対応するマネープールの公開タイプは限定公開である | ||
ALTER TABLE restricted_publication_scope | ||
ADD CONSTRAINT fk_money_pool_restricted | ||
CHECK ((SELECT type FROM money_pool WHERE id = pool_id) = 'restricted'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters