Skip to content

Commit

Permalink
Fix migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith committed Jan 21, 2017
1 parent 0527a35 commit a93982e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ defmodule CodeCorps.Mixfile do
defp aliases do
["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
"ecto.migrate": ["ecto.migrate", "ecto.dump"],
# "ecto.migrate": ["ecto.migrate", "ecto.dump"],
"ecto.rollback": ["ecto.rollback", "ecto.dump"],
"test": ["ecto.create --quiet", "ecto.migrate", "test"]]
end
Expand Down
45 changes: 45 additions & 0 deletions priv/repo/add_default_tasks_to_projects.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule CodeCorps.Repo.Seeds.AddDefaultTasksToProjects do
import Ecto.Changeset
import Ecto.Query

alias CodeCorps.Project
alias CodeCorps.Repo
alias CodeCorps.Task
alias CodeCorps.TaskList

def migrate_existing() do
Project
|> preload(:task_lists)
|> Repo.all()
|> Enum.each(&handle_project_migration/1)
end

defp handle_project_migration(project) do
cond do
project.task_lists != [] ->
IO.puts "Task lists already exist for #{project.title}, skipping migration."
true ->
IO.puts "Generating default task lists for #{project.title}."

{:ok, project} = Project.changeset(project, %{})
|> put_assoc(:task_lists, TaskList.default_task_lists())
|> Repo.update

add_existing_tasks_to_inbox(project, hd(project.task_lists))
end
end

defp add_existing_tasks_to_inbox(project, task_list) do
Task
|> CodeCorps.Helpers.Query.project_filter(%{ project_id: project.id })
|> Repo.all()
|> Enum.each(&assign_task_to_inbox(&1, task_list))
end

defp assign_task_to_inbox(task, task_list) do
Task.changeset(task, %{ task_list_id: task_list.id })
|> Repo.update()
end
end

CodeCorps.Repo.Seeds.AddDefaultTasksToProjects.migrate_existing()
46 changes: 0 additions & 46 deletions priv/repo/migrations/20161209192504_create_task_list.exs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
defmodule CodeCorps.Repo.Migrations.CreateTaskList do
use Ecto.Migration
import Ecto.Changeset
import Ecto.Query

alias CodeCorps.Project
alias CodeCorps.Repo
alias CodeCorps.Task
alias CodeCorps.TaskList

def change do
create table(:task_lists) do
Expand All @@ -23,44 +16,5 @@ defmodule CodeCorps.Repo.Migrations.CreateTaskList do
add :task_list_id, references(:task_lists, on_delete: :nothing)
add :order, :integer
end

flush

Application.ensure_all_started :timex
migrate_existing()
end

def migrate_existing() do
Project
|> preload(:task_lists)
|> Repo.all()
|> Enum.each(&handle_project_migration/1)
end

defp handle_project_migration(project) do
cond do
project.task_lists != [] ->
IO.puts "Task lists already exist for #{project.title}, skipping migration."
true ->
IO.puts "Generating default task lists for #{project.title}."

{:ok, project} = Project.changeset(project, %{})
|> put_assoc(:task_lists, TaskList.default_task_lists())
|> Repo.update

add_existing_tasks_to_inbox(project, hd(project.task_lists))
end
end

defp add_existing_tasks_to_inbox(project, task_list) do
Task
|> CodeCorps.Helpers.Query.project_filter(%{ project_id: project.id })
|> Repo.all()
|> Enum.each(&assign_task_to_inbox(&1, task_list))
end

defp assign_task_to_inbox(task, task_list) do
Task.changeset(task, %{ task_list_id: task_list.id })
|> Repo.update()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ defmodule CodeCorps.Repo.Migrations.AddEditableToTaskLists do
alter table(:task_lists) do
add :inbox, :boolean, default: false
end

flush

TaskList
|> where([task_list], task_list.name == "Inbox")
|> Repo.update_all(set: [inbox: true])
end

def down do
Expand Down
4 changes: 2 additions & 2 deletions priv/repo/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 9.5.1
-- Dumped by pg_dump version 9.5.1
-- Dumped from database version 9.5.4
-- Dumped by pg_dump version 9.5.4

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down
9 changes: 8 additions & 1 deletion web/models/task_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule CodeCorps.TaskList do
name: "Done",
position: 4
}
] |> Enum.map(fn (params) -> changeset(%TaskList{}, params) end)
] |> Enum.map(fn (params) -> create_changeset(%TaskList{}, params) end)
end

@doc """
Expand All @@ -47,4 +47,11 @@ defmodule CodeCorps.TaskList do
|> validate_required([:name, :position])
|> set_order(:position, :order, :project_id)
end

def create_changeset(struct, params) do
struct
|> cast(params, [:inbox])
|> changeset(params)
|> validate_required([:inbox])
end
end

0 comments on commit a93982e

Please sign in to comment.