Switch to sqlite (#22)

* Updated project to use sqlite

* Edited migrations directly because I fear no man

* Updated search functions and tests to work with sqlite

* Updated build tools to remove postgres
This commit is contained in:
Kieran
2024-02-16 10:11:41 -08:00
committed by GitHub
parent 44bd105e80
commit 8fcd41f45d
29 changed files with 121 additions and 186 deletions
@@ -1,17 +1,18 @@
defmodule Pinchflat.Repo.Migrations.CreateChannels do
defmodule Pinchflat.Repo.Migrations.CreateSources do
use Ecto.Migration
def change do
create table(:channels) do
create table(:sources) do
add :name, :string, null: false
add :channel_id, :string, null: false
add :collection_id, :string, null: false
add :collection_type, :string, null: false
add :original_url, :string, null: false
add :media_profile_id, references(:media_profiles, on_delete: :restrict), null: false
timestamps(type: :utc_datetime)
end
create index(:channels, [:media_profile_id])
create unique_index(:channels, [:channel_id, :media_profile_id])
create index(:sources, [:media_profile_id])
create unique_index(:sources, [:collection_id, :media_profile_id])
end
end
@@ -6,12 +6,12 @@ defmodule Pinchflat.Repo.Migrations.CreateMediaItems do
add :media_id, :string, null: false
add :title, :string
add :video_filepath, :string
add :channel_id, references(:channels, on_delete: :restrict), null: false
add :source_id, references(:sources, on_delete: :restrict), null: false
timestamps(type: :utc_datetime)
end
create index(:media_items, [:channel_id])
create unique_index(:media_items, [:media_id, :channel_id])
create index(:media_items, [:source_id])
create unique_index(:media_items, [:media_id, :source_id])
end
end
@@ -1,8 +1,8 @@
defmodule Pinchflat.Repo.Migrations.AddIndexFrequencyToChannels do
defmodule Pinchflat.Repo.Migrations.AddIndexFrequencyToSources do
use Ecto.Migration
def change do
alter table(:channels) do
alter table(:sources) do
add :index_frequency_minutes, :integer, default: 60 * 24, null: false
end
end
@@ -4,13 +4,13 @@ defmodule Pinchflat.Repo.Migrations.CreateTasks do
def change do
create table(:tasks) do
add :job_id, references(:oban_jobs, on_delete: :delete_all), null: false
# `restrict` because we need to be sure to delete pending tasks when a channel is deleted
add :channel_id, references(:channels, on_delete: :restrict), null: true
# `restrict` because we need to be sure to delete pending tasks when a source is deleted
add :source_id, references(:sources, on_delete: :restrict), null: true
timestamps(type: :utc_datetime)
end
create index(:tasks, [:job_id])
create index(:tasks, [:channel_id])
create index(:tasks, [:source_id])
end
end
@@ -3,13 +3,12 @@ defmodule Pinchflat.Repo.Migrations.CreateMediaMetadata do
def change do
create table(:media_metadata) do
add :client_response, :jsonb, null: false
add :client_response, :json, null: false
add :media_item_id, references(:media_items, on_delete: :delete_all), null: false
timestamps(type: :utc_datetime)
end
create unique_index(:media_metadata, [:media_item_id])
create index(:media_metadata, [:client_response], using: :gin)
end
end
@@ -3,7 +3,7 @@ defmodule Pinchflat.Repo.Migrations.AddMediaItemToTasks do
def change do
alter table(:tasks) do
# `restrict` because we need to be sure to delete pending tasks when a channel is deleted
# `restrict` because we need to be sure to delete pending tasks when a media item is deleted
add :media_item_id, references(:media_items, on_delete: :restrict), null: true
end
@@ -1,45 +0,0 @@
defmodule Pinchflat.Repo.Migrations.RenameChannelAndRelatedFields do
use Ecto.Migration
def change do
# Creation
create table(:sources) do
add :name, :string, null: false
add :collection_type, :string, null: false
add :collection_id, :string, null: false
add :original_url, :string, null: false
add :media_profile_id, references(:media_profiles, on_delete: :restrict), null: false
add :index_frequency_minutes, :integer, default: 60 * 24, null: false
timestamps(type: :utc_datetime)
end
alter table(:media_items) do
add :source_id, references(:sources, on_delete: :restrict), null: false
end
alter table(:tasks) do
# `restrict` because we need to be sure to delete pending tasks when a source is deleted
add :source_id, references(:sources, on_delete: :restrict), null: true
end
create index(:sources, [:media_profile_id])
create unique_index(:sources, [:collection_id, :media_profile_id])
create index(:media_items, [:source_id])
create unique_index(:media_items, [:media_id, :source_id])
# Deletion
drop index(:media_items, [:channel_id])
drop unique_index(:media_items, [:media_id, :channel_id])
alter table(:tasks) do
# `restrict` because we need to be sure to delete pending tasks when a source is deleted
remove :channel_id, references(:channels, on_delete: :restrict), null: true
end
alter table(:media_items) do
remove :channel_id, references(:channels, on_delete: :restrict), null: true
end
end
end
@@ -1,21 +0,0 @@
defmodule Pinchflat.Repo.Migrations.DropChannelsTable do
use Ecto.Migration
def up do
drop table(:channels)
end
def down do
create table(:channels) do
add :name, :string, null: false
add :channel_id, :string, null: false
add :original_url, :string, null: false
add :media_profile_id, references(:media_profiles, on_delete: :restrict), null: false
timestamps(type: :utc_datetime)
end
create index(:channels, [:media_profile_id])
create unique_index(:channels, [:channel_id, :media_profile_id])
end
end
@@ -2,27 +2,50 @@ defmodule Pinchflat.Repo.Migrations.AddSearchFieldToMediaItems do
use Ecto.Migration
def up do
# These all need to run as part of separate `execute` blocks. Do NOT ask me why.
execute """
ALTER TABLE media_items
ADD COLUMN searchable tsvector
GENERATED ALWAYS AS (
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(description, '')), 'B')
) STORED;
CREATE VIRTUAL TABLE media_items_search_index USING fts5(
title,
description,
tokenize=porter
);
"""
execute """
CREATE INDEX media_items_searchable_idx ON media_items USING gin(searchable);
CREATE TRIGGER media_items_search_index_insert AFTER INSERT ON media_items BEGIN
INSERT INTO media_items_search_index(
rowid,
title,
description
)
VALUES(
new.id,
new.title,
new.description
);
END;
"""
execute """
CREATE TRIGGER media_items_search_index_update AFTER UPDATE ON media_items BEGIN
UPDATE media_items_search_index SET
title = new.title,
description = new.description
WHERE
rowid = old.id;
END;
"""
execute """
CREATE TRIGGER media_items_search_index_delete AFTER DELETE ON media_items BEGIN
DELETE FROM media_items_search_index WHERE rowid = old.id;
END;
"""
end
def down do
execute """
DROP INDEX media_items_searchable_idx;
DROP TABLE media_items_search_index;
"""
alter table(:media_items) do
remove :searchable
end
end
end