psql-cm 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.0.4 - 2012-04-18
2
+
3
+ 'restore' action functional.
4
+
1
5
  # 0.0.3 - 2012-04-18
2
6
 
3
7
  'setup' action functional.
data/lib/psql-cm/base.rb CHANGED
@@ -32,7 +32,7 @@ module PSQLCM
32
32
 
33
33
  def schemas(name = 'postgres')
34
34
  @schemas = db(name).
35
- exec("SELECT nspname as name FROM pg_namespace WHERE nspname !~ '^pg_.*|information_schema';").
35
+ exec("SELECT nspname AS name FROM pg_namespace WHERE nspname !~ '^pg_.*|information_schema';").
36
36
  map{|row| row["name"]}
37
37
  debug "schemas> #{@schemas}"
38
38
  @schemas
@@ -77,7 +77,7 @@ module PSQLCM
77
77
  FileUtils.touch(base_file)
78
78
  FileUtils.touch(cm_file)
79
79
 
80
- command = "pg_dump --schema-only --no-owner --schema=#{schema} "
80
+ command = "pg_dump #{db(database).psql_args} --schema-only --no-owner --schema=#{schema} "
81
81
  if File.size(base_file) > 0
82
82
  command += "--file=#{cm_file} --table=psql_cm "
83
83
  else
@@ -95,10 +95,9 @@ module PSQLCM
95
95
  debug "setup> Setting up pg_psql_cm table in each target schema."
96
96
  tree.each_pair do |database, schema_hash|
97
97
  schema_hash.keys.each do |schema|
98
- debug "setup:#{database}> #{schema}"
99
- db(database).exec <<-SQL
98
+ sql = <<-SQL
100
99
  SET search_path = #{schema}, public;
101
- CREATE TABLE IF NOT EXISTS pg_psql_cm
100
+ CREATE TABLE IF NOT EXISTS #{schema}.pg_psql_cm
102
101
  (
103
102
  id bigserial NOT NULL PRIMARY KEY ,
104
103
  is_base boolean NOT NULL,
@@ -107,44 +106,45 @@ module PSQLCM
107
106
  content text NOT NULL
108
107
  );
109
108
  SQL
109
+ debug "setup:#{database}:#{schema}> sql\n#{sql}"
110
+ db(database).exec sql
110
111
  end
111
112
  end
112
113
  end
113
114
 
114
115
  def restore!
115
- # TODO: Restore psql-cm filesystem path files {base,cm}.sql into database
116
- # structure.
117
116
  unless config.sql_path
118
117
  $stdout.puts "Warning: --sql-path was not set, defaulting to $PWD/sql."
119
118
  config.sql_path = "#{ENV["PWD"]}/sql"
120
119
  end
121
120
 
122
121
  debug "restore> sql_path: #{config.sql_path}"
123
- FileUtils.mkdir(config.sql_path) unless File.directory?(config.sql_path)
122
+ File.directory?(config.sql_path) or
123
+ halt! "Cannot restore from a path that does not exist: #{config.sql_path}"
124
+
124
125
  Dir.chdir(config.sql_path) do
125
- tree.each_pair do |database, schema_hash|
126
- debug "restore> database: #{database}"
127
126
 
128
- File.directory?(File.join(config.sql_path,database)) or
129
- FileUtils.mkdir(File.join(config.sql_path,database))
127
+ Dir['*'].each do |database|
128
+ next unless File.directory? database
130
129
 
131
- schema_hash.each_pair do |schema, files|
132
- debug "restore> schema: #{schema}"
133
- File.directory?(File.join(config.sql_path,database,schema)) or
134
- FileUtils.mkdir(File.join(config.sql_path,database,schema))
130
+ Dir.chdir(database) do
131
+ sh "restore", "createdb #{db(database).psql_args} #{database}"
135
132
 
136
- base_file = File.join(config.sql_path,database,schema,'base.sql')
137
- cm_file = File.join(config.sql_path,database,schema,'cm.sql')
133
+ debug "restore:#{database}>"
134
+ Dir['*'].each do |schema|
135
+ next unless File.directory? schema
138
136
 
139
- FileUtils.touch(base_file)
140
- FileUtils.touch(cm_file)
137
+ base_file = File.join(config.sql_path,database,schema,'base.sql')
138
+ cm_file = File.join(config.sql_path,database,schema,'cm.sql')
141
139
 
