sequel_migration_builder 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzVhY2M3MmE2YWZmZTk2ZDZhNzhjN2JiOTc0YmQwM2FjOWUwYTRhYg==
5
+ data.tar.gz: !binary |-
6
+ ZWRlMDFiMTg1ZWZiNGYyMDQ5YTZkNzVjMWJjZTc5YjVkZWRmZjlkZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MjE5OGMyMDA2ZWE5MGZhYmM2MWViNDYwMDVkZjk0YjQ5Nzk5NmYxNDcyZGU5
10
+ MmM1YTNhNjM1ZmQ4YWUwY2U4NmYzYTg1Y2ZmNmFmNWNlMjlkYTU1YmRkZTA0
11
+ NTNlNjY5Y2ZkZmQzMWM2YjBhZWQ0ZTk4NGViMTdhMTZlZmFhYWI=
12
+ data.tar.gz: !binary |-
13
+ ZTYzMmU1ZGZhMTRiYjY0N2ZhNDU1NTEyNTM3YmQ1MTI2ZjY3NTExYzc3Y2Vh
14
+ N2Q3MDY3Zjg0YmRkNTY4YWE4NTNjYmZkMTRkMzYzODUxODkxY2VhMTAzMjk2
15
+ YjcxMGYwYjBjNGE3NjFkMWNiZmM2NGM5ZjQ4Yzc3MzgxOWMyY2Q=
data/Rakefile CHANGED
@@ -19,32 +19,22 @@ rescue LoadError
19
19
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
20
  end
21
21
 
22
- require 'spec/rake/spectask'
23
- Spec::Rake::SpecTask.new(:spec) do |spec|
24
- spec.libs << 'lib' << 'spec'
25
- spec.spec_files = FileList['spec/**/*_spec.rb']
22
+ require 'rspec/core'
23
+ require 'rspec/core/rake_task'
24
+ RSpec::Core::RakeTask.new(:spec) do |spec|
25
+ spec.pattern = FileList['spec/**/*_spec.rb']
26
26
  end
27
27
 
28
- Spec::Rake::SpecTask.new(:rcov) do |spec|
29
- spec.libs << 'lib' << 'spec'
30
- spec.pattern = 'spec/**/*_spec.rb'
28
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
29
+ spec.pattern = FileList['spec/**/*_spec.rb']
31
30
  spec.rcov = true
31
+ spec.rcov_opts = "-x spec/ -x /home"
32
32
  end
33
33
 
34
34
  task :spec => :check_dependencies
35
35
 
36
36
  task :default => :spec
37
37
 
38
- require 'rake/rdoctask'
39
- Rake::RDocTask.new do |rdoc|
40
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
-
42
- rdoc.rdoc_dir = 'rdoc'
43
- rdoc.title = "sequel_migration_builder #{version}"
44
- rdoc.rdoc_files.include('README*')
45
- rdoc.rdoc_files.include('lib/**/*.rb')
46
- end
47
-
48
38
  desc "Flog this baby!"
49
39
  task :flog do
50
40
  sh 'find lib -name "*.rb" | xargs flog'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.4.0
@@ -13,12 +13,16 @@ module Sequel
13
13
 
14
14
  # Creates a migration builder for the given database.
15
15
  #
16
- def initialize(db)
16
+ def initialize(db, options={})
17
17
  @db = db
18
18
  @db_tables = Schema::DbSchemaParser.for_db(db).parse_db_schema
19
19
  @db_table_names = @db.tables
20
20
  @indent = 0
21
21
  @result = []
22
+ @separate_alter_table_statements = !!options[:separate_alter_table_statements]
23
+ # Columns are only added and dropped, never altered. This helps
24
+ # us support Redshift.
25
+ @immutable_columns = !!options[:immutable_columns]
22
26
  end
23
27
 
24
28
  # Generates a string of ruby code to define a sequel
@@ -74,10 +78,19 @@ module Sequel
74
78
  each_table(current_table_names, tables) do |table_name, table, last_table|
75
79
  hsh = table.dup
76
80
  hsh[:columns] = hsh[:columns].map {|c| Schema::DbColumn.build_from_hash(c) }
