Skip to content

Commit ebb66ad

Browse files
authored
Merge pull request #656 from code-corps/fix-migrations
Fix migrations
2 parents 0527a35 + 31e06de commit ebb66ad

File tree

5 files changed

+55
-55
lines changed

5 files changed

+55
-55
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
defmodule CodeCorps.Repo.Seeds.AddDefaultTasksToProjects do
2+
import Ecto.Changeset
3+
import Ecto.Query
4+
5+
alias CodeCorps.Project
6+
alias CodeCorps.Repo
7+
alias CodeCorps.Task
8+
alias CodeCorps.TaskList
9+
10+
def migrate_existing() do
11+
Project
12+
|> preload(:task_lists)
13+
|> Repo.all()
14+
|> Enum.each(&handle_project_migration/1)
15+
end
16+
17+
defp handle_project_migration(project) do
18+
cond do
19+
project.task_lists != [] ->
20+
IO.puts "Task lists already exist for #{project.title}, skipping migration."
21+
true ->
22+
IO.puts "Generating default task lists for #{project.title}."
23+
24+
{:ok, project} = Project.changeset(project, %{})
25+
|> put_assoc(:task_lists, TaskList.default_task_lists())
26+
|> Repo.update
27+
28+
add_existing_tasks_to_inbox(project, hd(project.task_lists))
29+
end
30+
end
31+
32+
defp add_existing_tasks_to_inbox(project, task_list) do
33+
Task
34+
|> CodeCorps.Helpers.Query.project_filter(%{ project_id: project.id })
35+
|> Repo.all()
36+
|> Enum.each(&assign_task_to_inbox(&1, task_list))
37+
end
38+
39+
defp assign_task_to_inbox(task, task_list) do
40+
Task.changeset(task, %{ task_list_id: task_list.id })
41+
|> Repo.update()
42+
end
43+
end
44+
45+
CodeCorps.Repo.Seeds.AddDefaultTasksToProjects.migrate_existing()
Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
defmodule CodeCorps.Repo.Migrations.CreateTaskList do
22
use Ecto.Migration
3-
import Ecto.Changeset
4-
import Ecto.Query
5-
6-
alias CodeCorps.Project
7-
alias CodeCorps.Repo
8-
alias CodeCorps.Task
9-
alias CodeCorps.TaskList
103

114
def change do
125
create table(:task_lists) do
@@ -23,44 +16,5 @@ defmodule CodeCorps.Repo.Migrations.CreateTaskList do
2316
add :task_list_id, references(:task_lists, on_delete: :nothing)
2417
add :order, :integer
2518
end
26-
27-
flush
28-
29-
Application.ensure_all_started :timex
30-
migrate_existing()
31-
end
32-
33-
def migrate_existing() do
34-
Project
35-
|> preload(:task_lists)
36-
|> Repo.all()
37-
|> Enum.each(&handle_project_migration/1)
38-
end
39-
40-
defp handle_project_migration(project) do
41-
cond do
42-
project.task_lists != [] ->
43-
IO.puts "Task lists already exist for #{project.title}, skipping migration."
44-
true ->
45-
IO.puts "Generating default task lists for #{project.title}."
46-
47-
{:ok, project} = Project.changeset(project, %{})
48-
|> put_assoc(:task_lists, TaskList.default_task_lists())
49-
|> Repo.update
50-
51-
add_existing_tasks_to_inbox(project, hd(project.task_lists))
52-
end
53-
end
54-
55-
defp add_existing_tasks_to_inbox(project, task_list) do
56-
Task
57-
|> CodeCorps.Helpers.Query.project_filter(%{ project_id: project.id })
58-
|> Repo.all()
59-
|> Enum.each(&assign_task_to_inbox(&1, task_list))
60-
end
61-
62-
defp assign_task_to_inbox(task, task_list) do
63-
Task.changeset(task, %{ task_list_id: task_list.id })
64-
|> Repo.update()
6519
end
6620
end

priv/repo/migrations/20170106013143_add_editable_to_task_lists.exs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ defmodule CodeCorps.Repo.Migrations.AddEditableToTaskLists do
99
alter table(:task_lists) do
1010
add :inbox, :boolean, default: false
1111
end
12-
13-
flush
14-
15-
TaskList
16-
|> where([task_list], task_list.name == "Inbox")
17-
|> Repo.update_all(set: [inbox: true])
1812
end
1913

2014
def down do

priv/repo/structure.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
-- PostgreSQL database dump
33
--
44

5-
-- Dumped from database version 9.5.1
6-
-- Dumped by pg_dump version 9.5.1
5+
-- Dumped from database version 9.5.4
6+
-- Dumped by pg_dump version 9.5.4
77

88
SET statement_timeout = 0;
99
SET lock_timeout = 0;

web/models/task_list.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule CodeCorps.TaskList do
3535
name: "Done",
3636
position: 4
3737
}
38-
] |> Enum.map(fn (params) -> changeset(%TaskList{}, params) end)
38+
] |> Enum.map(fn (params) -> create_changeset(%TaskList{}, params) end)
3939
end
4040

4141
@doc """
@@ -47,4 +47,11 @@ defmodule CodeCorps.TaskList do
4747
|> validate_required([:name, :position])
4848
|> set_order(:position, :order, :project_id)
4949
end
50+
51+
def create_changeset(struct, params) do
52+
struct
53+
|> cast(params, [:inbox])
54+
|> changeset(params)
55+
|> validate_required([:inbox])
56+
end
5057
end

0 commit comments

Comments
 (0)