summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/migrations/extension_helpers.rb
blob: 435e9e0d2dc1ec0fa6c7bacd55a161bc4311860a (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
59
60
61
62
63
64
65
66
# frozen_string_literal: true

module Gitlab
  module Database
    module Migrations
      module ExtensionHelpers
        def create_extension(extension)
          execute("CREATE EXTENSION IF NOT EXISTS #{extension}")
        rescue ActiveRecord::StatementInvalid => e
          dbname = ApplicationRecord.database.database_name
          user = ApplicationRecord.database.username

          warn(<<~MSG) if e.to_s.include?('permission denied')
            GitLab requires the PostgreSQL extension '#{extension}' installed in database '#{dbname}', but
            the database user is not allowed to install the extension.

            You can either install the extension manually using a database superuser:

              CREATE EXTENSION IF NOT EXISTS #{extension}

            Or, you can solve this by logging in to the GitLab
            database (#{dbname}) using a superuser and running:

                ALTER #{user} WITH SUPERUSER

            This query will grant the user superuser permissions, ensuring any database extensions
            can be installed through migrations.

            For more information, refer to https://docs.gitlab.com/ee/install/postgresql_extensions.html.
          MSG

          raise
        end

        def drop_extension(extension)
          execute("DROP EXTENSION IF EXISTS #{extension}")
        rescue ActiveRecord::StatementInvalid => e
          dbname = ApplicationRecord.database.database_name
          user = ApplicationRecord.database.username

          warn(<<~MSG) if e.to_s.include?('permission denied')
            This migration attempts to drop the PostgreSQL extension '#{extension}'
            installed in database '#{dbname}', but the database user is not allowed
            to drop the extension.

            You can either drop the extension manually using a database superuser:

              DROP EXTENSION IF EXISTS #{extension}

            Or, you can solve this by logging in to the GitLab
            database (#{dbname}) using a superuser and running:

                ALTER #{user} WITH SUPERUSER

            This query will grant the user superuser permissions, ensuring any database extensions
            can be dropped through migrations.

            For more information, refer to https://docs.gitlab.com/ee/install/postgresql_extensions.html.
          MSG

          raise
        end
      end
    end
  end
end