77
- operations = Schema::AlterTableOperations.build(@db_tables[table_name], hsh)
81
+ operations = Schema::AlterTableOperations.
82
+ build(@db_tables[table_name], hsh, :immutable_columns => @immutable_columns)
78
83
  unless operations.empty?
79
- alter_table_statement table_name, operations
80
- add_blank_line unless last_table
84
+ all_operations = if @separate_alter_table_statements
85
+ operations.map {|o| [o] }
86
+ else
87
+ [operations]
88
+ end
89
+
90
+ all_operations.each_with_index do |o, i|
91
+ alter_table_statement table_name, o
92
+ add_blank_line unless last_table && i + 1 == all_operations.size
93
+ end
81
94
  end
82
95
  end
83
96
  end
@@ -1,11 +1,15 @@
1
1
  module Sequel
2
2
  module Schema
3
3
  class AlterTableOperations
4
+ def initialize(options={})
5
+ @immutable_columns = options[:immutable_columns]
6
+ end
7
+
4
8
  # Returns an array of operations to change the current database
5
9
  # table to be like the defined table.
6
10
  #
7
- def self.build(db_table, new_table)
8
- new.build(db_table, new_table)
11
+ def self.build(db_table, new_table, options={})
12
+ new(options).build(db_table, new_table)
9
13
  end
10
14
 
11
15
  def build(db_table, new_table)
@@ -46,10 +50,16 @@ module Sequel
46
50
  result = []
47
51
 
48
52
  diffs = db_column.diff(new_column)
49
- result << :change_type_statement if [:elements, :column_type, :size, :unsigned].any? {|sym| diffs.include?(sym) }
50
- # only need to explicitly set the default if we're not changing the column type.
51
- result << :change_default_statement if diffs.include?(:default) && result.empty?
52
- result << :change_null_statement if diffs.include?(:null)
53
+
54
+ if @immutable_columns && !diffs.empty?
55
+ result << :drop_statement
56
+ result << :add_statement
57
+ else
58
+ result << :change_type_statement if [:elements, :column_type, :size, :unsigned].any? {|sym| diffs.include?(sym) }
59
+ # only need to explicitly set the default if we're not changing the column type.
60
+ result << :change_default_statement if diffs.include?(:default) && result.empty?
61
+ result << :change_null_statement if diffs.include?(:null)
62
+ end
53
63
 
54
64
  result.map {|statement| new_column.__send__(statement) }
55
65
  end
@@ -24,7 +24,7 @@ module Sequel
24
24
  # can be strings or symbols.
25
25
  #
26
26
  def self.build_from_hash(attrs={})
27
- self.new *members.map {|key| attrs[key] || attrs[key.to_sym] }
27
+ self.new *members.map {|key| attrs[key.to_s] || attrs[key.to_sym] }
28
28
  end
29
29
 
30
30
  def initialize(*args)
@@ -2,16 +2,18 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
+ # stub: sequel_migration_builder 0.4.0 ruby lib
5
6
 
6
7
  Gem::Specification.new do |s|
7
- s.name = %q{sequel_migration_builder}
8
- s.version = "0.3.2"
8
+ s.name = "sequel_migration_builder"
9
+ s.version = "0.4.0"
9
10
 
10
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Roland Swingler}]
12
- s.date = %q{2012-05-11}
13
- s.description = %q{Build Sequel Migrations based on the differences between two schemas}
14
- s.email = %q{roland.swingler@gmail.com}
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Roland Swingler"]
14
+ s.date = "2014-09-01"
15
+ s.description = "Build Sequel Migrations based on the differences between two schemas"
16
+ s.email = "roland.swingler@gmail.com"
15
17
  s.extra_rdoc_files = [
16
18
  "LICENSE",
17
19
  "README.rdoc"
@@ -37,13 +39,12 @@ Gem::Specification.new do |s|
37
39
  "spec/spec.opts",
38
40
  "spec/spec_helper.rb"
39
41
  ]
