Upgrade to Pro — share decks privately, control downloads, hide ads and more …

最新のRailsでPostgreSQLを使う良さ

nyo_taro
November 27, 2023
370

 最新のRailsでPostgreSQLを使う良さ

- 前提
- PostgreSQLとは
- SaaSにおけるPostgreSQLの利用
- RailsのPostgreSQLサポート

nyo_taro

November 27, 2023
Tweet

Transcript

  1. 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
  2. JSON 、 JSONB データ型 : 非構造化データや動的なスキーマを持つデータ を扱うことができます。 配列データ型 : 単一のカラムに複数の値を格納することができます。

    hstore: キーと値のペアを格納するためのデータ型 範囲型( Range Types ) : 日付、数値、時間の範囲を表すためのデータ型 Rails の PostgreSQL サポート ( 一部抜粋 ) @nyo_taro
  3. ENUM の migration が使いやすくなる 外部キー制約に deferrable を指定可能になった PostgreSQL12 生成列( generated

    column) のサポート PostgreSQL15 NULLS NOT DISTINCT をサポート Rails7 での PostgreSQL サポートの強化 @nyo_taro
  4. 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
  5. 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
  6. user = User.create(first_name: ' 太郎', last_name: ' 山田') User.last.full_name #

    => " 山田太郎" PostgreSQL12 生成列( generated column) のサポート @nyo_taro
  7. message が同じでも tag が NULL であれば、複数のレコードが許容 UNIQUE (message, tag) message

    が同じで tag が NULL のレコードは一つしか許容されない UNIQUE NULLS NOT DISTINCT(message, tag) PostgreSQL15 NULLS NOT DISTINCT @nyo_taro
  8. `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