Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
最新のRailsでPostgreSQLを使う良さ
Search
nyo_taro
November 27, 2023
0
370
最新のRailsでPostgreSQLを使う良さ
- 前提
- PostgreSQLとは
- SaaSにおけるPostgreSQLの利用
- RailsのPostgreSQLサポート
nyo_taro
November 27, 2023
Tweet
Share
More Decks by nyo_taro
See All by nyo_taro
プロダクト価値を考えるための情報透明化とチーム文化づくり
nyo_taro
1
120
過去の失敗からリアーキテクチャをやらないと判断した理由
nyo_taro
0
95
ドキュメントファーストの非同期文化で知ったこと
nyo_taro
1
190
チームで品質を高めながらSaaS開発を続けるためにやったこと.pdf
nyo_taro
0
38
Featured
See All Featured
Six Lessons from altMBA
skipperchong
26
3.4k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
130k
Code Review Best Practice
trishagee
62
16k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
23k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
27
1.9k
How GitHub Uses GitHub to Build GitHub
holman
473
290k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
Building Better People: How to give real-time feedback that sticks.
wjessup
362
19k
Into the Great Unknown - MozCon
thekraken
31
1.4k
Making Projects Easy
brettharned
115
5.9k
Scaling GitHub
holman
458
140k
Transcript
最新の Rails で PostgreSQL を使う良さ @nyo_taro
井上 翔太朗 (@nyo_taro) セールステックのエンジニア 好きなエディタは VScode 特徴 ・八王子に生息 ・コーヒーは最近浅煎り ・
ZORN のファン 自己紹介 @nyo_taro
@nyo_taro
前提 PostgreSQL とは SaaS における PostgreSQL の利用 Rails の PostgreSQL
サポート 今回のはなすこと @nyo_taro
今日はこんな話をします Rails で使える PostgreSQL で基本的な機能について Rails と PostgreSQL の最新の使用することで得られる恩恵についてお話 します。
前提 @nyo_taro
PostgreSQL は、カリフォルニア大学バークレー校のコンピューター サイエン ス学部で開発された、オブジェクト リレーショナル データベース管理システ ム ( ORDBMS )
です。 PostgreSQL とは @nyo_taro
Rails では PostgreSQL はデフォルトでサポートされており Rails ガイドも PostgreSQL の使い方が丁寧に記載されています。 https://railsguides.jp/active_record_postgresql.html Rails
と PostgreSQL @nyo_taro
@nyo_taro
SaaS における PostgreSQL の利用 @nyo_taro
2016 年バージョン 9.5 で行単位セキュリティ( RLS )が追加された! 行レベルで DB テナントごとに取得・追加・削除・更新を保護してくれ許 可しなければその操作は拒否される
RLS(Row Level Security) @nyo_taro
複数企業 ( テナント ) が 1 つのシステムを共有する シングルテナントに比べて低コストで利用できる マルチテナント @nyo_taro
RLS あり RLS なし select * from users id name
tenant_id 1 a 1 4 b 1 select * from users id name tenant_id 1 a 1 2 a 2 3 a 3 4 b 1 @nyo_taro
PostgreSQL の機能の Rails サポート @nyo_taro
JSON 、 JSONB データ型 : 非構造化データや動的なスキーマを持つデータ を扱うことができます。 配列データ型 : 単一のカラムに複数の値を格納することができます。
hstore: キーと値のペアを格納するためのデータ型 範囲型( Range Types ) : 日付、数値、時間の範囲を表すためのデータ型 Rails の PostgreSQL サポート ( 一部抜粋 ) @nyo_taro
ENUM の migration が使いやすくなる 外部キー制約に deferrable を指定可能になった PostgreSQL12 生成列( generated
column) のサポート PostgreSQL15 NULLS NOT DISTINCT をサポート Rails7 での PostgreSQL サポートの強化 @nyo_taro
ENUM の migration が使いやすくなる before execute <<-SQL CREATE TYPE status
AS ENUM ('draft', 'published', 'archived', 'trashed'); SQL create_table :articles do |t| t.column :current_status, :status end after change_table :articles do |t| t.enum :current_status, enum_type: "status", default: "draft", null: false end create_enum :status, %w[draft published archived trashed] @nyo_taro
before execute <<-SQL ALTER TABLE users ADD COLUMN full_name character
varying GENERATED ALWAYS AS (last_name || first_name) STORED SQL after add_column :users, :full_name, :virtual, type: :text, as: "last_name || first_name", stored: true PostgreSQL12 生成列( generated column) のサポート @nyo_taro
user = User.create(first_name: ' 太郎', last_name: ' 山田') User.last.full_name #
=> " 山田太郎" PostgreSQL12 生成列( generated column) のサポート @nyo_taro
Rails 内でのテストも追加される @nyo_taro
message が同じでも tag が NULL であれば、複数のレコードが許容 UNIQUE (message, tag) message
が同じで tag が NULL のレコードは一つしか許容されない UNIQUE NULLS NOT DISTINCT(message, tag) PostgreSQL15 NULLS NOT DISTINCT @nyo_taro
`UNIQUE (message, tag)` `UNIQUE NULLS NOT DISTINCT(message, tag)` id message
tag 1 hello NULL 2 hello NULL 3 hello NULL id message tag 1 hello NULL 2 hello tag1 3 hello tag2 PostgreSQL15 NULLS NOT DISTINCT @nyo_taro
Rails を最新にすることで Rails の機能を使って DB の新機能を使える Rails を使うことでサポートしていないなどのテストもある PostgreSQL の最新の機能を使えることで開発の選択肢が広がる
まとめ @nyo_taro
ご静聴ありがとうございました! @nyo_taro