40
- s.homepage = %q{http://github.com/knaveofdiamonds/sequel_migration_builder}
41
- s.require_paths = [%q{lib}]
42
- s.rubygems_version = %q{1.8.6}
43
- s.summary = %q{Build Sequel Migrations based on the differences between two schemas}
42
+ s.homepage = "http://github.com/knaveofdiamonds/sequel_migration_builder"
43
+ s.rubygems_version = "2.2.1"
44
+ s.summary = "Build Sequel Migrations based on the differences between two schemas"
44
45
 
45
46
  if s.respond_to? :specification_version then
46
- s.specification_version = 3
47
+ s.specification_version = 4
47
48
 
48
49
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
50
  s.add_runtime_dependency(%q<sequel>, [">= 3.20.0"])
@@ -114,8 +114,8 @@ describe Sequel::Schema::DbColumn do
114
114
 
115
115
  it "should be buildable from a Hash" do
116
116
  Sequel::Schema::DbColumn.build_from_hash(:name => "foo",
117
- :column_type => "integer").column_type.should == "integer"
117
+ :column_type => "integer").column_type.should == "integer"
118
118
  Sequel::Schema::DbColumn.build_from_hash('name' => "foo",
119
- 'column_type' => "integer").name.should == "foo"
119
+ 'column_type' => "integer").name.should == "foo"
120
120
  end
121
121
  end
@@ -2,14 +2,14 @@ require File.dirname(__FILE__) + "/spec_helper"
2
2
 
3
3
  describe "Sequel::Schema::DbSchemaParser.for_db" do
4
4
  it "should return a DbSchemaParser" do
5
- Sequel::Schema::DbSchemaParser.for_db(stub(:database)).should \
5
+ Sequel::Schema::DbSchemaParser.for_db(double(:database)).should \
6
6
  be_kind_of(Sequel::Schema::DbSchemaParser)
7
7
  end
8
8
  end
9
9
 
10
10
  describe "A hash in the array returned by Sequel::Schema::DbSchemaParser#parse_table_schema" do
11
11
  before :each do
