psql-cm 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +7 -0
- data/README.md +28 -0
- data/lib/psql-cm/base.rb +32 -21
- data/lib/psql-cm/cli.rb +2 -2
- data/lib/psql-cm/database.rb +33 -12
- data/lib/psql-cm/version.rb +1 -1
- data/lib/psql-cm.rb +3 -0
- metadata +4 -3
data/History.md
ADDED
data/README.md
CHANGED
@@ -1,4 +1,32 @@
|
|
1
1
|
# PostgreSQL Change Management Tool
|
2
2
|
|
3
|
+
This project is a tool for schema change management within a PostgreSQL database
|
4
|
+
cluster.
|
3
5
|
|
6
|
+
# Setup
|
7
|
+
|
8
|
+
Setup the psql\_cm control tables on the target databases,
|
9
|
+
|
10
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
|
11
|
+
|
12
|
+
# Dump
|
13
|
+
|
14
|
+
Dump the current database schema to the specified --sql-path directory, if none
|
15
|
+
specified it dumps to $PWD/sql
|
16
|
+
|
17
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
|
18
|
+
|
19
|
+
# Restore
|
20
|
+
|
21
|
+
Restore a previously psql-cm dumped database schema into a brand new postgresql
|
22
|
+
database cluster.
|
23
|
+
|
24
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" restore
|
25
|
+
|
26
|
+
# Example
|
27
|
+
|
28
|
+
$ createdb psqlcm_test
|
29
|
+
$ psql psqlcm_test -c 'CREATE SCHEMA schema_one; CREATE SCHEMA schema_two'
|
30
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
|
31
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
|
4
32
|
|
data/lib/psql-cm/base.rb
CHANGED
@@ -14,24 +14,24 @@ module PSQLCM
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def config
|
17
|
-
require 'ostruct'
|
18
17
|
@config ||= OpenStruct.new
|
19
|
-
end
|
18
|
+
end
|
20
19
|
|
21
20
|
def databases
|
22
21
|
@databases = db.
|
23
22
|
exec("SELECT datname as name FROM pg_database WHERE datname !~ 'template*|postgres';").
|
24
23
|
map {|row| row["name"]}
|
25
24
|
|
26
|
-
|
27
|
-
unless config.databases.to_a.empty?
|
25
|
+
unless config.databases.to_a.empty? # filter out databases not specified.
|
28
26
|
@databases.select!{ |name| config.databases.include?(name) }
|
29
27
|
end
|
28
|
+
|
29
|
+
debug "databases> #{@databases}"
|
30
30
|
@databases
|
31
31
|
end
|
32
32
|
|
33
|
-
def schemas
|
34
|
-
@schemas = db.
|
33
|
+
def schemas(name = 'postgres')
|
34
|
+
@schemas = db(name).
|
35
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}"
|
@@ -41,35 +41,33 @@ module PSQLCM
|
|
41
41
|
def tree
|
42
42
|
return @tree if @tree
|
43
43
|
@tree = {}
|
44
|
-
databases.each do |
|
45
|
-
@
|
46
|
-
|
47
|
-
|
48
|
-
@tree[name][schema] = ['base.sql', 'cm.sql']
|
44
|
+
databases.each do |dbname|
|
45
|
+
@tree[dbname] = {}
|
46
|
+
schemas(dbname).each do |schema|
|
47
|
+
@tree[dbname][schema] = ['base.sql', 'cm.sql']
|
49
48
|
end
|
50
49
|
end
|
51
50
|
debug "tree> tree: #{@tree}"
|
52
51
|
@tree
|
53
52
|
end
|
54
53
|
|
55
|
-
def
|
54
|
+
def dump!
|
56
55
|
unless config.sql_path
|
57
56
|
$stdout.puts "Warning: --sql-path was not set, defaulting to $PWD/sql."
|
58
57
|
config.sql_path = "#{ENV["PWD"]}/sql"
|
59
58
|
end
|
60
59
|
|
60
|
+
debug "dump> sql_path: #{config.sql_path}"
|
61
61
|
FileUtils.mkdir(config.sql_path) unless File.directory?(config.sql_path)
|
62
|
-
|
63
|
-
debug "generate> sql_path: #{config.sql_path}"
|
64
62
|
Dir.chdir(config.sql_path) do
|
65
|
-
tree.each_pair do |database,
|
66
|
-
debug "
|
63
|
+
tree.each_pair do |database, schema_hash|
|
64
|
+
debug "dump> database: #{database}"
|
67
65
|
|
68
66
|
File.directory?(File.join(config.sql_path,database)) or
|
69
67
|
FileUtils.mkdir(File.join(config.sql_path,database))
|
70
68
|
|
71
|
-
|
72
|
-
debug "
|
69
|
+
schema_hash.each_pair do |schema, files|
|
70
|
+
debug "dump> schema: #{schema}"
|
73
71
|
File.directory?(File.join(config.sql_path,database,schema)) or
|
74
72
|
FileUtils.mkdir(File.join(config.sql_path,database,schema))
|
75
73
|
|
@@ -86,12 +84,21 @@ module PSQLCM
|
|
86
84
|
command += "--file=#{base_file} --exclude-table=psql_cm "
|
87
85
|
end
|
88
86
|
command += "#{database}"
|
89
|
-
debug "
|
87
|
+
debug "dump> #{command}"
|
90
88
|
|
91
89
|
%x[#{command}]
|
92
90
|
end
|
93
91
|
end
|
94
92
|
end
|
93
|
+
end # def dump!
|
94
|
+
|
95
|
+
def setup!
|
96
|
+
# Create psql_cm tables for each schema on the target db.
|
97
|
+
end
|
98
|
+
|
99
|
+
def load!
|
100
|
+
# TODO: Load psql-cm filesystem path files {base,cm}.sql into database
|
101
|
+
# structure.
|
95
102
|
end
|
96
103
|
|
97
104
|
def run!(action = config.action, parent_id = config.parent_id)
|
@@ -100,8 +107,12 @@ module PSQLCM
|
|
100
107
|
::PSQLCM.debug "Starting Console"
|
101
108
|
require 'psql-cm/cli'
|
102
109
|
::PSQLCM::Console.run!
|
103
|
-
when "
|
104
|
-
|
110
|
+
when "dump"
|
111
|
+
dump!
|
112
|
+
when "restore"
|
113
|
+
restore!
|
114
|
+
when "setup"
|
115
|
+
setup!
|
105
116
|
else
|
106
117
|
halt! "Action '#{action}' is not handled."
|
107
118
|
end
|
data/lib/psql-cm/cli.rb
CHANGED
@@ -10,7 +10,7 @@ module PSQLCM
|
|
10
10
|
options.separator ""
|
11
11
|
options.separator "Specific options:"
|
12
12
|
|
13
|
-
options.on("-s", "--sql-path PATH", "Path to
|
13
|
+
options.on("-s", "--sql-path PATH", "Path to dump SQL cm files into.") do |path|
|
14
14
|
::PSQLCM.config.sql_path = path
|
15
15
|
end
|
16
16
|
|
@@ -19,7 +19,7 @@ module PSQLCM
|
|
19
19
|
end
|
20
20
|
|
21
21
|
options.on("-u", "--uri URI", "Path to the sink database connection file.") do |uri|
|
22
|
-
::PSQLCM.
|
22
|
+
::PSQLCM.config.uri = uri
|
23
23
|
end
|
24
24
|
|
25
25
|
options.on("-D", "--[no-]debug", "Output debugging information.") do |debug|
|
data/lib/psql-cm/database.rb
CHANGED
@@ -1,24 +1,45 @@
|
|
1
|
-
require 'uri'
|
2
|
-
|
3
1
|
module PSQLCM
|
4
|
-
|
5
|
-
|
2
|
+
class Connection < Delegator
|
3
|
+
def initialize(options = {})
|
4
|
+
@name = options[:dbname] || 'postgres'
|
5
|
+
@config = ::PSQLCM.config.connection.merge(options)
|
6
|
+
|
7
|
+
super # For delegator pattern:
|
8
|
+
@delegated_object = db
|
9
|
+
end
|
10
|
+
|
11
|
+
# Delegator to PG::Connection
|
12
|
+
def __getobj__ ; @db end
|
13
|
+
def __setobj__(object) ; end
|
14
|
+
|
6
15
|
def db
|
7
|
-
|
8
|
-
connect!
|
16
|
+
@db || connect!
|
9
17
|
end
|
10
18
|
|
11
19
|
def connect!
|
12
|
-
@
|
13
|
-
@db = PG.connect(@config.connection)
|
20
|
+
@db = PG.connect(@config)
|
14
21
|
end
|
15
22
|
|
16
|
-
def reconnect!
|
17
|
-
|
23
|
+
def reconnect!(name = @name)
|
24
|
+
close!
|
18
25
|
connect!
|
19
26
|
end
|
20
27
|
|
21
|
-
def
|
28
|
+
def close!
|
29
|
+
@db.close
|
30
|
+
end
|
31
|
+
end # class Connection
|
32
|
+
|
33
|
+
class << self
|
34
|
+
def db(name = 'postgres')
|
35
|
+
@db ||= {}
|
36
|
+
return @db[name] if @db[name]
|
37
|
+
@config.connection || configure!
|
38
|
+
@db[name] = Connection.new(:dbname => name)
|
39
|
+
end
|
40
|
+
|
41
|
+
# "postgres://{user}:{password}@{host}:{port}/{database}"
|
42
|
+
def configure!
|
22
43
|
uri = URI.parse(::PSQLCM.config.uri)
|
23
44
|
|
24
45
|
query = uri.query.to_s.split('&')
|
@@ -37,5 +58,5 @@ module PSQLCM
|
|
37
58
|
}.delete_if { |key, value| value.nil? }
|
38
59
|
end
|
39
60
|
end # class << self
|
40
|
-
end # module PSQLCM
|
41
61
|
|
62
|
+
end # module PSQLCM
|
data/lib/psql-cm/version.rb
CHANGED
data/lib/psql-cm.rb
CHANGED
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.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 0.13.2
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 0.13.2
|
30
30
|
description: PostgreSQL Change Management Tool
|
31
31
|
email:
|
32
32
|
- wayneeseguin@gmail.com
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- lib/psql-cm.rb
|
44
44
|
- LICENCE
|
45
45
|
- README.md
|
46
|
+
- History.md
|
46
47
|
- spec/psql-cm_spec.rb
|
47
48
|
- spec/spec_helper.rb
|
48
49
|
homepage: http://rubygems.org/gems/psql-cm/
|