psql-cm 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 0.1.1 - 2012-04-27
2
+
3
+ Documentation and debugging enhancements.
4
+
5
+ Setup now properly uses --no-privileges
6
+
7
+ Restore now ensures it uses the correct schema and loops over the change rows.
8
+
1
9
  # 0.1.0 - 2012-04-24
2
10
 
3
11
  Ensure pgpass file is created if it does not exist.
data/lib/psql-cm/base.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  module PSQLCM
2
2
  class << self
3
- def verbose(message)
4
- $stdout.puts message if (config.verbose || config.debug)
3
+ def verbose(*message)
4
+ $stdout.puts message.join(' ') if (config.verbose || config.debug)
5
5
  end
6
6
 
7
- def debug(message)
8
- $stdout.puts message if config.debug
7
+ def debug(*message)
8
+ $stdout.puts message.join(' ') if config.debug
9
9
  end
10
10
 
11
- def halt!(message)
12
- $stderr.puts message
11
+ def halt!(*message)
12
+ $stderr.puts message.join(' ')
13
13
  exit 1
14
14
  end
15
15
 
@@ -4,7 +4,7 @@ module PSQLCM
4
4
 
5
5
  def initialize(options = {})
6
6
  @config = ::PSQLCM.config.connection.merge(options)
7
- @config[:dbname] = options[:dbname] || 'postgres'
7
+ @config[:dbname] = options[:dbname] if options[:dbname]
8
8
 
9
9
  super # For delegator pattern:
10
10
  @delegated_object = db
@@ -84,10 +84,17 @@ module PSQLCM
84
84
  timeout = query.detect { |k| k.match /connect_timeout=/ }.to_s.sub(/.*=/,'')
85
85
  sslmode = query.detect { |k| k.match /sslmode=/ }.to_s.sub(/.*=/,'')
86
86
 
