active_record_inline_schema 0.5.9 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,14 @@
1
+ 0.6.0 / 2012-11-29
2
+
3
+ * Breaking changes
4
+
5
+ * auto_upgrade!'s first argument is now a generic options hash - if you want to pass create table options, use options[:create_table]
6
+
7
+ * Enhancements
8
+
9
+ * Add "gentle" mode where unrecognized columns and indexes are not dropped. Just pass :gentle => true to auto_upgrade!
10
+ * Better detect when a user specifies a non-standard primary key but doesn't actually specify any columns.
11
+
1
12
  0.5.9 / 2012-07-26
2
13
 
3
14
  * Bug fixes
@@ -21,7 +21,7 @@ module ActiveRecordInlineSchema::ActiveRecordClassMethods
21
21
  inline_schema_config.add_ideal_index column_name, options
22
22
  end
23
23
 
24
- def auto_upgrade!(create_table_options = nil)
25
- inline_schema_config.apply create_table_options
24
+ def auto_upgrade!(options = {})
25
+ inline_schema_config.apply options
26
26
  end
27
27
  end
@@ -1,10 +1,6 @@
1
1
  require 'set'
2
2
 
3
3
  class ActiveRecordInlineSchema::Config
4
- DEFAULT_CREATE_TABLE_OPTIONS = {
5
- :mysql => 'ENGINE=InnoDB'
6
- }
7
-
8
4
  attr_reader :model
9
5
  attr_reader :ideal_columns
10
6
  attr_reader :ideal_indexes
@@ -23,13 +19,15 @@ class ActiveRecordInlineSchema::Config
23
19
  ideal_indexes.add Index.new(self, column_name, options)
24
20
  end
25
21
 
26
- def apply(create_table_options)
22
+ def apply(options)
27
23
  non_standard_primary_key = if (primary_key_column = find_ideal_column(model.primary_key))
28
24
  primary_key_column.type != :primary_key
25
+ elsif model.primary_key != 'id'
26
+ true
29
27
  end
30
-
28
+
31
29
  if non_standard_primary_key
32
- if postgresql? or sqlite?
30
+ if primary_key_column and (postgresql? or sqlite?)
33
31
  primary_key_column.options[:null] = false
34
32
  end
35
33
  else
@@ -43,10 +41,8 @@ class ActiveRecordInlineSchema::Config
43
41
 
44
42
  # Table doesn't exist, create it
45
43
  unless connection.table_exists? model.table_name
46
- create_table_options ||= DEFAULT_CREATE_TABLE_OPTIONS[database_type]
47
-
48
44
  statements = []
49
- statements << "CREATE TABLE #{model.quoted_table_name} (#{table_definition.to_sql}) #{create_table_options}"
45
+ statements << "CREATE TABLE #{model.quoted_table_name} (#{table_definition.to_sql}) #{options[:create_table]}"
50
46
 
51
47
  if non_standard_primary_key
52
48
  if postgresql?
@@ -69,10 +65,12 @@ class ActiveRecordInlineSchema::Config
69
65
  end
70
66
 
71
67
  # Remove fields from db no longer in schema
72
- existing_column_names.reject do |existing_column_name|
73
- find_ideal_column existing_column_name
74
- end.each do |existing_column_name|
75
- connection.remove_column model.table_name, existing_column_name
68
+ unless options[:gentle]
69
+ existing_column_names.reject do |existing_column_name|
70
+ find_ideal_column existing_column_name
71
+ end.each do |existing_column_name|
72
+ connection.remove_column model.table_name, existing_column_name
73
+ end
76
74
  end
77
75
 
78
76
  # Add fields to db new to schema
@@ -83,9 +81,11 @@ class ActiveRecordInlineSchema::Config
83
81
  end
84
82
 
85
83
  # Change attributes of existent columns
86
- existing_columns_hash.each do |existing_column_name, existing_column|
87
- next if existing_column_name.to_s == model.primary_key.to_s
88
- ideal_column = find_ideal_column existing_column_name
84
+ existing_columns_hash.reject do |existing_column_name, existing_column|
85
+ existing_column_name.to_s == model.primary_key.to_s
86
+ end.each do |existing_column_name, existing_column|
87
+ next unless (ideal_column = find_ideal_column(existing_column_name))
88
+
89
89
  option_changes = {}
