Skip to content

Commit e03f6e6

Browse files
authored
Merge pull request #723 from code-corps/712-add-migration-task-for-organization-memberships-to-project-memberships
Add migration scripts to migrate existing data
2 parents ee4ae75 + 8fe32ab commit e03f6e6

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
defmodule CodeCorps.Repo.Scripts.MigrateOrganizations do
2+
require Logger
3+
4+
alias CodeCorps.{OrganizationMembership, Project, ProjectUser, Repo, User}
5+
6+
def run do
7+
OrganizationMembership
8+
|> Repo.all()
9+
|> Repo.preload([:member, {:organization, :projects}])
10+
|> Enum.map(&migrate_member/1)
11+
|> aggregate_results
12+
|> log
13+
end
14+
15+
defp migrate_member(%OrganizationMembership{
16+
member: user,
17+
organization: %{projects: [project]},
18+
role: role
19+
}) do
20+
create_membership(project, user, role)
21+
end
22+
23+
defp create_membership(%Project{id: project_id}, %User{id: user_id}, role) do
24+
%ProjectUser{} |> build_changeset(project_id, user_id, role) |> Repo.insert()
25+
end
26+
27+
defp build_changeset(struct, project_id, user_id, role) do
28+
attrs = %{project_id: project_id, user_id: user_id, role: role}
29+
struct
30+
|> Ecto.Changeset.cast(attrs, [:project_id, :user_id, :role])
31+
|> Ecto.Changeset.unique_constraint(:project, name: :project_users_user_id_project_id_index)
32+
end
33+
34+
defp aggregate_results(results) do
35+
passing_count = Enum.count(results, fn({status, _}) -> status == :ok end)
36+
error_count = Enum.count(results, fn({status, _}) -> status == :error end)
37+
{passing_count, error_count}
38+
end
39+
40+
defp log({passing_count, error_count}) do
41+
Logger.info("#{passing_count} memberships migrated, #{error_count} errors.")
42+
end
43+
end
44+
45+
CodeCorps.Repo.Scripts.MigrateOrganizations.run()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
defmodule CodeCorps.Repo.Scripts.MigrateOrganizationOwners do
2+
require Logger
3+
4+
import Ecto.Query
5+
6+
alias CodeCorps.{OrganizationMembership, Repo}
7+
8+
def run do
9+
OrganizationMembership
10+
|> where([m], m.role == "owner")
11+
|> Repo.all()
12+
|> Repo.preload([:organization])
13+
|> Enum.map(&migrate_owner/1)
14+
|> aggregate_results
15+
|> log
16+
end
17+
18+
defp migrate_owner(%OrganizationMembership{member_id: user_id, organization: organization}) do
19+
organization |> Ecto.Changeset.cast(%{owner_id: user_id}, [:owner_id]) |> Repo.update()
20+
end
21+
22+
defp aggregate_results(results) do
23+
passing_count = Enum.count(results, fn({status, _}) -> status == :ok end)
24+
error_count = Enum.count(results, fn({status, _}) -> status == :error end)
25+
{passing_count, error_count}
26+
end
27+
28+
defp log({passing_count, error_count}) do
29+
Logger.info("#{passing_count} owners migrated, #{error_count} errors.")
30+
end
31+
end
32+
33+
CodeCorps.Repo.Scripts.MigrateOrganizationOwners.run()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
defmodule CodeCorps.Repo.Scripts.MigrateProjectOwners do
2+
require Logger
3+
4+
import Ecto.Query
5+
6+
alias CodeCorps.{OrganizationMembership, Repo}
7+
8+
def run do
9+
OrganizationMembership
10+
|> where([m], m.role == "owner")
11+
|> Repo.all()
12+
|> Repo.preload([:member, {:organization, :projects}])
13+
|> Enum.map(&migrate_owner/1)
14+
|> aggregate_results
15+
|> log
16+
end
17+
18+
defp migrate_owner(%OrganizationMembership{member_id: user_id, organization: %{projects: [project]}}) do
19+
project |> Ecto.Changeset.cast(%{owner_id: user_id}, [:owner_id]) |> Repo.update()
20+
end
21+
22+
defp aggregate_results(results) do
23+
passing_count = Enum.count(results, fn({status, _}) -> status == :ok end)
24+
error_count = Enum.count(results, fn({status, _}) -> status == :error end)
25+
{passing_count, error_count}
26+
end
27+
28+
defp log({passing_count, error_count}) do
29+
Logger.info("#{passing_count} owners migrated, #{error_count} errors.")
30+
end
31+
end
32+
33+
CodeCorps.Repo.Scripts.MigrateProjectOwners.run()

priv/repo/seeds.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ end
5454
organizations = [
5555
%{
5656
name: "Code Corps",
57-
description: "Help build and fund public software projects for social good"
57+
description: "Help build and fund public software projects for social good",
58+
owner_id: 1
5859
},
5960
]
6061

@@ -74,7 +75,8 @@ projects = [
7475
%{
7576
title: "Code Corps",
7677
description: "A basic project for use in development",
77-
organization_id: 1
78+
organization_id: 1,
79+
owner_id: 1
7880
}
7981
]
8082

priv/repo/structure.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2608,5 +2608,5 @@ ALTER TABLE ONLY user_tasks
26082608
-- PostgreSQL database dump complete
26092609
--
26102610

2611-
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250);
2611+
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170228144515), (20170228145755);
26122612

0 commit comments

Comments
 (0)