pg_migrate 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +34 -34
  3. data/.gitmodules +3 -3
  4. data/Gemfile +9 -9
  5. data/Gemfile.lock +39 -39
  6. data/LICENSE +21 -21
  7. data/README.md +33 -33
  8. data/Rakefile +2 -2
  9. data/bin/pg_migrate +6 -6
  10. data/lib/pg_migrate/builder.rb +214 -214
  11. data/lib/pg_migrate/command_line.rb +242 -242
  12. data/lib/pg_migrate/config_parser.rb +48 -48
  13. data/lib/pg_migrate/manifest_reader.rb +102 -102
  14. data/lib/pg_migrate/migration.rb +11 -11
  15. data/lib/pg_migrate/migrator.rb +94 -94
  16. data/lib/pg_migrate/package.rb +152 -152
  17. data/lib/pg_migrate/package_templates/Gemfile.erb +3 -3
  18. data/lib/pg_migrate/package_templates/bin/migrate.rb +9 -9
  19. data/lib/pg_migrate/package_templates/gemspec.erb +21 -21
  20. data/lib/pg_migrate/package_templates/lib/gem/version.rb +3 -3
  21. data/lib/pg_migrate/package_templates/lib/gem.rb +12 -12
  22. data/lib/pg_migrate/props.rb +19 -19
  23. data/lib/pg_migrate/sql_reader.rb +51 -51
  24. data/lib/pg_migrate/templates/bootstrap.erb +175 -175
  25. data/lib/pg_migrate/templates/up.erb +30 -30
  26. data/lib/pg_migrate/util.rb +73 -73
  27. data/lib/pg_migrate/version.rb +3 -3
  28. data/lib/pg_migrate.rb +40 -40
  29. data/pg_migrate.gemspec +29 -29
  30. data/spec/database.yml +8 -8
  31. data/spec/pg_migrate/builder_spec.rb +113 -113
  32. data/spec/pg_migrate/command_line_spec.rb +53 -53
  33. data/spec/pg_migrate/config_parser_spec.rb +18 -18
  34. data/spec/pg_migrate/db_utility.rb +73 -73
  35. data/spec/pg_migrate/input_manifests/single_manifest/manifest +3 -3
  36. data/spec/pg_migrate/input_manifests/single_manifest/up/single1.sql +29 -29
  37. data/spec/pg_migrate/manifest_reader_spec.rb +19 -19
  38. data/spec/pg_migrate/migrator_spec.rb +68 -68
  39. data/spec/pg_migrate/package_spec.rb +38 -38
  40. data/spec/pg_migrate/sql_reader_spec.rb +21 -21
  41. data/spec/spec_helper.rb +15 -15
  42. metadata +5 -5
