Skip to content

Commit 1e7343c

Browse files
authored
Merge pull request #29 from epochtalk/board-slugs
feat: implement slugs for boards table
2 parents 24a9fc4 + 92aab3b commit 1e7343c

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

lib/epoch/board.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule Epoch.Board do
44
@primary_key {:id, :binary_id, autogenerate: true}
55
schema "boards" do
66
field :name, :string
7+
field :slug, :string
78
field :description, :string
89
field :post_count, :integer
910
field :thread_count, :integer
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Epoch.Repo.Migrations.BoardSlugs do
2+
use Ecto.Migration
3+
import Ecto.Query
4+
5+
def up do
6+
# add slug column
7+
alter table(:boards) do
8+
add :slug, :string, size: 100
9+
end
10+
11+
#index
12+
create unique_index(:boards, [:slug])
13+
14+
# flush so query populating slug will work
15+
flush()
16+
17+
# update existing boards, set slug = id
18+
from(b in "boards",
19+
update: [set: [slug: b.id]],
20+
where: true)
21+
|> Epoch.Repo.update_all([])
22+
23+
# modify boards after slug update, don't allow null
24+
alter table(:boards) do
25+
modify :slug, :string, size: 100, null: false
26+
end
27+
end
28+
29+
def down do
30+
alter table(:boards) do
31+
remove :slug
32+
end
33+
end
34+
end

test/epoch_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule EpochTest do
55
def create_board(name \\ "Testing Board") do
66
%Epoch.Board{
77
name: name,
8+
slug: :crypto.strong_rand_bytes(5) |> Base.url_encode64 |> binary_part(0, 5),
89
description: "Testing grounds for discussion",
910
created_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1011
updated_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)

test/epoch_test/post_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule EpochTest.PostTest do
55
test "post" do
66
b = %Epoch.Board{
77
name: "Testing Board",
8+
slug: :crypto.strong_rand_bytes(5) |> Base.url_encode64 |> binary_part(0, 5),
89
description: "Testing grounds for discussion",
910
created_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second),
1011
updated_at: NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)

0 commit comments

Comments
 (0)