Skip to content

Commit d6e3a24

Browse files
committed
Use ULID for user primary key
BREAKING CHANGE: This change breaks all sorts of rules by editing migrations. This would not be normally recommended but we’re allowing an exception for this new feature which is still behind a feature flag. Adds the sqlite-ulid gem Updates database.yml to enable support for the new extension Modifies existing migration to make user primary key ULIDs and updates the type for user foreign keys Credit for implementation to https://fractaledmind.github.io/2023/09/22/enhancing-rails-sqlite-ulid-primary-keys/
1 parent 765cf12 commit d6e3a24

File tree

8 files changed

+16
-7
lines changed

8 files changed

+16
-7
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ gem "rails", "~> 7.1", group: [:default, :wasm] # Bundle edge Rails instead: gem
88
gem "puma", ">= 5.0" # Use the Puma web server [https://github.com/puma/puma]
99
gem "sqlite3", force_ruby_platform: true # Use sqlite3 as the database for Active Record [https://github.com/sparklemotion/sqlite3-ruby]
1010
gem "activerecord-enhancedsqlite3-adapter" # Enhanced SQLite3 adapter for Active Record [https://github.com/fractaledmind/activerecord-enhancedsqlite3-adapter]
11+
gem "sqlite-ulid" # A SQLite extension for generating and working with ULIDs [https://github.com/asg017/sqlite-ulid]
1112

1213
gem "solid_cache" # A database-backed ActiveSupport::Cache::Store [https://github.com/rails/solid_cache]
1314
gem "solid_queue" # A database-backed ActiveJob backend [https://github.com/basecamp/solid_queue]

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ GEM
457457
actionpack (>= 5.2)
458458
activesupport (>= 5.2)
459459
sprockets (>= 3.0.0)
460+
sqlite-ulid (0.2.1-arm64-darwin)
461+
sqlite-ulid (0.2.1-x86_64-linux)
460462
sqlite3 (1.7.3)
461463
mini_portile2 (~> 2.8.0)
462464
standard (1.35.1)
@@ -565,6 +567,7 @@ DEPENDENCIES
565567
sitepress-rails
566568
solid_cache
567569
solid_queue
570+
sqlite-ulid
568571
sqlite3
569572
standard
570573
stimulus-rails

config/database.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ default: &default
88
adapter: sqlite3
99
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 3 } %>
1010
timeout: 5000
11+
extensions:
12+
- sqlite_ulid
1113

1214
development:
1315
primary:

db/migrate/20240605030240_create_users.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class CreateUsers < ActiveRecord::Migration[7.1]
22
def change
3-
create_table :users do |t|
3+
create_table :users, force: true, id: false do |t|
4+
t.primary_key :id, :string, default: -> { "ULID()" }
45
t.string :email, null: false
56
t.string :password_digest, null: false
67

db/migrate/20240607120410_create_email_exchanges.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class CreateEmailExchanges < ActiveRecord::Migration[7.1]
22
def change
33
create_table :email_exchanges do |t|
44
t.string :email, null: false
5-
t.references :user, null: false, foreign_key: true
5+
t.references :user, null: false, foreign_key: true, type: :string
66
t.string :status, null: false, default: 0 # pending
77

88
t.timestamps

db/migrate/20240609132723_create_notifications.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def change
33
create_table :notifications do |t|
44
t.string :type
55
t.references :notification_event, null: false, foreign_key: true
6-
t.references :recipient, polymorphic: true, null: false, type: :bigint
6+
t.references :recipient, polymorphic: true, null: false, type: :string
77
t.datetime :read_at
88
t.datetime :seen_at
99

db/schema.rb

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/views/components/code_block/app_file_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ def render_page(*, **)
3333
end
3434

3535
it "renders contents of file by heterogeneous line number collection" do
36-
page = render_page(CodeBlock::AppFile.new("config/database.yml", lines: [7..8, 58]))
36+
page = render_page(CodeBlock::AppFile.new("config/database.yml", lines: [7..8, 60]))
3737
expect(page).to have_content(<<~YAML.strip)
3838
default: &default
3939
adapter: sqlite3
4040
YAML
4141

42+
puts page
43+
4244
expect(page).to have_content("database: storage/production/data.sqlite3")
4345
expect(page).not_to have_content("database: storage/development/data.sqlite3")
4446
end

0 commit comments

Comments
 (0)