psql-cm 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.md +7 -0
- data/README.md +104 -16
- data/lib/psql-cm/base.rb +11 -0
- data/lib/psql-cm/database.rb +10 -8
- data/lib/psql-cm/version.rb +1 -1
- metadata +1 -1
data/History.md
CHANGED
data/README.md
CHANGED
@@ -1,46 +1,134 @@
|
|
1
1
|
# PostgreSQL Change Management Tool
|
2
2
|
|
3
|
-
|
4
|
-
cluster.
|
3
|
+
## What psql-cm is
|
5
4
|
|
6
|
-
|
5
|
+
This project is a tool to assist with an ITIL like change management process
|
6
|
+
for *database schemas* within a PostgreSQL database cluster.
|
7
|
+
|
8
|
+
Specifically psql-cm is a tool which encodes *one* change management process
|
9
|
+
for a complex multi database, multi schema PostgreSQL system.
|
10
|
+
|
11
|
+
This means that psql-cm may be much more than you need for a simple
|
12
|
+
single database system. Please take the time to understand the process and
|
13
|
+
what problems it solves. In order for psql-cm to be effective it must be
|
14
|
+
combined with complimentary process and adherence.
|
15
|
+
|
16
|
+
## What psql-cm is not
|
17
|
+
|
18
|
+
psql-cm is not intended on being a solution whatsoever for data backup.
|
19
|
+
|
20
|
+
For backup of data instead use the
|
21
|
+
[pg\_dump](http://www.postgresql.org/docs/current/static/app-pgdump.html)
|
22
|
+
command line utility for backing up data in addition to a
|
23
|
+
[repliaction](http://www.postgresql.org/docs/current/static/different-replication-solutions.html)
|
24
|
+
technique tailored to your needs.
|
25
|
+
|
26
|
+
## The process
|
27
|
+
|
28
|
+
# Using psql-cm
|
29
|
+
|
30
|
+
## Prerequisites
|
7
31
|
|
8
32
|
- [Ruby >= 1.9.3](http://www.ruby-lang.org/en/)
|
9
33
|
- [Postgresql >= 9.1+](http://www.postgresql.org/)
|
10
34
|
- [Git SCM](http://git-scm.com/)
|
11
35
|
|
12
|
-
|
36
|
+
## Installation
|
13
37
|
|
14
38
|
Once the prerequisites have been satisfied on your system, using the
|
15
39
|
'gem' command from Ruby 1.9.3 do:
|
16
40
|
|
17
|
-
|
41
|
+
$ gem install psql-scm
|
18
42
|
|
19
|
-
|
43
|
+
## Setup
|
20
44
|
|
21
45
|
Setup the psql\_cm control tables on the target databases, use a comma (',')
|
22
46
|
to separate multiple database names.
|
23
47
|
|
24
|
-
|
48
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
|
25
49
|
|
26
|
-
|
50
|
+
## Dump
|
27
51
|
|
28
52
|
Dump the current database schema to the specified --sql-path directory, if none
|
29
53
|
specified it dumps to $PWD/sql
|
30
54
|
|
31
|
-
|
55
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
|
32
56
|
|
33
|
-
|
57
|
+
## Restore
|
34
58
|
|
35
59
|
Restore a previously psql-cm dumped database schema into a brand new postgresql
|
36
60
|
database cluster.
|
37
61
|
|
38
|
-
|
62
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" restore
|
63
|
+
|
64
|
+
## Command line parameters
|
65
|
+
|
66
|
+
--databases argument may take multiple database targets, to do this pass them
|
67
|
+
in ',' separated format, no spaces. Specifically the format is,
|
68
|
+
|
69
|
+
$ psql-cm --databases psqlcm_test,psqlcm_test2,... ...
|
70
|
+
|
71
|
+
--uri has the format,
|
72
|
+
|
73
|
+
$ psql-cm --uri "postgres://{user}:{password}@{host}:{port}/{database}?{sslmode}={mode}"
|
74
|
+
|
75
|
+
user, password, port, the ? and everything after it (the query) are all optional.
|
76
|
+
|
77
|
+
sslmode mode may be one of disable, allow, prefer, require if used.
|
78
|
+
|
79
|
+
# Walkthrough
|
80
|
+
|
81
|
+
First let's create a PostgreSQL database for us to work with,
|
82
|
+
|
83
|
+
$ createdb psqlcm_test
|
84
|
+
|
85
|
+
Next let's create two schemas in addition to the public schema (which is added
|
86
|
+
by default when the database is created) and a table for each schema for our
|
87
|
+
database.
|
88
|
+
|
89
|
+
$ psql psqlcm_test -c '
|
90
|
+
SET search_path = public;
|
91
|
+
CREATE SCHEMA schema_one;
|
92
|
+
CREATE TABLE a_bool(a BOOL);
|
93
|
+
|
94
|
+
SET search_path = schema_one;
|
95
|
+
CREATE TABLE an_integer(an INTEGER);
|
96
|
+
|
97
|
+
CREATE SCHEMA schema_two;
|
98
|
+
SET search_path = schema_two;
|
99
|
+
CREATE TABLE a_varchar(a VARCHAR);'
|
100
|
+
|
101
|
+
Now that we have a base set of database(s) and schemas that we wish to apply
|
102
|
+
change management process to we can setup the psql-cm control tables.
|
103
|
+
|
104
|
+
The setup action adds one table called 'pg\_psql\_cm' to each of the target
|
105
|
+
database schemas.
|
106
|
+
|
107
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
|
108
|
+
|
109
|
+
Use a PostgreSQL client tool (psql/pgAdmin/Navicat/...) and examine the schemas
|
110
|
+
for the psqlcm\_test database for which there should be three:
|
111
|
+
|
112
|
+
public
|
113
|
+
schema_one
|
114
|
+
schema_two
|
115
|
+
|
116
|
+
each with two tables, the pg\_psql\_cm control table and one other table.
|
117
|
+
|
118
|
+
Next we'll dump the schema to sql/ within our working directory
|
119
|
+
|
120
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
|
121
|
+
|
122
|
+
At this point we have the base schema for the psqlcm\_test database recorded and
|
123
|
+
we can test to see that this is true by droping the database and then running
|
124
|
+
the psql-cm restore action.
|
125
|
+
|
126
|
+
$ dropdb psqlcm_test
|
127
|
+
$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" restore
|
39
128
|
|
40
|
-
|
129
|
+
Once again use yoru favorite client tool and verify that the schema is inded
|
130
|
+
what it was after setup was run.
|
41
131
|
|
42
|
-
|
43
|
-
|
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
|
132
|
+
Note that one caveat is that psql-cm does not handle ROLEs and USERs so these
|
133
|
+
will have to be accounted for after doing a restore.
|
46
134
|
|
data/lib/psql-cm/base.rb
CHANGED
@@ -87,6 +87,10 @@ module PSQLCM
|
|
87
87
|
sh 'dump', command
|
88
88
|
end
|
89
89
|
end
|
90
|
+
|
91
|
+
sh 'git', "git init; git add ." unless File.exists?('.git') && File.directory?('.git')
|
92
|
+
|
93
|
+
sh 'git', "git commit -a -m 'PostgreSQL Change Management (psql-cm).\nDatabases: #{databases}\nTree: #{tree}'"
|
90
94
|
end
|
91
95
|
end # def dump!
|
92
96
|
|
@@ -148,6 +152,11 @@ module PSQLCM
|
|
148
152
|
end
|
149
153
|
end
|
150
154
|
end
|
155
|
+
end # def restore!
|
156
|
+
|
157
|
+
def change!
|
158
|
+
puts "TODO: allow change string and/or file to be specified and add to the
|
159
|
+
specified database scema control table"
|
151
160
|
end
|
152
161
|
|
153
162
|
def run!(action = config.action, parent_id = config.parent_id)
|
@@ -162,6 +171,8 @@ module PSQLCM
|
|
162
171
|
restore!
|
163
172
|
when "setup"
|
164
173
|
setup!
|
174
|
+
when "change"
|
175
|
+
change!
|
165
176
|
else
|
166
177
|
halt! "Action '#{action}' is not handled."
|
167
178
|
end
|
data/lib/psql-cm/database.rb
CHANGED
@@ -36,18 +36,20 @@ module PSQLCM
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def pgpass # Ensure a pgpass entry exists for this connection.
|
39
|
-
pgpass_line = [
|
40
|
-
|
41
|
-
@config[:password]
|
42
|
-
].join(':')
|
39
|
+
pgpass_line = [ @config[:host], @config[:port], @config[:dbname],
|
40
|
+
@config[:user], @config[:password] ].join(':')
|
43
41
|
|
44
|
-
content = File.open(File.join(ENV['HOME'], '.pgpass'), 'r')
|
45
|
-
|
42
|
+
content = File.open(File.join(ENV['HOME'], '.pgpass'), 'r') do |file|
|
43
|
+
file.read
|
44
|
+
end.split("\n")
|
45
|
+
|
46
|
+
unless content.detect{ |line| line == pgpass_line }
|
46
47
|
File.open(File.join(ENV['HOME'], '.pgpass'), 'w') do |file|
|
47
|
-
|
48
|
+
content << pgpass_line
|
49
|
+
file.write(content.join("\n") + "\n")
|
48
50
|
end
|
51
|
+
File.chmod(0600, File.join(ENV['HOME'], '.pgpass'))
|
49
52
|
end
|
50
|
-
|
51
53
|
pgpass_line
|
52
54
|
end # def pgpass
|
53
55
|
|
data/lib/psql-cm/version.rb
CHANGED