Created
June 10, 2021 02:59
-
-
Save mazz/62331384d2d3e379bd402e304d329715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule FaithfulWord.Repo.Migrations.CreateChannels do | |
use Ecto.Migration | |
def change do | |
create table(:channels, primary_key: false) do | |
add :id, :binary_id, primary_key: true | |
add :basename, :string | |
add :description, :text | |
add :ordinal, :integer | |
add :private, :boolean | |
# add :uuid, :uuid | |
add :org_id, references(:orgs, on_delete: :nothing) | |
add :owner_id, references(:users, on_delete: :nothing) | |
timestamps(type: :utc_datetime_usec) | |
end | |
create index(:channels, [:org_id]) | |
create index(:channels, [:owner_id]) | |
# create index(:channels, [:uuid]) | |
create index(:channels, [:inserted_at]) | |
flush() | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule FaithfulWord.Channels.Channel do | |
use Ecto.Schema | |
import Ecto.Changeset | |
alias FaithfulWord.Type.ChannelHashId | |
alias FaithfulWord.Schema.{User} | |
@type id :: String.t() | |
@type t :: %__MODULE__{ | |
id: Ecto.UUID.t(), | |
name: String.t(), | |
description: String.t(), | |
private: boolean(), | |
ordinal: integer() | nil, | |
total_posts: integer() | nil | |
} | |
schema "channels" do | |
belongs_to :owner, User | |
field :name, :string | |
field :description, :string | |
field :ordinal, :integer | |
field :private, :boolean, default: true | |
# field :uuid, Ecto.UUID | |
field :org_id, :id | |
field :hash_id, :string | |
field :total_posts, :integer, virtual: true | |
field :total_users, :integer, virtual: true | |
has_many :playlists, FaithfulWord.Schema.Playlist, on_delete: :nothing | |
timestamps(type: :utc_datetime_usec) | |
end | |
@required_fields [:id, :name, :org_id] | |
@optional_fields [:private, :description, :ordinal, :owner_id, :hash_id] | |
def changeset(channel, attrs) do | |
channel | |
|> cast(attrs, @required_fields ++ @optional_fields) | |
|> unique_constraint(:name) | |
|> validate_format(:name, ~r/^[a-zA-Z0-9_]{3,21}$/, | |
message: | |
"Must be between 3-21 characters long, cannot have spaces, underscores are the only special characters allowed." | |
) | |
|> validate_required(@required_fields) | |
end | |
@doc """ | |
Generate hash ID for channels | |
## Examples | |
iex> FaithfulWord.Schema.MediaItem.changeset_generate_hash_id(%FaithfulWord.Schema.Video{id: 42, hash_id: nil}) | |
#Ecto.Changeset<action: nil, changes: %{hash_id: \"4VyJ\"}, errors: [], data: #FaithfulWord.Schema.Video<>, valid?: true> | |
""" | |
def changeset_generate_hash_id(channel) do | |
change(channel, hash_id: ChannelHashId.encode(channel.id)) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DDL: | |
-- DDL generated by Postico 1.5.10 | |
-- Not all database features are supported. Do not use for backup. | |
-- Table Definition ---------------------------------------------- | |
CREATE TABLE channels ( | |
id uuid PRIMARY KEY, | |
name character varying(255), | |
description text, | |
ordinal integer, | |
private boolean, | |
org_id bigint REFERENCES orgs(id), | |
owner_id bigint REFERENCES users(id), | |
inserted_at timestamp without time zone NOT NULL, | |
updated_at timestamp without time zone NOT NULL, | |
hash_id character varying(12) | |
); | |
-- Indices ------------------------------------------------------- | |
CREATE UNIQUE INDEX channels_pkey ON channels(id uuid_ops); | |
CREATE INDEX channels_org_id_index ON channels(org_id int8_ops); | |
CREATE INDEX channels_owner_id_index ON channels(owner_id int8_ops); | |
CREATE INDEX channels_inserted_at_index ON channels(inserted_at timestamp_ops); | |
CREATE UNIQUE INDEX channels_hash_id_index ON channels(hash_id text_ops); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment