summaryrefslogtreecommitdiff
path: root/src/invidious/database/migrations/0008_create_playlists_table.cr
blob: 6aa16e1a94fb78b06e34a1c8e093166ee238c5d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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