Permalink
Browse files

setup action now works. Version 0.0.3.

  • Loading branch information...
1 parent 98a32d5 commit 6f8691c4b4e713e2a71eb0e9c3d3ba7ab1f95ee5 @wayneeseguin committed Apr 18, 2012
Showing with 91 additions and 17 deletions.
  1. +4 −0 History.md
  2. +24 −10 README.md
  3. +61 −5 lib/psql-cm/base.rb
  4. +1 −1 lib/psql-cm/version.rb
  5. +1 −1 psql-cm.gemspec
View
@@ -1,3 +1,7 @@
+# 0.0.3 - 2012-04-18
+
+'setup' action functional.
+
# 0.0.2 - 2012-04-18
Initial 'dump' feature.
View
@@ -3,30 +3,44 @@
This project is a tool for schema change management within a PostgreSQL database
cluster.
-# Setup (Currently being implemented)
+# Prerequisites
-Setup the psql\_cm control tables on the target databases,
+- [Ruby >= 1.9.3](http://www.ruby-lang.org/en/)
+- [Postgresql >= 9.1+](http://www.postgresql.org/)
+- [Git SCM](http://git-scm.com/)
- $ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
+# Installation of psql-cm
+
+Once the prerequisites have been satisfied on your system, using the
+'gem' command from Ruby 1.9.3 do:
+
+ user$ gem install psql-scm
+
+# Setup
+
+Setup the psql\_cm control tables on the target databases, use a comma (',')
+to separate multiple database names.
+
+ user$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
# Dump
Dump the current database schema to the specified --sql-path directory, if none
specified it dumps to $PWD/sql
- $ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
+ user$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
-# Restore (Not Implemented Yet)
+# Restore (Currently being implemented)
Restore a previously psql-cm dumped database schema into a brand new postgresql
database cluster.
- $ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" restore
+ user$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" restore
# Example
- $ createdb psqlcm_test
- $ psql psqlcm_test -c 'CREATE SCHEMA schema_one; CREATE SCHEMA schema_two'
- $ echo psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
- $ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
+ user$ createdb psqlcm_test
+ user$ psql psqlcm_test -c 'CREATE SCHEMA schema_one; CREATE SCHEMA schema_two'
+ user$ echo psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" setup
+ user$ psql-cm --databases psqlcm_test --uri "postgres://127.0.0.1:5432" dump
View
@@ -84,21 +84,70 @@ def dump!
command += "--file=#{base_file} --exclude-table=psql_cm "
end
command += "#{database}"
- debug "dump> #{command}"
-
- %x[#{command}]
+ sh 'dump', command
end
end
end
end # def dump!
def setup!
# Create psql_cm tables for each schema on the target db.
+ debug "setup> Setting up pg_psql_cm table in each target schema."
+ tree.each_pair do |database, schema_hash|
+ schema_hash.keys.each do |schema|
+ debug "setup:#{database}> #{schema}"
+ db(database).exec <<-SQL
+ SET search_path = #{schema}, public;
+ CREATE TABLE IF NOT EXISTS pg_psql_cm
+ (
+ id bigserial NOT NULL PRIMARY KEY ,
+ is_base boolean NOT NULL,
+ created_at timestamp with time zone DEFAULT now(),
+ implementer text NOT NULL,
+ content text NOT NULL
+ );
+ SQL
+ end
+ end
end
- def load!
- # TODO: Load psql-cm filesystem path files {base,cm}.sql into database
+ def restore!
+ # TODO: Restore psql-cm filesystem path files {base,cm}.sql into database
# structure.
+ unless config.sql_path
+ $stdout.puts "Warning: --sql-path was not set, defaulting to $PWD/sql."
+ config.sql_path = "#{ENV["PWD"]}/sql"
+ end
+
+ debug "restore> sql_path: #{config.sql_path}"
+ FileUtils.mkdir(config.sql_path) unless File.directory?(config.sql_path)
+ Dir.chdir(config.sql_path) do
+ tree.each_pair do |database, schema_hash|
+ debug "restore> database: #{database}"
+
+ File.directory?(File.join(config.sql_path,database)) or
+ FileUtils.mkdir(File.join(config.sql_path,database))
+
+ schema_hash.each_pair do |schema, files|
+ debug "restore> schema: #{schema}"
+ File.directory?(File.join(config.sql_path,database,schema)) or
+ FileUtils.mkdir(File.join(config.sql_path,database,schema))
+
+ base_file = File.join(config.sql_path,database,schema,'base.sql')
+ cm_file = File.join(config.sql_path,database,schema,'cm.sql')
+
+ FileUtils.touch(base_file)
+ FileUtils.touch(cm_file)
+
+ command = "psql #{database} < #{base_file}"
+ sh 'restore', command
+
+ next if File.size(cm_file) == 0
+ command = "psql #{database} < #{cm_file}"
+ sh 'restore', command
+ end
+ end
+ end
end
def run!(action = config.action, parent_id = config.parent_id)
@@ -118,6 +167,13 @@ def run!(action = config.action, parent_id = config.parent_id)
end
end
+ private
+
+ def sh(tag, command)
+ debug "sh:#{tag}> #{command}"
+ %x[#{command}]
+ end
+
end # class << self
end
View
@@ -1,4 +1,4 @@
module PSQLCM
- Version = '0.0.2'
+ Version = '0.0.3'
end
View
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
spec.description = 'PostgreSQL Change Management Tool'
spec.required_ruby_version = '~> 1.9.3'
- spec.required_rubygems_version = '>= 1.8.22'
+ spec.required_rubygems_version = '>= 1.3.6'
spec.add_dependency 'pg', '>= 0.13.2'
spec.require_path = 'lib'

0 comments on commit 6f8691c

Please sign in to comment.