90
90
 
91
91
  # First, check if the field type changed
@@ -106,10 +106,12 @@ class ActiveRecordInlineSchema::Config
106
106
  end
107
107
 
108
108
  # Remove old index
109
- existing_index_names.reject do |existing_index_name|
110
- find_ideal_index existing_index_name
111
- end.each do |existing_index_name|
112
- connection.remove_index model.table_name, :name => existing_index_name
109
+ unless options[:gentle]
110
+ existing_index_names.reject do |existing_index_name|
111
+ find_ideal_index existing_index_name
112
+ end.each do |existing_index_name|
113
+ connection.remove_index model.table_name, :name => existing_index_name
114
+ end
113
115
  end
114
116
 
115
117
  # Add indexes
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordInlineSchema
2
- VERSION = "0.5.9"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -47,6 +47,43 @@ describe ActiveRecordInlineSchema do
47
47
  Pet6.columns_hash['yesno'].type.must_equal :boolean
48
48
  end
49
49
 
50
+ it "deletes unrecognized columns by default" do
51
+ Pet6.auto_upgrade!
52
+ ActiveRecord::Base.connection.add_column Pet6.table_name, 'foo', :string
53
+ Pet6.safe_reset_column_information
54
+ Pet6.column_names.must_include 'foo'
55
+ Pet6.auto_upgrade!
56
+ Pet6.column_names.wont_include 'foo'
57
+ end
58
+
59
+ it "deletes unrecognized columns by default" do
60
+ Pet6.auto_upgrade!
61
+ ActiveRecord::Base.connection.add_index Pet6.table_name, 'yesno', :name => 'testtest'
62
+ Pet6.safe_reset_column_information
63
+ Pet6.db_indexes.must_include 'testtest'
64
+ Pet6.auto_upgrade!
65
+ Pet6.db_indexes.wont_include 'testtest'
66
+ end
67
+
68
+ it "doesn't delete unrecognized columns in gentle mode" do
69
+ Pet6.auto_upgrade!
70
+ ActiveRecord::Base.connection.add_column Pet6.table_name, 'foo', :string
71
+ Pet6.safe_reset_column_information
72
+ Pet6.column_names.must_include 'foo'
73
+ Pet6.auto_upgrade! :gentle => true
74
+ Pet6.column_names.must_include 'foo'
75
+ Pet6.columns_hash['foo'].type.must_equal :string
76
+ end
77
+
78
+ it "doesn't delete unrecognized columns in gentle mode" do
79
+ Pet6.auto_upgrade!
80
+ ActiveRecord::Base.connection.add_index Pet6.table_name, 'yesno', :name => 'testtest'
81
+ Pet6.safe_reset_column_information
82
+ Pet6.db_indexes.must_include 'testtest'
83
+ Pet6.auto_upgrade! :gentle => true
84
+ Pet6.db_indexes.must_include 'testtest'
85
+ end
86
+
50
87
  it 'has #key,col,property,attribute inside model' do
51
88
  (!!ActiveRecord::Base.connection.table_exists?(Post.table_name)).must_equal false
52
89
  (!!ActiveRecord::Base.connection.table_exists?(Category.table_name)).must_equal false
data/spec/spec_helper.rb CHANGED
@@ -10,8 +10,6 @@ end
10
10
  require 'minitest/spec'
11
11
  require 'minitest/autorun'
12
12
  require 'minitest/reporters'
13
- MiniTest::Unit.runner = MiniTest::SuiteRunner.new
14
- MiniTest::Unit.runner.reporters << MiniTest::Reporters::SpecReporter.new
15
13
 
16
14
  require 'active_record_inline_schema'
17
15
 
data/spec/sqlite3_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require File.expand_path('../spec_helper.rb', __FILE__)
2
2
 
3
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:', :pool => 32)
3
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
4
4
 
5
5
  # require 'logger'
6
6
  # ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new($stdout)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_inline_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-26 00:00:00.000000000 Z
13
+ date: 2012-11-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport