Skip to content

Commit 2841568

Browse files
committed
add new job field
1 parent 1be2a55 commit 2841568

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/algora/interviews/schemas/job_interview.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ defmodule Algora.Interviews.JobInterview do
1919

2020
belongs_to :user, Algora.Accounts.User
2121
belongs_to :job_posting, Algora.Jobs.JobPosting
22+
belongs_to :org, Algora.Accounts.User
2223

2324
timestamps()
2425
end
2526

2627
def changeset(job_interview, attrs) do
2728
job_interview
28-
|> cast(attrs, [:user_id, :job_posting_id, :status, :notes, :scheduled_at, :completed_at, :company_feedback, :candidate_feedback, :company_feedback_token, :candidate_feedback_token])
29-
|> validate_required([:user_id, :job_posting_id, :status])
29+
|> cast(attrs, [:user_id, :job_posting_id, :org_id, :status, :notes, :scheduled_at, :completed_at, :company_feedback, :candidate_feedback, :company_feedback_token, :candidate_feedback_token])
30+
|> validate_required([:user_id, :job_posting_id, :org_id, :status])
3031
|> validate_inclusion(:status, @interview_statuses)
3132
|> foreign_key_constraint(:user_id)
3233
|> foreign_key_constraint(:job_posting_id)
34+
|> foreign_key_constraint(:org_id)
35+
|> unique_constraint([:user_id, :org_id])
3336
|> unique_constraint(:company_feedback_token)
3437
|> unique_constraint(:candidate_feedback_token)
3538
|> generate_id()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Algora.Repo.Migrations.AddOrgIdToJobInterview do
2+
use Ecto.Migration
3+
4+
def up do
5+
# Add org_id as nullable first
6+
alter table(:job_interviews) do
7+
add :org_id, references(:users, on_delete: :delete_all, type: :string), null: true
8+
end
9+
10+
# Backfill org_id from job_posting.user_id
11+
execute """
12+
UPDATE job_interviews
13+
SET org_id = job_postings.user_id
14+
FROM job_postings
15+
WHERE job_interviews.job_posting_id = job_postings.id
16+
"""
17+
18+
# Make org_id non-nullable
19+
alter table(:job_interviews) do
20+
modify :org_id, :string, null: false, from: {:string, null: true}
21+
end
22+
23+
# Add unique index on (user_id, org_id)
24+
create unique_index(:job_interviews, [:user_id, :org_id])
25+
end
26+
27+
def down do
28+
drop index(:job_interviews, [:user_id, :org_id])
29+
30+
alter table(:job_interviews) do
31+
remove :org_id
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)