-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7827] - Encapsulate Withdrawal (#4826)
- Loading branch information
Showing
13 changed files
with
208 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: trainee_withdrawals | ||
# | ||
# id :bigint not null, primary key | ||
# another_reason :string | ||
# date :date | ||
# discarded_at :datetime | ||
# future_interest :enum | ||
# trigger :enum | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# trainee_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_trainee_withdrawals_on_discarded_at (discarded_at) | ||
# index_trainee_withdrawals_on_trainee_id (trainee_id) | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (trainee_id => trainees.id) | ||
# | ||
class TraineeWithdrawal < ApplicationRecord | ||
belongs_to :trainee | ||
has_many :trainee_withdrawal_reasons, dependent: :destroy | ||
|
||
enum :trigger, { provider: "provider", trainee: "trainee" }, prefix: :triggered_by | ||
enum :future_interest, { yes: "yes", no: "no", unknown: "unknown" }, suffix: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
db/data/20241120205446_migrate_withdrawal_data_to_withdrawals.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
class MigrateWithdrawalDataToWithdrawals < ActiveRecord::Migration[7.2] | ||
def up | ||
safety_assured do | ||
execute <<~SQL | ||
WITH new_withdrawals AS ( | ||
INSERT INTO trainee_withdrawals (trainee_id, date, created_at, updated_at) | ||
SELECT id, withdraw_date, NOW(), NOW() | ||
FROM trainees | ||
WHERE state = 4 | ||
RETURNING trainee_id, id | ||
) | ||
UPDATE trainee_withdrawal_reasons twr | ||
SET trainee_withdrawal_id = nw.id | ||
FROM new_withdrawals nw | ||
WHERE twr.trainee_id = nw.trainee_id; | ||
SQL | ||
end | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateTraineeWithdrawals < ActiveRecord::Migration[7.2] | ||
def up | ||
safety_assured { | ||
execute <<-SQL | ||
CREATE TYPE trigger_type AS ENUM ('provider', 'trainee'); | ||
CREATE TYPE future_interest_type AS ENUM ('yes', 'no', 'unknown'); | ||
SQL | ||
} | ||
|
||
create_table :trainee_withdrawals do |t| | ||
t.belongs_to :trainee, null: false, foreign_key: true | ||
t.date :date | ||
t.column :trigger, :trigger_type | ||
t.string :another_reason | ||
t.column :future_interest, :future_interest_type | ||
t.datetime :discarded_at | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :trainee_withdrawals, :discarded_at | ||
end | ||
|
||
def down | ||
drop_table :trainee_withdrawals | ||
|
||
safety_assured { | ||
execute <<-SQL | ||
DROP TYPE trigger_type; | ||
DROP TYPE future_interest_type; | ||
SQL | ||
} | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
db/migrate/20241120115559_associate_withdrawal_with_trainee_withdrawal_reason.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
class AssociateWithdrawalWithTraineeWithdrawalReason < ActiveRecord::Migration[7.2] | ||
disable_ddl_transaction! | ||
|
||
def up | ||
# Add the withdrawal_id column | ||
add_column :trainee_withdrawal_reasons, :trainee_withdrawal_id, :bigint | ||
|
||
# Add the foreign key without validation | ||
add_foreign_key :trainee_withdrawal_reasons, :trainee_withdrawals, column: :trainee_withdrawal_id, validate: false | ||
|
||
# Add the index concurrently | ||
add_index :trainee_withdrawal_reasons, %i[trainee_withdrawal_id withdrawal_reason_id], | ||
unique: true, | ||
name: "uniq_idx_trainee_withdrawal_id_withdrawal_reason_id", | ||
algorithm: :concurrently | ||
end | ||
|
||
def down | ||
# Remove the index | ||
remove_index :trainee_withdrawal_reasons, name: "uniq_idx_trainee_withdrawal_id_withdrawal_reason_id" | ||
|
||
# Remove the foreign key | ||
remove_foreign_key :trainee_withdrawal_reasons, column: :trainee_withdrawal_id | ||
|
||
# Remove the trainee_withdrawal_id column | ||
remove_column :trainee_withdrawal_reasons, :trainee_withdrawal_id | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
db/migrate/20241120205445_validate_associate_withdrawal_with_trainee_withdrawal_reason.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class ValidateAssociateWithdrawalWithTraineeWithdrawalReason < ActiveRecord::Migration[7.2] | ||
def change | ||
validate_foreign_key :trainee_withdrawal_reasons, :trainee_withdrawals | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :trainee_withdrawal, class: "TraineeWithdrawal" do | ||
trainee | ||
date { "2024-11-18" } | ||
trigger { :provider } | ||
another_reason { "Not enough coffee" } | ||
future_interest { :yes } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe TraineeWithdrawal do | ||
describe "associations" do | ||
it { is_expected.to belong_to(:trainee) } | ||
it { is_expected.to have_many(:trainee_withdrawal_reasons).dependent(:destroy) } | ||
end | ||
|
||
describe "enums" do | ||
it "defines the correct values for trigger enum" do | ||
expect(described_class.triggers).to eq("provider" => "provider", "trainee" => "trainee") | ||
end | ||
|
||
it "defines the correct values for future_interest enum" do | ||
expect(described_class.future_interests).to eq("yes" => "yes", "no" => "no", "unknown" => "unknown") | ||
end | ||
end | ||
end |