Class: ActiveRecord::Tasks::PostgreSQLDatabaseTasks

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_tasks.rb

Instance Method Summary collapse

Instance Method Details

#data_dump(filename) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pg_tasks.rb', line 73

def data_dump(filename)
  set_psql_env
  command = 'pg_dump -F c -a -T schema_migrations -x -O -f ' \
    "#{Shellwords.escape(filename.to_s)} " \
    "#{Shellwords.escape(configuration['database'])}"
  unless Kernel.system(command)
    raise 'Error during data_dump'
  else
    $stdout.puts "The data of '#{configuration['database']} " \
      "has been dumped to '#{filename}'"
  end
end

#data_restore(filename) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pg_tasks.rb', line 86

def data_restore(filename)
  set_psql_env
  command = 'pg_restore --disable-triggers --exit-on-error ' \
    '--single-transaction -a -x -O ' \
    "-d #{Shellwords.escape(configuration['database'])} " \
    "#{Shellwords.escape(filename.to_s)}"
  unless Kernel.system(command)
    raise 'Error during data_restore '
  else
    $stdout.puts "Data from '#{filename}' has been restored to \
                  '#{configuration['database']}'"
  end
end

#structure_and_data_dump(filename) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pg_tasks.rb', line 100

def structure_and_data_dump(filename)
  set_psql_env
  command = "pg_dump -F c -x -O -f \
  #{Shellwords.escape(filename)} \
  #{Shellwords.escape(configuration['database'])}"
  unless Kernel.system(command)
    raise 'Error during structure_and_data_dump'
  else
    $stdout.puts 'Structure and data of ' \
      "'#{configuration['database']}' has been dumped to '#{filename}'"
  end
end

#structure_and_data_restore(filename) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pg_tasks.rb', line 113

def structure_and_data_restore(filename)
  set_psql_env
  command = 'pg_restore --disable-triggers --exit-on-error ' \
    '--single-transaction -x -O -d ' \
    "#{Shellwords.escape(configuration['database'])} " \
    "#{Shellwords.escape(filename.to_s)}"
  unless Kernel.system(command)
    raise 'Error during structure_and_data_restore '
  else
    $stdout.puts "Structure and data of '#{configuration['database']}' " \
      "has been restored to '#{filename}'"
  end
end

#terminate_connectionsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pg_tasks.rb', line 127

def terminate_connections
  set_psql_env
  database = configuration['database']
  command = "psql -c \"SELECT pg_terminate_backend(pg_stat_activity.pid) " \
    ' FROM pg_stat_activity ' \
    "WHERE pg_stat_activity.datname = '#{database}' " \
    " AND pid <> pg_backend_pid();\" '#{database}'"
  unless Kernel.system(command)
    raise 'Error during terminate_connections'
  else
    $stdout.puts "Connections to '#{database}' have been terminated."
  end
end