Skip to content

Commit 24a9fc4

Browse files
authored
Merge pull request #28 from epochtalk/thread-slugs
feat: add slug column to threads, populate existing thread slugs with…
2 parents e01c431 + d508cd9 commit 24a9fc4

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

lib/epoch/thread.ex

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule Epoch.Thread do
66
belongs_to :board, Epoch.Board, type: :binary_id
77
field :locked, :boolean
88
field :sticky, :boolean
9+
field :slug, :string
910
field :moderated, :boolean
1011
field :post_count, :integer
1112
field :created_at, :naive_datetime
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Epoch.Repo.Migrations.ThreadSlugs do
2+
use Ecto.Migration
3+
import Ecto.Query
4+
5+
def up do
6+
# add slug column
7+
alter table(:threads) do
8+
add :slug, :string, size: 100
9+
end
10+
11+
#index
12+
create unique_index(:threads, [:slug])
13+
14+
# flush so query populating slug will work
15+
flush()
16+
17+
# update existing threads, set slug = id
18+
from(t in "threads",
19+
update: [set: [slug: t.id]],
20+
where: true)
21+
|> Epoch.Repo.update_all([])
22+
23+
# modify threads after slug update, don't allow null
24+
alter table(:threads) do
25+
modify :slug, :string, size: 100, null: false
26+
end
27+
end
28+
29+
def down do
30+
alter table(:threads) do
31+
remove :slug
32+
end
33+
end
34+
end

test/epoch_test.exs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule EpochTest do
1818
def create_thread(board \\ create_board()) do
1919
%Epoch.Thread{
2020
board: board,
21+
slug: :crypto.strong_rand_bytes(5) |> Base.url_encode64 |> binary_part(0, 5),
2122
created_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
2223
updated_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
2324
} |> Epoch.Repo.insert!

test/epoch_test/board_test.exs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
defmodule EpochTest.BoardTest do
1+
defmodule EpochTest.BoardTest do
22
use Epoch.DataCase
33
import Ecto.Query
44
alias Epoch.Board
55
alias Epoch.Repo
66

77
doctest Epoch
88

9+
setup do
10+
Repo.delete_all(Board)
11+
:ok
12+
end
13+
914
test "board" do
1015
board_created = EpochTest.create_board()
1116
board_retrieved = from(

test/epoch_test/post_test.exs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule EpochTest.PostTest do
1111
} |> Epoch.Repo.insert!
1212
t = %Epoch.Thread{
1313
board: b,
14+
slug: :crypto.strong_rand_bytes(5) |> Base.url_encode64 |> binary_part(0, 5),
1415
created_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1516
updated_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
1617
} |> Epoch.Repo.insert!

0 commit comments

Comments
 (0)