-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add custom migration implementation #2878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8ec992a
Add custom migration implementation
matthewmcgarvey cf13c11
Migrations tweaks
matthewmcgarvey 5965428
Run migrations through CLI instead of when app starts
matthewmcgarvey bf054df
Do not check for pending migrations on app start
matthewmcgarvey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| abstract class Invidious::Database::Migration | ||
| macro inherited | ||
| Migrator.migrations << self | ||
| end | ||
|
|
||
| @@version : Int64? | ||
|
|
||
| def self.version(version : Int32 | Int64) | ||
| @@version = version.to_i64 | ||
| end | ||
|
|
||
| getter? completed = false | ||
|
|
||
| def initialize(@db : DB::Database) | ||
| end | ||
|
|
||
| abstract def up(conn : DB::Connection) | ||
|
|
||
| def migrate | ||
| # migrator already ignores completed migrations | ||
| # but this is an extra check to make sure a migration doesn't run twice | ||
| return if completed? | ||
|
|
||
| @db.transaction do |txn| | ||
| up(txn.connection) | ||
| track(txn.connection) | ||
| @completed = true | ||
| end | ||
| end | ||
|
|
||
| def version : Int64 | ||
| @@version.not_nil! | ||
| end | ||
|
|
||
| private def track(conn : DB::Connection) | ||
| conn.exec("INSERT INTO #{Migrator::MIGRATIONS_TABLE} (version) VALUES ($1)", version) | ||
| end | ||
| end |
30 changes: 30 additions & 0 deletions
30
src/invidious/database/migrations/0001_create_channels_table.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| module Invidious::Database::Migrations | ||
| class CreateChannelsTable < Migration | ||
| version 1 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| conn.exec <<-SQL | ||
| CREATE TABLE IF NOT EXISTS public.channels | ||
| ( | ||
| id text NOT NULL, | ||
| author text, | ||
| updated timestamp with time zone, | ||
| deleted boolean, | ||
| subscribed timestamp with time zone, | ||
| CONSTRAINT channels_id_key UNIQUE (id) | ||
| ); | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| GRANT ALL ON TABLE public.channels TO current_user; | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| CREATE INDEX IF NOT EXISTS channels_id_idx | ||
| ON public.channels | ||
| USING btree | ||
| (id COLLATE pg_catalog."default"); | ||
| SQL | ||
| end | ||
| end | ||
| end |
28 changes: 28 additions & 0 deletions
28
src/invidious/database/migrations/0002_create_videos_table.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| module Invidious::Database::Migrations | ||
| class CreateVideosTable < Migration | ||
| version 2 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| conn.exec <<-SQL | ||
| CREATE UNLOGGED TABLE IF NOT EXISTS public.videos | ||
| ( | ||
| id text NOT NULL, | ||
| info text, | ||
| updated timestamp with time zone, | ||
| CONSTRAINT videos_pkey PRIMARY KEY (id) | ||
| ); | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| GRANT ALL ON TABLE public.videos TO current_user; | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| CREATE UNIQUE INDEX IF NOT EXISTS id_idx | ||
| ON public.videos | ||
| USING btree | ||
| (id COLLATE pg_catalog."default"); | ||
| SQL | ||
| end | ||
| end | ||
| end |
35 changes: 35 additions & 0 deletions
35
src/invidious/database/migrations/0003_create_channel_videos_table.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| module Invidious::Database::Migrations | ||
| class CreateChannelVideosTable < Migration | ||
| version 3 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| conn.exec <<-SQL | ||
| CREATE TABLE IF NOT EXISTS public.channel_videos | ||
| ( | ||
| id text NOT NULL, | ||
| title text, | ||
| published timestamp with time zone, | ||
| updated timestamp with time zone, | ||
| ucid text, | ||
| author text, | ||
| length_seconds integer, | ||
| live_now boolean, | ||
| premiere_timestamp timestamp with time zone, | ||
| views bigint, | ||
| CONSTRAINT channel_videos_id_key UNIQUE (id) | ||
| ); | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| GRANT ALL ON TABLE public.channel_videos TO current_user; | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| CREATE INDEX IF NOT EXISTS channel_videos_ucid_idx | ||
| ON public.channel_videos | ||
| USING btree | ||
| (ucid COLLATE pg_catalog."default"); | ||
| SQL | ||
| end | ||
| end | ||
| end |
34 changes: 34 additions & 0 deletions
34
src/invidious/database/migrations/0004_create_users_table.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| module Invidious::Database::Migrations | ||
| class CreateUsersTable < Migration | ||
| version 4 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| conn.exec <<-SQL | ||
| CREATE TABLE IF NOT EXISTS public.users | ||
| ( | ||
| updated timestamp with time zone, | ||
| notifications text[], | ||
| subscriptions text[], | ||
| email text NOT NULL, | ||
| preferences text, | ||
| password text, | ||
| token text, | ||
| watched text[], | ||
| feed_needs_update boolean, | ||
| CONSTRAINT users_email_key UNIQUE (email) | ||
| ); | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| GRANT ALL ON TABLE public.users TO current_user; | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| CREATE UNIQUE INDEX IF NOT EXISTS email_unique_idx | ||
| ON public.users | ||
| USING btree | ||
| (lower(email) COLLATE pg_catalog."default"); | ||
| SQL | ||
| end | ||
| end | ||
| end |
28 changes: 28 additions & 0 deletions
28
src/invidious/database/migrations/0005_create_session_ids_table.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| module Invidious::Database::Migrations | ||
| class CreateSessionIdsTable < Migration | ||
| version 5 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| conn.exec <<-SQL | ||
| CREATE TABLE IF NOT EXISTS public.session_ids | ||
| ( | ||
| id text NOT NULL, | ||
| email text, | ||
| issued timestamp with time zone, | ||
| CONSTRAINT session_ids_pkey PRIMARY KEY (id) | ||
| ); | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| GRANT ALL ON TABLE public.session_ids TO current_user; | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| CREATE INDEX IF NOT EXISTS session_ids_id_idx | ||
| ON public.session_ids | ||
| USING btree | ||
| (id COLLATE pg_catalog."default"); | ||
| SQL | ||
| end | ||
| end | ||
| end |
27 changes: 27 additions & 0 deletions
27
src/invidious/database/migrations/0006_create_nonces_table.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| module Invidious::Database::Migrations | ||
| class CreateNoncesTable < Migration | ||
| version 6 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| conn.exec <<-SQL | ||
| CREATE TABLE IF NOT EXISTS public.nonces | ||
| ( | ||
| nonce text, | ||
| expire timestamp with time zone, | ||
| CONSTRAINT nonces_id_key UNIQUE (nonce) | ||
| ); | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| GRANT ALL ON TABLE public.nonces TO current_user; | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| CREATE INDEX IF NOT EXISTS nonces_nonce_idx | ||
| ON public.nonces | ||
| USING btree | ||
| (nonce COLLATE pg_catalog."default"); | ||
| SQL | ||
| end | ||
| end | ||
| end |
20 changes: 20 additions & 0 deletions
20
src/invidious/database/migrations/0007_create_annotations_table.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| module Invidious::Database::Migrations | ||
| class CreateAnnotationsTable < Migration | ||
| version 7 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| conn.exec <<-SQL | ||
| CREATE TABLE IF NOT EXISTS public.annotations | ||
| ( | ||
| id text NOT NULL, | ||
| annotations xml, | ||
| CONSTRAINT annotations_id_key UNIQUE (id) | ||
| ); | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| GRANT ALL ON TABLE public.annotations TO current_user; | ||
| SQL | ||
| end | ||
| end | ||
| end |
50 changes: 50 additions & 0 deletions
50
src/invidious/database/migrations/0008_create_playlists_table.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| module Invidious::Database::Migrations | ||
| class CreatePlaylistsTable < Migration | ||
| version 8 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| if !privacy_type_exists?(conn) | ||
| conn.exec <<-SQL | ||
| CREATE TYPE public.privacy AS ENUM | ||
| ( | ||
| 'Public', | ||
| 'Unlisted', | ||
| 'Private' | ||
| ); | ||
| SQL | ||
| end | ||
|
|
||
| conn.exec <<-SQL | ||
| CREATE TABLE IF NOT EXISTS public.playlists | ||
| ( | ||
| title text, | ||
| id text primary key, | ||
| author text, | ||
| description text, | ||
| video_count integer, | ||
| created timestamptz, | ||
| updated timestamptz, | ||
| privacy privacy, | ||
| index int8[] | ||
| ); | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| GRANT ALL ON public.playlists TO current_user; | ||
| SQL | ||
| end | ||
|
|
||
| private def privacy_type_exists?(conn : DB::Connection) : Bool | ||
|
||
| request = <<-SQL | ||
| SELECT 1 AS one | ||
| FROM pg_type | ||
| INNER JOIN pg_namespace ON pg_namespace.oid = pg_type.typnamespace | ||
| WHERE pg_namespace.nspname = 'public' | ||
| AND pg_type.typname = 'privacy' | ||
| LIMIT 1; | ||
| SQL | ||
|
|
||
| !conn.query_one?(request, as: Int32).nil? | ||
| end | ||
| end | ||
| end | ||
27 changes: 27 additions & 0 deletions
27
src/invidious/database/migrations/0009_create_playlist_videos_table.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| module Invidious::Database::Migrations | ||
| class CreatePlaylistVideosTable < Migration | ||
| version 9 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| conn.exec <<-SQL | ||
| CREATE TABLE IF NOT EXISTS public.playlist_videos | ||
| ( | ||
| title text, | ||
| id text, | ||
| author text, | ||
| ucid text, | ||
| length_seconds integer, | ||
| published timestamptz, | ||
| plid text references playlists(id), | ||
| index int8, | ||
| live_now boolean, | ||
| PRIMARY KEY (index,plid) | ||
| ); | ||
| SQL | ||
|
|
||
| conn.exec <<-SQL | ||
| GRANT ALL ON TABLE public.playlist_videos TO current_user; | ||
| SQL | ||
| end | ||
| end | ||
| end |
11 changes: 11 additions & 0 deletions
11
src/invidious/database/migrations/0010_make_videos_unlogged.cr
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module Invidious::Database::Migrations | ||
| class MakeVideosUnlogged < Migration | ||
| version 10 | ||
|
|
||
| def up(conn : DB::Connection) | ||
| conn.exec <<-SQL | ||
| ALTER TABLE public.videos SET UNLOGGED; | ||
| SQL | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a good idea, only issue is that it really messes with my flow of using docker-compose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right. We should probably make a wrapper script for that?
Also, we probably shouldn't
exitif migrations are run. Passing--migratewould always bump the DB no matter what, without breaking the execution process. And we could make that the default on Docker, so owners of small instances aren't too surprised.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I'm concerned that we're going to roll out a breaking change to everyone with this PR. Everyone will update as normal and then find out that they can't run the service and will have to update their command, and I'm not sure the best way to roll that out.
What I'm going to do is leave this as it is, and remove the pending migration check. That way we can merge this change, it won't break anyone, and people can test out the migrations process. Then we can make a follow-up PR with the implementation that requires the migrations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am struggling to run these CLI arguments, when I type
sudo docker exec -it invidious-invidious-1 invidious -vI get