Skip to content

Commit

Permalink
Added priv scripts to migrate organization memberships and project ow…
Browse files Browse the repository at this point in the history
…ners
  • Loading branch information
begedin committed Feb 28, 2017
1 parent 533f9fa commit 0b620e9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
45 changes: 45 additions & 0 deletions priv/repo/scripts/migrate_organization_memberships.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule CodeCorps.Repo.Scripts.MigrateOrganizations do
require Logger

alias CodeCorps.{OrganizationMembership, Project, ProjectUser, Repo, User}

def run do
OrganizationMembership
|> Repo.all()
|> Repo.preload([:member, {:organization, :projects}])
|> Enum.map(&migrate_member/1)
|> aggregate_results
|> log
end

defp migrate_member(%OrganizationMembership{
member: user,
organization: %{projects: [project]},
role: role
}) do
create_membership(project, user, role)
end

defp create_membership(%Project{id: project_id}, %User{id: user_id}, role) do
%ProjectUser{} |> build_changeset(project_id, user_id, role) |> Repo.insert()
end

defp build_changeset(struct, project_id, user_id, role) do
attrs = %{project_id: project_id, user_id: user_id, role: role}
struct
|> Ecto.Changeset.cast(attrs, [:project_id, :user_id, :role])
|> Ecto.Changeset.unique_constraint(:project, name: :project_users_user_id_project_id_index)
end

defp aggregate_results(results) do
passing_count = Enum.count(results, fn({status, _}) -> status == :ok end)
error_count = Enum.count(results, fn({status, _}) -> status == :error end)
{passing_count, error_count}
end

defp log({passing_count, error_count}) do
Logger.info("#{passing_count} memberships migrated, #{error_count} errors.")
end
end

CodeCorps.Repo.Scripts.MigrateOrganizations.run()
33 changes: 33 additions & 0 deletions priv/repo/scripts/migrate_project_owners.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule CodeCorps.Repo.Scripts.MigrateProjectOwners do
require Logger

import Ecto.Query

alias CodeCorps.{OrganizationMembership, Repo}

def run do
OrganizationMembership
|> where([m], m.role == "owner")
|> Repo.all()
|> Repo.preload([:member, {:organization, :projects}])
|> Enum.map(&migrate_owner/1)
|> aggregate_results
|> log
end

defp migrate_owner(%OrganizationMembership{member_id: user_id, organization: %{projects: [project]}}) do
project |> Ecto.Changeset.cast(%{owner_id: user_id}, [:owner_id]) |> Repo.update()
end

defp aggregate_results(results) do
passing_count = Enum.count(results, fn({status, _}) -> status == :ok end)
error_count = Enum.count(results, fn({status, _}) -> status == :error end)
{passing_count, error_count}
end

defp log({passing_count, error_count}) do
Logger.info("#{passing_count} owners migrated, #{error_count} errors.")
end
end

CodeCorps.Repo.Scripts.MigrateProjectOwners.run()
6 changes: 4 additions & 2 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ end
organizations = [
%{
name: "Code Corps",
description: "Help build and fund public software projects for social good"
description: "Help build and fund public software projects for social good",
owner_id: 1
},
]

Expand All @@ -74,7 +75,8 @@ projects = [
%{
title: "Code Corps",
description: "A basic project for use in development",
organization_id: 1
organization_id: 1,
owner_id: 1
}
]

Expand Down
2 changes: 1 addition & 1 deletion priv/repo/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2608,5 +2608,5 @@ ALTER TABLE ONLY user_tasks
-- PostgreSQL database dump complete
--

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);
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);

0 comments on commit 0b620e9

Please sign in to comment.