Skip to content

Commit

Permalink
Merge pull request #595 from Freika/chore/database-user-constraints
Browse files Browse the repository at this point in the history
Add database constraints for users table
  • Loading branch information
Freika authored Jan 22, 2025
2 parents 94712b5 + f402755 commit f472f6b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ gem 'sidekiq-cron'
gem 'sidekiq-limit_fetch'
gem 'sprockets-rails'
gem 'stimulus-rails'
gem 'strong_migrations'
gem 'tailwindcss-rails'
gem 'turbo-rails'
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
Expand All @@ -54,6 +55,7 @@ group :test do
end

group :development do
gem 'database_consistency', require: false
gem 'foreman'
gem 'rubocop-rails', require: false
end
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ GEM
data_migrate (11.2.0)
activerecord (>= 6.1)
railties (>= 6.1)
database_consistency (2.0.0)
activerecord (>= 3.2)
date (3.4.1)
debug (1.10.0)
irb (~> 1.10)
Expand Down Expand Up @@ -392,6 +394,8 @@ GEM
stimulus-rails (1.3.4)
railties (>= 6.0.0)
stringio (3.1.2)
strong_migrations (2.1.0)
activerecord (>= 6.1)
super_diff (0.15.0)
attr_extras (>= 6.2.4)
diff-lcs
Expand Down Expand Up @@ -442,6 +446,7 @@ DEPENDENCIES
bootsnap
chartkick
data_migrate
database_consistency
debug
devise
dotenv-rails
Expand Down Expand Up @@ -478,6 +483,7 @@ DEPENDENCIES
simplecov
sprockets-rails
stimulus-rails
strong_migrations
super_diff
tailwindcss-rails
turbo-rails
Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class User < ApplicationRecord
after_create :create_api_key
before_save :strip_trailing_slashes

validates :email, presence: true
validates :reset_password_token, uniqueness: true, allow_nil: true

attribute :admin, :boolean, default: false

def countries_visited
stats.pluck(:toponyms).flatten.map { _1['country'] }.uniq.compact
end
Expand Down
26 changes: 26 additions & 0 deletions config/initializers/strong_migrations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Mark existing migrations as safe
StrongMigrations.start_after = 20_250_122_150_500

# Set timeouts for migrations
# If you use PgBouncer in transaction mode, delete these lines and set timeouts on the database user
StrongMigrations.lock_timeout = 10.seconds
StrongMigrations.statement_timeout = 1.hour

# Analyze tables after indexes are added
# Outdated statistics can sometimes hurt performance
StrongMigrations.auto_analyze = true

# Set the version of the production database
# so the right checks are run in development
# StrongMigrations.target_version = 10

# Add custom checks
# StrongMigrations.add_check do |method, args|
# if method == :add_index && args[0].to_s == "users"
# stop! "No more indexes on the users table"
# end
# end

# Make some operations safe by default
# See https://github.com/ankane/strong_migrations#safe-by-default
# StrongMigrations.safe_by_default = true
8 changes: 8 additions & 0 deletions db/migrate/20241226202204_add_database_users_constraints.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class AddDatabaseUsersConstraints < ActiveRecord::Migration[8.0]
def change
add_check_constraint :users, 'email IS NOT NULL', name: 'users_email_null', validate: false
add_check_constraint :users, 'admin IS NOT NULL', name: 'users_admin_null', validate: false
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class ValidateAddDatabaseUsersConstraints < ActiveRecord::Migration[8.0]
def up
validate_check_constraint :users, name: 'users_email_null'
change_column_null :users, :email, false
remove_check_constraint :users, name: 'users_email_null'
end

def down
add_check_constraint :users, 'email IS NOT NULL', name: 'users_email_null', validate: false
change_column_null :users, :email, true
end
end
2 changes: 2 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f472f6b

Please sign in to comment.