psql-cm 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +4 -0
- data/README.md +23 -9
- data/lib/psql-cm/base.rb +61 -5
- data/lib/psql-cm/version.rb +1 -1
- metadata +2 -2
data/History.md
CHANGED
data/README.md
CHANGED
@@ -3,30 +3,44 @@
|
|
3
3
|
This project is a tool for schema change management within a PostgreSQL database
|
4
4
|
cluster.
|
5
5
|
|
6
|
+
# Prerequisites
|
7
|
+
|
8
|
+
- [Ruby >= 1.9.3](http://www.ruby-lang.org/en/)
|
9
|
+
- [Postgresql >= 9.1+](http://www.postgresql.org/)
|
10
|
+
- [Git SCM](http://git-scm.com/)
|
11
|
+
|
12
|
+
# Installation of psql-cm
|
13
|
+
|
14
|
+
Once the prerequisites have been satisfied on your system, using the
|
15
|
+
'gem' command from Ruby 1.9.3 do:
|
16
|
+
|
17
|
+
user$ gem install psql-scm
|
18
|
+
|
6
19
|
# Setup
|
7
20
|
|
8
|
-
Setup the psql\_cm control tables on the target databases,
|
21
|
+
Setup the psql\_cm control tables on the target databases, use a comma (',')
|
22
|
+
to separate multiple database names.
|
9
23
|
|
10
|
-
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
|
24
|
+
user$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
|
11
25
|
|
12
26
|
# Dump
|
13
27
|
|
14
28
|
Dump the current database schema to the specified --sql-path directory, if none
|
15
29
|
specified it dumps to $PWD/sql
|
16
30
|
|
17
|
-
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
|
31
|
+
user$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
|
18
32
|
|
19
|
-
# Restore
|
33
|
+
# Restore (Currently being implemented)
|
20
34
|
|
21
35
|
Restore a previously psql-cm dumped database schema into a brand new postgresql
|
22
36
|
database cluster.
|
23
37
|
|
24
|
-
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" restore
|
38
|
+
user$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" restore
|
25
39
|
|
26
40
|
# Example
|
27
41
|
|
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
|
42
|
+
user$ createdb psqlcm_test
|
43
|
+
user$ psql psqlcm_test -c 'CREATE SCHEMA schema_one; CREATE SCHEMA schema_two'
|
44
|
+
user$ echo psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
|
45
|
+
user$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
|
32
46
|
|
data/lib/psql-cm/base.rb
CHANGED
@@ -84,9 +84,7 @@ module PSQLCM
|
|
84
84
|
command += "--file=#{base_file} --exclude-table=psql_cm "
|
85
85
|
end
|
86
86
|
command += "#{database}"
|
87
|
-
|
88
|
-
|
89
|
-
%x[#{command}]
|
87
|
+
sh 'dump', command
|
90
88
|
end
|
91
89
|
end
|
92
90
|
end
|
@@ -94,11 +92,62 @@ module PSQLCM
|
|
94
92
|
|
95
93
|
def setup!
|
96
94
|
# Create psql_cm tables for each schema on the target db.
|
95
|
+
debug "setup> Setting up pg_psql_cm table in each target schema."
|
96
|
+
tree.each_pair do |database, schema_hash|
|
97
|
+
schema_hash.keys.each do |schema|
|
98
|
+
debug "setup:#{database}> #{schema}"
|
99
|
+
db(database).exec <<-SQL
|
100
|
+
SET search_path = #{schema}, public;
|
101
|
+
CREATE TABLE IF NOT EXISTS pg_psql_cm
|
102
|
+
(
|
103
|
+
id bigserial NOT NULL PRIMARY KEY ,
|
104
|
+
is_base boolean NOT NULL,
|
105
|
+
created_at timestamp with time zone DEFAULT now(),
|
106
|
+
implementer text NOT NULL,
|
107
|
+
content text NOT NULL
|
108
|
+
);
|
109
|
+
SQL
|
110
|
+
end
|
111
|
+
end
|
97
112
|
end
|
98
113
|
|
99
|
-
def
|
100
|
-
# TODO:
|
114
|
+
def restore!
|
115
|
+
# TODO: Restore psql-cm filesystem path files {base,cm}.sql into database
|
101
116
|
# structure.
|
117
|
+
unless config.sql_path
|
118
|
+
$stdout.puts "Warning: --sql-path was not set, defaulting to $PWD/sql."
|
119
|
+
config.sql_path = "#{ENV["PWD"]}/sql"
|
120
|
+
end
|
121
|
+
|
122
|
+
debug "restore> sql_path: #{config.sql_path}"
|
123
|
+
FileUtils.mkdir(config.sql_path) unless File.directory?(config.sql_path)
|
124
|
+
Dir.chdir(config.sql_path) do
|
125
|
+
tree.each_pair do |database, schema_hash|
|
126
|
+
debug "restore> database: #{database}"
|
127
|
+
|
128
|
+
File.directory?(File.join(config.sql_path,database)) or
|
129
|
+
FileUtils.mkdir(File.join(config.sql_path,database))
|
130
|
+
|
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))
|
135
|
+
|
136
|
+
base_file = File.join(config.sql_path,database,schema,'base.sql')
|
137
|
+
cm_file = File.join(config.sql_path,database,schema,'cm.sql')
|
138
|
+
|
139
|
+
FileUtils.touch(base_file)
|
140
|
+
FileUtils.touch(cm_file)
|
141
|
+
|
142
|
+
command = "psql #{database} < #{base_file}"
|
143
|
+
sh 'restore', command
|
144
|
+
|
145
|
+
next if File.size(cm_file) == 0
|
146
|
+
command = "psql #{database} < #{cm_file}"
|
147
|
+
sh 'restore', command
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
102
151
|
end
|
103
152
|
|
104
153
|
def run!(action = config.action, parent_id = config.parent_id)
|
@@ -118,6 +167,13 @@ module PSQLCM
|
|
118
167
|
end
|
119
168
|
end
|
120
169
|
|
170
|
+
private
|
171
|
+
|
172
|
+
def sh(tag, command)
|
173
|
+
debug "sh:#{tag}> #{command}"
|
174
|
+
%x[#{command}]
|
175
|
+
end
|
176
|
+
|
121
177
|
end # class << self
|
122
178
|
end
|
123
179
|
|
data/lib/psql-cm/version.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.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
65
65
|
- !ruby/object:Gem::Version
|
66
|
-
version: 1.
|
66
|
+
version: 1.3.6
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
69
|
rubygems_version: 1.8.22
|