Class: Naifa::Plugins::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/naifa/plugins/postgres.rb,
lib/naifa/plugins/postgres/cli.rb,
lib/naifa/plugins/postgres/settings.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

DEFAULT_FILENAME =
'db_backup'.freeze
DEFAULT_SETTINGS =
{
  filename: DEFAULT_FILENAME,
  path: './data/db_dumps',
  environments: {
    production: {
      type: :heroku,
      remote: 'production'
    },
    staging: {
      type: :heroku,
      remote: 'staging'
    },
    remote_example: {
      type: :remote,
      host: '',
      database: '',
      username: '',
      backup_options: ['-Fc'],
      restore_options: ['--verbose', '--clean', '--no-acl', '--no-owner']
    },
    local_example: {
      type: :local,
      database: '',
      username: '',
      backup_options: ['-Fc'],
      restore_options: ['--verbose', '--clean', '--no-acl', '--no-owner']
    },
    development: {
      type: :docker,
      app_name: 'db',
      database: '',
      username: '\$POSTGRES_USER',
      path: '/db_dumps/',
      backup_options: ['-Fc'],
      restore_options: ['--verbose', '--clean', '--no-acl', '--no-owner']
    }
  },
  backup: {
    environment: :staging
  },
  restore: {
    environment: :development
  }
}.with_indifferent_access.freeze

Class Method Summary collapse

Class Method Details

.backup(options = {}) ⇒ Object



18
19
20
21
22
# File 'lib/naifa/plugins/postgres.rb', line 18

def self.backup(options={})
  filename = options.fetch(:filename, DEFAULT_FILENAME)

  _backup(filename, options)
end

.restore(options = {}) ⇒ Object



12
13
14
15
16
# File 'lib/naifa/plugins/postgres.rb', line 12

def self.restore(options={})
  filename = options.fetch(:filename, DEFAULT_FILENAME)

  _restore(filename, options)
end

.sync(options = {}) ⇒ Object



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
# File 'lib/naifa/plugins/postgres.rb', line 24

def self.sync(options={})
  options ||= {}
  filename = options.fetch(:filename, DEFAULT_FILENAME)

  backup_settings = options.fetch(:backup, {})
  restore_settings = options.fetch(:restore, {})
  environments_settings = options.fetch(:environments, {})

  if backup_settings[:environment].blank? ||
      environments_settings[backup_settings[:environment]].blank? ||
      restore_settings[:environment].blank? ||
      environments_settings[restore_settings[:environment]].blank?

    raise Thor::Error, "Sync (Backup and Restore) environments are not defined"
  end

  if environments_settings[backup_settings[:environment]][:type] == :heroku &&
     environments_settings[restore_settings[:environment]][:type] == :heroku

     Heroku::Postgres.sync(
       environments_settings[backup_settings[:environment]][:remote].presence || backup_settings[:environment],
       environments_settings[restore_settings[:environment]][:remote].presence || restore_settings[:environment]
     )
  else
    _backup(filename, options)
    _restore(filename, options)
  end
end