forked from aiwilliams/dataset
/
postgresql.rb
34 lines (30 loc) · 1006 Bytes
/
postgresql.rb
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
module Dataset
module Database # :nodoc:
# The interface to a PostgreSQL database, this will capture by creating a dump
# file and restore by loading one of the same.
#
class Postgresql < Base
def initialize(database_spec, storage_path)
@database = database_spec[:database]
@username = database_spec[:username]
@password = database_spec[:password]
@storage_path = storage_path
FileUtils.mkdir_p(@storage_path)
end
def capture(datasets)
return if datasets.nil? || datasets.empty?
`pg_dump -c #{@database} > #{storage_path(datasets)}`
end
def restore(datasets)
store = storage_path(datasets)
if File.file?(store)
`psql -U #{@username} -p #{@password} -e #{@database} < #{store}`
true
end
end
def storage_path(datasets)
"#{@storage_path}/#{datasets.collect {|c| c.__id__}.join('_')}.sql"
end
end
end
end