Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add legacy migration guide #129

Merged
merged 1 commit into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions guides/legacy_migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Migrating from legacy system

A common setup with Ueberauth is to have provider details as columns in the `users` table. To migrate from that you should copy over the provider details to the `user_identities` table.

The below shows an example with a legacy system using facebook:

```elixir
defmodule MyApp.Repo.Migrations.FacebookUserToUserIdentities do
use Ecto.Migration

import Ecto.Query
alias MyApp.Repo

def up do
create_user_identities()

alter table(:users) do
remove :facebook_id
remove :facebook_access_token
end
end

def down do
alter table(:users) do
add :facebook_id, :string
add :facebook_access_token, :string
end

flush()

set_identity_columns()
end

defp create_user_identities() do
user_identities =
"users"
|> where([u], not is_nil(u.facebook_id))
|> select([u], %{user_id: u.id, provider: "facebook", uid: u.facebook_id, inserted_at: u.inserted_at, updated_at: u.inserted_at})
# Or with access token:
# |> select([u], %{user_id: u.id, provider: "facebook", uid: u.facebook_id, access_token: u.facebook_access_token, inserted_at: u.inserted_at, updated_at: u.inserted_at})
|> Repo.all()
|> IO.inspect()

Repo.insert_all("user_identities", user_identities)
end

defp set_identity_columns() do
"user_identities"
|> select([i], {i.user_id, %{facebook_id: i.uid}})
# Or with access token:
# |> select([i], {i.user_id, %{facebook_id: i.uid, facebook_access_token: i.access_token}})
|> where([i], i.provider == "facebook")
|> Repo.all()
|> Enum.each(fn {id, update} ->
"users"
|> where([u], u.id == ^id)
|> update(set: ^Map.to_list(update))
|> Repo.update_all([])
end)
end
end
```
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ defmodule PowAssent.MixProject do
"CHANGELOG.md": [filename: "CHANGELOG"],
"guides/set_up_pow.md": [],
"guides/capture_access_token.md": [],
"guides/legacy_migration.md": [],
],
groups_for_modules: [
Ecto: ~r/^PowAssent.Ecto/,
Expand Down