@@ -1,242 +1,242 @@
1
- module PgMigrate
2
- class CommandLine < Thor
3
-
4
- @@packaged_source = '.'
5
-
6
- def initialize(*args)
7
- super
8
- end
9
-
10
- def self.packaged_source
11
- @@packaged_source
12
- end
13
-
14
- def self.packaged_source=(value)
15
- @@packaged_source = value
16
- end
17
-
18
-
19
- desc "up", "migrates the database forwards, applying migrations found in the source directory"
20
- method_option :source, :aliases => "-s", :default => nil, :banner => 'input directory', :desc => "a pg_migrate built manifest. Should contain your processed manifest and up|down|test folders"
21
- method_option :connopts, :aliases => "-c", :type => :hash, :banner => "connection options", :desc => "database connection options used by gem 'pg': dbname|host|hostaddr|port|user|password|connection_timeout|options|sslmode|krbsrvname|gsslib|service"
22
- method_option :verbose, :aliases => "-v", :type => :boolean, :banner => "verbose", :desc=> "set to raise verbosity"
23
-
24
- def up
25
- source = options[:source]
26
-
27
- if source.nil?
28
- source = @@packaged_source
29
- end
30
-
31
- method_defaults = {"verbose" => false}
32
- local_options = set_defaults_from_file(method_defaults, "up", source)
33
- local_options = local_options.merge(options)
34
-
35
- bootstrap_logger(local_options["verbose"])
36
-
37
- manifest_reader = ManifestReader.new
38
- sql_reader = SqlReader.new
39
-
40
- connopts = local_options["connopts"]
41
- connopts ||= {}
42
-
43
- if !connopts["port"].nil?
44
- connopts["port"] = connopts["port"].to_i
45
- end
46
-
47
- migrator = Migrator.new(manifest_reader, sql_reader, connopts)
48
-
49
- begin
50
- migrator.migrate(source)
51
- rescue Exception => e
52
- if !local_options["verbose"]
53
- # catch common exceptions and make pretty on command-line
54
- if !e.message.index("ManifestReader: code=unloadable_manifest").nil?
55
- puts "Unable to load manifest in source directory '#{source}' . Check -s|--source option and run again."
56
- exit 1
57
- else
58
- raise e
59
- end
60
- else
61
- raise e
62
- end
63
- end
64
-
65
-
66
- end
67
-
68
- desc "down", "not implemented"
69
-
70
- def down
71
- local_options = options
72
- options = set_defaults_from_file(location_options)
73
- bootstrap_logger(options[:verbose])
74
-
75
- raise 'Not implemented'
76
- end
77
-
78
- desc "build", "processes a pg_migrate source directory and places the result in the specified output directory"
79
- method_option :source, :aliases => "-s", :default => nil, :banner => 'input directory', :desc => "the input directory containing a manifest file and up|down|test folders"
80
- method_option :out, :aliases => "-o", :banner => "output directory", :desc => "where the processed migrations will be placed"
81
- method_option :force, :aliases => "-f", :type => :boolean, :banner => "overwrite out", :desc => "if specified, the out directory will be created before processing occurs, replacing any existing directory"
82
- method_option :verbose, :aliases => "-v", :type => :boolean, :banner => "verbose", :desc=> "set to raise verbosity"
83
- method_option :test, :aliases => "-t", :default => false, :type => :boolean, :banner => "run tests", :desc => "run tests by creating a test database and executing migrations"
84
- method_option :oob_connopts, :aliases => "-b",:default=> nil, :type => :hash, :banner => "out-of-band connection options", :desc => "this is a 'landing pad' database from which pg_migrate can execute 'create/drop' against the database specified by the connopts argument. database connection options used by gem 'pg': dbname|host|hostaddr|port|user|password|connection_timeout|options|sslmode|krbsrvname|gsslib|service"
85
- method_option :connopts, :aliases => "-c", :default => nil, :type => :hash, :banner => "connection options", :desc => "database connection options used by gem 'pg': dbname|host|hostaddr|port|user|password|connection_timeout|options|sslmode|krbsrvname|gsslib|service"
86
-
87
- def build
88
- source = options[:source]
89
-
90
- if source.nil?
91
- source = @@packaged_source
92
- end
93
-
94
- method_defaults = {"force" => false, "verbose" => false, "test" => false}
95
- local_options = set_defaults_from_file(method_defaults, "build", source)
96
- local_options = local_options.merge(options)
97
-
98
- bootstrap_logger(local_options["verbose"])
99
-
100
- if !local_options["out"]
101
- puts "error: --out not specified"
102
- exit 1
103
- end
104
-
105
- if local_options["test"].to_s == "true"
106
- if !local_options["oob_connopts"]
107
- puts "error: --oob_connopts not specified when test = true"
108
- exit 1
109
- else
110
- # type safety; if string is found, convert to hash
111
- local_options["oob_connopts"] = parse_to_hash(local_options["oob_connopts"])
112
- end
113
-
114
-
115
- if !local_options["connopts"]
116
- puts "error: --connopts not specified when test = true"
117
- exit 1
118
- else
119
- # type safety; if string is found, convert to hash
120
- local_options["connopts"] = parse_to_hash(local_options["connopts"])
121
- end
122
-
123
- end
124
-
125
- manifest_reader = ManifestReader.new
126
- sql_reader = SqlReader.new
127
- builder = Builder.new(manifest_reader, sql_reader)
128
-
129
- begin
130
- builder.build(source, local_options["out"],
131
- :force => local_options["force"],
132
- :test => local_options["test"],
133
- :oob_connopts => local_options["oob_connopts"],
134
- :connopts => local_options["connopts"])
135
- rescue PG::Error => pge
136
- puts "test failure"
137
- puts pge
138
- exit 1
139
- end
140
-
141
- end
142
-
143
-
144
- desc "package", "packages a built pg_migrate project into a custom gem containing schemas and simpler migration interface"
145
- method_option :source, :aliases => "-s", :default => nil, :banner => 'input directory', :desc => "the input directory containing a manifest file and up|down|test folders that has been previously built by pg_migrate build"
146
- method_option :out, :aliases => "-o", :banner => "output directory", :desc => "where the gem will be placed (as well as the exploded gem's contents)"
147
- method_option :name, :aliases => "-n", :banner => "the name of the schema gem", :desc => "the name of the gem"
148
- method_option :version, :aliases => "-e", :banner => "the version of the schema gem", :desc => "the version of the gem"
149
- method_option :force, :aliases => "-f", :type => :boolean, :banner => "overwrite out", :desc => "if specified, the out directory will be created before processing occurs, replacing any existing directory"
150
- method_option :verbose, :aliases => "-v", :type => :boolean, :banner => "verbose", :desc=> "set to raise verbosity"
151
-
152
- def package
153
- source = options[:source]
154
-
155
- if source.nil?
156
- source = @@packaged_source
157
- end
158
-
159
- method_defaults = {"force" => false, "verbose" => false}
160
- local_options = set_defaults_from_file(method_defaults, "package", source)
161
- local_options = local_options.merge(options)
162
-
163
- if !local_options["out"]
164
- puts "error: --out not specified"
165
- exit 1
166
- end
167
- if !local_options["name"]
168
- puts "error: --version not specified"
169
- exit 1
170
- end
171
- if !local_options["version"]
172
- puts "error: --version not specified"
173
- exit 1
174
- end
175
-
176
- bootstrap_logger(local_options["verbose"])
177
-
178
- manifest_reader = ManifestReader.new
179
- builder = Package.new(manifest_reader)
180
- builder.package(source, local_options["out"], local_options["name"], local_options["version"], :force => local_options["force"])
181
- end
182
-
183
- no_tasks do
184
- def bootstrap_logger(verbose)
185
- # bootstrap logger
186
- if verbose
187
- Logging.logger.root.level = :debug
188
- else
189
- Logging.logger.root.level = :info
190
- end
191
-
192
- Logging.logger.root.appenders = Logging.appenders.stdout
193
- end
194
-
195
-
196
- def set_defaults_from_file(default_options, context, source)
197
- @file_defaults = @file_defaults ||= load_file(context, source)
198
- merged = default_options.merge(@file_defaults)
199
- end
200
-
201
- def load_file(context, source)
202
-
203
- defaults = nil
204
- config = File.join(source, PG_CONFIG)
205
- if FileTest::exist? (config)
206
- puts "found #{PG_CONFIG}"
207
- defaults = Properties.new(config)
208
- else
209
- defaults = Properties.new
210
- end
211
-
212
- map = {}
213
- defaults.each_pair do |k, v|
214
- map[k.upcase] = v
215
-
216
- # make a context-removed version of a key, if it starts with 'context.'
217
- prefix = "#{context}."
218
- if k.start_with? prefix
219
- map[k[prefix.length..-1]] = v
220
- end
221
- end
222
-
223
- return map
224
-
225
- end
226
-
227
- def parse_to_hash(value)
228
- hash = {}
229
-
230
- bits = value.split()
231
- bits.each do |bit|
232
- key, value = bit.split(':',2)
233
- hash[key] = value
234
- end
235
-
236
- return hash
237
- end
238
- end
239
-
240
-
241
- end
242
- end
1
+ module PgMigrate
2
+ class CommandLine < Thor
3
+
4
+ @@packaged_source = '.'
5
+
6
+ def initialize(*args)
7
+ super
8
+ end
9
+
10
+ def self.packaged_source
11
+ @@packaged_source
12
+ end
13
+
14
+ def self.packaged_source=(value)
15
+ @@packaged_source = value
16
+ end
17
+
18
+
19
+ desc "up", "migrates the database forwards, applying migrations found in the source directory"
20
+ method_option :source, :aliases => "-s", :default => nil, :banner => 'input directory', :desc => "a pg_migrate built manifest. Should contain your processed manifest and up|down|test folders"
21
+ method_option :connopts, :aliases => "-c", :type => :hash, :banner => "connection options", :desc => "database connection options used by gem 'pg': dbname|host|hostaddr|port|user|password|connection_timeout|options|sslmode|krbsrvname|gsslib|service"
22
+ method_option :verbose, :aliases => "-v", :type => :boolean, :banner => "verbose", :desc=> "set to raise verbosity"
23
+
24
+ def up
25
+ source = options[:source]
26
+
27
+ if source.nil?
28
+ source = @@packaged_source
29
+ end
30
+
31
+ method_defaults = {"verbose" => false}
32
+ local_options = set_defaults_from_file(method_defaults, "up", source)
33
+ local_options = local_options.merge(options)
34
+
35
+ bootstrap_logger(local_options["verbose"])
36
+
37
+ manifest_reader = ManifestReader.new
38
+ sql_reader = SqlReader.new
39
+
40
+ connopts = local_options["connopts"]
41
+ connopts ||= {}
42
+
43
+ if !connopts["port"].nil?
44
+ connopts["port"] = connopts["port"].to_i
45
+ end
46
+
47
+ migrator = Migrator.new(manifest_reader, sql_reader, connopts)
48
+
49
+ begin
50
+ migrator.migrate(source)
51
+ rescue Exception => e
52
+ if !local_options["verbose"]
53
+ # catch common exceptions and make pretty on command-line
54
+ if !e.message.index("ManifestReader: code=unloadable_manifest").nil?
55
+ puts "Unable to load manifest in source directory '#{source}' . Check -s|--source option and run again."
56
+ exit 1
57
+ else
58
+ raise e
59
+ end
60
+ else
61
+ raise e
62
+ end
63
+ end
64
+
65
+
66
+ end
67
+
68
+ desc "down", "not implemented"
69
+
70
+ def down
71
+ local_options = options
72
+ options = set_defaults_from_file(location_options)
73
+ bootstrap_logger(options[:verbose])
74
+
75
+ raise 'Not implemented'
76
+ end
77
+
78
+ desc "build", "processes a pg_migrate source directory and places the result in the specified output directory"
79
+ method_option :source, :aliases => "-s", :default => nil, :banner => 'input directory', :desc => "the input directory containing a manifest file and up|down|test folders"
80
+ method_option :out, :aliases => "-o", :banner => "output directory", :desc => "where the processed migrations will be placed"
81
+ method_option :force, :aliases => "-f", :type => :boolean, :banner => "overwrite out", :desc => "if specified, the out directory will be created before processing occurs, replacing any existing directory"
82
+ method_option :verbose, :aliases => "-v", :type => :boolean, :banner => "verbose", :desc=> "set to raise verbosity"
83
+ method_option :test, :aliases => "-t", :default => false, :type => :boolean, :banner => "run tests", :desc => "run tests by creating a test database and executing migrations"
84
+ method_option :oob_connopts, :aliases => "-b",:default=> nil, :type => :hash, :banner => "out-of-band connection options", :desc => "this is a 'landing pad' database from which pg_migrate can execute 'create/drop' against the database specified by the connopts argument. database connection options used by gem 'pg': dbname|host|hostaddr|port|user|password|connection_timeout|options|sslmode|krbsrvname|gsslib|service"
85
+ method_option :connopts, :aliases => "-c", :default => nil, :type => :hash, :banner => "connection options", :desc => "database connection options used by gem 'pg': dbname|host|hostaddr|port|user|password|connection_timeout|options|sslmode|krbsrvname|gsslib|service"
86
+
87
+ def build
88
+ source = options[:source]
89
+
90
+ if source.nil?
91
+ source = @@packaged_source
92
+ end
93
+
94
+ method_defaults = {"force" => false, "verbose" => false, "test" => false}
95
+ local_options = set_defaults_from_file(method_defaults, "build", source)
96
+ local_options = local_options.merge(options)
97
+
98
+ bootstrap_logger(local_options["verbose"])
99
+
100
+ if !local_options["out"]
101
+ puts "error: --out not specified"
102
+ exit 1
103
+ end
104
+
105
+ if local_options["test"].to_s == "true"
106
+ if !local_options["oob_connopts"]
107
+ puts "error: --oob_connopts not specified when test = true"
108
+ exit 1
109
+ else
110
+ # type safety; if string is found, convert to hash
111
+ local_options["oob_connopts"] = parse_to_hash(local_options["oob_connopts"])
112
+ end
113
+
114
+
115
+ if !local_options["connopts"]
116
+ puts "error: --connopts not specified when test = true"
117
+ exit 1
118
+ else
119
+ # type safety; if string is found, convert to hash
120
+ local_options["connopts"] = parse_to_hash(local_options["connopts"])
121
+ end
122
+
123
+ end
124
+
125
+ manifest_reader = ManifestReader.new
126
+ sql_reader = SqlReader.new
127
+ builder = Builder.new(manifest_reader, sql_reader)
128
+
129
+ begin
130
+ builder.build(source, local_options["out"],
131
+ :force => local_options["force"],
132
+ :test => local_options["test"],
133
+ :oob_connopts => local_options["oob_connopts"],
134
+ :connopts => local_options["connopts"])
135
+ rescue PG::Error => pge
136
+ puts "test failure"
137
+ puts pge
138
+ exit 1
139
+ end
140
+
141
+ end
142
+
143
+
144
+ desc "package", "packages a built pg_migrate project into a custom gem containing schemas and simpler migration interface"
145
+ method_option :source, :aliases => "-s", :default => nil, :banner => 'input directory', :desc => "the input directory containing a manifest file and up|down|test folders that has been previously built by pg_migrate build"
146
+ method_option :out, :aliases => "-o", :banner => "output directory", :desc => "where the gem will be placed (as well as the exploded gem's contents)"
147
+ method_option :name, :aliases => "-n", :banner => "the name of the schema gem", :desc => "the name of the gem"
148
+ method_option :version, :aliases => "-e", :banner => "the version of the schema gem", :desc => "the version of the gem"
149
+ method_option :force, :aliases => "-f", :type => :boolean, :banner => "overwrite out", :desc => "if specified, the out directory will be created before processing occurs, replacing any existing directory"
150
+ method_option :verbose, :aliases => "-v", :type => :boolean, :banner => "verbose", :desc=> "set to raise verbosity"
151
+
152
+ def package
153
+ source = options[:source]
154
+
155
+ if source.nil?
156
+ source = @@packaged_source
157
+ end
158
+
159
+ method_defaults = {"force" => false, "verbose" => false}
160
+ local_options = set_defaults_from_file(method_defaults, "package", source)
161
+ local_options = local_options.merge(options)
162
+
163
+ if !local_options["out"]
164
+ puts "error: --out not specified"
165
+ exit 1
166
+ end
167
+ if !local_options["name"]
168
+ puts "error: --version not specified"
169
+ exit 1
170
+ end
171
+ if !local_options["version"]
172
+ puts "error: --version not specified"
173
+ exit 1
174
+ end
175
+
176
+ bootstrap_logger(local_options["verbose"])
177
+
178
+ manifest_reader = ManifestReader.new
179
+ builder = Package.new(manifest_reader)
180
+ builder.package(source, local_options["out"], local_options["name"], local_options["version"], :force => local_options["force"])
181
+ end
182
+
183
+ no_tasks do
184
+ def bootstrap_logger(verbose)
185
+ # bootstrap logger
186
+ if verbose
187
+ Logging.logger.root.level = :debug
188
+ else
189
+ Logging.logger.root.level = :info
190
+ end
191
+
192
+ Logging.logger.root.appenders = Logging.appenders.stdout
193
+ end
194
+
195
+
196
+ def set_defaults_from_file(default_options, context, source)
197
+ @file_defaults = @file_defaults ||= load_file(context, source)
198
+ merged = default_options.merge(@file_defaults)
199
+ end
200
+
201
+ def load_file(context, source)
202
+
203
+ defaults = nil
204
+ config = File.join(source, PG_CONFIG)
205
+ if FileTest::exist? (config)
206
+ puts "found #{PG_CONFIG}"
207
+ defaults = Properties.new(config)
208
+ else
209
+ defaults = Properties.new
210
+ end
211
+
212
+ map = {}
213
+ defaults.each_pair do |k, v|
214
+ map[k.upcase] = v
215
+
216
+ # make a context-removed version of a key, if it starts with 'context.'
217
+ prefix = "#{context}."
218
+ if k.start_with? prefix
219
+ map[k[prefix.length..-1]] = v
220
+ end
221
+ end
222
+
223
+ return map
224
+
225
+ end
226
+
227
+ def parse_to_hash(value)
228
+ hash = {}
229
+
230
+ bits = value.split()
231
+ bits.each do |bit|
232
+ key, value = bit.split(':',2)
233
+ hash[key] = value
234
+ end
235
+
236
+ return hash
237
+ end
238
+ end
239
+
240
+
241
+ end
242
+ end
@@ -1,49 +1,49 @@
1
- require 'yaml'
2
-
3
- module PgMigrate
4
-
5
- class ConfigParser
6
-
7
- def self.rails(path, environment)
8
-
9
- config = {}
10
-
11
- rails_config = YAML.load_file(path)
12
-
13
- if !rails_config.has_key?(environment)
14
- raise "no environment #{environment} found in rails config file: #{path}"
15
- end
16
-
17
- rails_config = rails_config[environment]
18
-
19
- # populate from rails YAML to PG
20
-
21
- # required parameters 1st
22
- if !rails_config.has_key?("database")
23
- raise "no database key found in #{path} with environment #{environment}"
24
- end
25
-
26
- config[:dbname] = rails_config["database"]
27
-
28
- if rails_config.has_key?("host")
29
- config[:host] = rails_config["host"]
30
- end
31
-
32
- if rails_config.has_key?("port")
33
- config[:port] = rails_config["port"]
34
- end
35
-
36
- if rails_config.has_key?("username")
37
- config[:user] = rails_config["username"]
38
- end
39
-
40
- if rails_config.has_key?("password")
41
- config[:password] = rails_config["password"]
42
- end
43
-
44
- return config
45
-
46
- end
47
-
48
- end
1
+ require 'yaml'
2
+
3
+ module PgMigrate
4
+
5
+ class ConfigParser
6
+
7
+ def self.rails(path, environment)
8
+
9
+ config = {}
10
+
11
+ rails_config = YAML.load_file(path)
12
+
13
+ if !rails_config.has_key?(environment)
14
+ raise "no environment #{environment} found in rails config file: #{path}"
15
+ end
16
+
17
+ rails_config = rails_config[environment]
18
+
19
+ # populate from rails YAML to PG
20
+
21
+ # required parameters 1st
22
+ if !rails_config.has_key?("database")
23
+ raise "no database key found in #{path} with environment #{environment}"
24
+ end
25
+
26
+ config[:dbname] = rails_config["database"]
27
+
28
+ if rails_config.has_key?("host")
29
+ config[:host] = rails_config["host"]
30
+ end
31
+
32
+ if rails_config.has_key?("port")
33
+ config[:port] = rails_config["port"]
34
+ end
35
+
36
+ if rails_config.has_key?("username")
37
+ config[:user] = rails_config["username"]
38
+ end
39
+
40
+ if rails_config.has_key?("password")
41
+ config[:password] = rails_config["password"]
42
+ end
43
+
44
+ return config
45
+
46
+ end
47
+
48
+ end
49
49
  end