87
+ database = uri.path.split('/').first.to_s
88
+ database = 'postgres' if database.empty?
89
+
90
+ unless @config.databases.detect { |name| name == database }
91
+ @config.databases << database
92
+ end
93
+
87
94
  @config.connection = {
88
95
  :host => uri.host,
89
96
  :port => uri.port || 5432,
90
- :dbname => "postgres", # uri.path.sub('/',''),
97
+ :dbname => database,
91
98
  :user => uri.user || ENV['USER'],
92
99
  :password => uri.password,
93
100
  :connect_timeout => timeout.empty? ? 20 : timeout.to_i,
data/lib/psql-cm/dump.rb CHANGED
@@ -15,8 +15,9 @@ module PSQLCM
15
15
  cm_file = File.join(sql_path,database,"#{schema}.sql")
16
16
 
17
17
  sh %W[ pg_dump #{db(database).psql_args} --no-privileges --no-owner
18
- --schema=#{schema} --file=#{cm_file} --table=#{schema}.#{config.cm_table}
19
- #{database} ].join(' ')
18
+ --schema=#{schema} --file=#{cm_file}
19
+ --table=#{schema}.#{config.cm_table} #{database}
20
+ ].join(' ')
20
21
  end
21
22
  end
22
23
 
@@ -19,42 +19,52 @@ module PSQLCM
19
19
  schema = cm_file.sub(".sql",'')
20
20
  ensure_schema_exists(database,schema)
21
21
 
22
- debug "restore> #{database}:#{schema} < #{cm_file}"
23
- sh "psql #{db(database).psql_args} #{database} < #{cm_file}"
24
-
25
- ensure_cm_table_exists(database,schema)
26
- Tempfile.open('base.sql') do |temp_file|
27
- row = db(database).exec("SELECT content from #{schema}.#{config.cm_table}
28
- WHERE is_base IS true ORDER BY created_at
29
- DESC LIMIT 1;")
30
- temp_file.write(row)
31
- sh "psql #{db(database).psql_args} #{database} < #{temp_file.path}"
22
+ psqlrc_file = File.join(ENV['HOME'],'.psqlrc')
23
+ FileUtils.touch(psqlrc_file) unless File.exists?(psqlrc_file)
24
+ psqlrc = File.read(psqlrc_file)
25
+ File.open(psqlrc_file,'w') do |file|
26
+ file.rewind
27
+ file.write "SET search_path TO #{schema}; "
32
28
  end
33
29
 
34
- sql = "SELECT content from #{schema}.#{config.cm_table} where is_base IS false ORDER BY created_at ASC;"
35
- debug "sql> #{sql}"
36
- db(database).exec(sql).each do |row|
37
- debug "change>\n#{row['content']}"
38
- Tempfile.open('base.sql') do |temp_file|
39
- temp_file.write(row['content'])
40
- temp_file.close
30
+ begin
31
+ tag = "restore:#{database}:#{schema}>"
32
+ debug tag, cm_file
33
+ sh "psql #{db(database).psql_args} #{database} < #{cm_file}"
34
+
35
+ ensure_cm_table_exists(database,schema)
41
36
 
42
- psqlrc_file = File.join(ENV['HOME'],'.psqlrc')
43
- FileUtils.touch(psqlrc_file) unless File.exists?(psqlrc_file)
44
- psqlrc = File.read(psqlrc_file)
45
- File.open(psqlrc_file,'w') do |file|
46
- file.rewind
47
- file.write "SET search_path TO #{schema}; "
37
+ sql = "SELECT content from #{schema}.#{config.cm_table}
38
+ WHERE is_base IS true ORDER BY created_at ASC;"
39
+ debug tag, "base:sql> #{sql}"
40
+ db(database).exec(sql).each do |base_row|
41
+ debug "BASE content:", base_row['content']
42
+ Tempfile.open('base.sql') do |file|
43
+ file.write(base_row['content'])
44
+ sh "psql #{db(database).psql_args} #{database} < #{file.path}"
48
45
  end
49
- begin
50
- sh "psql #{db(database).psql_args} #{database} < #{temp_file.path}"
51
- ensure
52
- File.open(psqlrc_file,'w') do |file|
53
- file.rewind
54
- file.write psqlrc
55
- end
46
+ end
47
+
48
+ sql = "SELECT content from #{schema}.#{config.cm_table}
49
+ WHERE is_base IS false
50
+ ORDER BY created_at ASC;"
51
+
52
+ debug tag, "changes:sql> #{sql}"
53
+ changes = db(database).exec(sql)
54
+ debug tag, "change:count>#{changes.cmd_tuples}"
55
+ changes.each do |row|
56
+ debug tag, "content>\n#{row['content']}"
57
+ Tempfile.open('change.sql') do |file|
58
+ file.write(row['content'])
59
+ file.close
60
+ sh "psql #{db(database).psql_args} #{database} < #{file.path}"
56
61
  end
57
62
  end
63
+ ensure
64
+ File.open(psqlrc_file,'w') do |file|
65
+ file.rewind
66
+ file.write psqlrc
67
+ end
58
68
  end
59
69
  end
60
70
  end
@@ -62,5 +72,5 @@ module PSQLCM
62
72
  end
63
73
  end # def restore!
64
74
 
65
- end
66
- end
75
+ end # class << self
76
+ end # module PSQLCM
data/lib/psql-cm/setup.rb CHANGED
@@ -8,7 +8,10 @@ module PSQLCM
8
8
  ensure_cm_table_exists(database,schema)
9
9
 
10
10
  Tempfile.open('base.sql') do |temp_file|
11
- sh " pg_dump #{db(database).psql_args} --schema-only --no-owner --schema=#{schema} --file=#{temp_file.path} #{database}"
11
+ sh %W[ pg_dump #{db(database).psql_args}
12
+ --schema-only --no-owner --no-privileges
13
+ --schema=#{schema} --file=#{temp_file.path} #{database}
14
+ ].join(' ')
12
15
 
13
16
  content = %x{cat #{temp_file.path}}
14
17
  name = %x{git config user.name}.strip
@@ -1,4 +1,4 @@
1
1
  module PSQLCM
2
- Version = '0.1.0'
2
+ Version = '0.1.1'
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.1.0
4
+ version: 0.1.1
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-24 00:00:00.000000000 Z
12
+ date: 2012-04-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg