Top Level Namespace

Defined Under Namespace

Modules: PgCron

Constant Summary collapse

DEFAULT_PG_CRON_VERSION =

rubocop:disable Style/SpecialGlobalVars

"1.3.0".freeze

Instance Method Summary collapse

Instance Method Details

#add_pg_cron_to_shared_preload_libraries ⇒ Object

rubocop:enable Layout/LineLength



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tasks/pg_cron/utils.rb', line 71

def add_pg_cron_to_shared_preload_libraries
  psql_config_file_path = PgCron.connection.execute("SHOW config_file").first&.dig("config_file")
  config_file_content = ""
  File.open(psql_config_file_path, "r") do |file|
    file.each_line do |line|
      if line.match(/(#\s*)?shared_preload_libraries =.*/)
        libraries = line.split("=").last.match(/'.*'/).to_a.first.gsub("'", "").split(",").map(&:strip).filter do |s|
          s != "pg_cron"
        end
        libraries << "pg_cron"
        line = "shared_preload_libraries = '#{libraries.join(",")}' \n"
      end

      config_file_content << line
    end
  end

  File.atomic_write(psql_config_file_path, "/tmp") { |file| file.write config_file_content }
end

#create_extension ⇒ Object



111
112
113
# File 'lib/tasks/pg_cron/utils.rb', line 111

def create_extension
  PgCron.connection.enable_extension("pg_cron")
end

#drop_extension ⇒ Object



120
121
122
# File 'lib/tasks/pg_cron/utils.rb', line 120

def drop_extension
  PgCron.connection.disable_extension("pg_cron")
end

#fetch_psql_brew_service_version ⇒ Object



14
15
16
17
# File 'lib/tasks/pg_cron/utils.rb', line 14

def fetch_psql_brew_service_version
  psql_services = `brew services list | grep "started" | grep "postgresql"`
  psql_services.split(" ").first.strip
end

#install_pg_cron ⇒ Object

rubocop:disable Layout/LineLength



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tasks/pg_cron/utils.rb', line 56

def install_pg_cron
  latest_pg_cron_path = `brew --prefix postgresql`.strip
  compilation_path = "#{latest_pg_cron_path}/bin:$PATH"
  pg_cron_version = DEFAULT_PG_CRON_VERSION
  `cd /tmp && \
    curl -s -L https://github.com/citusdata/pg_cron/archive/refs/tags/v#{pg_cron_version}.zip --output pg_cron.zip && \
    unzip -u pg_cron.zip && \
    cd pg_cron-#{pg_cron_version} && \
    PATH=#{compilation_path} make && PATH=#{compilation_path} sudo make install && \
    cp $(brew --prefix postgresql)/share/postgresql/extension/pg_cron* $(brew --prefix #{fetch_psql_brew_service_version})/share/#{fetch_psql_brew_service_version}/extension/ && \
    rm -rf pg_cron*
  `
end

#parse_pg_cron_version(version) ⇒ Object



50
51
52
53
# File 'lib/tasks/pg_cron/utils.rb', line 50

def parse_pg_cron_version(version)
  major_minor_tuple = version.split(".").first(2)
  { major: major_minor_tuple[0], minor: major_minor_tuple[1] }
end

#pg_cron_is_installed(requested_version) ⇒ Object

rubocop:enable Style/GuardClause rubocop:enable Layout/LineLength



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tasks/pg_cron/utils.rb', line 34

def pg_cron_is_installed(requested_version)
  result = PgCron.connection.execute <<-SQL
    SELECT
      extversion
    FROM
      pg_extension
    WHERE
    extname='pg_cron'
  SQL

  extension_version = result.first&.dig("extversion")
  parsed_extension_version = extension_version ? parse_pg_cron_version(extension_version) : nil
  parsed_requested_version = parse_pg_cron_version(requested_version)
  parsed_extension_version == parsed_requested_version
end

#remove_pg_cron_from_shared_preload_libraries ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tasks/pg_cron/utils.rb', line 91

def remove_pg_cron_from_shared_preload_libraries
  psql_config_file_path = PgCron.connection.execute("SHOW config_file").first&.dig("config_file")
  config_file_content = ""
  File.open(psql_config_file_path, "r") do |file|
    file.each_line do |line|
      if line.match(/(#\s*)?shared_preload_libraries =.*/)
        libraries = line.split("=").last.match(/'.*'/).to_a.first.gsub("'", "").split(",").map(&:strip).filter do |s|
          s != "pg_cron"
        end
        line = "shared_preload_libraries = '#{libraries.join(",")}' \n"
        line.prepend("#") if libraries.empty?
      end

      config_file_content << line
    end
  end

  File.atomic_write(psql_config_file_path, "/tmp") { |file| file.write config_file_content }
end

#restart_psql_service ⇒ Object



115
116
117
118
# File 'lib/tasks/pg_cron/utils.rb', line 115

def restart_psql_service
  `brew services restart #{fetch_psql_brew_service_version}`
  sleep 1 # Prevent timing issues
end

#validate_homebrew_is_installed ⇒ Object



5
6
7
8
# File 'lib/tasks/pg_cron/utils.rb', line 5

def validate_homebrew_is_installed
  `brew --version`
  raise "This rake task only support Darwin based OS" unless $?.success?
end

#validate_latest_psql_is_installed ⇒ Object

rubocop:disable Style/GuardClause rubocop:disable Layout/LineLength



25
26
27
28
29
30
# File 'lib/tasks/pg_cron/utils.rb', line 25

def validate_latest_psql_is_installed
  `brew list postgresql`
  unless $?.success?
    raise "Due to pg_cron compilation issues the postgresql brew package is needed, please install the latest psql brew package: brew install postgresql"
  end
end

#validate_psql_is_running_with_brew ⇒ Object



10
11
12
# File 'lib/tasks/pg_cron/utils.rb', line 10

def validate_psql_is_running_with_brew
  fetch_psql_brew_service_version.present?
end

#validate_rails_environment ⇒ Object



19
20
21
# File 'lib/tasks/pg_cron/utils.rb', line 19

def validate_rails_environment
  raise "This rake task should only be used in development" unless Rails.env.development?
end