wrolpi/alembic/versions/3b6918aeca4b_file_group.py

243 lines
9.4 KiB
Python
Raw Normal View History

Making example label a tag. Tags can be created and deleted in UI. Adding tag_file using id to avoid session error. Using UTC in test. Matching tag_names style in test. Tag icon is displayed on file rows. Archives, Videos and Files can be filtered by their tags. Tags can be added and removed from UI. Tags are displayed in VideoPlayer. The tags of a file are returned in it's JSON. Video favorites are converted to Favorite tag on channel config import. Updating tests to match frontend usage. PDF creator and modification time are displayed. Reworking file search to use Video.__json__ for complex video relations. Archives can be viewed. Removing ebook.title. Reworking alembic migration to be an upgrade rather than a replace. Skipping test on circleci. Clearing out video caption index only if extract_subtitles if off. Channel tests pass. Some video tests pass. Missing videos can be found. A video can be downloaded. Orphaned files can be found. Videos can be tagged. Videos can be searched. Videos can be modeled. Converting some tests to use FileGroup.primary_path. Adding archive and domains to alembic migration. Testing cascading Archive titles. Archives now uses FileGroup. archive.lib tests pass. Archive is partially converted to FileGroup. Calculating FileGroup.primary file on refresh, rather than on select. wrolpi tests pass. Using FilesType to convert between str/pathlib.Path for FileGroup.files. Using list of files in FileGroup.files instead of dictionary. Storing files in dedicated FileGroup.files to avoid overwriting them. Adding comment explaining CASE. Not overwriting FileGroup.data on refresh if no change has been detected. Removing File model in favor of FileGroup.data. Adding typing to Files methods. Files are indexed using the primary mimetype of the FileGroup. files test_lib tests pass. Removing file_group_file in favor of simple one-to-many relationship. PDF files are indexed during refresh. Adding Tag and TagFile to tag files with specific tags. Only iterating when full_stem_ids has contents. Testing that FileGroups are related to their Files. Files and file_groups are created and linked. Bump webpack from 5.75.0 to 5.76.1 in /app (#160) Bumps [webpack](https://github.com/webpack/webpack) from 5.75.0 to 5.76.1. - [Release notes](https://github.com/webpack/webpack/releases) - [Commits](https://github.com/webpack/webpack/compare/v5.75.0...v5.76.1) --- updated-dependencies: - dependency-name: webpack dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-24 08:58:18 -06:00
"""File group.
Revision ID: 3b6918aeca4b
Revises: 930b9b7adb79
Create Date: 2023-02-27 14:52:46.943351
"""
import os
from alembic import op
from sqlalchemy.orm import Session
# revision identifiers, used by Alembic.
revision = '3b6918aeca4b'
down_revision = '930b9b7adb79'
branch_labels = None
depends_on = None
DOCKERIZED = True if os.environ.get('DOCKER', '').lower().startswith('t') else False
def upgrade():
bind = op.get_bind()
session = Session(bind=bind)
session.execute('DELETE FROM video') # noqa
session.execute('ALTER TABLE video DROP COLUMN caption_path')
session.execute('ALTER TABLE video DROP COLUMN censored')
session.execute('ALTER TABLE video DROP COLUMN favorite')
session.execute('ALTER TABLE video DROP COLUMN info_json_path')
session.execute('ALTER TABLE video DROP COLUMN modification_datetime')
session.execute('ALTER TABLE video DROP COLUMN poster_path')
session.execute('ALTER TABLE video DROP COLUMN size')
session.execute('ALTER TABLE video DROP COLUMN title')
session.execute('ALTER TABLE video DROP COLUMN validated')
session.execute('ALTER TABLE video DROP COLUMN video_path')
session.execute('DELETE FROM archive') # noqa
session.execute('ALTER TABLE archive DROP COLUMN readability_json_path')
session.execute('ALTER TABLE archive DROP COLUMN readability_path')
session.execute('ALTER TABLE archive DROP COLUMN readability_txt_path')
session.execute('ALTER TABLE archive DROP COLUMN screenshot_path')
session.execute('ALTER TABLE archive DROP COLUMN singlefile_path')
session.execute('ALTER TABLE archive DROP COLUMN title')
session.execute('ALTER TABLE archive DROP COLUMN validated')
session.execute('DELETE FROM ebook') # noqa
session.execute('ALTER TABLE ebook DROP COLUMN cover_path')
session.execute('ALTER TABLE ebook DROP COLUMN ebook_path')
session.execute('ALTER TABLE ebook DROP COLUMN title')
session.execute('DROP TABLE IF EXISTS file')
session.execute('''
CREATE TABLE file_group (
id BIGSERIAL PRIMARY KEY,
data JSON,
files JSON,
idempotency TIMESTAMP WITH TIME ZONE,
indexed BOOLEAN DEFAULT FALSE,
mimetype TEXT NOT NULL,
model TEXT,
modification_datetime TIMESTAMP WITH TIME ZONE,
primary_path TEXT NOT NULL UNIQUE,
size BIGINT,
title TEXT,
a_text TEXT,
b_text TEXT,
c_text TEXT,
d_text TEXT,
textsearch tsvector GENERATED ALWAYS AS (
setweight(to_tsvector('english'::regconfig, COALESCE(a_text, '')), 'A'::"char") ||
setweight(to_tsvector('english'::regconfig, COALESCE(b_text, '')), 'B'::"char") ||
setweight(to_tsvector('english'::regconfig, COALESCE(c_text, '')), 'C'::"char") ||
setweight(to_tsvector('english'::regconfig, COALESCE(d_text, '')), 'D'::"char")
) STORED
)''')
session.execute(
'ALTER TABLE video ADD COLUMN file_group_id BIGINT REFERENCES file_group(id) ON DELETE CASCADE NOT NULL UNIQUE')
session.execute(
'ALTER TABLE ebook ADD COLUMN file_group_id BIGINT REFERENCES file_group(id) ON DELETE CASCADE NOT NULL UNIQUE')
session.execute(
'ALTER TABLE archive ADD COLUMN file_group_id BIGINT REFERENCES file_group(id) ON DELETE CASCADE NOT NULL UNIQUE')
Making example label a tag. Tags can be created and deleted in UI. Adding tag_file using id to avoid session error. Using UTC in test. Matching tag_names style in test. Tag icon is displayed on file rows. Archives, Videos and Files can be filtered by their tags. Tags can be added and removed from UI. Tags are displayed in VideoPlayer. The tags of a file are returned in it's JSON. Video favorites are converted to Favorite tag on channel config import. Updating tests to match frontend usage. PDF creator and modification time are displayed. Reworking file search to use Video.__json__ for complex video relations. Archives can be viewed. Removing ebook.title. Reworking alembic migration to be an upgrade rather than a replace. Skipping test on circleci. Clearing out video caption index only if extract_subtitles if off. Channel tests pass. Some video tests pass. Missing videos can be found. A video can be downloaded. Orphaned files can be found. Videos can be tagged. Videos can be searched. Videos can be modeled. Converting some tests to use FileGroup.primary_path. Adding archive and domains to alembic migration. Testing cascading Archive titles. Archives now uses FileGroup. archive.lib tests pass. Archive is partially converted to FileGroup. Calculating FileGroup.primary file on refresh, rather than on select. wrolpi tests pass. Using FilesType to convert between str/pathlib.Path for FileGroup.files. Using list of files in FileGroup.files instead of dictionary. Storing files in dedicated FileGroup.files to avoid overwriting them. Adding comment explaining CASE. Not overwriting FileGroup.data on refresh if no change has been detected. Removing File model in favor of FileGroup.data. Adding typing to Files methods. Files are indexed using the primary mimetype of the FileGroup. files test_lib tests pass. Removing file_group_file in favor of simple one-to-many relationship. PDF files are indexed during refresh. Adding Tag and TagFile to tag files with specific tags. Only iterating when full_stem_ids has contents. Testing that FileGroups are related to their Files. Files and file_groups are created and linked. Bump webpack from 5.75.0 to 5.76.1 in /app (#160) Bumps [webpack](https://github.com/webpack/webpack) from 5.75.0 to 5.76.1. - [Release notes](https://github.com/webpack/webpack/releases) - [Commits](https://github.com/webpack/webpack/compare/v5.75.0...v5.76.1) --- updated-dependencies: - dependency-name: webpack dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-24 08:58:18 -06:00
session.execute('''
CREATE TABLE tag (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
color VARCHAR(7)
)''')
session.execute('''
CREATE TABLE tag_file (
tag_id INTEGER REFERENCES tag(id),
file_group_id BIGINT REFERENCES file_group(id) ON DELETE CASCADE,
2023-04-10 08:41:35 -06:00
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP,
Making example label a tag. Tags can be created and deleted in UI. Adding tag_file using id to avoid session error. Using UTC in test. Matching tag_names style in test. Tag icon is displayed on file rows. Archives, Videos and Files can be filtered by their tags. Tags can be added and removed from UI. Tags are displayed in VideoPlayer. The tags of a file are returned in it's JSON. Video favorites are converted to Favorite tag on channel config import. Updating tests to match frontend usage. PDF creator and modification time are displayed. Reworking file search to use Video.__json__ for complex video relations. Archives can be viewed. Removing ebook.title. Reworking alembic migration to be an upgrade rather than a replace. Skipping test on circleci. Clearing out video caption index only if extract_subtitles if off. Channel tests pass. Some video tests pass. Missing videos can be found. A video can be downloaded. Orphaned files can be found. Videos can be tagged. Videos can be searched. Videos can be modeled. Converting some tests to use FileGroup.primary_path. Adding archive and domains to alembic migration. Testing cascading Archive titles. Archives now uses FileGroup. archive.lib tests pass. Archive is partially converted to FileGroup. Calculating FileGroup.primary file on refresh, rather than on select. wrolpi tests pass. Using FilesType to convert between str/pathlib.Path for FileGroup.files. Using list of files in FileGroup.files instead of dictionary. Storing files in dedicated FileGroup.files to avoid overwriting them. Adding comment explaining CASE. Not overwriting FileGroup.data on refresh if no change has been detected. Removing File model in favor of FileGroup.data. Adding typing to Files methods. Files are indexed using the primary mimetype of the FileGroup. files test_lib tests pass. Removing file_group_file in favor of simple one-to-many relationship. PDF files are indexed during refresh. Adding Tag and TagFile to tag files with specific tags. Only iterating when full_stem_ids has contents. Testing that FileGroups are related to their Files. Files and file_groups are created and linked. Bump webpack from 5.75.0 to 5.76.1 in /app (#160) Bumps [webpack](https://github.com/webpack/webpack) from 5.75.0 to 5.76.1. - [Release notes](https://github.com/webpack/webpack/releases) - [Commits](https://github.com/webpack/webpack/compare/v5.75.0...v5.76.1) --- updated-dependencies: - dependency-name: webpack dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-24 08:58:18 -06:00
UNIQUE (tag_id, file_group_id)
)''')
session.execute('''
INSERT INTO tag (name, color) VALUES
2023-03-29 17:37:13 -06:00
('Automotive', '#353535'),
('Clothing', '#9858C5'),
('Comms', '#ECD71C'),
('Cooking', '#00A04A'),
('Electronics', '#1b731f'),
('Entertainment', '#981473'),
('Favorite', '#000000'),
('Fire Safety', '#d16711'),
('Firearms', '#6c6c6c'),
('First Aid', '#C40405'),
('Food Preservation', '#676816'),
('Food', '#2F8055'),
('Fuel', '#c47fb3'),
('Garden', '#3E8B22'),
('Heating', '#ff5400'),
('Husbandry','#B58039'),
('Hygiene', '#adadad'),
('Knots', '#9d6a4c'),
('Light', '#EEEEEE'),
('Mechanical', '#7A7A7A'),
('Medical', '#4a869c'),
('Navigation', '#1ab3ce'),
('Nuclear', '#0dff00'),
2023-05-10 15:24:44 -06:00
('Pests', '#050068'),
2023-03-29 17:37:13 -06:00
('Power', '#D837FF'),
('Primitive', '#A18A62'),
2023-03-29 17:37:13 -06:00
('Radio', '#134cbb'),
('Schooling', '#6d752e'),
('Security', '#333333'),
('Shelter', '#625641'),
('Social', '#490c73'),
('Tools', '#f0d730'),
('WROL', '#4700ab'),
('Water', '#291BC2')
Making example label a tag. Tags can be created and deleted in UI. Adding tag_file using id to avoid session error. Using UTC in test. Matching tag_names style in test. Tag icon is displayed on file rows. Archives, Videos and Files can be filtered by their tags. Tags can be added and removed from UI. Tags are displayed in VideoPlayer. The tags of a file are returned in it's JSON. Video favorites are converted to Favorite tag on channel config import. Updating tests to match frontend usage. PDF creator and modification time are displayed. Reworking file search to use Video.__json__ for complex video relations. Archives can be viewed. Removing ebook.title. Reworking alembic migration to be an upgrade rather than a replace. Skipping test on circleci. Clearing out video caption index only if extract_subtitles if off. Channel tests pass. Some video tests pass. Missing videos can be found. A video can be downloaded. Orphaned files can be found. Videos can be tagged. Videos can be searched. Videos can be modeled. Converting some tests to use FileGroup.primary_path. Adding archive and domains to alembic migration. Testing cascading Archive titles. Archives now uses FileGroup. archive.lib tests pass. Archive is partially converted to FileGroup. Calculating FileGroup.primary file on refresh, rather than on select. wrolpi tests pass. Using FilesType to convert between str/pathlib.Path for FileGroup.files. Using list of files in FileGroup.files instead of dictionary. Storing files in dedicated FileGroup.files to avoid overwriting them. Adding comment explaining CASE. Not overwriting FileGroup.data on refresh if no change has been detected. Removing File model in favor of FileGroup.data. Adding typing to Files methods. Files are indexed using the primary mimetype of the FileGroup. files test_lib tests pass. Removing file_group_file in favor of simple one-to-many relationship. PDF files are indexed during refresh. Adding Tag and TagFile to tag files with specific tags. Only iterating when full_stem_ids has contents. Testing that FileGroups are related to their Files. Files and file_groups are created and linked. Bump webpack from 5.75.0 to 5.76.1 in /app (#160) Bumps [webpack](https://github.com/webpack/webpack) from 5.75.0 to 5.76.1. - [Release notes](https://github.com/webpack/webpack/releases) - [Commits](https://github.com/webpack/webpack/compare/v5.75.0...v5.76.1) --- updated-dependencies: - dependency-name: webpack dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-24 08:58:18 -06:00
''')
session.execute('UPDATE wrolpi_flag SET refresh_complete = false')
if not DOCKERIZED:
session.execute('ALTER TABLE public.file_group OWNER TO wrolpi')
session.execute('ALTER TABLE public.tag OWNER TO wrolpi')
session.execute('ALTER TABLE public.tag_file OWNER TO wrolpi')
def downgrade():
bind = op.get_bind()
session = Session(bind=bind)
session.execute('DROP TABLE IF EXISTS tag_file')
session.execute('DROP TABLE IF EXISTS tag')
session.execute('ALTER TABLE video DROP COLUMN file_group_id')
session.execute('ALTER TABLE ebook DROP COLUMN file_group_id')
session.execute('ALTER TABLE archive DROP COLUMN file_group_id')
session.execute('DROP TABLE file_group')
session.execute('''
create table file
(
path text not null primary key,
idempotency timestamp with time zone,
indexed boolean default false,
mimetype text,
suffix text,
model text,
associated boolean default false,
modification_datetime timestamp with time zone,
size bigint,
title text,
a_text text,
b_text text,
c_text text,
d_text text,
textsearch tsvector generated always as ((
((setweight(to_tsvector('english'::regconfig, COALESCE(a_text, ''::text)), 'A'::"char") ||
setweight(to_tsvector('english'::regconfig, COALESCE(b_text, ''::text)), 'B'::"char")) ||
setweight(to_tsvector('english'::regconfig, COALESCE(c_text, ''::text)), 'C'::"char")) ||
setweight(to_tsvector('english'::regconfig, COALESCE(d_text, ''::text)), 'D'::"char"))) stored,
full_stem text
);
alter table file
owner to postgres;
create index file_mimetype_idx
on file (mimetype);
create index file_textsearch_idx
on file using gin (textsearch);
create index file_modification_datetime_idx
on file (modification_datetime);
create index file_size_idx
on file (size);
create index file_model_idx
on file (model);
create index file_idempotency_idx
on file (idempotency);
create index file_suffix_idx
on file (suffix);
create index file_full_stem_idx
on file (full_stem);
''')
session.execute('DELETE FROM video') # noqa
session.execute('ALTER TABLE video ADD COLUMN caption_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE video ADD COLUMN censored BOOLEAN DEFAULT FALSE')
session.execute('ALTER TABLE video ADD COLUMN favorite TIMESTAMPTZ')
session.execute('ALTER TABLE video ADD COLUMN info_json_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE video ADD COLUMN poster_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE video ADD COLUMN video_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE video ADD COLUMN modification_datetime TIMESTAMPTZ')
session.execute('ALTER TABLE video ADD COLUMN size BIGINT')
session.execute('ALTER TABLE video ADD COLUMN title TEXT')
session.execute('ALTER TABLE video ADD COLUMN validated BOOLEAN DEFAULT FALSE')
session.execute('DELETE FROM archive') # noqa
session.execute('ALTER TABLE archive ADD COLUMN readability_json_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE archive ADD COLUMN readability_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE archive ADD COLUMN readability_txt_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE archive ADD COLUMN screenshot_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE archive ADD COLUMN singlefile_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE archive ADD COLUMN title TEXT')
session.execute('ALTER TABLE archive ADD COLUMN validated BOOLEAN DEFAULT FALSE')
session.execute('DELETE FROM ebook') # noqa
session.execute('ALTER TABLE ebook ADD COLUMN cover_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE ebook ADD COLUMN ebook_path TEXT REFERENCES file(path)')
session.execute('ALTER TABLE ebook ADD COLUMN title TEXT')
session.execute('UPDATE wrolpi_flag SET refresh_complete = false')
if not DOCKERIZED:
session.execute('ALTER TABLE public.file OWNER TO wrolpi')