active_record_inline_schema 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +8 -1
- data/README.md +9 -1
- data/lib/active_record_inline_schema/config.rb +46 -11
- data/lib/active_record_inline_schema/version.rb +1 -1
- data/spec/models.rb +7 -1
- data/spec/shared_examples.rb +14 -7
- metadata +3 -3
data/CHANGELOG
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
0.6.1 / 2013-03-15
|
2
|
+
|
3
|
+
* Enhancements
|
4
|
+
|
5
|
+
* :dry_run option that just prints info to your log
|
6
|
+
* if AR::Base#primary_key is false-y (e.g. you set Pet.primary_key = false), don't automatically create a primary key column
|
7
|
+
|
1
8
|
0.6.0 / 2012-11-29
|
2
9
|
|
3
10
|
* Breaking changes
|
@@ -123,4 +130,4 @@ After I made this list, I decided to diverge from mini_record v0.2.1 instead of
|
|
123
130
|
* automagically generate fields from associations
|
124
131
|
* revised difference checking code
|
125
132
|
|
126
|
-
Pity the forker (me)!
|
133
|
+
Pity the forker (me)!
|
data/README.md
CHANGED
@@ -4,6 +4,8 @@ Define table structure (columns and indexes) inside your ActiveRecord models lik
|
|
4
4
|
|
5
5
|
Specify columns like you would with ActiveRecord migrations and then run .auto_upgrade! Based on the mini_record gem from Davide D'Agostino, it adds fewer aliases, doesn't create timestamps and relationship columns automatically.
|
6
6
|
|
7
|
+
You don't have to be connected to the database when you run the DSL methods.
|
8
|
+
|
7
9
|
## Production use
|
8
10
|
|
9
11
|
Over 2 years in [Brighter Planet's environmental impact API](http://impact.brighterplanet.com) and [reference data service](http://data.brighterplanet.com).
|
@@ -40,6 +42,12 @@ Lots and lots of use in the [`earth` library](https://github.com/brighterplanet/
|
|
40
42
|
end
|
41
43
|
ApiResponse.auto_upgrade!
|
42
44
|
|
45
|
+
# you can also do this
|
46
|
+
ApiResponse.auto_upgrade! :dry_run => true
|
47
|
+
|
48
|
+
# this won't delete columns that it doesn't know about
|
49
|
+
ApiResponse.auto_upgrade! :gentle => true
|
50
|
+
|
43
51
|
## Credits
|
44
52
|
|
45
53
|
Massive thanks to DAddYE, who you follow on twitter [@daddye](http://twitter.com/daddye) and look at his site at [daddye.it](http://www.daddye.it)
|
@@ -55,7 +63,7 @@ Forked from [`mini_record` version v0.2.1](https://github.com/DAddYE/mini_record
|
|
55
63
|
|
56
64
|
## Copyright
|
57
65
|
|
58
|
-
Copyright
|
66
|
+
Copyright 2013 Seamus Abshere
|
59
67
|
|
60
68
|
Adapted from [mini_record](https://github.com/DAddYE/mini_record), which is copyright 2011 Davide D'Agostino
|
61
69
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'set'
|
2
|
+
require 'logger'
|
2
3
|
|
3
4
|
class ActiveRecordInlineSchema::Config
|
4
5
|
attr_reader :model
|
@@ -19,18 +20,28 @@ class ActiveRecordInlineSchema::Config
|
|
19
20
|
ideal_indexes.add Index.new(self, column_name, options)
|
20
21
|
end
|
21
22
|
|
23
|
+
def log_dry(msg)
|
24
|
+
(ActiveRecord::Base.logger || (@logger ||= Logger.new($stderr))).info "[ActiveRecordInlineSchema DRY RUN] #{msg}"
|
25
|
+
end
|
26
|
+
|
22
27
|
def apply(options)
|
23
|
-
|
24
|
-
|
28
|
+
dry_run = options.fetch(:dry_run, false)
|
29
|
+
has_primary_key = true
|
30
|
+
non_standard_primary_key = false
|
31
|
+
|
32
|
+
if !model.primary_key
|
33
|
+
has_primary_key = false
|
34
|
+
elsif (primary_key_column = find_ideal_column(model.primary_key))
|
35
|
+
non_standard_primary_key = (primary_key_column.type != :primary_key)
|
25
36
|
elsif model.primary_key != 'id'
|
26
|
-
true
|
37
|
+
non_standard_primary_key = true
|
27
38
|
end
|
28
39
|
|
29
40
|
if non_standard_primary_key
|
30
41
|
if primary_key_column and (postgresql? or sqlite?)
|
31
42
|
primary_key_column.options[:null] = false
|
32
43
|
end
|
33
|
-
|
44
|
+
elsif has_primary_key
|
34
45
|
add_ideal_column :id, :type => :primary_key
|
35
46
|
end
|
36
47
|
|
@@ -54,7 +65,11 @@ class ActiveRecordInlineSchema::Config
|
|
54
65
|
end
|
55
66
|
|
56
67
|
statements.each do |sql|
|
57
|
-
|
68
|
+
if dry_run
|
69
|
+
log_dry sql
|
70
|
+
else
|
71
|
+
connection.execute sql
|
72
|
+
end
|
58
73
|
end
|
59
74
|
safe_reset_column_information
|
60
75
|
end
|
@@ -69,7 +84,11 @@ class ActiveRecordInlineSchema::Config
|
|
69
84
|
existing_column_names.reject do |existing_column_name|
|
70
85
|
find_ideal_column existing_column_name
|
71
86
|
end.each do |existing_column_name|
|
72
|
-
|
87
|
+
if dry_run
|
88
|
+
log_dry "remove column #{model.table_name}.#{existing_column_name}"
|
89
|
+
else
|
90
|
+
connection.remove_column model.table_name, existing_column_name
|
91
|
+
end
|
73
92
|
end
|
74
93
|
end
|
75
94
|
|
@@ -77,7 +96,11 @@ class ActiveRecordInlineSchema::Config
|
|
77
96
|
ideal_columns.reject do |ideal_column|
|
78
97
|
find_existing_column ideal_column.name
|
79
98
|
end.each do |ideal_column|
|
80
|
-
|
99
|
+
if dry_run
|
100
|
+
log_dry "add column #{model.table_name}.#{ideal_column.name} #{ideal_column.type} #{ideal_column.options.inspect}"
|
101
|
+
else
|
102
|
+
connection.add_column model.table_name, ideal_column.name, ideal_column.type, ideal_column.options
|
103
|
+
end
|
81
104
|
end
|
82
105
|
|
83
106
|
# Change attributes of existent columns
|
@@ -101,7 +124,11 @@ class ActiveRecordInlineSchema::Config
|
|
101
124
|
|
102
125
|
# Change the column if applicable
|
103
126
|
if type_changed or option_changes.any?
|
104
|
-
|
127
|
+
if dry_run
|
128
|
+
log_dry "change column #{model.table_name}.#{existing_column_name} #{ideal_column.type} #{option_changes.inspect}"
|
129
|
+
else
|
130
|
+
connection.change_column model.table_name, existing_column_name, ideal_column.type, option_changes
|
131
|
+
end
|
105
132
|
end
|
106
133
|
end
|
107
134
|
|
@@ -110,7 +137,11 @@ class ActiveRecordInlineSchema::Config
|
|
110
137
|
existing_index_names.reject do |existing_index_name|
|
111
138
|
find_ideal_index existing_index_name
|
112
139
|
end.each do |existing_index_name|
|
113
|
-
|
140
|
+
if dry_run
|
141
|
+
log_dry "remove index #{model.table_name} #{existing_index_name}"
|
142
|
+
else
|
143
|
+
connection.remove_index model.table_name, :name => existing_index_name
|
144
|
+
end
|
114
145
|
end
|
115
146
|
end
|
116
147
|
|
@@ -118,7 +149,11 @@ class ActiveRecordInlineSchema::Config
|
|
118
149
|
ideal_indexes.reject do |ideal_index|
|
119
150
|
find_existing_index ideal_index.name
|
120
151
|
end.each do |ideal_index|
|
121
|
-
|
152
|
+
if dry_run
|
153
|
+
log_dry "add index #{model.table_name}.#{ideal_index.column_name} #{ideal_index.options.inspect}"
|
154
|
+
else
|
155
|
+
connection.add_index model.table_name, ideal_index.column_name, ideal_index.options
|
156
|
+
end
|
122
157
|
end
|
123
158
|
|
124
159
|
safe_reset_column_information
|
@@ -190,6 +225,6 @@ class ActiveRecordInlineSchema::Config
|
|
190
225
|
end
|
191
226
|
|
192
227
|
def postgresql?
|
193
|
-
connection.adapter_name =~ /
|
228
|
+
connection.adapter_name =~ /postgres/i
|
194
229
|
end
|
195
230
|
end
|
data/spec/models.rb
CHANGED
@@ -109,6 +109,12 @@ class Pet6 < ActiveRecord::Base
|
|
109
109
|
col :yesno, :type => :boolean
|
110
110
|
end
|
111
111
|
|
112
|
+
class Pet7 < ActiveRecord::Base
|
113
|
+
include SpecHelper
|
114
|
+
self.primary_key = false
|
115
|
+
col :name
|
116
|
+
end
|
117
|
+
|
112
118
|
case ENV['DB_ADAPTER']
|
113
119
|
when 'mysql'
|
114
120
|
class CustomMysql < ActiveRecord::Base
|
@@ -122,4 +128,4 @@ when 'postgresql'
|
|
122
128
|
col :inet, :type => 'inet'
|
123
129
|
col :bytea, :type => 'bytea'
|
124
130
|
end
|
125
|
-
end
|
131
|
+
end
|
data/spec/shared_examples.rb
CHANGED
@@ -6,12 +6,7 @@ describe ActiveRecordInlineSchema do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
describe :regressions do
|
9
|
-
def assert_unique(model, column_name, v)
|
10
|
-
e = if sqlite?
|
11
|
-
ActiveRecord::StatementInvalid
|
12
|
-
else
|
13
|
-
ActiveRecord::RecordNotUnique
|
14
|
-
end
|
9
|
+
def assert_unique(model, column_name, v, e = ActiveRecord::RecordNotUnique)
|
15
10
|
lambda do
|
16
11
|
2.times do
|
17
12
|
p = model.new
|
@@ -22,8 +17,13 @@ describe ActiveRecordInlineSchema do
|
|
22
17
|
end
|
23
18
|
|
24
19
|
it "properly creates tables with only one column, an auto-increment primary key" do
|
20
|
+
e = if sqlite?
|
21
|
+
ActiveRecord::StatementInvalid
|
22
|
+
else
|
23
|
+
ActiveRecord::RecordNotUnique
|
24
|
+
end
|
25
25
|
Pet3.auto_upgrade!
|
26
|
-
assert_unique Pet3, :id, 1
|
26
|
+
assert_unique Pet3, :id, 1, e
|
27
27
|
end
|
28
28
|
|
29
29
|
it "properly creates with only one column, a string primary key" do
|
@@ -47,6 +47,13 @@ describe ActiveRecordInlineSchema do
|
|
47
47
|
Pet6.columns_hash['yesno'].type.must_equal :boolean
|
48
48
|
end
|
49
49
|
|
50
|
+
it "doesn't force you to have a primary key" do
|
51
|
+
Pet7.auto_upgrade!
|
52
|
+
Pet7.primary_key.must_equal false
|
53
|
+
Pet7.columns_hash['id'].must_equal nil
|
54
|
+
Pet7.columns_hash['name'].type.must_equal :string
|
55
|
+
end
|
56
|
+
|
50
57
|
it "deletes unrecognized columns by default" do
|
51
58
|
Pet6.auto_upgrade!
|
52
59
|
ActiveRecord::Base.connection.add_column Pet6.table_name, 'foo', :string
|
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.6.
|
4
|
+
version: 0.6.1
|
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:
|
13
|
+
date: 2013-03-15 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 1.8.
|
96
|
+
rubygems_version: 1.8.25
|
97
97
|
signing_key:
|
98
98
|
specification_version: 3
|
99
99
|
summary: Define table structure (columns and indexes) inside your ActiveRecord models
|