12
- @parser = Sequel::Schema::DbSchemaParser.for_db(stub(:database))
12
+ @parser = Sequel::Schema::DbSchemaParser.for_db(double(:database))
13
13
  @schema = [[:example_column,
14
14
  { :type => :integer,
15
15
  :default => "1",
@@ -116,7 +116,7 @@ end
116
116
 
117
117
  describe "Sequel::Schema::DbSchemaParser#parse_db_schema" do
118
118
  it "should extract a list of table definitions from a database" do
119
- mock_db = mock(:db)
119
+ mock_db = double(:db)
120
120
  mock_db.should_receive(:tables).at_least(:once).and_return([:table1])
121
121
  mock_db.should_receive(:schema).with(:table1).and_return([])
122
122
  mock_db.should_receive(:indexes).with(:table1, :partial => true)
@@ -130,7 +130,7 @@ end
130
130
 
131
131
  describe "Parsing a text column" do
132
132
  it "should not raise an error because it does not have a size" do
133
- parser = Sequel::Schema::DbSchemaParser.for_db(stub(:database))
133
+ parser = Sequel::Schema::DbSchemaParser.for_db(double(:database))
134
134
  schema = [[:example_column,
135
135
  { :type => :string,
136
136
  :default => nil,
@@ -145,7 +145,7 @@ end
145
145
 
146
146
  describe "Parsing an enum column" do
147
147
  it "should not raise an error when enum values contains brackets" do
148
- parser = Sequel::Schema::DbSchemaParser.for_db(stub(:database))
148
+ parser = Sequel::Schema::DbSchemaParser.for_db(double(:database))
149
149
  schema = [[:example_column,
150
150
  { :type => :enum,
151
151
  :default => nil,
@@ -154,11 +154,11 @@ describe "Parsing an enum column" do
154
154
  :db_type => "enum('foo (bar)', 'baz')",
155
155
  :allow_null => true }]]
156
156
 
157
- lambda { parser.parse_table_schema(schema) }.should_not raise_error(SyntaxError)
157
+ lambda { parser.parse_table_schema(schema) }.should_not raise_error
158
158
  end
159
159
 
160
160
  it "should correctly parse elements with escaped '' in them" do
161
- parser = Sequel::Schema::DbSchemaParser.for_db(stub(:database))
161
+ parser = Sequel::Schema::DbSchemaParser.for_db(double(:database))
162
162
  schema = [[:example_column,
163
163
  { :type => :enum,
164
164
  :default => nil,
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + "/spec_helper"
3
3
  describe Sequel::MigrationBuilder do
4
4
 
5
5
  it "should return nil if the table hash is empty and the database has no tables" do
6
- mock_db = mock(:database)
6
+ mock_db = double(:database)
7
7
  mock_db.should_receive(:tables).at_least(:once).and_return([])
8
8
  Sequel::MigrationBuilder.new(mock_db).generate_migration({}).should be_nil
9
9
  end
@@ -24,7 +24,7 @@ Sequel.migration do
24
24
  end
25
25
  END
26
26
 
27
- mock_db = mock(:database)
27
+ mock_db = double(:database)
28
28
  mock_db.should_receive(:tables).at_least(:once).and_return([])
29
29
  Sequel::MigrationBuilder.new(mock_db).generate_migration(tables).should == expected
30
30
  end
@@ -54,13 +54,13 @@ Sequel.migration do
54
54
  end
55
55
  END
56
56
 
57
- mock_db = mock(:database)
57
+ mock_db = double(:database)
58
58
  mock_db.should_receive(:tables).at_least(:once).and_return([])
59
59
  Sequel::MigrationBuilder.new(mock_db).generate_migration(tables).should == expected
60
60
  end
61
61
 
62
62
  it "should add the primary key of the table" do
63
- mock_db = mock(:database)
63
+ mock_db = double(:database)
64
64
  mock_db.should_receive(:tables).at_least(:once).and_return([])
65
65
  table = {
66
66
  :primary_key => :foo,
@@ -79,7 +79,7 @@ END
79
79
  end
80
80
 
81
81
  it "should add the non-integer primary key of the table" do
82
- mock_db = mock(:database)
82
+ mock_db = double(:database)
83
83
  mock_db.should_receive(:tables).at_least(:once).and_return([])
84
84
  table = {
85
85
  :primary_key => :foo,
@@ -100,7 +100,7 @@ END
100
100
  end
101
101
 
102
102
  it "should add the table options do the create_table statement" do
103
- mock_db = mock(:database)
103
+ mock_db = double(:database)
104
104
  mock_db.should_receive(:tables).at_least(:once).and_return([])
105
105
  table = {
106
106
  :table_options => {:engine => "myisam"},
@@ -118,7 +118,7 @@ END
118
118
  end
119
119
 
120
120
  it "should add indexes to the create_table statement" do
121
- mock_db = mock(:database)
121
+ mock_db = double(:database)
122
122
  mock_db.should_receive(:tables).at_least(:once).and_return([])
123
123
  table = {
124
124
  :indexes => {:foo_index => {:columns => :foo, :unique => true}},
@@ -143,7 +143,7 @@ END
143
143
  { :indexes => {:foo_index => {:columns => :foo, :unique => true}},
144
144
  :columns => [{:name => :foo, :column_type => :integer}, {:name => :bar, :column_type => :varchar}]}
145
145
  }
146
- @mock_db = mock(:database)
146
+ @mock_db = double(:database)
147
147
  @mock_db.should_receive(:tables).at_least(:once).and_return([:example_table])
148
148
  @mock_db.should_receive(:indexes).with(:example_table, :partial => true).and_return({})
149
149
  @mock_db.should_receive(:schema).with(:example_table).and_return([[:foo, {:type => :integer, :db_type => "smallint(5) unsigned", :allow_null => true, :ruby_default => 10}]])
@@ -164,11 +164,56 @@ END
164
164
  Sequel::MigrationBuilder.new(@mock_db).
165
165
  generate_migration_body(@tables).join("\n").should == expected.strip
166
166
  end
167
+
168
+ it "should return separate alter table statements when option is set" do
169
+ expected = <<-END
170
+ change do
171
+ alter_table :example_table do
172
+ set_column_type :foo, :integer, :default => nil
173
+ end
174
+
175
+ alter_table :example_table do
176
+ set_column_allow_null :foo, false
177
+ end
178
+
179
+ alter_table :example_table do
180
+ add_column :bar, :varchar, :null => false
181
+ end
182
+
183
+ alter_table :example_table do
184
+ add_index :foo, :name => :foo_index, :unique => true
185
+ end
186
+ end
187
+ END
188
+ Sequel::MigrationBuilder.new(@mock_db, :separate_alter_table_statements => true).
189
+ generate_migration_body(@tables).join("\n").should == expected.strip
190
+ end
191
+
192
+ it "should drop and add columns instead of changing them if immutable_columns is set" do
193
+ tables = { :example_table =>
194
+ { :indexes => nil,
195
+ :columns => [{:name => :foo, :column_type => :integer}] }
196
+ }
197
+
198
+ expected = <<-END
199
+ change do
200
+ alter_table :example_table do
201
+ drop_column :foo
202
+ end
203
+
204
+ alter_table :example_table do
205
+ add_column :foo, :integer, :null => false
206
+ end
207
+ end
208
+ END
209
+ Sequel::MigrationBuilder.new(@mock_db, :separate_alter_table_statements => true, :immutable_columns => true).
210
+ generate_migration_body(tables).join("\n").should == expected.strip
211
+ end
167
212
  end
168
213
 
169
214
  it "should drop the table if the table exists in the database but not the table hash" do
170
215
  pending # Deal with in a later version.
171
- mock_db = mock(:database)
216
+ mock_db = double(:database)
172
217
  mock_db.should_receive(:tables).at_least(:once).and_return([:example_table])
173
218
 
174
219
  expected = <<-END
data/spec/spec_helper.rb CHANGED
@@ -3,9 +3,8 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
  require 'sequel'
5
5
  require 'sequel/migration_builder'
6
- require 'spec'
7
- require 'spec/autorun'
6
+ require 'rspec'
8
7
 
9
- Spec::Runner.configure do |config|
10
-
8
+
9
+ RSpec.configure do |config|
11
10
  end
metadata CHANGED
@@ -1,64 +1,51 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sequel_migration_builder
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 2
10
- version: 0.3.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Roland Swingler
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2012-05-11 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: sequel
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 87
29
- segments:
30
- - 3
31
- - 20
32
- - 0
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
33
19
  version: 3.20.0
34
20
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: rspec
38
21
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 13
45
- segments:
46
- - 1
47
- - 2
48
- - 9
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.20.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
49
33
  version: 1.2.9
50
34
  type: :development
51
- version_requirements: *id002
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.9
52
41
  description: Build Sequel Migrations based on the differences between two schemas
53
42
  email: roland.swingler@gmail.com
54
43
  executables: []
55
-
56
44
  extensions: []
57
-
58
- extra_rdoc_files:
45
+ extra_rdoc_files:
59
46
  - LICENSE
60
47
  - README.rdoc
61
- files:
48
+ files:
62
49
  - .document
63
50
  - CHANGELOG
64
51
  - LICENSE
@@ -80,36 +67,25 @@ files:
80
67
  - spec/spec_helper.rb
81
68
  homepage: http://github.com/knaveofdiamonds/sequel_migration_builder
82
69
  licenses: []
83
-
70
+ metadata: {}
84
71
  post_install_message:
85
72
  rdoc_options: []
86
-
87
- require_paths:
73
+ require_paths:
88
74
  - lib
89
- required_ruby_version: !ruby/object:Gem::Requirement
90
- none: false
91
- requirements:
92
- - - ">="
93
- - !ruby/object:Gem::Version
94
- hash: 3
95
- segments:
96
- - 0
97
- version: "0"
98
- required_rubygems_version: !ruby/object:Gem::Requirement
99
- none: false
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- hash: 3
104
- segments:
105
- - 0
106
- version: "0"
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
107
85
  requirements: []
108
-
109
86
  rubyforge_project:
110
- rubygems_version: 1.8.6
87
+ rubygems_version: 2.2.1
111
88
  signing_key:
112
- specification_version: 3
89
+ specification_version: 4
113
90
  summary: Build Sequel Migrations based on the differences between two schemas
114
91
  test_files: []
115
-