Skip to content

Commit

Permalink
chore: conversion script
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodiaslobo committed Aug 10, 2024
1 parent 26b795f commit 00ef3b2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/cesium_link/redirects/redirect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ defmodule CesiumLink.Redirects.Redirect do
|> validate_format(:slug, ~r{^[a-z0-9-]+$},
message: "must contain only lowercase letters, numbers, and hyphens"
)
|> unique_constraint(:slug, message: "already in use")
|> unique_constraint([:slug, :type], message: "already in use")
end
end
120 changes: 120 additions & 0 deletions lib/mix/tasks/convert.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
defmodule Mix.Tasks.Convert do
@moduledoc """
Task to read data exported (in CSV files, one for each table) from the old cesium.link project and import it to database following the new schemas.
"""
use Mix.Task

alias CesiumLink.Redirects.Redirect
alias CesiumLink.Repo
alias CesiumLink.Links.Link

def run(files) do
case length(files) do
3 ->
Mix.Task.run("app.start")

convert_links(Enum.at(files, 0))
convert_redirects(Enum.at(files, 1))
convert_forms(Enum.at(files, 2))

_ ->
Mix.shell().error("Invalid number of arguments")
end
end

defp convert_links(file) do
file
|> parse_csv()
|> Enum.each(fn [
_id,
_version,
archived,
attention,
visits,
created_at,
emoji,
index,
_slug,
title,
updated_at,
url
] ->
%Link{
archived: to_bool(archived),
attention: to_bool(attention),
visits: String.to_integer(visits),
inserted_at: to_datetime(created_at),
emoji: to_emoji(emoji),
index: String.to_integer(index),
name: title,
edited_at: to_datetime(updated_at),
url: url
}
|> Repo.insert()
end)
end

defp convert_redirects(file) do
file
|> parse_csv()
|> Enum.each(fn [_id, _version, created_at, name, slug, updated_at, url, visits] ->
%Redirect{
inserted_at: to_datetime(created_at),
name: name,
slug: slug,
edited_at: to_datetime(updated_at),
url: url,
visits: String.to_integer(visits),
type: :default
}
|> Repo.insert()
end)
end

defp convert_forms(file) do
file
|> parse_csv()
|> Enum.each(fn [_id, _version, created_at, name, slug, updated_at, url, visits] ->
%Redirect{
inserted_at: to_datetime(created_at),
name: name,
slug: slug,
edited_at: to_datetime(updated_at),
url: url,
visits: String.to_integer(visits),
type: :form
}
|> Repo.insert()
end)
end

defp parse_csv(path) do
case path |> File.read() do
{:ok, content} ->
content
|> String.replace(~r/\r\n/, "\n")
|> String.split("\n")
|> Enum.map(&String.split(&1, ","))
|> Enum.drop(-1)

{:error, reason} ->
raise reason
end
end

defp to_bool("true"), do: true
defp to_bool("false"), do: false

defp to_datetime(date) do
case Timex.parse(date, "{ISO:Extended}") do
{:ok, date} -> date |> DateTime.truncate(:second)
{:error, _} -> Timex.now() |> DateTime.truncate(:second)
end
end

defp to_emoji(emoji) do
emoji
|> String.replace(":", "")
|> String.trim(" ")
end
end
2 changes: 1 addition & 1 deletion priv/repo/migrations/20240418190329_create_redirects.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ defmodule CesiumLink.Repo.Migrations.CreateRedirects do
timestamps(type: :utc_datetime)
end

create unique_index(:redirects, [:slug])
create unique_index(:redirects, [:slug, :type])
end
end

0 comments on commit 00ef3b2

Please sign in to comment.