sleede/fab-manager

rake rails db:seed fails when system `TZ` and `TIME_ZONE` do not match

trombik opened this issue · 3 comments

Describe the bug

rake rails db:seed fails when the system timezone is UTC, and TIME_ZONE is +09 (or any +N). this error is caused by DateTime.current, which can be future date in UTC.

fab-manager/db/seeds.rb

Lines 87 to 95 in e1256ec

# Create the default admin if none exists yet
if Role.where(name: 'admin').joins(:users).count.zero?
admin = User.new(username: 'admin', email: ENV['ADMIN_EMAIL'], password: ENV['ADMIN_PASSWORD'],
password_confirmation: Rails.application.secrets.admin_password, group_id: Group.find_by(slug: 'admins').id,
profile_attributes: { first_name: 'admin', last_name: 'admin', phone: '0123456789' },
statistic_profile_attributes: { gender: true, birthday: DateTime.current })
admin.add_role 'admin'
admin.save!
end

a possible fix is shown below.

diff --git a/db/seeds.rb b/db/seeds.rb
index b0b1088..4d32cbe 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -89,7 +89,7 @@ if Role.where(name: 'admin').joins(:users).count.zero?
   admin = User.new(username: 'admin', email: ENV['ADMIN_EMAIL'], password: ENV['ADMIN_PASSWORD'],
                    password_confirmation: Rails.application.secrets.admin_password, group_id: Group.find_by(slug: 'admins').id,
                    profile_attributes: { first_name: 'admin', last_name: 'admin', phone: '0123456789' },
-                   statistic_profile_attributes: { gender: true, birthday: DateTime.current })
+                   statistic_profile_attributes: { gender: true, birthday: DateTime.current - 1 })
   admin.add_role 'admin'
   admin.save!
 end

To Reproduce

Steps to reproduce the behavior:

  1. configure the system TZ to UTC
  2. set TIME_ZONE in .env to +N country, such as Asia/Tokyo
  3. run bundle exec rails db:seed in early morning or after midnight
  4. See below
rails aborted!
ActiveRecord::RecordInvalid: Validation failed: Statistic profile birthday The date of birth must be in the past
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/validations.rb:80:in `raise_validation_error'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/validations.rb:52:in `save!'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/transactions.rb:315:in `block in save!'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/transactions.rb:387:in `block in with_transaction_returning_status'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `block in transaction'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/connection_adapters/abstract/transaction.rb:239:in `block in within_new_transaction'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/connection_adapters/abstract/transaction.rb:236:in `synchronize'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/connection_adapters/abstract/transaction.rb:236:in `within_new_transaction'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `transaction'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/transactions.rb:212:in `transaction'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/transactions.rb:385:in `with_transaction_returning_status'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/transactions.rb:315:in `save!'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/suppressor.rb:48:in `save!'
/usr/local/fab/fab_manager/db/seeds.rb:94:in `<main>'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:55:in `load'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activesupport-5.2.6.3/lib/active_support/dependencies.rb:285:in `block in load'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activesupport-5.2.6.3/lib/active_support/dependencies.rb:257:in `load_dependency'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activesupport-5.2.6.3/lib/active_support/dependencies.rb:285:in `load'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/railties-5.2.6.3/lib/rails/engine.rb:556:in `load_seed'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/tasks/database_tasks.rb:281:in `load_seed'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activerecord-5.2.6.3/lib/active_record/railties/databases.rake:194:in `block (2 levels) in <main>'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/railties-5.2.6.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/railties-5.2.6.3/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/railties-5.2.6.3/lib/rails/command.rb:48:in `invoke'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/railties-5.2.6.3/lib/rails/commands.rb:18:in `<main>'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activesupport-5.2.6.3/lib/active_support/dependencies.rb:291:in `block in require'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activesupport-5.2.6.3/lib/active_support/dependencies.rb:257:in `load_dependency'
/usr/local/fab/fab_manager/vendor/bundle/ruby/2.7/gems/activesupport-5.2.6.3/lib/active_support/dependencies.rb:291:in `require'
bin/rails:4:in `<main>'

Expected behavior

bundle exec rails db:seed succeeds.

Screenshots

N/A

Server:

  • OS: FreeBSD 13.0
  • Kernel: FreeBSD default-freebsd-130-amd64.vagrantup.com 13.0-RELEASE-p8 FreeBSD 13.0-RELEASE-p8 #0: Tue Mar 15 09:36:28 UTC 2022 root@amd64-builder.daemonology.net:/usr/obj/usr/src/amd64.amd64/sys/GENERIC amd64
  • Fab-manager version v5.3.13 at eb6ce88db171a67de9e0c0e28401828ae9274b0d

Browser:

  • Name: Firefox
  • Version: 99.0_1

Additional context

another possible fix is to set either the system timezone or TIME_ZONE to an identical timezone. however, there are cases where system timezone in UTC is preferred, say, when you have machines in multiple time zones. also, TZ cannot be changed by users who do not have system-level privileges.

Hi Trombik,

Thanks for your report.

I think we simply should not use DateTime here since birthday is a date.
I reproduced the problem on my local and it seems that replacing DateTime.current with Date.current does the trick.

Issue will be fix in the next release.

Have a nice day

thanks for the answer. looking forward to the next release.

p.s. i have been impressed with the product.