Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20201111145317_add_relation_to_indexes_view.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 318b77c1dc6f3acaea7182aa70bca242e06906d3 (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
51
52
53
54
55
56
57
58
# frozen_string_literal: true

class AddRelationToIndexesView < ActiveRecord::Migration[6.0]
  DOWNTIME = false

  def up
    execute(<<~SQL)
      DROP VIEW postgres_indexes;

      CREATE VIEW postgres_indexes AS
      SELECT (pg_namespace.nspname::text || '.'::text) || pg_class.relname::text AS identifier,
        pg_index.indexrelid,
        pg_namespace.nspname AS schema,
        pg_class.relname AS name,
        pg_indexes.tablename,
        pg_index.indisunique AS "unique",
        pg_index.indisvalid AS valid_index,
        pg_class.relispartition AS partitioned,
        pg_index.indisexclusion AS exclusion,
        pg_index.indexprs IS NOT NULL as expression,
        pg_index.indpred IS NOT NULL as partial,
        pg_indexes.indexdef AS definition,
        pg_relation_size(pg_class.oid::regclass) AS ondisk_size_bytes
      FROM pg_index
        JOIN pg_class ON pg_class.oid = pg_index.indexrelid
        JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
        JOIN pg_indexes ON pg_class.relname = pg_indexes.indexname
      WHERE pg_namespace.nspname <> 'pg_catalog'::name
        AND (pg_namespace.nspname = ANY (ARRAY["current_schema"(), 'gitlab_partitions_dynamic'::name, 'gitlab_partitions_static'::name]));
    SQL
  end

  def down
    execute(<<~SQL)
      DROP VIEW postgres_indexes;

      CREATE VIEW postgres_indexes AS
      SELECT (pg_namespace.nspname::text || '.'::text) || pg_class.relname::text AS identifier,
        pg_index.indexrelid,
        pg_namespace.nspname AS schema,
        pg_class.relname AS name,
        pg_index.indisunique AS "unique",
        pg_index.indisvalid AS valid_index,
        pg_class.relispartition AS partitioned,
        pg_index.indisexclusion AS exclusion,
        pg_index.indexprs IS NOT NULL as expression,
        pg_index.indpred IS NOT NULL as partial,
        pg_indexes.indexdef AS definition,
        pg_relation_size(pg_class.oid::regclass) AS ondisk_size_bytes
      FROM pg_index
        JOIN pg_class ON pg_class.oid = pg_index.indexrelid
        JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
        JOIN pg_indexes ON pg_class.relname = pg_indexes.indexname
      WHERE pg_namespace.nspname <> 'pg_catalog'::name
        AND (pg_namespace.nspname = ANY (ARRAY["current_schema"(), 'gitlab_partitions_dynamic'::name, 'gitlab_partitions_static'::name]));
    SQL
  end
end