forked from fixin.me/fixin.me
Generate admin password on db:seed
Test admin account creation in db:seed
This commit is contained in:
11
DESIGN.md
11
DESIGN.md
@@ -32,3 +32,14 @@ whenever a change is considered, to avoid regressions.
|
|||||||
* as a standard with hardware implementations ensures both: computing
|
* as a standard with hardware implementations ensures both: computing
|
||||||
efficiency and hardware/3rd party library compatibility as opposed to Ruby
|
efficiency and hardware/3rd party library compatibility as opposed to Ruby
|
||||||
custom `BigDecimal` type
|
custom `BigDecimal` type
|
||||||
|
|
||||||
|
### Database layer vs application layer data model constraints
|
||||||
|
* database constraints are the final frontier against data corruption,
|
||||||
|
* they should safeguard against data _consistency_ loss under _all_ data
|
||||||
|
(not schema) manipulation scenarios, including application level logic
|
||||||
|
errors and direct data manipulation,
|
||||||
|
* application constraints can be as restrictive as database constraints or more,
|
||||||
|
but not less, as it doesn't serve any use case,
|
||||||
|
* proper application level constraints should prevent unhandled database
|
||||||
|
exception occurences, e.g `ActiveRecord::InvalidForeignKey` for operations
|
||||||
|
performed through Models (i.e. not `#delete_all` etc.)
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ Devise.setup do |config|
|
|||||||
|
|
||||||
# ==> Configuration for :validatable
|
# ==> Configuration for :validatable
|
||||||
# Range for password length.
|
# Range for password length.
|
||||||
config.password_length = 5..128
|
config.password_length = 5..32
|
||||||
|
|
||||||
# Email regex used to validate email formats. It simply asserts that
|
# Email regex used to validate email formats. It simply asserts that
|
||||||
# one (and only one) @ exists in the given string. This is mainly
|
# one (and only one) @ exists in the given string. This is mainly
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class CreateUnits < ActiveRecord::Migration[7.0]
|
class CreateUnits < ActiveRecord::Migration[7.0]
|
||||||
def change
|
def change
|
||||||
create_table :units do |t|
|
create_table :units do |t|
|
||||||
t.references :user, foreign_key: true
|
t.references :user, foreign_key: {on_delete: :cascade}
|
||||||
t.string :symbol, null: false, limit: 15
|
t.string :symbol, null: false, limit: 15
|
||||||
t.text :description
|
t.text :description
|
||||||
t.decimal :multiplier, null: false, precision: 30, scale: 15, default: 1.0
|
t.decimal :multiplier, null: false, precision: 30, scale: 15, default: 1.0
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class CreateQuantities < ActiveRecord::Migration[7.2]
|
class CreateQuantities < ActiveRecord::Migration[7.2]
|
||||||
def change
|
def change
|
||||||
create_table :quantities do |t|
|
create_table :quantities do |t|
|
||||||
t.references :user, foreign_key: true
|
t.references :user, foreign_key: {on_delete: :cascade}
|
||||||
t.string :name, null: false, limit: 31
|
t.string :name, null: false, limit: 31
|
||||||
t.text :description
|
t.text :description
|
||||||
t.references :parent, foreign_key: {to_table: :quantities, on_delete: :cascade}
|
t.references :parent, foreign_key: {to_table: :quantities, on_delete: :cascade}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class CreateReadouts < ActiveRecord::Migration[7.2]
|
class CreateReadouts < ActiveRecord::Migration[7.2]
|
||||||
def change
|
def change
|
||||||
create_table :readouts do |t|
|
create_table :readouts do |t|
|
||||||
t.references :user, null: false, foreign_key: true
|
t.references :user, null: false, foreign_key: {on_delete: :cascade}
|
||||||
t.references :quantity, null: false, foreign_key: true
|
t.references :quantity, null: false, foreign_key: true
|
||||||
# :category + :value + :unit as a separate table? (NumericValue, TextValue)
|
# :category + :value + :unit as a separate table? (NumericValue, TextValue)
|
||||||
t.integer :category, null: false, default: 0
|
t.integer :category, null: false, default: 0
|
||||||
|
|||||||
@@ -71,10 +71,10 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_21_230456) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
add_foreign_key "quantities", "quantities", column: "parent_id", on_delete: :cascade
|
add_foreign_key "quantities", "quantities", column: "parent_id", on_delete: :cascade
|
||||||
add_foreign_key "quantities", "users"
|
add_foreign_key "quantities", "users", on_delete: :cascade
|
||||||
add_foreign_key "readouts", "quantities"
|
add_foreign_key "readouts", "quantities"
|
||||||
add_foreign_key "readouts", "units"
|
add_foreign_key "readouts", "units"
|
||||||
add_foreign_key "readouts", "users"
|
add_foreign_key "readouts", "users", on_delete: :cascade
|
||||||
add_foreign_key "units", "units", column: "base_id", on_delete: :cascade
|
add_foreign_key "units", "units", column: "base_id", on_delete: :cascade
|
||||||
add_foreign_key "units", "users"
|
add_foreign_key "units", "users", on_delete: :cascade
|
||||||
end
|
end
|
||||||
|
|||||||
14
db/seeds.rb
14
db/seeds.rb
@@ -7,17 +7,21 @@
|
|||||||
User.transaction do
|
User.transaction do
|
||||||
break if User.find_by status: :admin
|
break if User.find_by status: :admin
|
||||||
|
|
||||||
User.create! email: Rails.configuration.admin, password: 'admin', status: :admin do |user|
|
email = Rails.configuration.admin
|
||||||
|
password_length = SecureRandom.rand(Rails.configuration.devise.password_length)
|
||||||
|
password = SecureRandom.alphanumeric(password_length)
|
||||||
|
|
||||||
|
User.create!(email: email, password: password, status: :admin) do |user|
|
||||||
user.skip_confirmation!
|
user.skip_confirmation!
|
||||||
print "Creating #{user.status} account '#{user.email}' with password '#{user.password}'..."
|
print "Creating #{user.status} account '#{user.email}'" \
|
||||||
|
" with password '#{user.password}'..."
|
||||||
end
|
end
|
||||||
puts "done."
|
puts "done."
|
||||||
|
|
||||||
rescue ActiveRecord::RecordInvalid => exception
|
rescue ActiveRecord::RecordInvalid => exception
|
||||||
puts "failed. #{exception.message}"
|
puts "failed.", exception.message
|
||||||
end
|
end
|
||||||
|
|
||||||
# Formulas will be deleted as dependent on Quantities
|
# Formulas will be deleted as dependent on Quantities
|
||||||
#[Source, Quantity, Unit].each { |model| model.defaults.delete_all }
|
#[Source, Quantity, Unit].each { |model| model.defaults.delete_all }
|
||||||
|
|
||||||
require_relative 'seeds/units.rb'
|
load "db/seeds/units.rb"
|
||||||
|
|||||||
@@ -71,10 +71,10 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_21_230456) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
add_foreign_key "quantities", "quantities", column: "parent_id", on_delete: :cascade
|
add_foreign_key "quantities", "quantities", column: "parent_id", on_delete: :cascade
|
||||||
add_foreign_key "quantities", "users"
|
add_foreign_key "quantities", "users", on_delete: :cascade
|
||||||
add_foreign_key "readouts", "quantities"
|
add_foreign_key "readouts", "quantities"
|
||||||
add_foreign_key "readouts", "units"
|
add_foreign_key "readouts", "units"
|
||||||
add_foreign_key "readouts", "users"
|
add_foreign_key "readouts", "users", on_delete: :cascade
|
||||||
add_foreign_key "units", "units", column: "base_id", on_delete: :cascade
|
add_foreign_key "units", "units", column: "base_id", on_delete: :cascade
|
||||||
add_foreign_key "units", "users"
|
add_foreign_key "units", "users", on_delete: :cascade
|
||||||
end
|
end
|
||||||
|
|||||||
24
test/system/tasks_test.rb
Normal file
24
test/system/tasks_test.rb
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
require 'application_system_test_case'
|
||||||
|
|
||||||
|
# NOTE: remove constant to avoid warnings due to double loading of
|
||||||
|
# rails/tasks/statistics.rake. To be removed after upgrade to Rails 8.1.
|
||||||
|
Object.send(:remove_const, :STATS_DIRECTORIES)
|
||||||
|
|
||||||
|
Rails.application.load_tasks
|
||||||
|
# NOTE: for some reason task for checking pending migrations messes up
|
||||||
|
# transaction when run during test. It causes all DB changes made before its
|
||||||
|
# execution to be rolled back.
|
||||||
|
# Run it before tests, so any rake task dependent on it will see it as
|
||||||
|
# #already_invoked and won't run it during test. It is redundant anyway, as
|
||||||
|
# migrations are run before starting test suite.
|
||||||
|
Rake::Task['db:abort_if_pending_migrations'].invoke
|
||||||
|
|
||||||
|
class TasksTest < ApplicationSystemTestCase
|
||||||
|
test "db:seed creates admin account" do
|
||||||
|
User.admin.delete_all
|
||||||
|
assert_output /Creating admin account/ do
|
||||||
|
Rake::Task['db:seed'].execute
|
||||||
|
end
|
||||||
|
assert User.admin.exists?
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user