mongify 0.2.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -0
- data/Gemfile.lock +1 -1
- data/README.rdoc +3 -2
- data/lib/mongify/database/column.rb +14 -9
- data/lib/mongify/translation/process.rb +0 -1
- data/lib/mongify/version.rb +1 -1
- data/spec/mongify/database/column_spec.rb +46 -13
- data/spec/support/database_generator.rb +5 -4
- data/spec/support/database_output.txt +5 -5
- metadata +3 -3
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
== 0.3 / 12 Sep 2012
|
2
|
+
* Added ability to work with :key columns that were not :integer values
|
3
|
+
* You can now specify :as value for a type :key column
|
4
|
+
* Tranlsation generator will prefill :as for :key columns
|
5
|
+
* Updated README
|
6
|
+
* Improved tests
|
7
|
+
* Small refactoring
|
1
8
|
== 0.2.2 / 11 Sep 2012
|
2
9
|
* Fix broken specs (Pranas Kiziela)
|
3
10
|
* Fix translation process when embedding multiple objects (Pranas Kiziela)
|
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -88,7 +88,7 @@ Creating a translation is pretty straight forward. It looks something like this:
|
|
88
88
|
end
|
89
89
|
|
90
90
|
table "preferences", :embed_in => :users, :as => :object do
|
91
|
-
column "id", :key
|
91
|
+
column "id", :key, :as => :string
|
92
92
|
column "user_id", :integer, :references => "users"
|
93
93
|
column "notify_by_email", :boolean
|
94
94
|
end
|
@@ -203,7 +203,7 @@ Leaving a column out when defining a table will result in the column being *igno
|
|
203
203
|
==== Types
|
204
204
|
|
205
205
|
Before we cover the options, you need to know what types of columns are supported:
|
206
|
-
:key # Columns that are primary keys need to be marked as :key type
|
206
|
+
:key # Columns that are primary keys need to be marked as :key type. You can provide an :as if your :key is not an integer column
|
207
207
|
:integer # Will be converted to a integer
|
208
208
|
:float # Will be converted to a float
|
209
209
|
:decimal # Will be converted to a string (due to MongoDB Ruby Drivers not supporting BigDecimal, read details in Mongify::Database::Column under Decimal Storage)
|
@@ -249,6 +249,7 @@ If you have any issues, please feel free to report them here: {issue tracker}[ht
|
|
249
249
|
== TODO
|
250
250
|
* Allow deeper embedding
|
251
251
|
* Test in different databases
|
252
|
+
* Give an ability to mark source DB columns as imported (allowing Mongify to run as a on going converter)
|
252
253
|
|
253
254
|
== Known Issues
|
254
255
|
* Can't do anything to an embedded table
|
@@ -16,7 +16,7 @@ module Mongify
|
|
16
16
|
# ==== Types
|
17
17
|
#
|
18
18
|
# Types of columns are supported:
|
19
|
-
# :key # Columns that are primary keys need to be marked as :key type
|
19
|
+
# :key # Columns that are primary keys need to be marked as :key type. You can provide an :as if your :key is not an integer column
|
20
20
|
# :integer # Will be converted to a integer
|
21
21
|
# :float # Will be converted to a float
|
22
22
|
# :decimal # Will be converted to a string *(you can change default behaviour read below)
|
@@ -86,9 +86,10 @@ module Mongify
|
|
86
86
|
def self.auto_detect(column)
|
87
87
|
case column.sql_name.downcase
|
88
88
|
when 'id'
|
89
|
-
column.
|
89
|
+
column.as = column.type
|
90
|
+
column.type = :key
|
90
91
|
when /(.*)_id/
|
91
|
-
column.references = $1.to_s.pluralize unless column.referenced?
|
92
|
+
column.references = $1.to_s.pluralize unless column.referenced?
|
92
93
|
end
|
93
94
|
end
|
94
95
|
|
@@ -122,7 +123,7 @@ module Mongify
|
|
122
123
|
return {} if ignored?
|
123
124
|
case type
|
124
125
|
when :key
|
125
|
-
{"pre_mongified_id" => value
|
126
|
+
{"pre_mongified_id" => type_cast(value)}
|
126
127
|
else
|
127
128
|
{"#{self.name}" => type_cast(value)}
|
128
129
|
end
|
@@ -132,7 +133,10 @@ module Mongify
|
|
132
133
|
# Mainly used during print out of translation file
|
133
134
|
def to_print
|
134
135
|
"column \"#{sql_name}\", :#{type}".tap do |output|
|
135
|
-
output_options = options.map
|
136
|
+
output_options = options.map do |k, v|
|
137
|
+
next if v.nil?
|
138
|
+
":#{k} => #{v.is_a?(Symbol) ? ":#{v}" : %Q["#{v}"] }"
|
139
|
+
end.compact
|
136
140
|
output << ", #{output_options.join(', ')}" unless output_options.blank?
|
137
141
|
end
|
138
142
|
end
|
@@ -197,17 +201,17 @@ module Mongify
|
|
197
201
|
# Used when trying to figure out how to convert a decimal value
|
198
202
|
# @return [String] passed option['as'] or defaults to 'string'
|
199
203
|
def as
|
200
|
-
options['as'] ||=
|
204
|
+
options['as'] ||= :string
|
201
205
|
end
|
202
206
|
# Sets option['as'] to either 'string' or 'integer', defults to 'string' for unknown values
|
203
207
|
# @param [String|Symbol] value, can be either 'string' or 'integer
|
204
208
|
def as=(value)
|
205
|
-
value = value.to_s.downcase
|
206
|
-
options['as'] = [
|
209
|
+
value = value.to_s.downcase.to_sym
|
210
|
+
options['as'] = [:string, :integer].include?(value) ? value : :string
|
207
211
|
end
|
208
212
|
# Returns true if :as was passed as integer
|
209
213
|
def as_integer?
|
210
|
-
self.as ==
|
214
|
+
self.as == :integer
|
211
215
|
end
|
212
216
|
|
213
217
|
# Get the scale option for decimal to integer conversion
|
@@ -230,6 +234,7 @@ module Mongify
|
|
230
234
|
def type_cast(value)
|
231
235
|
return nil if value.nil?
|
232
236
|
case type
|
237
|
+
when :key then options['as'] == :string ? value.to_s : value.to_i #If :as is provided, check if it's string, otherwise integer
|
233
238
|
when :string then value.to_s
|
234
239
|
when :text then value.to_s
|
235
240
|
when :integer then value.to_i
|
@@ -142,7 +142,6 @@ module Mongify
|
|
142
142
|
Mongify::Status.publish('remove_pre_mongified', :size => 1, :name => "Removing pre_mongified_id #{t.name}", :action => 'add')
|
143
143
|
no_sql_connection.remove_pre_mongified_ids(t.name)
|
144
144
|
Mongify::Status.publish('remove_pre_mongified', :action => 'finish')
|
145
|
-
# Mongify::Status.publish('remove_pre_mongified')
|
146
145
|
end
|
147
146
|
end
|
148
147
|
|
data/lib/mongify/version.rb
CHANGED
@@ -41,17 +41,26 @@ describe Mongify::Database::Column do
|
|
41
41
|
|
42
42
|
context "id column" do
|
43
43
|
before(:each) do
|
44
|
-
@col = mock(:sql_name => 'id'
|
44
|
+
@col = mock(:sql_name => 'id')
|
45
45
|
end
|
46
46
|
it "should detect column with type :integer as a :key column" do
|
47
|
+
@col.stub(:type).and_return(:integer)
|
47
48
|
@col.should_receive('type=').with(:key)
|
49
|
+
@col.should_receive('as=').with(:integer)
|
48
50
|
Mongify::Database::Column.auto_detect(@col)
|
49
51
|
end
|
50
|
-
it "should
|
51
|
-
@
|
52
|
-
@
|
53
|
-
|
52
|
+
it "should detected as a :key even if type is :string" do
|
53
|
+
@column = Mongify::Database::Column.new('id', :string, :auto_detect => true)
|
54
|
+
@column.should be_key
|
55
|
+
@column.as.should == :string
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should detect as a :key with as == integer " do
|
59
|
+
@column = Mongify::Database::Column.new('id', :integer, :auto_detect => true)
|
60
|
+
@column.should be_key
|
61
|
+
@column.as.should == :integer
|
54
62
|
end
|
63
|
+
|
55
64
|
end
|
56
65
|
context "references" do
|
57
66
|
before(:each) do
|
@@ -66,9 +75,9 @@ describe Mongify::Database::Column do
|
|
66
75
|
@col.should_receive('references=').never
|
67
76
|
Mongify::Database::Column.auto_detect(@col)
|
68
77
|
end
|
69
|
-
it "should
|
78
|
+
it "should detect column references even if column type is not :integer" do
|
70
79
|
@col.stub(:type).and_return(:string)
|
71
|
-
@col.should_receive('references=').
|
80
|
+
@col.should_receive('references=').once
|
72
81
|
Mongify::Database::Column.auto_detect(@col)
|
73
82
|
end
|
74
83
|
end
|
@@ -130,17 +139,26 @@ describe Mongify::Database::Column do
|
|
130
139
|
context "as" do
|
131
140
|
subject {Mongify::Database::Column.new('total', :decimal)}
|
132
141
|
it "should default to string" do
|
133
|
-
subject.as.should ==
|
142
|
+
subject.as.should == :string
|
134
143
|
end
|
135
144
|
it "should allow it to be set to integer" do
|
136
|
-
subject.as =
|
145
|
+
subject.as = :integer
|
137
146
|
subject.should be_as_integer
|
138
147
|
end
|
139
148
|
it "should not allow other values" do
|
140
149
|
subject.as = "zuza"
|
141
|
-
subject.as.should ==
|
150
|
+
subject.as.should == :string
|
151
|
+
end
|
152
|
+
it "should not allow other values even as sym" do
|
153
|
+
subject.as = :zuza
|
154
|
+
subject.as.should == :string
|
155
|
+
end
|
156
|
+
it "should convert to symble" do
|
157
|
+
subject.as = 'integer'
|
158
|
+
subject.as.should == :integer
|
142
159
|
end
|
143
160
|
end
|
161
|
+
|
144
162
|
context "scale" do
|
145
163
|
subject {Mongify::Database::Column.new('total', :decimal, :as => 'integer')}
|
146
164
|
it "should be defaulted to 0" do
|
@@ -170,6 +188,12 @@ describe Mongify::Database::Column do
|
|
170
188
|
@column = Mongify::Database::Column.new('user_id', :integer, :auto_detect => true)
|
171
189
|
@column.to_print.should == %Q[column "user_id", :integer, :references => "users"]
|
172
190
|
end
|
191
|
+
|
192
|
+
it "should print :key with :as" do
|
193
|
+
@column.as = :integer
|
194
|
+
@column.type = :key
|
195
|
+
@column.to_print.should == %q{column "first_name", :key, :as => :integer}
|
196
|
+
end
|
173
197
|
end
|
174
198
|
|
175
199
|
context :referenced? do
|
@@ -194,9 +218,18 @@ describe Mongify::Database::Column do
|
|
194
218
|
@column = Mongify::Database::Column.new('id', :key)
|
195
219
|
@column.translate(123123).should == {"pre_mongified_id" => 123123}
|
196
220
|
end
|
197
|
-
it "should return an integer for pre_mongified_id" do
|
221
|
+
it "should return an integer for pre_mongified_id (by default)" do
|
198
222
|
@column = Mongify::Database::Column.new('id', :key)
|
199
|
-
@column.translate('123123')
|
223
|
+
result = @column.translate('123123')
|
224
|
+
result.should == {"pre_mongified_id" => 123123}
|
225
|
+
result['pre_mongified_id'].should be_a_kind_of Integer
|
226
|
+
|
227
|
+
end
|
228
|
+
it "should return a string for pre_mongified_id when :as => :string is provided" do
|
229
|
+
@column = Mongify::Database::Column.new('id', :key, :as => :string)
|
230
|
+
result = @column.translate('p123')
|
231
|
+
result.should == {"pre_mongified_id" => 'p123'}
|
232
|
+
result['pre_mongified_id'].should be_a_kind_of String
|
200
233
|
end
|
201
234
|
end
|
202
235
|
context :type_cast do
|
@@ -265,7 +298,7 @@ describe Mongify::Database::Column do
|
|
265
298
|
|
266
299
|
context :integer do
|
267
300
|
before(:each) do
|
268
|
-
@column = Mongify::Database::Column.new('price', :decimal, :as =>
|
301
|
+
@column = Mongify::Database::Column.new('price', :decimal, :as => :integer)
|
269
302
|
@value = 101.123455
|
270
303
|
end
|
271
304
|
it "should be as_integer" do
|
@@ -45,7 +45,8 @@ class DatabaseGenerator
|
|
45
45
|
t.timestamps
|
46
46
|
end
|
47
47
|
|
48
|
-
conn.create_table(:preferences) do |t|
|
48
|
+
conn.create_table(:preferences, :id => false) do |t|
|
49
|
+
t.string :id
|
49
50
|
t.integer :user_id
|
50
51
|
t.boolean :notify_by_email
|
51
52
|
t.timestamps
|
@@ -95,9 +96,9 @@ class DatabaseGenerator
|
|
95
96
|
{:user_id => 1, :notify_by_email => true},
|
96
97
|
{:user_id => 2, :notify_by_email => true},
|
97
98
|
{:user_id => 3, :notify_by_email => false},
|
98
|
-
].
|
99
|
-
conn.insert("INSERT INTO preferences (user_id, notify_by_email, created_at, updated_at)
|
100
|
-
VALUES (#{v[:user_id]}, '#{v[:notify_by_email]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
|
99
|
+
].each_with_index do |v, idx|
|
100
|
+
conn.insert("INSERT INTO preferences (id, user_id, notify_by_email, created_at, updated_at)
|
101
|
+
VALUES ('p#{idx+1}',#{v[:user_id]}, '#{v[:notify_by_email]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
|
101
102
|
end
|
102
103
|
|
103
104
|
#Notes
|
@@ -1,5 +1,5 @@
|
|
1
1
|
table "users" do
|
2
|
-
column "id", :key
|
2
|
+
column "id", :key, :as => :integer
|
3
3
|
column "first_name", :string
|
4
4
|
column "last_name", :string
|
5
5
|
column "created_at", :datetime
|
@@ -7,7 +7,7 @@ table "users" do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
table "posts" do
|
10
|
-
column "id", :key
|
10
|
+
column "id", :key, :as => :integer
|
11
11
|
column "title", :string
|
12
12
|
column "owner_id", :integer, :references => "owners"
|
13
13
|
column "body", :text
|
@@ -17,7 +17,7 @@ table "posts" do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
table "comments" do
|
20
|
-
column "id", :key
|
20
|
+
column "id", :key, :as => :integer
|
21
21
|
column "body", :text
|
22
22
|
column "post_id", :integer, :references => "posts"
|
23
23
|
column "user_id", :integer, :references => "users"
|
@@ -26,7 +26,7 @@ table "comments" do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
table "preferences" do
|
29
|
-
column "id", :key
|
29
|
+
column "id", :key, :as => :string
|
30
30
|
column "user_id", :integer, :references => "users"
|
31
31
|
column "notify_by_email", :boolean
|
32
32
|
column "created_at", :datetime
|
@@ -34,7 +34,7 @@ table "preferences" do
|
|
34
34
|
end
|
35
35
|
|
36
36
|
table "notes" do
|
37
|
-
column "id", :key
|
37
|
+
column "id", :key, :as => :integer
|
38
38
|
column "user_id", :integer, :references => "users"
|
39
39
|
column "notable_id", :integer, :references => "notables"
|
40
40
|
column "notable_type", :string
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -356,7 +356,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
356
356
|
version: '0'
|
357
357
|
segments:
|
358
358
|
- 0
|
359
|
-
hash:
|
359
|
+
hash: 993534914857598805
|
360
360
|
requirements: []
|
361
361
|
rubyforge_project:
|
362
362
|
rubygems_version: 1.8.23
|