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.
- 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 -29
- 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/pg_migrate.gemspec
CHANGED
@@ -1,29 +1,29 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path('../lib/pg_migrate/version', __FILE__)
|
3
|
-
lib=File.expand_path('../lib', __FILE__)
|
4
|
-
|
5
|
-
Gem::Specification.new do |gem|
|
6
|
-
gem.authors = ["Seth Call"]
|
7
|
-
gem.email = ["sethcall@gmail.com"]
|
8
|
-
gem.description = %q{Simple migration tool focused on Postgresql}
|
9
|
-
gem.summary = %q{Create migration scripts in raw SQL that work regardless if they are run from the pg_migrate command-line, psql, or native code integration. More documentation exists on the project homepage.)}
|
10
|
-
gem.homepage = "https://github.com/sethcall/pg_migrate"
|
11
|
-
|
12
|
-
gem.files = `git ls-files`.split($\)
|
13
|
-
gem.files += ['lib/pg_migrate/templates/bootstrap.erb']
|
14
|
-
gem.files += ['lib/pg_migrate/templates/up.erb']
|
15
|
-
gem.files.delete("lib/pg_migrate/templates")
|
16
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
-
gem.name = "pg_migrate"
|
19
|
-
gem.require_paths = ["lib"]
|
20
|
-
gem.version = PgMigrate::VERSION
|
21
|
-
|
22
|
-
gem.add_dependency('logging', '1.7.2')
|
23
|
-
|
24
|
-
gem.add_dependency('pg', '0.
|
25
|
-
gem.add_dependency('thor', '0.15.4')
|
26
|
-
#gem.add_dependency('rubygems', '1.8.24')
|
27
|
-
|
28
|
-
end
|
29
|
-
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/pg_migrate/version', __FILE__)
|
3
|
+
lib=File.expand_path('../lib', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ["Seth Call"]
|
7
|
+
gem.email = ["sethcall@gmail.com"]
|
8
|
+
gem.description = %q{Simple migration tool focused on Postgresql}
|
9
|
+
gem.summary = %q{Create migration scripts in raw SQL that work regardless if they are run from the pg_migrate command-line, psql, or native code integration. More documentation exists on the project homepage.)}
|
10
|
+
gem.homepage = "https://github.com/sethcall/pg_migrate"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.files += ['lib/pg_migrate/templates/bootstrap.erb']
|
14
|
+
gem.files += ['lib/pg_migrate/templates/up.erb']
|
15
|
+
gem.files.delete("lib/pg_migrate/templates")
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.name = "pg_migrate"
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
gem.version = PgMigrate::VERSION
|
21
|
+
|
22
|
+
gem.add_dependency('logging', '1.7.2')
|
23
|
+
|
24
|
+
gem.add_dependency('pg', '0.17.1')
|
25
|
+
gem.add_dependency('thor', '0.15.4')
|
26
|
+
#gem.add_dependency('rubygems', '1.8.24')
|
27
|
+
|
28
|
+
end
|
29
|
+
|
data/spec/database.yml
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
test:
|
2
|
-
adapter: postgresql
|
3
|
-
encoding: unicode
|
4
|
-
database: pg_migrate_test
|
5
|
-
pool: 5
|
6
|
-
username: postgres
|
7
|
-
password: postgres
|
8
|
-
host: localhost
|
1
|
+
test:
|
2
|
+
adapter: postgresql
|
3
|
+
encoding: unicode
|
4
|
+
database: pg_migrate_test
|
5
|
+
pool: 5
|
6
|
+
username: postgres
|
7
|
+
password: postgres
|
8
|
+
host: localhost
|
9
9
|
port: 5432
|
@@ -1,114 +1,114 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Builder do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
@manifest_reader = ManifestReader.new
|
7
|
-
@sql_reader = SqlReader.new
|
8
|
-
@standard_builder = Builder.new(@manifest_reader, @sql_reader)
|
9
|
-
@dbutil = DbUtility.new
|
10
|
-
end
|
11
|
-
|
12
|
-
it "create bootstrap.sql" do
|
13
|
-
standard_builder = @standard_builder
|
14
|
-
target = Files.create :path => "target/bootstrap_test", :timestamp => false do
|
15
|
-
standard_builder.create_bootstrap_script(Dir.pwd)
|
16
|
-
|
17
|
-
# the .sql file should exist after
|
18
|
-
FileTest::exists?(BOOTSTRAP_FILENAME).should == true
|
19
|
-
|
20
|
-
content = nil
|
21
|
-
|
22
|
-
# dynamic content should be in the file
|
23
|
-
File.open(BOOTSTRAP_FILENAME, 'r') { |reader| content = reader.read }
|
24
|
-
|
25
|
-
content.start_with?('-- pg_migrate bootstrap').should == true
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
it "creates indempotent migrations" do
|
31
|
-
|
32
|
-
def run_bootstrap(output_dir)
|
33
|
-
run_migration(BOOTSTRAP_FILENAME, output_dir)
|
34
|
-
end
|
35
|
-
|
36
|
-
def run_migration(migration_path, output_dir)
|
37
|
-
@dbutil.connect_test_database() do |conn|
|
38
|
-
statements = @sql_reader.load_migration(File.join(output_dir, UP_DIRNAME, migration_path))
|
39
|
-
|
40
|
-
statements.each do |statement|
|
41
|
-
conn.exec(statement)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def verify_bootstrap()
|
47
|
-
# come back in, and verify that the bootstrap tables are there
|
48
|
-
@dbutil.connect_test_database() do |conn|
|
49
|
-
conn.exec("SELECT table_name FROM information_schema.tables WHERE table_name = $1", [PG_MIGRATE_TABLE]) do |result|
|
50
|
-
result.ntuples.should == 1
|
51
|
-
result.getvalue(0, 0).should == PG_MIGRATE_TABLE
|
52
|
-
end
|
53
|
-
|
54
|
-
conn.exec("SELECT table_name FROM information_schema.tables WHERE table_name = $1", [PG_MIGRATIONS_TABLE]) do |result|
|
55
|
-
result.ntuples.should == 1
|
56
|
-
result.getvalue(0, 0).should == PG_MIGRATIONS_TABLE
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
single_manifest=File.expand_path('spec/pg_migrate/input_manifests/single_manifest')
|
63
|
-
single_manifest = File.join(single_manifest, '.')
|
64
|
-
|
65
|
-
input_dir = nil
|
66
|
-
target = Files.create :path => "target", :timestamp => false do
|
67
|
-
input_dir = dir "input_single_manifest", :src => single_manifest do
|
68
|
-
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
output_dir = File.join('target', 'output_single_manifest')
|
73
|
-
|
74
|
-
FileUtils.rm_rf(output_dir)
|
75
|
-
|
76
|
-
@standard_builder.build(input_dir, output_dir)
|
77
|
-
|
78
|
-
@dbutil.create_new_test_database()
|
79
|
-
|
80
|
-
# run bootstrap once, and verify the tables now exist
|
81
|
-
run_bootstrap(output_dir)
|
82
|
-
verify_bootstrap()
|
83
|
-
|
84
|
-
# run bootstrap again, and verify no error (implicitly), and that the tables now exist
|
85
|
-
run_bootstrap(output_dir)
|
86
|
-
verify_bootstrap()
|
87
|
-
|
88
|
-
# now run single1.sql
|
89
|
-
run_migration('single1.sql', output_dir)
|
90
|
-
|
91
|
-
@dbutil.connect_test_database() do |conn|
|
92
|
-
conn.exec("SELECT table_name FROM information_schema.tables WHERE table_name = $1", ["emp"]) do |result|
|
93
|
-
result.ntuples.should == 1
|
94
|
-
result.getvalue(0, 0).should == "emp"
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
# run it again. a very certain exception should occur... 'pg_migrate: code=migration_exists'
|
99
|
-
begin
|
100
|
-
run_migration('single1.sql', output_dir)
|
101
|
-
false.should == true
|
102
|
-
rescue Exception => e
|
103
|
-
e.message.index('pg_migrate: code=migration_exists').should_not == nil
|
104
|
-
end
|
105
|
-
|
106
|
-
@dbutil.connect_test_database() do |conn|
|
107
|
-
conn.exec("SELECT table_name FROM information_schema.tables WHERE table_name = $1", ["emp"]) do |result|
|
108
|
-
result.ntuples.should == 1
|
109
|
-
result.getvalue(0, 0).should == "emp"
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Builder do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@manifest_reader = ManifestReader.new
|
7
|
+
@sql_reader = SqlReader.new
|
8
|
+
@standard_builder = Builder.new(@manifest_reader, @sql_reader)
|
9
|
+
@dbutil = DbUtility.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it "create bootstrap.sql" do
|
13
|
+
standard_builder = @standard_builder
|
14
|
+
target = Files.create :path => "target/bootstrap_test", :timestamp => false do
|
15
|
+
standard_builder.create_bootstrap_script(Dir.pwd)
|
16
|
+
|
17
|
+
# the .sql file should exist after
|
18
|
+
FileTest::exists?(BOOTSTRAP_FILENAME).should == true
|
19
|
+
|
20
|
+
content = nil
|
21
|
+
|
22
|
+
# dynamic content should be in the file
|
23
|
+
File.open(BOOTSTRAP_FILENAME, 'r') { |reader| content = reader.read }
|
24
|
+
|
25
|
+
content.start_with?('-- pg_migrate bootstrap').should == true
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
it "creates indempotent migrations" do
|
31
|
+
|
32
|
+
def run_bootstrap(output_dir)
|
33
|
+
run_migration(BOOTSTRAP_FILENAME, output_dir)
|
34
|
+
end
|
35
|
+
|
36
|
+
def run_migration(migration_path, output_dir)
|
37
|
+
@dbutil.connect_test_database() do |conn|
|
38
|
+
statements = @sql_reader.load_migration(File.join(output_dir, UP_DIRNAME, migration_path))
|
39
|
+
|
40
|
+
statements.each do |statement|
|
41
|
+
conn.exec(statement)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def verify_bootstrap()
|
47
|
+
# come back in, and verify that the bootstrap tables are there
|
48
|
+
@dbutil.connect_test_database() do |conn|
|
49
|
+
conn.exec("SELECT table_name FROM information_schema.tables WHERE table_name = $1", [PG_MIGRATE_TABLE]) do |result|
|
50
|
+
result.ntuples.should == 1
|
51
|
+
result.getvalue(0, 0).should == PG_MIGRATE_TABLE
|
52
|
+
end
|
53
|
+
|
54
|
+
conn.exec("SELECT table_name FROM information_schema.tables WHERE table_name = $1", [PG_MIGRATIONS_TABLE]) do |result|
|
55
|
+
result.ntuples.should == 1
|
56
|
+
result.getvalue(0, 0).should == PG_MIGRATIONS_TABLE
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
single_manifest=File.expand_path('spec/pg_migrate/input_manifests/single_manifest')
|
63
|
+
single_manifest = File.join(single_manifest, '.')
|
64
|
+
|
65
|
+
input_dir = nil
|
66
|
+
target = Files.create :path => "target", :timestamp => false do
|
67
|
+
input_dir = dir "input_single_manifest", :src => single_manifest do
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
output_dir = File.join('target', 'output_single_manifest')
|
73
|
+
|
74
|
+
FileUtils.rm_rf(output_dir)
|
75
|
+
|
76
|
+
@standard_builder.build(input_dir, output_dir)
|
77
|
+
|
78
|
+
@dbutil.create_new_test_database()
|
79
|
+
|
80
|
+
# run bootstrap once, and verify the tables now exist
|
81
|
+
run_bootstrap(output_dir)
|
82
|
+
verify_bootstrap()
|
83
|
+
|
84
|
+
# run bootstrap again, and verify no error (implicitly), and that the tables now exist
|
85
|
+
run_bootstrap(output_dir)
|
86
|
+
verify_bootstrap()
|
87
|
+
|
88
|
+
# now run single1.sql
|
89
|
+
run_migration('single1.sql', output_dir)
|
90
|
+
|
91
|
+
@dbutil.connect_test_database() do |conn|
|
92
|
+
conn.exec("SELECT table_name FROM information_schema.tables WHERE table_name = $1", ["emp"]) do |result|
|
93
|
+
result.ntuples.should == 1
|
94
|
+
result.getvalue(0, 0).should == "emp"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# run it again. a very certain exception should occur... 'pg_migrate: code=migration_exists'
|
99
|
+
begin
|
100
|
+
run_migration('single1.sql', output_dir)
|
101
|
+
false.should == true
|
102
|
+
rescue Exception => e
|
103
|
+
e.message.index('pg_migrate: code=migration_exists').should_not == nil
|
104
|
+
end
|
105
|
+
|
106
|
+
@dbutil.connect_test_database() do |conn|
|
107
|
+
conn.exec("SELECT table_name FROM information_schema.tables WHERE table_name = $1", ["emp"]) do |result|
|
108
|
+
result.ntuples.should == 1
|
109
|
+
result.getvalue(0, 0).should == "emp"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
114
|
end
|
@@ -1,54 +1,54 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe CommandLine do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
@dbutil = DbUtility.new
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
it "build using config file" do
|
11
|
-
|
12
|
-
single_manifest=File.expand_path('spec/pg_migrate/input_manifests/single_manifest')
|
13
|
-
single_manifest=File.join(single_manifest, '.')
|
14
|
-
|
15
|
-
input_dir = nil
|
16
|
-
target = Files.create :path => "target", :timestamp => false do
|
17
|
-
input_dir = dir "input_single_manifest", :src => single_manifest do
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
output_dir = File.join("target", 'output_single_manifest')
|
23
|
-
|
24
|
-
FileUtils.rm_rf(output_dir)
|
25
|
-
|
26
|
-
# make a properties file on the fly, with the out parameter specified
|
27
|
-
props = Properties.new
|
28
|
-
props['build.out'] = output_dir
|
29
|
-
props['build.force'] = "true"
|
30
|
-
props['build.test'] = "false"
|
31
|
-
#props['up.connopts'] = "dbname:pg_migrate_test host:localhost port:5432 user:postgres password:postgres"
|
32
|
-
|
33
|
-
# and put that properties file in the input dir
|
34
|
-
File.open(File.join(input_dir, PG_CONFIG), 'w') { |f| f.write(props) }
|
35
|
-
|
36
|
-
# invoke pg_migrate build, with the hopes that the output dir is honored
|
37
|
-
result = `bundle exec pg_migrate build -s #{input_dir}`
|
38
|
-
|
39
|
-
puts "pg_migrate build output: #{result}"
|
40
|
-
|
41
|
-
$?.exitstatus.should == 0
|
42
|
-
|
43
|
-
FileTest::exist?(output_dir).should == true
|
44
|
-
FileTest::exist?(File.join(output_dir, MANIFEST_FILENAME)).should == true
|
45
|
-
FileTest::exist?(File.join(output_dir, PG_CONFIG)).should == true
|
46
|
-
FileTest::exist?(File.join(output_dir, UP_DIRNAME)).should == true
|
47
|
-
FileTest::exist?(File.join(output_dir, UP_DIRNAME, BOOTSTRAP_FILENAME)).should == true
|
48
|
-
FileTest::exist?(File.join(output_dir, UP_DIRNAME, "single1.sql")).should == true
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CommandLine do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@dbutil = DbUtility.new
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
it "build using config file" do
|
11
|
+
|
12
|
+
single_manifest=File.expand_path('spec/pg_migrate/input_manifests/single_manifest')
|
13
|
+
single_manifest=File.join(single_manifest, '.')
|
14
|
+
|
15
|
+
input_dir = nil
|
16
|
+
target = Files.create :path => "target", :timestamp => false do
|
17
|
+
input_dir = dir "input_single_manifest", :src => single_manifest do
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
output_dir = File.join("target", 'output_single_manifest')
|
23
|
+
|
24
|
+
FileUtils.rm_rf(output_dir)
|
25
|
+
|
26
|
+
# make a properties file on the fly, with the out parameter specified
|
27
|
+
props = Properties.new
|
28
|
+
props['build.out'] = output_dir
|
29
|
+
props['build.force'] = "true"
|
30
|
+
props['build.test'] = "false"
|
31
|
+
#props['up.connopts'] = "dbname:pg_migrate_test host:localhost port:5432 user:postgres password:postgres"
|
32
|
+
|
33
|
+
# and put that properties file in the input dir
|
34
|
+
File.open(File.join(input_dir, PG_CONFIG), 'w') { |f| f.write(props) }
|
35
|
+
|
36
|
+
# invoke pg_migrate build, with the hopes that the output dir is honored
|
37
|
+
result = `bundle exec pg_migrate build -s #{input_dir}`
|
38
|
+
|
39
|
+
puts "pg_migrate build output: #{result}"
|
40
|
+
|
41
|
+
$?.exitstatus.should == 0
|
42
|
+
|
43
|
+
FileTest::exist?(output_dir).should == true
|
44
|
+
FileTest::exist?(File.join(output_dir, MANIFEST_FILENAME)).should == true
|
45
|
+
FileTest::exist?(File.join(output_dir, PG_CONFIG)).should == true
|
46
|
+
FileTest::exist?(File.join(output_dir, UP_DIRNAME)).should == true
|
47
|
+
FileTest::exist?(File.join(output_dir, UP_DIRNAME, BOOTSTRAP_FILENAME)).should == true
|
48
|
+
FileTest::exist?(File.join(output_dir, UP_DIRNAME, "single1.sql")).should == true
|
49
|
+
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
54
|
end
|
@@ -1,19 +1,19 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ConfigParser do
|
4
|
-
it "parse my test database.yml file" do
|
5
|
-
config = ConfigParser.rails("spec/database.yml", "test")
|
6
|
-
config.should == {
|
7
|
-
:dbname => "pg_migrate_test",
|
8
|
-
:user => "postgres",
|
9
|
-
:password => "postgres",
|
10
|
-
:host => "localhost",
|
11
|
-
:port => 5432
|
12
|
-
}
|
13
|
-
end
|
14
|
-
|
15
|
-
it "run single migration" do
|
16
|
-
config = ConfigParser.rails("spec/database.yml", "test")
|
17
|
-
end
|
18
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ConfigParser do
|
4
|
+
it "parse my test database.yml file" do
|
5
|
+
config = ConfigParser.rails("spec/database.yml", "test")
|
6
|
+
config.should == {
|
7
|
+
:dbname => "pg_migrate_test",
|
8
|
+
:user => "postgres",
|
9
|
+
:password => "postgres",
|
10
|
+
:host => "localhost",
|
11
|
+
:port => 5432
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
it "run single migration" do
|
16
|
+
config = ConfigParser.rails("spec/database.yml", "test")
|
17
|
+
end
|
18
|
+
|
19
19
|
end
|
@@ -1,73 +1,73 @@
|
|
1
|
-
module PgMigrate
|
2
|
-
|
3
|
-
class DbUtility
|
4
|
-
|
5
|
-
DEFAULT_OPTIONS = {
|
6
|
-
:dbtestname => "pg_migrate_test",
|
7
|
-
:dbsuperuser => "postgres",
|
8
|
-
:dbsuperpass => "postgres",
|
9
|
-
:dbhost => "localhost",
|
10
|
-
:dbport => 5432
|
11
|
-
}
|
12
|
-
|
13
|
-
def initialize(options=DEFAULT_OPTIONS)
|
14
|
-
|
15
|
-
options = DEFAULT_OPTIONS.merge(options)
|
16
|
-
|
17
|
-
@dbtestname = options[:dbtestname]
|
18
|
-
@dbsuperuser = options[:dbsuperuser]
|
19
|
-
@dbsuperpass = options[:dbsuperpass]
|
20
|
-
@dbhost = options[:dbhost]
|
21
|
-
@dbport = options[:dbport]
|
22
|
-
end
|
23
|
-
|
24
|
-
def pg_connection_hasher()
|
25
|
-
return {
|
26
|
-
:port => @dbport,
|
27
|
-
:user => @dbsuperuser,
|
28
|
-
:password => @dbsuperpass,
|
29
|
-
:host => @dbhost
|
30
|
-
}
|
31
|
-
end
|
32
|
-
|
33
|
-
|
34
|
-
def create_new_test_database()
|
35
|
-
|
36
|
-
# this will presumably do the right default thing,
|
37
|
-
# to get us into a 'default' database where we can execute 'create database' from
|
38
|
-
conn_properties = pg_connection_hasher
|
39
|
-
|
40
|
-
conn_properties.delete(:dbname)
|
41
|
-
|
42
|
-
conn = PG::Connection.new(conn_properties)
|
43
|
-
|
44
|
-
conn.exec("DROP DATABASE IF EXISTS #{@dbtestname}").clear
|
45
|
-
conn.exec("CREATE DATABASE #{@dbtestname}").clear
|
46
|
-
|
47
|
-
conn.close
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
def connect_test_database(&block)
|
52
|
-
conn = nil
|
53
|
-
|
54
|
-
begin
|
55
|
-
conn_properties = pg_connection_hasher
|
56
|
-
|
57
|
-
conn_properties[:dbname] = @dbtestname
|
58
|
-
conn = PG::Connection.open(conn_properties)
|
59
|
-
|
60
|
-
yield conn
|
61
|
-
|
62
|
-
ensure
|
63
|
-
if !conn.nil?
|
64
|
-
conn.close
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
1
|
+
module PgMigrate
|
2
|
+
|
3
|
+
class DbUtility
|
4
|
+
|
5
|
+
DEFAULT_OPTIONS = {
|
6
|
+
:dbtestname => "pg_migrate_test",
|
7
|
+
:dbsuperuser => "postgres",
|
8
|
+
:dbsuperpass => "postgres",
|
9
|
+
:dbhost => "localhost",
|
10
|
+
:dbport => 5432
|
11
|
+
}
|
12
|
+
|
13
|
+
def initialize(options=DEFAULT_OPTIONS)
|
14
|
+
|
15
|
+
options = DEFAULT_OPTIONS.merge(options)
|
16
|
+
|
17
|
+
@dbtestname = options[:dbtestname]
|
18
|
+
@dbsuperuser = options[:dbsuperuser]
|
19
|
+
@dbsuperpass = options[:dbsuperpass]
|
20
|
+
@dbhost = options[:dbhost]
|
21
|
+
@dbport = options[:dbport]
|
22
|
+
end
|
23
|
+
|
24
|
+
def pg_connection_hasher()
|
25
|
+
return {
|
26
|
+
:port => @dbport,
|
27
|
+
:user => @dbsuperuser,
|
28
|
+
:password => @dbsuperpass,
|
29
|
+
:host => @dbhost
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def create_new_test_database()
|
35
|
+
|
36
|
+
# this will presumably do the right default thing,
|
37
|
+
# to get us into a 'default' database where we can execute 'create database' from
|
38
|
+
conn_properties = pg_connection_hasher
|
39
|
+
|
40
|
+
conn_properties.delete(:dbname)
|
41
|
+
|
42
|
+
conn = PG::Connection.new(conn_properties)
|
43
|
+
|
44
|
+
conn.exec("DROP DATABASE IF EXISTS #{@dbtestname}").clear
|
45
|
+
conn.exec("CREATE DATABASE #{@dbtestname}").clear
|
46
|
+
|
47
|
+
conn.close
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
def connect_test_database(&block)
|
52
|
+
conn = nil
|
53
|
+
|
54
|
+
begin
|
55
|
+
conn_properties = pg_connection_hasher
|
56
|
+
|
57
|
+
conn_properties[:dbname] = @dbtestname
|
58
|
+
conn = PG::Connection.open(conn_properties)
|
59
|
+
|
60
|
+
yield conn
|
61
|
+
|
62
|
+
ensure
|
63
|
+
if !conn.nil?
|
64
|
+
conn.close
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# name=single
|
2
|
-
# this is a comment
|
3
|
-
|
1
|
+
# name=single
|
2
|
+
# this is a comment
|
3
|
+
|
4
4
|
single1.sql
|
@@ -1,29 +1,29 @@
|
|
1
|
-
-- this is a SQL comment
|
2
|
-
|
3
|
-
select 1;
|
4
|
-
|
5
|
-
select 2;
|
6
|
-
|
7
|
-
select
|
8
|
-
3;
|
9
|
-
|
10
|
-
create table emp(id BIGSERIAL PRIMARY KEY, name varchar(255));
|
11
|
-
|
12
|
-
-- a sql function
|
13
|
-
CREATE FUNCTION clean_emp() RETURNS void AS '
|
14
|
-
DELETE FROM emp;
|
15
|
-
' LANGUAGE SQL;
|
16
|
-
|
17
|
-
-- a sql function on one line
|
18
|
-
CREATE FUNCTION clean_emp2() RETURNS void AS 'DELETE FROM emp;' LANGUAGE SQL;
|
19
|
-
|
20
|
-
CREATE FUNCTION populate() RETURNS integer AS $$
|
21
|
-
DECLARE
|
22
|
-
-- declarations
|
23
|
-
BEGIN
|
24
|
-
PERFORM clean_emp2();
|
25
|
-
END;
|
26
|
-
$$ LANGUAGE plpgsql;
|
27
|
-
|
28
|
-
|
29
|
-
|
1
|
+
-- this is a SQL comment
|
2
|
+
|
3
|
+
select 1;
|
4
|
+
|
5
|
+
select 2;
|
6
|
+
|
7
|
+
select
|
8
|
+
3;
|
9
|
+
|
10
|
+
create table emp(id BIGSERIAL PRIMARY KEY, name varchar(255));
|
11
|
+
|
12
|
+
-- a sql function
|
13
|
+
CREATE FUNCTION clean_emp() RETURNS void AS '
|
14
|
+
DELETE FROM emp;
|
15
|
+
' LANGUAGE SQL;
|
16
|
+
|
17
|
+
-- a sql function on one line
|
18
|
+
CREATE FUNCTION clean_emp2() RETURNS void AS 'DELETE FROM emp;' LANGUAGE SQL;
|
19
|
+
|
20
|
+
CREATE FUNCTION populate() RETURNS integer AS $$
|
21
|
+
DECLARE
|
22
|
+
-- declarations
|
23
|
+
BEGIN
|
24
|
+
PERFORM clean_emp2();
|
25
|
+
END;
|
26
|
+
$$ LANGUAGE plpgsql;
|
27
|
+
|
28
|
+
|
29
|
+
|