142
- command = "psql #{database} < #{base_file}"
143
- sh 'restore', command
140
+ debug "restore:#{database}:#{schema}> #{base_file}"
141
+ sh 'restore', "psql #{db(database).psql_args} #{database} < #{base_file}"
144
142
 
145
- next if File.size(cm_file) == 0
146
- command = "psql #{database} < #{cm_file}"
147
- sh 'restore', command
143
+ next if File.size(cm_file) == 0
144
+
145
+ debug "restore:#{database}:#{schema}> #{cm_file}"
146
+ sh 'restore', "psql #{db(database).psql_args} #{database} < #{cm_file}"
147
+ end
148
148
  end
149
149
  end
150
150
  end
@@ -167,8 +167,6 @@ module PSQLCM
167
167
  end
168
168
  end
169
169
 
170
- private
171
-
172
170
  def sh(tag, command)
173
171
  debug "sh:#{tag}> #{command}"
174
172
  %x[#{command}]
@@ -1,8 +1,10 @@
1
1
  module PSQLCM
2
+
2
3
  class Connection < Delegator
4
+
3
5
  def initialize(options = {})
4
- @name = options[:dbname] || 'postgres'
5
6
  @config = ::PSQLCM.config.connection.merge(options)
7
+ @config[:dbname] = options[:dbname] || 'postgres'
6
8
 
7
9
  super # For delegator pattern:
8
10
  @delegated_object = db
@@ -13,6 +15,10 @@ module PSQLCM
13
15
  def __setobj__(object) ; end
14
16
 
15
17
  def db
18
+ unless @config[:dbname] == 'postgres'
19
+ ::PSQLCM.sh 'createdb', "createdb #{psql_args} #{@config[:dbname]}"
20
+ end
21
+
16
22
  @db || connect!
17
23
  end
18
24
 
@@ -20,7 +26,7 @@ module PSQLCM
20
26
  @db = PG.connect(@config)
21
27
  end
22
28
 
23
- def reconnect!(name = @name)
29
+ def reconnect!(name = @config[:dbname])
24
30
  close!
25
31
  connect!
26
32
  end
@@ -28,6 +34,28 @@ module PSQLCM
28
34
  def close!
29
35
  @db.close
30
36
  end
37
+
38
+ def pgpass # Ensure a pgpass entry exists for this connection.
39
+ pgpass_line = [
40
+ @config[:host], @config[:port], @config[:dbname], @config[:user],
41
+ @config[:password]
42
+ ].join(':')
43
+
44
+ content = File.open(File.join(ENV['HOME'], '.pgpass'), 'r') { |file| file.read }
45
+ unless content.lines.detect{ |line| line == pgpass_line }
46
+ File.open(File.join(ENV['HOME'], '.pgpass'), 'w') do |file|
47
+ file.write(content + pgpass_line + "\n")
48
+ end
49
+ end
50
+
51
+ pgpass_line
52
+ end # def pgpass
53
+
54
+ def psql_args
55
+ pgpass
56
+ "-h #{@config[:host]} -p #{@config[:port]} -U #{@config[:user]}"
57
+ end # def psql_args
58
+
31
59
  end # class Connection
32
60
 
33
61
  class << self
@@ -35,6 +63,7 @@ module PSQLCM
35
63
  @db ||= {}
36
64
  return @db[name] if @db[name]
37
65
  @config.connection || configure!
66
+
38
67
  @db[name] = Connection.new(:dbname => name)
39
68
  end
40
69
 
@@ -51,7 +80,7 @@ module PSQLCM
51
80
  :host => uri.host,
52
81
  :port => uri.port || 5432,
53
82
  :dbname => "postgres", # uri.path.sub('/',''),
54
- :user => uri.user,
83
+ :user => uri.user || ENV['USER'],
55
84
  :password => uri.password,
56
85
  :connect_timeout => timeout.empty? ? 20 : timeout.to_i,
57
86
  :sslmode => sslmode.empty? ? "disable" : sslmode # (disable|allow|prefer|require)
@@ -1,4 +1,4 @@
1
1
  module PSQLCM
2
- Version = '0.0.3'
2
+ Version = '0.0.4'
3
3
  end
4
4
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: psql-cm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-18 00:00:00.000000000 Z
12
+ date: 2012-04-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg