Skip to content

Instantly share code, notes, and snippets.

@mazz
Last active December 4, 2023 05:07
Show Gist options
  • Save mazz/50c8b51421d3e784f3d164dc3bd7963c to your computer and use it in GitHub Desktop.
Save mazz/50c8b51421d3e784f3d164dc3bd7963c to your computer and use it in GitHub Desktop.
defmodule Word.Repo.Migrations.CreateMusic do
use Ecto.Migration
def change do
execute "CREATE EXTENSION IF NOT EXISTS citext", ""
create table(:music, primary_key: false) do
add :id, :id, primary_key: true
add :absolute_id, :integer
add :uuid, :uuid
add :basename, :string
add :slug, :citext
timestamps(type: :utc_datetime)
# timestamps()
end
create index(:music, [:uuid, :slug])
end
end
defmodule FaithfulWord.Repo.Migrations.CreateMediamusic do
use Ecto.Migration
def change do
create table(:mediamusic, primary_key: false) do
add :id, :id, primary_key: true
add :absolute_id, :integer
add :uuid, :uuid
add :track_number, :integer
add :localizedname, :string
add :path, :string
add :language_id, :string
add :presenter_name, :string
add :source_material, :string
add :music_id, references(:music, on_delete: :nothing, type: :id)
add :audio_urls, {:array, :string}, null: false, default: []
add :poster_path, :string
add :duration, :float, default: 0.0
add :size_bytes, :bigint, default: 0
add :media_format, :string, null: false
add :media_urls, {:array, :string}, null: false, default: []
add :published_at, :utc_datetime_usec, null: true
timestamps(type: :utc_datetime)
end
create index(:mediamusic, [:music_id])
create index(:mediamusic, [:uuid])
end
end
defmodule Word.Schema.MediaMusic do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :id, autogenerate: true}
schema "mediamusic" do
field :absolute_id, :integer
field :language_id, :string
field :localizedname, :string
field :path, :string
field :presenter_name, :string
field :source_material, :string
field :track_number, :integer
field :uuid, Ecto.UUID
# field :music_id, :id
field :audio_urls, {:array, :string}, default: []
field :poster_path, :string
field :duration, :float, default: 0.0
field :size_bytes, :integer, default: 0
field :view_count, :integer, default: 0
field :media_format, Ecto.Enum, values: [:video, :audio], default: :audio
field :media_urls, {:array, :string}, default: []
field :published_at, :utc_datetime_usec
belongs_to :music, Word.Schema.Music
timestamps(type: :utc_datetime)
end
@doc false
def changeset(media_music, attrs) do
media_music
|> cast(attrs, [
:id,
:music_id,
:absolute_id,
:uuid,
:track_number,
:localizedname,
:path,
:language_id,
:presenter_name,
:source_material,
:poster_path,
:duration,
:size_bytes,
:view_count,
:media_format,
:media_urls,
# :audio_urls,
:published_at
])
|> validate_required([
# :absolute_id,
:id,
:music_id,
:uuid,
# :track_number,
:localizedname,
:path,
# :language_id,
# :presenter_name,
# :source_material
])
end
end
defmodule Word.Schema.Music do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :id, autogenerate: true}
schema "music" do
field :absolute_id, :integer
field :basename, :string
field :slug, :string
field :uuid, Ecto.UUID
field :total_media_music, :integer, virtual: true
# timestamps()
has_many :mediamusic, Word.Schema.MediaMusic
has_many :musictitle, Word.Schema.MusicTitle
timestamps(type: :utc_datetime)
end
@doc false
def changeset(music, attrs) do
music
|> cast(attrs, [:id, :absolute_id, :uuid, :basename, :slug])
|> validate_required([:id, :uuid, :basename, :slug])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment