mongify 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +6 -0
- data/Gemfile.lock +8 -12
- data/README.rdoc +49 -15
- data/features/process.feature +1 -1
- data/lib/mongify/database/column.rb +20 -7
- data/lib/mongify/database/no_sql_connection.rb +1 -2
- data/lib/mongify/database/table.rb +20 -12
- data/lib/mongify/translation.rb +13 -14
- data/lib/mongify/translation/printer.rb +1 -1
- data/lib/mongify/translation/process.rb +4 -8
- data/lib/mongify/version.rb +1 -1
- data/spec/files/simple_translation.rb +3 -3
- data/spec/mongify/database/column_spec.rb +34 -7
- data/spec/mongify/database/no_sql_connection_spec.rb +1 -1
- data/spec/mongify/database/table_spec.rb +34 -2
- data/spec/mongify/translation/process_spec.rb +22 -8
- data/spec/mongify/translation_spec.rb +11 -4
- metadata +4 -4
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 0.0.9 / 19 Jan 2011
|
2
|
+
* Added ability to rename tables
|
3
|
+
* Fixed bug 'pre_mogified_id' not being removed in all records
|
4
|
+
* Added ability to ignore tables
|
5
|
+
* Added ability to ignore columns
|
6
|
+
* Added ability to rename columns
|
1
7
|
== 0.0.8 / 18 Jan 2011
|
2
8
|
* Added ability to embed as object on an embedded table
|
3
9
|
* Some bugs fixed
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mongify (0.0.
|
4
|
+
mongify (0.0.9)
|
5
5
|
activerecord (>= 2.3.10)
|
6
6
|
activesupport (>= 2.3.10)
|
7
7
|
bson_ext (>= 1.1.5)
|
@@ -15,8 +15,8 @@ GEM
|
|
15
15
|
activerecord (2.3.10)
|
16
16
|
activesupport (= 2.3.10)
|
17
17
|
activesupport (2.3.10)
|
18
|
-
bson (1.
|
19
|
-
bson_ext (1.
|
18
|
+
bson (1.2.0)
|
19
|
+
bson_ext (1.2.0)
|
20
20
|
builder (3.0.0)
|
21
21
|
cucumber (0.10.0)
|
22
22
|
builder (>= 2.1.2)
|
@@ -30,8 +30,8 @@ GEM
|
|
30
30
|
json (1.4.6)
|
31
31
|
mocha (0.9.10)
|
32
32
|
rake
|
33
|
-
mongo (1.
|
34
|
-
bson (>= 1.
|
33
|
+
mongo (1.2.0)
|
34
|
+
bson (>= 1.2.0)
|
35
35
|
mysql (2.8.1)
|
36
36
|
mysql2 (0.2.6)
|
37
37
|
net-ssh (2.0.24)
|
@@ -45,7 +45,9 @@ GEM
|
|
45
45
|
rspec-expectations (2.4.0)
|
46
46
|
diff-lcs (~> 1.1.2)
|
47
47
|
rspec-mocks (2.4.0)
|
48
|
-
sqlite3
|
48
|
+
sqlite3 (1.3.3)
|
49
|
+
sqlite3-ruby (1.3.3)
|
50
|
+
sqlite3 (>= 1.3.3)
|
49
51
|
term-ansicolor (1.0.5)
|
50
52
|
watchr (0.7)
|
51
53
|
yard (0.6.4)
|
@@ -54,16 +56,10 @@ PLATFORMS
|
|
54
56
|
ruby
|
55
57
|
|
56
58
|
DEPENDENCIES
|
57
|
-
activerecord (>= 2.3.10)
|
58
|
-
activesupport (>= 2.3.10)
|
59
|
-
bson_ext (>= 1.1.5)
|
60
59
|
cucumber (>= 0.10)
|
61
60
|
mocha (>= 0.9.8)
|
62
61
|
mongify!
|
63
|
-
mongo (>= 1.1.5)
|
64
62
|
mysql (>= 2.8.1)
|
65
|
-
mysql2
|
66
|
-
net-ssh (>= 2.0)
|
67
63
|
rcov (>= 0.9.9)
|
68
64
|
rspec (>= 2.0)
|
69
65
|
sqlite3-ruby (>= 1.3)
|
data/README.rdoc
CHANGED
@@ -90,12 +90,21 @@ Save the file as <tt>"translation_file.rb"</tt> and run the command:
|
|
90
90
|
|
91
91
|
mongify process translation_file.rb -c database.config
|
92
92
|
|
93
|
-
== Translation Options
|
93
|
+
== Translation Layout and Options
|
94
94
|
|
95
95
|
When dealing with a translation, there is a few options you can set
|
96
96
|
|
97
97
|
=== Table
|
98
98
|
|
99
|
+
==== Structure
|
100
|
+
|
101
|
+
Structure for defining a table is as follows:
|
102
|
+
table "table_name", {options} do
|
103
|
+
# columns go here...
|
104
|
+
end
|
105
|
+
|
106
|
+
==== Options
|
107
|
+
|
99
108
|
Table Options are as follow:
|
100
109
|
table "table_name" # Does a straight copy of the table
|
101
110
|
table "table_name", :embed_in => :users # Embeds table_name into users, assuming a user_id is present in table_name.
|
@@ -104,20 +113,47 @@ Table Options are as follow:
|
|
104
113
|
# This will also assume you want the table embedded as an array.
|
105
114
|
table "table_name", :embed_in => :users, :as => :object # Embeds table_name into users as a one to one relationship
|
106
115
|
# This also assumes you have a user_id present in table_name
|
107
|
-
<em>You can also specify both
|
116
|
+
<em>You can also specify both <b>:on</b> and <b>:as</b> options when embedding</em>
|
117
|
+
|
118
|
+
table "table_name", :rename_to => 'my_table' # This will allow you to rename the table as it's getting process
|
119
|
+
# Just remember that columns that use :reference need to
|
120
|
+
# reference the new name.
|
121
|
+
|
122
|
+
table "table_name", :ignore => true # This will ignore the whole table (like it doesn't exist)
|
123
|
+
# This option is good for tables like: schema_migrations
|
108
124
|
|
109
125
|
=== Columns
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
126
|
+
|
127
|
+
==== Structure
|
128
|
+
|
129
|
+
Structure for defining a column is as follows:
|
130
|
+
column "name", :type, {options}
|
131
|
+
<em>Columns with no type given will be set to <tt>:string</tt></em>
|
132
|
+
|
133
|
+
==== Notes
|
134
|
+
Leaving a column out when defining a table will result in a copy of the information (as a string).
|
135
|
+
|
136
|
+
==== Types
|
137
|
+
|
138
|
+
Before we cover the options, you need to know what types of columns are supported:
|
139
|
+
:key # Columns that are primary keys need to be marked as :key type
|
140
|
+
:integer # Will be converted to a integer
|
141
|
+
:float # Will be converted to a float
|
142
|
+
:decimal # Will be converted to a BigDecimal
|
143
|
+
:string # Will be converted to a string
|
144
|
+
:text # Will be converted to a string
|
145
|
+
:datetime # Specifying a field as :datetime will convert it to Time format as it gets imported into MongoDB
|
146
|
+
:binary # Will be converted to a string
|
147
|
+
|
148
|
+
==== Options
|
149
|
+
|
114
150
|
column "post_id", :integer, :referneces => :posts # Referenced columns need to be marked as such, this will mean that they will be updated
|
115
151
|
# with the new BSON::ObjectID.
|
116
|
-
|
117
|
-
|
118
|
-
column "name"
|
119
|
-
|
120
|
-
|
152
|
+
<b>NOTE: if you rename the table 'posts', you should set the :references to the new name</b>
|
153
|
+
|
154
|
+
column "name", :string, :ignore => true # Ignoring a column will make the column NOT copy over to the new database
|
155
|
+
|
156
|
+
column "surname", :string, :rename_to => 'last_name'# Rename_to allows you to rename the column
|
121
157
|
|
122
158
|
<em>**this is subject to change during the alpha (0.0.x release)**</em>
|
123
159
|
|
@@ -135,9 +171,7 @@ More testing will be done once the basic functionality is done
|
|
135
171
|
|
136
172
|
=== while processing
|
137
173
|
|
138
|
-
*
|
139
|
-
* Allow renaming of columns
|
140
|
-
* Allow deleting of columns
|
174
|
+
* Work with polymorphic tables
|
141
175
|
* Allow running a proc on column value
|
142
176
|
* Update user on what is being processed
|
143
177
|
|
@@ -146,7 +180,7 @@ More testing will be done once the basic functionality is done
|
|
146
180
|
|
147
181
|
=== Requirements
|
148
182
|
|
149
|
-
You just need bundler 1.0
|
183
|
+
You just need bundler 1.0.8
|
150
184
|
|
151
185
|
gem install bundler
|
152
186
|
bundle install
|
data/features/process.feature
CHANGED
@@ -11,7 +11,7 @@ Feature: Processing a translation
|
|
11
11
|
Then it succeeds
|
12
12
|
And there should be 3 users in mongodb
|
13
13
|
And there should be 3 posts in mongodb
|
14
|
-
And the first post's
|
14
|
+
And the first post's user_id should be first user
|
15
15
|
And there should be 0 comments in mongodb
|
16
16
|
And the first post should have 1 comment
|
17
17
|
|
@@ -5,25 +5,30 @@ module Mongify
|
|
5
5
|
# A column in the sql table
|
6
6
|
#
|
7
7
|
class Column
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :sql_name, :type, :options
|
9
9
|
|
10
|
-
AVAILABLE_OPTIONS = ['references', '
|
10
|
+
AVAILABLE_OPTIONS = ['references', 'ignore', 'rename_to']
|
11
11
|
|
12
|
-
def initialize(
|
13
|
-
@
|
12
|
+
def initialize(sql_name, type=:string, options={})
|
13
|
+
@sql_name = sql_name
|
14
14
|
type = :string if type.nil?
|
15
15
|
@type = type.is_a?(Symbol) ? type : type.to_sym
|
16
|
-
@options =
|
16
|
+
@options = options.stringify_keys
|
17
17
|
|
18
18
|
auto_detect!
|
19
19
|
|
20
20
|
self
|
21
21
|
end
|
22
22
|
|
23
|
+
def name
|
24
|
+
@name ||= rename_to || sql_name
|
25
|
+
end
|
26
|
+
|
23
27
|
def translate(value)
|
28
|
+
return {} if ignored?
|
24
29
|
case type
|
25
30
|
when :key
|
26
|
-
{"
|
31
|
+
{"pre_mongified_id" => value}
|
27
32
|
else
|
28
33
|
{"#{name}" => type_cast(value)}
|
29
34
|
end
|
@@ -53,10 +58,14 @@ module Mongify
|
|
53
58
|
end
|
54
59
|
end
|
55
60
|
|
56
|
-
def
|
61
|
+
def referenced?
|
57
62
|
!self.options['references'].nil?
|
58
63
|
end
|
59
64
|
|
65
|
+
def renamed?
|
66
|
+
self.name != self.sql_name
|
67
|
+
end
|
68
|
+
|
60
69
|
#######
|
61
70
|
private
|
62
71
|
#######
|
@@ -82,6 +91,10 @@ module Mongify
|
|
82
91
|
def key?
|
83
92
|
self.type == :key
|
84
93
|
end
|
94
|
+
|
95
|
+
def ignored?
|
96
|
+
!!self.ignore
|
97
|
+
end
|
85
98
|
|
86
99
|
def auto_detect!
|
87
100
|
case name.downcase
|
@@ -57,10 +57,9 @@ module Mongify
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def remove_pre_mongified_ids(collection_name)
|
60
|
-
db[collection_name].update({}, { '$unset' => { 'pre_mongified_id' => 1} })
|
60
|
+
db[collection_name].update({}, { '$unset' => { 'pre_mongified_id' => 1} }, :multi => true)
|
61
61
|
end
|
62
62
|
|
63
|
-
|
64
63
|
def reset!
|
65
64
|
@connection = nil
|
66
65
|
@db = nil
|
@@ -5,14 +5,14 @@ module Mongify
|
|
5
5
|
#
|
6
6
|
class Table
|
7
7
|
|
8
|
-
attr_accessor :name
|
8
|
+
attr_accessor :name, :sql_name
|
9
9
|
attr_reader :options, :columns
|
10
10
|
|
11
|
-
def initialize(
|
11
|
+
def initialize(sql_name, options={}, &block)
|
12
12
|
@columns = []
|
13
13
|
@column_lookup = {}
|
14
14
|
@options = options.stringify_keys
|
15
|
-
self.
|
15
|
+
self.sql_name = sql_name
|
16
16
|
|
17
17
|
self.instance_exec(&block) if block_given?
|
18
18
|
|
@@ -21,20 +21,26 @@ module Mongify
|
|
21
21
|
self
|
22
22
|
end
|
23
23
|
|
24
|
+
def name
|
25
|
+
@name ||= @options['rename_to']
|
26
|
+
@name ||= self.sql_name
|
27
|
+
end
|
28
|
+
|
29
|
+
def ignored?
|
30
|
+
@options['ignore']
|
31
|
+
end
|
32
|
+
|
24
33
|
#Add a Database Column
|
25
34
|
def add_column(column)
|
26
35
|
raise Mongify::DatabaseColumnExpected, "Expected a Mongify::Database::Column" unless column.is_a?(Mongify::Database::Column)
|
27
|
-
|
28
|
-
@columns << column
|
36
|
+
add_and_index_column(column)
|
29
37
|
end
|
30
38
|
|
31
39
|
|
32
40
|
def column(name, type=nil, options={})
|
33
|
-
options
|
41
|
+
options, type = type, nil if type.is_a?(Hash)
|
34
42
|
type = type.to_sym if type
|
35
|
-
|
36
|
-
@columns << (col = Mongify::Database::Column.new(name, type, options))
|
37
|
-
col
|
43
|
+
add_and_index_column(Mongify::Database::Column.new(name, type, options))
|
38
44
|
end
|
39
45
|
|
40
46
|
def find_column(name)
|
@@ -44,7 +50,7 @@ module Mongify
|
|
44
50
|
|
45
51
|
|
46
52
|
def reference_columns
|
47
|
-
@columns.reject{ |c| !c.
|
53
|
+
@columns.reject{ |c| !c.referenced? }
|
48
54
|
end
|
49
55
|
|
50
56
|
def translate(row)
|
@@ -83,8 +89,10 @@ module Mongify
|
|
83
89
|
private
|
84
90
|
#######
|
85
91
|
|
86
|
-
def
|
87
|
-
@column_lookup[
|
92
|
+
def add_and_index_column(column)
|
93
|
+
@column_lookup[column.sql_name] = @columns.size
|
94
|
+
@columns << column
|
95
|
+
column
|
88
96
|
end
|
89
97
|
|
90
98
|
def import_columns
|
data/lib/mongify/translation.rb
CHANGED
@@ -5,8 +5,6 @@ module Mongify
|
|
5
5
|
# Actually runs the translation from sql to no sql
|
6
6
|
#
|
7
7
|
class Translation
|
8
|
-
attr_reader :tables, :sql_connection, :no_sql_connection
|
9
|
-
|
10
8
|
include Printer
|
11
9
|
include Process
|
12
10
|
class << self
|
@@ -18,7 +16,7 @@ module Mongify
|
|
18
16
|
|
19
17
|
def load(connection)
|
20
18
|
raise Mongify::SqlConnectionRequired, "Can only read from Mongify::Database::SqlConnection" unless connection.is_a?(Mongify::Database::SqlConnection)
|
21
|
-
return unless connection.has_connection?
|
19
|
+
return unless connection.valid? && connection.has_connection?
|
22
20
|
translation = self.new
|
23
21
|
connection.tables.each do |t|
|
24
22
|
columns = []
|
@@ -31,18 +29,25 @@ module Mongify
|
|
31
29
|
end
|
32
30
|
end
|
33
31
|
|
34
|
-
|
35
32
|
def initialize
|
36
|
-
@
|
33
|
+
@all_tables = []
|
37
34
|
end
|
38
35
|
|
39
36
|
def table(table_name, options={}, &block)
|
40
37
|
table = Mongify::Database::Table.new(table_name, options, &block)
|
41
|
-
@
|
38
|
+
@all_tables << table
|
42
39
|
end
|
43
40
|
|
44
41
|
def add_table(table)
|
45
|
-
@
|
42
|
+
@all_tables << table
|
43
|
+
end
|
44
|
+
|
45
|
+
def all_tables
|
46
|
+
@all_tables
|
47
|
+
end
|
48
|
+
|
49
|
+
def tables
|
50
|
+
all_tables.reject{ |t| t.ignored? }
|
46
51
|
end
|
47
52
|
|
48
53
|
def copy_tables
|
@@ -53,11 +58,5 @@ module Mongify
|
|
53
58
|
tables.reject{|t| !t.embed?}
|
54
59
|
end
|
55
60
|
|
56
|
-
#######
|
57
|
-
private
|
58
|
-
#######
|
59
|
-
|
60
|
-
|
61
|
-
|
62
61
|
end
|
63
|
-
end
|
62
|
+
end
|
@@ -4,12 +4,8 @@ module Mongify
|
|
4
4
|
#
|
5
5
|
class Translation
|
6
6
|
module Process
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
def no_sql_connection=(value)
|
11
|
-
@no_sql_connection=value
|
12
|
-
end
|
7
|
+
attr_accessor :sql_connection, :no_sql_connection
|
8
|
+
|
13
9
|
def process(sql_connection, no_sql_connection)
|
14
10
|
raise Mongify::SqlConnectionRequired, "Can only read from Mongify::Database::SqlConnection" unless sql_connection.is_a?(Mongify::Database::SqlConnection)
|
15
11
|
raise Mongify::NoSqlConnectionRequired, "Can only write to Mongify::Database::NoSqlConnection" unless no_sql_connection.is_a?(Mongify::Database::NoSqlConnection)
|
@@ -32,7 +28,7 @@ module Mongify
|
|
32
28
|
|
33
29
|
def copy_data
|
34
30
|
self.copy_tables.each do |t|
|
35
|
-
sql_connection.select_rows(t.
|
31
|
+
sql_connection.select_rows(t.sql_name).each do |row|
|
36
32
|
no_sql_connection.insert_into(t.name, t.translate(row))
|
37
33
|
end
|
38
34
|
end
|
@@ -40,7 +36,7 @@ module Mongify
|
|
40
36
|
|
41
37
|
def copy_embedded_tables
|
42
38
|
self.embed_tables.each do |t|
|
43
|
-
sql_connection.select_rows(t.
|
39
|
+
sql_connection.select_rows(t.sql_name).each do |row|
|
44
40
|
target_row = no_sql_connection.find_one(t.embed_in, {:pre_mongified_id => row[t.embed_on]})
|
45
41
|
next unless target_row.present?
|
46
42
|
row = t.translate(row)
|
data/lib/mongify/version.rb
CHANGED
@@ -9,7 +9,7 @@ end
|
|
9
9
|
table "posts" do
|
10
10
|
column "id", :key
|
11
11
|
column "title", :string
|
12
|
-
column "owner_id", :integer, :references => :users
|
12
|
+
column "owner_id", :integer, :references => :users, :rename_to => 'user_id'
|
13
13
|
column "body", :text
|
14
14
|
column "published_at", :datetime
|
15
15
|
column "created_at", :datetime
|
@@ -29,7 +29,7 @@ table "preferences", :embed_in => :users, :as => :object do
|
|
29
29
|
column "id", :key
|
30
30
|
column "user_id", :integer, :references => "users"
|
31
31
|
column "notify_by_email", :boolean
|
32
|
-
column "created_at", :datetime
|
33
|
-
column "updated_at", :datetime
|
32
|
+
column "created_at", :datetime, :ignore => true
|
33
|
+
column "updated_at", :datetime, :ignore => true
|
34
34
|
end
|
35
35
|
|
@@ -8,6 +8,9 @@ describe Mongify::Database::Column do
|
|
8
8
|
it "should have name" do
|
9
9
|
@column.name.should == 'first_name'
|
10
10
|
end
|
11
|
+
it "should have a sql_name" do
|
12
|
+
@column.sql_name.should == 'first_name'
|
13
|
+
end
|
11
14
|
|
12
15
|
it "should get setup options" do
|
13
16
|
@column = Mongify::Database::Column.new('account_id', :integer, :references => 'accounts')
|
@@ -19,7 +22,7 @@ describe Mongify::Database::Column do
|
|
19
22
|
@column.type.should == :string
|
20
23
|
end
|
21
24
|
|
22
|
-
context "key" do
|
25
|
+
context "key?" do
|
23
26
|
it "should be true" do
|
24
27
|
@column = Mongify::Database::Column.new('id', :key)
|
25
28
|
@column.should be_a_key
|
@@ -31,6 +34,29 @@ describe Mongify::Database::Column do
|
|
31
34
|
end
|
32
35
|
end
|
33
36
|
|
37
|
+
context "rename_to" do
|
38
|
+
before(:each) do
|
39
|
+
@column = Mongify::Database::Column.new('surname', :string, :rename_to => 'last_name')
|
40
|
+
end
|
41
|
+
it "should have the right sql_name" do
|
42
|
+
@column.sql_name.should == 'surname'
|
43
|
+
end
|
44
|
+
it "should have the right name" do
|
45
|
+
@column.name.should == 'last_name'
|
46
|
+
end
|
47
|
+
it "should translate to new name" do
|
48
|
+
@column.translate('value').should == {'last_name' => 'value'}
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be renamed" do
|
52
|
+
@column.should be_renamed
|
53
|
+
end
|
54
|
+
it "should be not renamed" do
|
55
|
+
col = Mongify::Database::Column.new('surname', :string)
|
56
|
+
col.should_not be_renamed
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
34
60
|
context "options" do
|
35
61
|
it "should allow to be set by name" do
|
36
62
|
@column = Mongify::Database::Column.new('first_name')
|
@@ -73,16 +99,12 @@ describe Mongify::Database::Column do
|
|
73
99
|
@column = Mongify::Database::Column.new('user_id', :integer)
|
74
100
|
@column.to_print.should == %Q[column "user_id", :integer, :references => "users"]
|
75
101
|
end
|
76
|
-
it "should output nil options" do
|
77
|
-
@column.default = nil
|
78
|
-
@column.to_print.should == %Q[column "first_name", :string]
|
79
|
-
end
|
80
102
|
end
|
81
103
|
|
82
|
-
context :
|
104
|
+
context :referenced? do
|
83
105
|
it "should be true" do
|
84
106
|
@column = Mongify::Database::Column.new('user_id', :integer, :references => 'users')
|
85
|
-
@column.should
|
107
|
+
@column.should be_a_referenced
|
86
108
|
end
|
87
109
|
end
|
88
110
|
|
@@ -91,6 +113,11 @@ describe Mongify::Database::Column do
|
|
91
113
|
@column = Mongify::Database::Column.new('first_name', :string)
|
92
114
|
@column.translate('bob').should == {'first_name' => 'bob'}
|
93
115
|
end
|
116
|
+
it "should ignore an ignored column" do
|
117
|
+
@column = Mongify::Database::Column.new('first_name', :string, :ignore => true)
|
118
|
+
@column.should be_ignored
|
119
|
+
@column.translate('bob').should == {}
|
120
|
+
end
|
94
121
|
context "datetime" do
|
95
122
|
it "should return a datetime format" do
|
96
123
|
@column = Mongify::Database::Column.new('created_at', :datetime)
|
@@ -120,7 +120,7 @@ describe Mongify::Database::NoSqlConnection do
|
|
120
120
|
|
121
121
|
context "remove_pre_mongified_ids" do
|
122
122
|
it "should call update with unset" do
|
123
|
-
@collection.should_receive(:update).with({},{'$unset' => {'pre_mongified_id' => 1}})
|
123
|
+
@collection.should_receive(:update).with({},{'$unset' => {'pre_mongified_id' => 1}}, {:multi=>true})
|
124
124
|
@mongodb_connection.remove_pre_mongified_ids('users')
|
125
125
|
end
|
126
126
|
end
|
@@ -8,19 +8,40 @@ describe Mongify::Database::Table do
|
|
8
8
|
it "should have name" do
|
9
9
|
@table.name.should == "users"
|
10
10
|
end
|
11
|
+
it "should have sql_name" do
|
12
|
+
@table.sql_name.should == "users"
|
13
|
+
end
|
11
14
|
it "should allow you to change table name" do
|
12
15
|
@table.name = 'accounts'
|
13
16
|
@table.name.should == 'accounts'
|
14
17
|
end
|
15
18
|
|
19
|
+
it "should be ingored" do
|
20
|
+
table = Mongify::Database::Table.new('users', :ignore => true)
|
21
|
+
table.should be_ignored
|
22
|
+
end
|
23
|
+
|
16
24
|
it "should get setup options" do
|
17
25
|
@table = Mongify::Database::Table.new('users', :embed_in => 'accounts', :as => 'users')
|
18
26
|
@table.options.should == {'embed_in' => 'accounts', 'as' => 'users'}
|
19
27
|
end
|
20
28
|
|
29
|
+
context "rename_to" do
|
30
|
+
before(:each) do
|
31
|
+
@table = Mongify::Database::Table.new('users', :rename_to => 'people')
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should have new name" do
|
35
|
+
@table.name.should == "people"
|
36
|
+
end
|
37
|
+
it "should have sql_name" do
|
38
|
+
@table.sql_name.should == "users"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
21
42
|
context "column_index (find_column)" do
|
22
43
|
it "should add column index on column creation" do
|
23
|
-
@table.should_receive(:
|
44
|
+
@table.should_receive(:add_and_index_column)
|
24
45
|
@table.column('first_name', :string)
|
25
46
|
end
|
26
47
|
end
|
@@ -31,16 +52,27 @@ describe Mongify::Database::Table do
|
|
31
52
|
end
|
32
53
|
|
33
54
|
it "should work without a type" do
|
34
|
-
col = @table.column 'name'
|
55
|
+
col = @table.column 'name'
|
35
56
|
col.type.should == :string
|
36
57
|
end
|
37
58
|
|
59
|
+
it "should work without a type with options" do
|
60
|
+
col = @table.column 'name', :rename_to => 'first_name'
|
61
|
+
col.type.should == :string
|
62
|
+
col.should be_renamed
|
63
|
+
end
|
64
|
+
|
38
65
|
it "should be able to find" do
|
39
66
|
@table.column 'another'
|
40
67
|
col = @table.column 'dark'
|
41
68
|
@table.find_column('dark').should == col
|
42
69
|
end
|
43
70
|
|
71
|
+
it "should be searchable with sql_name only" do
|
72
|
+
col = @table.column 'surname', :string, :rename_to => 'last_name'
|
73
|
+
@table.find_column('surname').should == col
|
74
|
+
end
|
75
|
+
|
44
76
|
it "should return nil if not found" do
|
45
77
|
@table.column 'dark'
|
46
78
|
@table.find_column('blue').should be_nil
|
@@ -63,22 +63,30 @@ describe Mongify::Translation::Process do
|
|
63
63
|
@no_sql_connection = mock()
|
64
64
|
@translation.stub(:no_sql_connection).and_return(@no_sql_connection)
|
65
65
|
|
66
|
-
@table = mock(:translate => {
|
66
|
+
@table = mock(:translate => {'first_name'=> 'Timmy', 'last_name' => 'Zuza', 'preference_id' => 1},
|
67
|
+
:name => 'users',
|
68
|
+
:embed? => false,
|
69
|
+
:sql_name => 'users')
|
70
|
+
|
67
71
|
@translation.stub(:tables).and_return([@table])
|
68
72
|
end
|
69
73
|
|
70
74
|
context "copy_data" do
|
71
75
|
it "should call translate on the tables" do
|
72
|
-
@no_sql_connection.should_receive(:insert_into).and_return(true)
|
73
|
-
@
|
76
|
+
@no_sql_connection.should_receive(:insert_into).with("users", {"last_name"=>"Zuza", "preference_id"=>1, "first_name"=>"Timmy"}).and_return(true)
|
77
|
+
@translation.send(:copy_data)
|
78
|
+
end
|
79
|
+
it "should allow rename of table" do
|
80
|
+
@table.stub(:name).and_return('people')
|
81
|
+
@no_sql_connection.should_receive(:insert_into).with("people", {"last_name"=>"Zuza", "preference_id"=>1, "first_name"=>"Timmy"}).and_return(true)
|
74
82
|
@translation.send(:copy_data)
|
75
83
|
end
|
76
84
|
end
|
77
85
|
|
78
86
|
context "copy_embed_tables" do
|
79
87
|
before(:each) do
|
80
|
-
@target_table = mock(:name => 'posts', :embed? => false)
|
81
|
-
@embed_table = mock(:translate => {}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => false)
|
88
|
+
@target_table = mock(:name => 'posts', :embed? => false, :sql_name => 'posts')
|
89
|
+
@embed_table = mock(:translate => {}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => false, :sql_name => 'comments')
|
82
90
|
@no_sql_connection.stub(:find_one).and_return({'_id' => 500})
|
83
91
|
@translation.stub(:tables).and_return([@target_table, @embed_table])
|
84
92
|
@translation.stub(:fetch_reference_ids).and_return({})
|
@@ -90,23 +98,29 @@ describe Mongify::Translation::Process do
|
|
90
98
|
@translation.send(:copy_embedded_tables)
|
91
99
|
end
|
92
100
|
it "should remove the pre_mongified_id before embedding" do
|
93
|
-
@embed_table = mock(:translate => {'first_name' => 'bob', 'pre_mongified_id' => 1}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => false)
|
101
|
+
@embed_table = mock(:translate => {'first_name' => 'bob', 'pre_mongified_id' => 1}, :name => 'comments', :sql_name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => false)
|
94
102
|
@translation.stub(:tables).and_return([@target_table, @embed_table])
|
95
103
|
@no_sql_connection.should_receive(:update).with("posts", 500, {"$addToSet"=>{"comments"=>{'first_name' => 'bob'}}})
|
96
104
|
@translation.send(:copy_embedded_tables)
|
97
105
|
end
|
98
106
|
it "should remove the parent_id from the embedding row" do
|
99
|
-
@embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => false)
|
107
|
+
@embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'comments', :sql_name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => false)
|
100
108
|
@translation.stub(:tables).and_return([@target_table, @embed_table])
|
101
109
|
@no_sql_connection.should_receive(:update).with("posts", 500, {"$addToSet"=>{"comments"=>{'first_name' => 'bob'}}})
|
102
110
|
@translation.send(:copy_embedded_tables)
|
103
111
|
end
|
104
112
|
it "should call $addToSet on update of an embed_as_object table" do
|
105
|
-
@embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => true)
|
113
|
+
@embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'comments', :sql_name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => true)
|
106
114
|
@translation.stub(:tables).and_return([@target_table, @embed_table])
|
107
115
|
@no_sql_connection.should_receive(:update).with("posts", 500, {"$set"=>{"comments"=>{'first_name' => 'bob'}}})
|
108
116
|
@translation.send(:copy_embedded_tables)
|
109
117
|
end
|
118
|
+
it "should allow rename of table" do
|
119
|
+
@embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'notes', :sql_name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts', :embed_as_object? => true)
|
120
|
+
@translation.stub(:tables).and_return([@target_table, @embed_table])
|
121
|
+
@no_sql_connection.should_receive(:update).with("posts", 500, {"$set"=>{"notes"=>{'first_name' => 'bob'}}})
|
122
|
+
@translation.send(:copy_embedded_tables)
|
123
|
+
end
|
110
124
|
end
|
111
125
|
|
112
126
|
context "update_reference_ids" do
|
@@ -17,9 +17,10 @@ describe Mongify::Translation do
|
|
17
17
|
before(:each) do
|
18
18
|
@connection = Mongify::Database::SqlConnection.new
|
19
19
|
@connection.stub(:has_connection?).and_return(true)
|
20
|
+
@connection.stub(:valid?).and_return(true)
|
20
21
|
@connection.stub(:tables).and_return(['users'])
|
21
22
|
col = mock(:name => 'first_name', :type => 'string', :default => nil)
|
22
|
-
@connection.stub(:columns_for).and_return([col])
|
23
|
+
@connection.stub(:columns_for).with('users').and_return([col])
|
23
24
|
end
|
24
25
|
it "should return a translation" do
|
25
26
|
Mongify::Translation.load(@connection).should be_a(Mongify::Translation)
|
@@ -49,10 +50,16 @@ describe Mongify::Translation do
|
|
49
50
|
|
50
51
|
context "tables reference" do
|
51
52
|
before(:each) do
|
52
|
-
@copy_table = mock(:name => 'users', :embed? => false)
|
53
|
-
@embed_table = mock(:name => 'comments', :embed? => true)
|
53
|
+
@copy_table = mock(:name => 'users', :embed? => false, :ignored? => false)
|
54
|
+
@embed_table = mock(:name => 'comments', :embed? => true, :ignored? => false)
|
55
|
+
@ignored_table = mock(:name => 'apples', :ignored? => true, :embed? => false)
|
54
56
|
@translation = Mongify::Translation.new()
|
55
|
-
@translation.stub(:
|
57
|
+
@translation.stub(:all_tables).and_return([@copy_table, @embed_table, @ignored_table])
|
58
|
+
end
|
59
|
+
context "tables" do
|
60
|
+
it "should not show ignored" do
|
61
|
+
|
62
|
+
end
|
56
63
|
end
|
57
64
|
context "copy_tables" do
|
58
65
|
it "should return tables that are not embeded" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew Kalek
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-19 00:00:00 -05:00
|
19
19
|
default_executable: mongify
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|