pg_migrate 0.1.7 → 0.1.11
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.
- checksums.yaml +4 -4
- data/.gitignore +34 -34
- data/.gitmodules +3 -3
- data/Gemfile +9 -9
- data/Gemfile.lock +39 -39
- data/LICENSE +21 -21
- data/README.md +33 -33
- data/Rakefile +2 -2
- data/bin/pg_migrate +6 -6
- data/lib/pg_migrate/builder.rb +214 -214
- data/lib/pg_migrate/command_line.rb +242 -242
- data/lib/pg_migrate/config_parser.rb +48 -48
- data/lib/pg_migrate/manifest_reader.rb +102 -102
- data/lib/pg_migrate/migration.rb +11 -11
- data/lib/pg_migrate/migrator.rb +94 -94
- data/lib/pg_migrate/package.rb +152 -152
- data/lib/pg_migrate/package_templates/Gemfile.erb +3 -3
- data/lib/pg_migrate/package_templates/bin/migrate.rb +9 -9
- data/lib/pg_migrate/package_templates/gemspec.erb +21 -21
- data/lib/pg_migrate/package_templates/lib/gem/version.rb +3 -3
- data/lib/pg_migrate/package_templates/lib/gem.rb +12 -12
- data/lib/pg_migrate/props.rb +19 -19
- data/lib/pg_migrate/sql_reader.rb +51 -51
- data/lib/pg_migrate/templates/bootstrap.erb +175 -175
- data/lib/pg_migrate/templates/up.erb +30 -30
- data/lib/pg_migrate/util.rb +73 -73
- data/lib/pg_migrate/version.rb +3 -3
- data/lib/pg_migrate.rb +40 -40
- data/pg_migrate.gemspec +29 -28
- data/spec/database.yml +8 -8
- data/spec/pg_migrate/builder_spec.rb +113 -113
- data/spec/pg_migrate/command_line_spec.rb +53 -53
- data/spec/pg_migrate/config_parser_spec.rb +18 -18
- data/spec/pg_migrate/db_utility.rb +73 -73
- data/spec/pg_migrate/input_manifests/single_manifest/manifest +3 -3
- data/spec/pg_migrate/input_manifests/single_manifest/up/single1.sql +29 -29
- data/spec/pg_migrate/manifest_reader_spec.rb +19 -19
- data/spec/pg_migrate/migrator_spec.rb +68 -68
- data/spec/pg_migrate/package_spec.rb +38 -38
- data/spec/pg_migrate/sql_reader_spec.rb +21 -21
- data/spec/spec_helper.rb +15 -15
- metadata +5 -5
data/lib/pg_migrate/package.rb
CHANGED
@@ -1,152 +1,152 @@
|
|
1
|
-
require 'erb'
|
2
|
-
require 'fileutils'
|
3
|
-
require 'rubygems'
|
4
|
-
|
5
|
-
begin
|
6
|
-
# this occurs in rubygems < 2.0.0
|
7
|
-
require 'rubygems/builder'
|
8
|
-
rescue LoadError
|
9
|
-
# this occurs in rubygems > 2.0.0
|
10
|
-
require 'rubygems/package'
|
11
|
-
end
|
12
|
-
|
13
|
-
module PgMigrate
|
14
|
-
class Package
|
15
|
-
|
16
|
-
attr_accessor :manifest_reader
|
17
|
-
|
18
|
-
def initialize(manifest_reader)
|
19
|
-
@log = Logging.logger[self]
|
20
|
-
@manifest_reader = manifest_reader
|
21
|
-
@template_dir = File.join(File.dirname(__FILE__), 'package_templates')
|
22
|
-
end
|
23
|
-
|
24
|
-
def package(built_migration_path, output_dir, name, version, options={:force=>true})
|
25
|
-
gemspec = create_gem(built_migration_path, output_dir, name, version, options[:force])
|
26
|
-
build_gem(gemspec, output_dir)
|
27
|
-
end
|
28
|
-
|
29
|
-
def create_gem (built_migration_path, output_dir, name, version, force)
|
30
|
-
# validate that manifest is valid
|
31
|
-
@log.debug "validating output dir is manifest"
|
32
|
-
|
33
|
-
if !FileTest::exist?(built_migration_path)
|
34
|
-
raise "built manifest path does not exist #{built_migration_path}"
|
35
|
-
end
|
36
|
-
|
37
|
-
if built_migration_path == output_dir
|
38
|
-
raise "source and destination can not be the same path"
|
39
|
-
end
|
40
|
-
|
41
|
-
loaded_manifest = @manifest_reader.load_input_manifest(built_migration_path)
|
42
|
-
@manifest_reader.validate_migration_paths(built_migration_path, loaded_manifest)
|
43
|
-
|
44
|
-
@log.debug "preparing to build gem"
|
45
|
-
|
46
|
-
target = File.join(output_dir, name)
|
47
|
-
|
48
|
-
# stolen almost verbatim from bundler: https://github.com/carlhuda/bundler/blob/master/lib/bundler/cli.rb
|
49
|
-
constant_name = name.split('_').map { |p| p[0..0].upcase + p[1..-1] }.join
|
50
|
-
constant_name = constant_name.split('-').map { |q| q[0..0].upcase + q[1..-1] }.join('::') if constant_name =~ /-/
|
51
|
-
constant_array = constant_name.split('::')
|
52
|
-
# end stolen
|
53
|
-
|
54
|
-
author = "pgmigrate"
|
55
|
-
email = "pgmigrate@pgmigrate.io"
|
56
|
-
pg_migrate_version = PgMigrate::VERSION
|
57
|
-
gemfiles = ["Gemfile", "#{name}.gemspec", "lib/#{name}.rb", "lib/#{name}/version.rb", "bin/#{name}"]
|
58
|
-
gemfiles += userfiles(built_migration_path, name)
|
59
|
-
gemspec_path = File.join(output_dir, "#{name}.gemspec")
|
60
|
-
|
61
|
-
@log.debug "building gem"
|
62
|
-
|
63
|
-
output = Pathname.new(output_dir)
|
64
|
-
if !output.exist?
|
65
|
-
if !force
|
66
|
-
raise "Output directory '#{output_dir}' does not exist. Create it or specify force=true"
|
67
|
-
else
|
68
|
-
output.mkpath
|
69
|
-
end
|
70
|
-
else
|
71
|
-
# verify that it's is a directory
|
72
|
-
if !output.directory?
|
73
|
-
raise "output_dir #{output_dir} is a file; not a directory."
|
74
|
-
else
|
75
|
-
@log.debug("deleting & recreating existing output_dir #{output_dir}")
|
76
|
-
output.rmtree
|
77
|
-
output.mkpath
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
FileUtils.mkdir_p(output_dir)
|
82
|
-
FileUtils.mkdir_p(File.join(output_dir, "bin"))
|
83
|
-
FileUtils.mkdir_p(File.join(output_dir, "lib", name))
|
84
|
-
run_template("Gemfile.erb", binding, File.join(output_dir, "Gemfile"))
|
85
|
-
run_template("gemspec.erb", binding, gemspec_path)
|
86
|
-
run_template("lib/gem.rb", binding, File.join(output_dir, "lib", "#{name}.rb"))
|
87
|
-
run_template("lib/gem/version.rb", binding, File.join(output_dir, "lib", name, "version.rb"))
|
88
|
-
run_template("bin/migrate.rb", binding, File.join(output_dir, "bin", "#{name}"))
|
89
|
-
copy_schema(built_migration_path, File.join(output_dir, "lib", name, "schemas"))
|
90
|
-
|
91
|
-
return gemspec_path
|
92
|
-
end
|
93
|
-
|
94
|
-
def copy_schema(built_migration_path, output_dir)
|
95
|
-
FileUtils.cp_r(File.join(built_migration_path, '.'), output_dir)
|
96
|
-
end
|
97
|
-
|
98
|
-
def build_gem(gemspec_path, output_dir)
|
99
|
-
@log.debug "building gem"
|
100
|
-
|
101
|
-
@log.debug "loading gem specification #{gemspec_path}"
|
102
|
-
spec = Gem::Specification.load(gemspec_path)
|
103
|
-
|
104
|
-
if spec.nil?
|
105
|
-
raise 'unable to build gem from specification'
|
106
|
-
end
|
107
|
-
|
108
|
-
@log.debug "packaging gem"
|
109
|
-
Dir.chdir(output_dir) do
|
110
|
-
if defined?(Gem::Builder)
|
111
|
-
Gem::Builder.new(spec).build
|
112
|
-
else
|
113
|
-
Gem::Package.build(spec)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
#Gem::Package.build spec, false
|
117
|
-
end
|
118
|
-
|
119
|
-
def userfiles(built_migration_path, name)
|
120
|
-
|
121
|
-
gempaths = []
|
122
|
-
Find.find(built_migration_path) do |path|
|
123
|
-
if path == ".."
|
124
|
-
Find.prune
|
125
|
-
else
|
126
|
-
# make relative
|
127
|
-
|
128
|
-
relative = path[built_migration_path.length..-1]
|
129
|
-
gempath = File.join("lib", name, "schemas", relative)
|
130
|
-
gempaths.push(gempath)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
return gempaths
|
135
|
-
end
|
136
|
-
|
137
|
-
# given an input template and binding, writes to an output file
|
138
|
-
def run_template(template, opt, output_filepath)
|
139
|
-
bootstrap_template = nil
|
140
|
-
File.open(File.join(@template_dir, template), 'r') do |reader|
|
141
|
-
bootstrap_template = reader.read
|
142
|
-
end
|
143
|
-
|
144
|
-
|
145
|
-
template = ERB.new(bootstrap_template, 0, "%<>")
|
146
|
-
content = template.result(opt)
|
147
|
-
File.open(output_filepath, 'w') do |writer|
|
148
|
-
writer.syswrite(content)
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
1
|
+
require 'erb'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'rubygems'
|
4
|
+
|
5
|
+
begin
|
6
|
+
# this occurs in rubygems < 2.0.0
|
7
|
+
require 'rubygems/builder'
|
8
|
+
rescue LoadError
|
9
|
+
# this occurs in rubygems > 2.0.0
|
10
|
+
require 'rubygems/package'
|
11
|
+
end
|
12
|
+
|
13
|
+
module PgMigrate
|
14
|
+
class Package
|
15
|
+
|
16
|
+
attr_accessor :manifest_reader
|
17
|
+
|
18
|
+
def initialize(manifest_reader)
|
19
|
+
@log = Logging.logger[self]
|
20
|
+
@manifest_reader = manifest_reader
|
21
|
+
@template_dir = File.join(File.dirname(__FILE__), 'package_templates')
|
22
|
+
end
|
23
|
+
|
24
|
+
def package(built_migration_path, output_dir, name, version, options={:force=>true})
|
25
|
+
gemspec = create_gem(built_migration_path, output_dir, name, version, options[:force])
|
26
|
+
build_gem(gemspec, output_dir)
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_gem (built_migration_path, output_dir, name, version, force)
|
30
|
+
# validate that manifest is valid
|
31
|
+
@log.debug "validating output dir is manifest"
|
32
|
+
|
33
|
+
if !FileTest::exist?(built_migration_path)
|
34
|
+
raise "built manifest path does not exist #{built_migration_path}"
|
35
|
+
end
|
36
|
+
|
37
|
+
if built_migration_path == output_dir
|
38
|
+
raise "source and destination can not be the same path"
|
39
|
+
end
|
40
|
+
|
41
|
+
loaded_manifest = @manifest_reader.load_input_manifest(built_migration_path)
|
42
|
+
@manifest_reader.validate_migration_paths(built_migration_path, loaded_manifest)
|
43
|
+
|
44
|
+
@log.debug "preparing to build gem"
|
45
|
+
|
46
|
+
target = File.join(output_dir, name)
|
47
|
+
|
48
|
+
# stolen almost verbatim from bundler: https://github.com/carlhuda/bundler/blob/master/lib/bundler/cli.rb
|
49
|
+
constant_name = name.split('_').map { |p| p[0..0].upcase + p[1..-1] }.join
|
50
|
+
constant_name = constant_name.split('-').map { |q| q[0..0].upcase + q[1..-1] }.join('::') if constant_name =~ /-/
|
51
|
+
constant_array = constant_name.split('::')
|
52
|
+
# end stolen
|
53
|
+
|
54
|
+
author = "pgmigrate"
|
55
|
+
email = "pgmigrate@pgmigrate.io"
|
56
|
+
pg_migrate_version = PgMigrate::VERSION
|
57
|
+
gemfiles = ["Gemfile", "#{name}.gemspec", "lib/#{name}.rb", "lib/#{name}/version.rb", "bin/#{name}"]
|
58
|
+
gemfiles += userfiles(built_migration_path, name)
|
59
|
+
gemspec_path = File.join(output_dir, "#{name}.gemspec")
|
60
|
+
|
61
|
+
@log.debug "building gem"
|
62
|
+
|
63
|
+
output = Pathname.new(output_dir)
|
64
|
+
if !output.exist?
|
65
|
+
if !force
|
66
|
+
raise "Output directory '#{output_dir}' does not exist. Create it or specify force=true"
|
67
|
+
else
|
68
|
+
output.mkpath
|
69
|
+
end
|
70
|
+
else
|
71
|
+
# verify that it's is a directory
|
72
|
+
if !output.directory?
|
73
|
+
raise "output_dir #{output_dir} is a file; not a directory."
|
74
|
+
else
|
75
|
+
@log.debug("deleting & recreating existing output_dir #{output_dir}")
|
76
|
+
output.rmtree
|
77
|
+
output.mkpath
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
FileUtils.mkdir_p(output_dir)
|
82
|
+
FileUtils.mkdir_p(File.join(output_dir, "bin"))
|
83
|
+
FileUtils.mkdir_p(File.join(output_dir, "lib", name))
|
84
|
+
run_template("Gemfile.erb", binding, File.join(output_dir, "Gemfile"))
|
85
|
+
run_template("gemspec.erb", binding, gemspec_path)
|
86
|
+
run_template("lib/gem.rb", binding, File.join(output_dir, "lib", "#{name}.rb"))
|
87
|
+
run_template("lib/gem/version.rb", binding, File.join(output_dir, "lib", name, "version.rb"))
|
88
|
+
run_template("bin/migrate.rb", binding, File.join(output_dir, "bin", "#{name}"))
|
89
|
+
copy_schema(built_migration_path, File.join(output_dir, "lib", name, "schemas"))
|
90
|
+
|
91
|
+
return gemspec_path
|
92
|
+
end
|
93
|
+
|
94
|
+
def copy_schema(built_migration_path, output_dir)
|
95
|
+
FileUtils.cp_r(File.join(built_migration_path, '.'), output_dir)
|
96
|
+
end
|
97
|
+
|
98
|
+
def build_gem(gemspec_path, output_dir)
|
99
|
+
@log.debug "building gem"
|
100
|
+
|
101
|
+
@log.debug "loading gem specification #{gemspec_path}"
|
102
|
+
spec = Gem::Specification.load(gemspec_path)
|
103
|
+
|
104
|
+
if spec.nil?
|
105
|
+
raise 'unable to build gem from specification'
|
106
|
+
end
|
107
|
+
|
108
|
+
@log.debug "packaging gem"
|
109
|
+
Dir.chdir(output_dir) do
|
110
|
+
if defined?(Gem::Builder)
|
111
|
+
Gem::Builder.new(spec).build
|
112
|
+
else
|
113
|
+
Gem::Package.build(spec)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
#Gem::Package.build spec, false
|
117
|
+
end
|
118
|
+
|
119
|
+
def userfiles(built_migration_path, name)
|
120
|
+
|
121
|
+
gempaths = []
|
122
|
+
Find.find(built_migration_path) do |path|
|
123
|
+
if path == ".."
|
124
|
+
Find.prune
|
125
|
+
else
|
126
|
+
# make relative
|
127
|
+
|
128
|
+
relative = path[built_migration_path.length..-1]
|
129
|
+
gempath = File.join("lib", name, "schemas", relative)
|
130
|
+
gempaths.push(gempath)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
return gempaths
|
135
|
+
end
|
136
|
+
|
137
|
+
# given an input template and binding, writes to an output file
|
138
|
+
def run_template(template, opt, output_filepath)
|
139
|
+
bootstrap_template = nil
|
140
|
+
File.open(File.join(@template_dir, template), 'r') do |reader|
|
141
|
+
bootstrap_template = reader.read
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
template = ERB.new(bootstrap_template, 0, "%<>")
|
146
|
+
content = template.result(opt)
|
147
|
+
File.open(output_filepath, 'w') do |writer|
|
148
|
+
writer.syswrite(content)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# A sample Gemfile
|
2
|
-
source "https://rubygems.org"
|
3
|
-
|
1
|
+
# A sample Gemfile
|
2
|
+
source "https://rubygems.org"
|
3
|
+
|
4
4
|
gemspec
|
@@ -1,10 +1,10 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'pg_migrate'
|
4
|
-
require '<%= name %>'
|
5
|
-
|
6
|
-
include PgMigrate
|
7
|
-
|
8
|
-
CommandLine.packaged_source = File.expand_path('../../lib/<%= name %>/schemas', __FILE__)
|
9
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pg_migrate'
|
4
|
+
require '<%= name %>'
|
5
|
+
|
6
|
+
include PgMigrate
|
7
|
+
|
8
|
+
CommandLine.packaged_source = File.expand_path('../../lib/<%= name %>/schemas', __FILE__)
|
9
|
+
|
10
10
|
CommandLine.start
|
@@ -1,21 +1,21 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path('../lib/<%= name %>/version', __FILE__)
|
3
|
-
lib=File.expand_path('../lib', __FILE__)
|
4
|
-
|
5
|
-
|
6
|
-
Gem::Specification.new do |gem|
|
7
|
-
gem.authors = ["<%= author %>"]
|
8
|
-
gem.email = ["<%= email %> "]
|
9
|
-
gem.description = %q{A pg_migrate bundle for <%= name %>}
|
10
|
-
gem.summary = %q{A pg_migrate bundle built by pg_migrate, containing the schemas of <%= name %>)}
|
11
|
-
gem.homepage = "https://github.com/sethcall/pg_migrate"
|
12
|
-
|
13
|
-
gem.files = <%= gemfiles %>
|
14
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
|
-
gem.name = "<%= name %>"
|
17
|
-
gem.require_paths = ["lib"]
|
18
|
-
gem.version = <%= constant_name %>::VERSION
|
19
|
-
|
20
|
-
gem.add_dependency('pg_migrate', '<%= pg_migrate_version %>')
|
21
|
-
end
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/<%= name %>/version', __FILE__)
|
3
|
+
lib=File.expand_path('../lib', __FILE__)
|
4
|
+
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.authors = ["<%= author %>"]
|
8
|
+
gem.email = ["<%= email %> "]
|
9
|
+
gem.description = %q{A pg_migrate bundle for <%= name %>}
|
10
|
+
gem.summary = %q{A pg_migrate bundle built by pg_migrate, containing the schemas of <%= name %>)}
|
11
|
+
gem.homepage = "https://github.com/sethcall/pg_migrate"
|
12
|
+
|
13
|
+
gem.files = <%= gemfiles %>
|
14
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
|
+
gem.name = "<%= name %>"
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.version = <%= constant_name %>::VERSION
|
19
|
+
|
20
|
+
gem.add_dependency('pg_migrate', '<%= pg_migrate_version %>')
|
21
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module <%= constant_name %>
|
2
|
-
VERSION = "<%= version %>"
|
3
|
-
end
|
1
|
+
module <%= constant_name %>
|
2
|
+
VERSION = "<%= version %>"
|
3
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
require '<%= name %>/version'
|
2
|
-
require 'pg_migrate'
|
3
|
-
|
4
|
-
module <%= constant_name %>
|
5
|
-
|
6
|
-
class Migrator
|
7
|
-
def migrate options={}
|
8
|
-
pgMigrator = PgMigrate::Migrator.new(PgMigrate::ManifestReader.new, PgMigrate::SqlReader.new, options)
|
9
|
-
pgMigrator.migrate(File.expand_path('../<%= name %>/schemas', __FILE__))
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
require '<%= name %>/version'
|
2
|
+
require 'pg_migrate'
|
3
|
+
|
4
|
+
module <%= constant_name %>
|
5
|
+
|
6
|
+
class Migrator
|
7
|
+
def migrate options={}
|
8
|
+
pgMigrator = PgMigrate::Migrator.new(PgMigrate::ManifestReader.new, PgMigrate::SqlReader.new, options)
|
9
|
+
pgMigrator.migrate(File.expand_path('../<%= name %>/schemas', __FILE__))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/pg_migrate/props.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
# from comment at http://devender.wordpress.com/2006/05/01/reading-and-writing-java-property-files-with-ruby/
|
2
|
-
|
3
|
-
module PgMigrate
|
4
|
-
class Properties < Hash
|
5
|
-
def initialize(filename = nil)
|
6
|
-
if (filename) then
|
7
|
-
File.open(filename).select { |line| not line=~/^[ \t]*(#.+)*$/ }.# ignore comments and blank lines
|
8
|
-
each { |line|
|
9
|
-
(k, v) = line.chomp.split('=', 2)
|
10
|
-
self[k.strip] = v.strip
|
11
|
-
}
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def to_s
|
16
|
-
self.map { |k, v| " #{k}=#{v}" }.join("\n")
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
1
|
+
# from comment at http://devender.wordpress.com/2006/05/01/reading-and-writing-java-property-files-with-ruby/
|
2
|
+
|
3
|
+
module PgMigrate
|
4
|
+
class Properties < Hash
|
5
|
+
def initialize(filename = nil)
|
6
|
+
if (filename) then
|
7
|
+
File.open(filename).select { |line| not line=~/^[ \t]*(#.+)*$/ }.# ignore comments and blank lines
|
8
|
+
each { |line|
|
9
|
+
(k, v) = line.chomp.split('=', 2)
|
10
|
+
self[k.strip] = v.strip
|
11
|
+
}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
self.map { |k, v| " #{k}=#{v}" }.join("\n")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,52 +1,52 @@
|
|
1
|
-
module PgMigrate
|
2
|
-
|
3
|
-
class SqlReader
|
4
|
-
|
5
|
-
def initialize
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
# read in a migration file,
|
11
|
-
# converting lines of text into SQL statements that can be executed with our database connection
|
12
|
-
def load_migration(migration_path)
|
13
|
-
statements = []
|
14
|
-
|
15
|
-
current_statement = ""
|
16
|
-
|
17
|
-
migration_lines = IO.readlines(migration_path)
|
18
|
-
migration_lines.each_with_index do |line, index|
|
19
|
-
line_stripped = line.strip
|
20
|
-
|
21
|
-
if line_stripped.empty? || line_stripped.start_with?('--')
|
22
|
-
# it's a comment; ignore
|
23
|
-
elsif line_stripped.start_with?("\\")
|
24
|
-
# it's a psql command; ignore
|
25
|
-
else
|
26
|
-
current_statement += " " + line_stripped;
|
27
|
-
|
28
|
-
if line_stripped.end_with?(";")
|
29
|
-
if current_statement =~ /^\s*CREATE\s+(OR\s+REPLACE\s+)?FUNCTION/i || current_statement =~ /^\s*DO\s+/i
|
30
|
-
# if we are in a function, a ';' isn't enough to end. We need to see if the last word was one of
|
31
|
-
# pltcl, plperl, plpgsql, plpythonu, sql
|
32
|
-
# you can extend languages in postgresql; detecting these isn't supported yet.
|
33
|
-
|
34
|
-
# we also detect anonymous functions (DO) and their ending sequence.
|
35
|
-
if current_statement =~ /(plpgsql|plperl|plpythonu|pltcl|sql)\s*;$/i || current_statement =~ /END\s*\$\$\s+(LANGUAGE\s+(plpgsql|plperl|plpythonu|pltcl|sql))?\s*;$/i
|
36
|
-
statements.push(current_statement[0...-1]) # strip off last ;
|
37
|
-
current_statement = ""
|
38
|
-
end
|
39
|
-
|
40
|
-
else
|
41
|
-
statements.push(current_statement[0...-1]) # strip off last ;
|
42
|
-
current_statement = ""
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
return statements
|
49
|
-
|
50
|
-
end
|
51
|
-
end
|
1
|
+
module PgMigrate
|
2
|
+
|
3
|
+
class SqlReader
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
# read in a migration file,
|
11
|
+
# converting lines of text into SQL statements that can be executed with our database connection
|
12
|
+
def load_migration(migration_path)
|
13
|
+
statements = []
|
14
|
+
|
15
|
+
current_statement = ""
|
16
|
+
|
17
|
+
migration_lines = IO.readlines(migration_path)
|
18
|
+
migration_lines.each_with_index do |line, index|
|
19
|
+
line_stripped = line.strip
|
20
|
+
|
21
|
+
if line_stripped.empty? || line_stripped.start_with?('--')
|
22
|
+
# it's a comment; ignore
|
23
|
+
elsif line_stripped.start_with?("\\")
|
24
|
+
# it's a psql command; ignore
|
25
|
+
else
|
26
|
+
current_statement += " " + line_stripped;
|
27
|
+
|
28
|
+
if line_stripped.end_with?(";")
|
29
|
+
if current_statement =~ /^\s*CREATE\s+(OR\s+REPLACE\s+)?FUNCTION/i || current_statement =~ /^\s*DO\s+/i
|
30
|
+
# if we are in a function, a ';' isn't enough to end. We need to see if the last word was one of
|
31
|
+
# pltcl, plperl, plpgsql, plpythonu, sql
|
32
|
+
# you can extend languages in postgresql; detecting these isn't supported yet.
|
33
|
+
|
34
|
+
# we also detect anonymous functions (DO) and their ending sequence.
|
35
|
+
if current_statement =~ /(plpgsql|plperl|plpythonu|pltcl|sql)\s*;$/i || current_statement =~ /END\s*\$\$\s+(LANGUAGE\s+(plpgsql|plperl|plpythonu|pltcl|sql))?\s*;$/i
|
36
|
+
statements.push(current_statement[0...-1]) # strip off last ;
|
37
|
+
current_statement = ""
|
38
|
+
end
|
39
|
+
|
40
|
+
else
|
41
|
+
statements.push(current_statement[0...-1]) # strip off last ;
|
42
|
+
current_statement = ""
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
return statements
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
52
|
end
|