mongify 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.1.1 / 28 Jan 2011
2
+ * Renamed GenerateDatabase to DatabaseGenerator
3
+ * Renamed Table#embed? to Table#embedded?
4
+ * Renamed Table#embed_as_object? to Table#embedded_as_object?
5
+ * Added support for polymorphic tables
6
+ * Did some file cleanup for unused files
1
7
  == 0.1.0 / 21 Jan 2011
2
8
  * Moved from Alpha to Beta! :)
3
9
  * Improved internal RDocs
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongify (0.1.0)
4
+ mongify (0.1.1)
5
5
  activerecord (>= 2.3.10)
6
6
  activesupport (>= 2.3.10)
7
7
  bson_ext (>= 1.1.5)
data/README.rdoc CHANGED
@@ -143,11 +143,18 @@ Table Options are as follow:
143
143
  table "table_name", :ignore => true # This will ignore the whole table (like it doesn't exist)
144
144
  # This option is good for tables like: schema_migrations
145
145
 
146
+ table "table_name", # This allows you to specify the table as being polymorphic
147
+ :polymorphic => 'notable', # and provide the name of the polymorphic relationship.
148
+ :embed_in => true # Setting embed_in => true allows the relationship to be
149
+ # embedded directly into the parent class.
150
+ # If you do not embed it, the polymorphic table will be copied in to
151
+ # MongoDB and the notable_id will be updated to the new BSON::ObjectID
152
+
146
153
  table "table_name" do # A table can take a before_save block that will be called just
147
154
  before_save do |row| # before the row is saved to the no sql database.
148
155
  row.admin = row.delete('permission').to_i > 50 # This gives you the ability to do very powerful things like:
149
156
  end # Moving records around, renaming records, changing values in row based on
150
- end # some values! Checkout {Mongify::Database::DataRow} to learn more
157
+ end # some values! Checkout Mongify::Database::DataRow to learn more
151
158
 
152
159
  === Columns
153
160
 
@@ -196,9 +203,7 @@ More testing will be done once the basic functionality is done
196
203
  == TODO
197
204
 
198
205
  * Allow you to set a :force => true for mongodb_connection, so that it drops existing database before importing
199
- * Work with polymorphic tables
200
206
  * Update user on what is being processed
201
- * Improve RDoc
202
207
 
203
208
  == Development
204
209
 
@@ -6,7 +6,7 @@ Feature: Processing a translation
6
6
  Scenario: Process
7
7
  Given a database exists
8
8
  And a blank mongodb
9
- When I run mongify process spec/files/simple_translation.rb -c spec/files/base_configuration.rb
9
+ When I run mongify process spec/files/translation.rb -c spec/files/base_configuration.rb
10
10
  Then it succeeds
11
11
  And there should be 3 users in mongodb
12
12
  And there should be 3 posts in mongodb
@@ -34,7 +34,7 @@ end
34
34
 
35
35
 
36
36
  Given /^a database exists$/ do
37
- GenerateDatabase.sqlite
37
+ DatabaseGenerator.sqlite
38
38
  end
39
39
 
40
40
  Then /^it should print out the database schema$/ do
@@ -1,15 +1,15 @@
1
1
  Given /^a blank mongodb$/ do
2
- GenerateDatabase.clear_mongodb
2
+ DatabaseGenerator.clear_mongodb
3
3
  end
4
4
 
5
5
  Then /^there should be (\d+) (.*) in mongodb$/ do |count, collection|
6
- GenerateDatabase.mongo_connection.db[collection].count.should == count.to_i
6
+ DatabaseGenerator.mongo_connection.db[collection].count.should == count.to_i
7
7
  end
8
8
 
9
9
  Then /^the (first|sencond|third) (.+)'s (.+) should be (first|second|thrid) (.+)$/ do |collection_place, collection, field, target_place, target|
10
- GenerateDatabase.mongo_connection.db[collection.pluralize].find.to_a.send(collection_place.to_sym)[field].should == GenerateDatabase.mongo_connection.db[target.pluralize].find.to_a.send(target_place)['_id']
10
+ DatabaseGenerator.mongo_connection.db[collection.pluralize].find.to_a.send(collection_place.to_sym)[field].should == DatabaseGenerator.mongo_connection.db[target.pluralize].find.to_a.send(target_place)['_id']
11
11
  end
12
12
 
13
13
  Then /^the (first|sencond|third) (.+) should have (\d+) (.+)$/ do |collection_place, collection, count, target|
14
- GenerateDatabase.mongo_connection.db[collection.pluralize].find.to_a.send(collection_place.to_sym)[target.pluralize].count == count
14
+ DatabaseGenerator.mongo_connection.db[collection.pluralize].find.to_a.send(collection_place.to_sym)[target.pluralize].count == count
15
15
  end
@@ -5,7 +5,7 @@ require 'tempfile'
5
5
  require 'fileutils'
6
6
  require 'mongify/cli/application'
7
7
  require 'mongify'
8
- require 'spec/support/generate_database'
8
+ require 'spec/support/database_generator'
9
9
  require 'spec/support/config_reader'
10
10
 
11
11
  ::CONNECTION_CONFIG = ConfigReader.new(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))) + '/spec/support/database.yml')
@@ -32,6 +32,14 @@ module Mongify
32
32
  # table "table_name", :ignore => true # This will ignore the whole table (like it doesn't exist)
33
33
  # # This option is good for tables like: schema_migrations
34
34
  #
35
+ # table "table_name", :polymorphic => 'commentable' # This will identfiy the table as polymorphic and the new ids
36
+ # # will be updated as needed.
37
+ #
38
+ # table "table_name", # This allows you to specify the table as being polymorphic
39
+ # :polymorphic => 'notable', # and provide the name of the polymorphic relationship.
40
+ # :embed_in => true # Setting embed_in => true allows the relationship to be
41
+ # # embedded directly into the parent class.
42
+ #
35
43
  # table "table_name" do # A table can take a before_save block that will be called just
36
44
  # before_save do |row| # before the row is saved to the no sql database.
37
45
  # row.admin = row.delete('permission').to_i > 50 # This gives you the ability to do very powerful things like:
@@ -66,6 +74,16 @@ module Mongify
66
74
  @options['ignore']
67
75
  end
68
76
 
77
+ # Returns true if table is marked as polymorphic
78
+ def polymorphic?
79
+ !!@options['polymorphic']
80
+ end
81
+
82
+ # Returns the name of the polymorphic association
83
+ def polymorphic_as
84
+ @options['polymorphic'].to_s
85
+ end
86
+
69
87
  # Add a Database Column to the table
70
88
  # This expects to get a {Mongify::Database::Column} or it will raise {Mongify::DatabaseColumnExpected} otherwise
71
89
  def add_column(column)
@@ -110,24 +128,24 @@ module Mongify
110
128
 
111
129
  # Returns the type of embed it will be [object or array]
112
130
  def embed_as
113
- return nil unless embed?
131
+ return nil unless embedded?
114
132
  return 'object' if @options['as'].to_s.downcase == 'object'
115
133
  'array'
116
134
  end
117
135
 
118
136
  # Returns true if table is being embed as an object
119
- def embed_as_object?
137
+ def embedded_as_object?
120
138
  embed_as == 'object'
121
139
  end
122
140
 
123
141
  # Returns true if this is an embedded table
124
- def embed?
142
+ def embedded?
125
143
  embed_in.present?
126
144
  end
127
145
 
128
146
  # Returns the name of the target column to embed on
129
147
  def embed_on
130
- return nil unless embed?
148
+ return nil unless embedded?
131
149
  (@options['on'] || "#{@options['embed_in'].to_s.singularize}_id").to_s
132
150
  end
133
151
 
@@ -69,6 +69,11 @@ module Mongify
69
69
  @all_tables = []
70
70
  end
71
71
 
72
+ # finds table by name
73
+ def find(name)
74
+ all_tables.find{ |t| t.name == name }
75
+ end
76
+
72
77
  # Creates a {Mongify::Database::Table} from the given input and adds it to the list of tables
73
78
  def table(table_name, options={}, &block)
74
79
  table = Mongify::Database::Table.new(table_name, options, &block)
@@ -88,17 +93,21 @@ module Mongify
88
93
 
89
94
  # Returns an array of all tables that have not been ingored
90
95
  def tables
91
- all_tables.reject{ |t| t.ignored? }
96
+ all_tables.reject{ |t| t.ignored? || t.polymorphic? }
92
97
  end
93
98
 
94
99
  # Returns an array of all tables that have not been ignored and are just straight copy tables
95
100
  def copy_tables
96
- tables.reject{|t| t.embed?}
101
+ tables.reject{|t| t.embedded?}
102
+ end
103
+
104
+ def polymorphic_tables
105
+ all_tables.reject{ |t| t.ignored? || !t.polymorphic? }
97
106
  end
98
107
 
99
108
  # Returns an array of all tables that have not been ignored and are to be embedded
100
109
  def embed_tables
101
- tables.reject{|t| !t.embed?}
110
+ tables.reject{|t| !t.embedded?}
102
111
  end
103
112
 
104
113
  end
@@ -20,6 +20,7 @@ module Mongify
20
20
  copy_data
21
21
  copy_embedded_tables
22
22
  update_reference_ids
23
+ copy_polymorphic_tables
23
24
  remove_pre_mongified_ids
24
25
  nil
25
26
  end
@@ -47,12 +48,40 @@ module Mongify
47
48
  row.delete(t.embed_on)
48
49
  row.merge!(fetch_reference_ids(t, row))
49
50
  row.delete('pre_mongified_id')
50
- save_function_call = t.embed_as_object? ? '$set' : '$addToSet'
51
+ save_function_call = t.embedded_as_object? ? '$set' : '$addToSet'
51
52
  no_sql_connection.update(t.embed_in, target_row['_id'], {save_function_call => {t.name => row}})
52
53
  end
53
54
  end
54
55
  end
55
56
 
57
+ # Moves over polymorphic data
58
+ def copy_polymorphic_tables
59
+ self.polymorphic_tables.each do |t|
60
+ polymorphic_id_col, polymorphic_type_col = "#{t.polymorphic_as}_id", "#{t.polymorphic_as}_type"
61
+ sql_connection.select_rows(t.sql_name).each do |row|
62
+ table_name = row[polymorphic_type_col].tableize
63
+ new_id = no_sql_connection.get_id_using_pre_mongified_id(table_name, row[polymorphic_id_col])
64
+ puts "getting new id for #{table_name}, #{row[polymorphic_id_col]} and getting #{new_id}"
65
+ if new_id
66
+ row = t.translate(row)
67
+ row.merge!(fetch_reference_ids(t, row))
68
+ row[polymorphic_id_col] = new_id
69
+ row.delete('pre_mongified_id')
70
+ if t.embedded?
71
+ row.delete(polymorphic_id_col)
72
+ row.delete(polymorphic_type_col)
73
+ save_function_call = t.embedded_as_object? ? '$set' : '$addToSet'
74
+ no_sql_connection.update(table_name, new_id, {save_function_call => {t.name => row}})
75
+ else
76
+ no_sql_connection.insert_into(t.name, row)
77
+ end
78
+ else
79
+ puts "#{table_name} table not found on #{t.sql_name} polymorphic import"
80
+ end
81
+ end
82
+ end
83
+ end
84
+
56
85
  # Updates the reference ids in the no sql database
57
86
  def update_reference_ids
58
87
  self.tables.each do |t|
@@ -1,4 +1,4 @@
1
1
  module Mongify
2
2
  # Mongify's Current Version Number
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
@@ -33,3 +33,15 @@ table "preferences", :embed_in => :users, :as => :object do
33
33
  column "updated_at", :datetime, :ignore => true
34
34
  end
35
35
 
36
+ table "notes", :embed_in => true, :polymorphic => 'notable' do
37
+ column "id", :key
38
+ column "user_id", :integer, :references => "users"
39
+ column "notable_id", :integer
40
+ column "notable_type", :string
41
+ column "body", :text
42
+ column "created_at", :datetime
43
+ column "updated_at", :datetime
44
+ end
45
+
46
+
47
+
@@ -12,7 +12,7 @@ describe Mongify::CLI::WorkerCommand do
12
12
  @no_sql_connection.stub(:has_connection?).and_return(true)
13
13
  @config.stub(:no_sql_connection).and_return(@no_sql_connection)
14
14
 
15
- @translation_file = 'spec/files/simple_translation.rb'
15
+ @translation_file = 'spec/files/translation.rb'
16
16
 
17
17
  Mongify::Translation.stub(:load).and_return(stub(:print => 'worked'))
18
18
  @view = mock('view').as_null_object
@@ -82,7 +82,7 @@ describe Mongify::CLI::WorkerCommand do
82
82
 
83
83
  context "process command" do
84
84
  before(:each) do
85
- @command = Mongify::CLI::WorkerCommand.new('process', @config, 'spec/files/simple_translation.rb')
85
+ @command = Mongify::CLI::WorkerCommand.new('process', @config, 'spec/files/translation.rb')
86
86
  Mongify::Translation.stub(:parse).and_return(mock(:process => true))
87
87
  end
88
88
  it "should report success" do
@@ -127,7 +127,7 @@ describe Mongify::Database::NoSqlConnection do
127
127
 
128
128
  describe "working connection" do
129
129
  before(:each) do
130
- @mongodb_connection = GenerateDatabase.mongo_connection
130
+ @mongodb_connection = DatabaseGenerator.mongo_connection
131
131
  end
132
132
 
133
133
  it "should work" do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Mongify::Database::SqlConnection do
4
4
  before(:all) do
5
- @db_path = GenerateDatabase.sqlite
5
+ @db_path = DatabaseGenerator.sqlite
6
6
  end
7
7
 
8
8
  before(:each) do
@@ -30,7 +30,7 @@ describe Mongify::Database::SqlConnection do
30
30
 
31
31
  context "MySql config" do
32
32
  before(:each) do
33
- @sql_connection = GenerateDatabase.mysql_connection
33
+ @sql_connection = DatabaseGenerator.mysql_connection
34
34
  end
35
35
 
36
36
  context "valid?" do
@@ -68,13 +68,13 @@ describe Mongify::Database::SqlConnection do
68
68
 
69
69
  context "tables" do
70
70
  it "should be able to get a list" do
71
- @sql_connection.tables.sort.should == ['comments', 'posts', 'preferences', 'users'].sort
71
+ @sql_connection.tables.should =~ ['comments', 'notes', 'posts', 'preferences', 'users']
72
72
  end
73
73
  end
74
74
 
75
75
  context "columns" do
76
76
  it "should see columns for a table" do
77
- @sql_connection.columns_for(:users).map{ |column| column.name }.sort.should == ['id', 'first_name', 'last_name', 'created_at', 'updated_at'].sort
77
+ @sql_connection.columns_for(:users).map{ |column| column.name }.should =~ ['id', 'first_name', 'last_name', 'created_at', 'updated_at']
78
78
  end
79
79
  end
80
80
  end
@@ -150,21 +150,21 @@ describe Mongify::Database::Table do
150
150
  it "should allow Object as a value" do
151
151
  Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id', :as => :object).embed_as.should == 'object'
152
152
  end
153
- context "embed_as_object?" do
153
+ context "embedded_as_object?" do
154
154
  it "should be true" do
155
- Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id', :as => :object).should be_embed_as_object
155
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id', :as => :object).should be_embedded_as_object
156
156
  end
157
157
  it "should be false" do
158
- Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id', :as => :array).should_not be_embed_as_object
158
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id', :as => :array).should_not be_embedded_as_object
159
159
  end
160
160
  end
161
161
  end
162
- context "embed?" do
162
+ context "embedded?" do
163
163
  it "should be true" do
164
- Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id').should be_embed
164
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id').should be_embedded
165
165
  end
166
166
  it "should be false" do
167
- Mongify::Database::Table.new('users').should_not be_embed
167
+ Mongify::Database::Table.new('users').should_not be_embedded
168
168
  end
169
169
  end
170
170
 
@@ -172,7 +172,7 @@ describe Mongify::Database::Table do
172
172
  it "should be post_id" do
173
173
  Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id').embed_on.should == 'post_id'
174
174
  end
175
- it "should be nil when not embed?" do
175
+ it "should be nil when not embedded?" do
176
176
  Mongify::Database::Table.new('users', :on => 'test').embed_on.should be_nil
177
177
  end
178
178
  it "should calculate embed_on from embed_in" do
@@ -185,7 +185,6 @@ describe Mongify::Database::Table do
185
185
  before(:each) do
186
186
  @table = Mongify::Database::Table.new('users')
187
187
  @table.before_save do |row|
188
- puts "my keys are: #{row.keys}"
189
188
  row.admin = row.delete('permission').to_i > 50
190
189
  end
191
190
  end
@@ -202,6 +201,31 @@ describe Mongify::Database::Table do
202
201
  end
203
202
  end
204
203
 
204
+ context "polymorphic" do
205
+ before(:each) do
206
+ @table = Mongify::Database::Table.new('comments', :polymorphic => :commentable)
207
+ end
208
+ it "should be true" do
209
+ @table.should be_polymorphic
210
+ end
211
+ it "should be false" do
212
+ Mongify::Database::Table.new('users').should_not be_polymorphic
213
+ end
214
+ it "should return 'commentable'" do
215
+ @table.polymorphic_as.should == 'commentable'
216
+ end
217
+
218
+ context "embed" do
219
+ before(:each) do
220
+ @table = Mongify::Database::Table.new('comments', :polymorphic => :commentable, :embed_in => true)
221
+ end
222
+ it "should be embedded" do
223
+ @table.should be_embedded
224
+ end
225
+
226
+ end
227
+ end
228
+
205
229
  context "translate" do
206
230
  before(:each) do
207
231
  @column1 = mock(:translate => {'first_name' => 'Timmy'}, :name => 'first_name')
@@ -2,9 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe Mongify::Translation::Process do
4
4
  before(:each) do
5
- GenerateDatabase.clear_mongodb
6
- @sql_connection = GenerateDatabase.sqlite_connection
7
- @no_sql_connection = GenerateDatabase.mongo_connection
5
+ DatabaseGenerator.clear_mongodb
6
+ @sql_connection = DatabaseGenerator.sqlite_connection
7
+ @no_sql_connection = DatabaseGenerator.mongo_connection
8
8
  @translation = Mongify::Translation.new
9
9
  end
10
10
 
@@ -47,7 +47,7 @@ describe Mongify::Translation::Process do
47
47
  it "should get correct information" do
48
48
  @no_sql_connection = mock()
49
49
  @translation.stub(:no_sql_connection).and_return(@no_sql_connection)
50
- @table = mock(:translate => {}, :name => 'users', :embed? => false)
50
+ @table = mock(:translate => {}, :name => 'users', :embedded? => false)
51
51
  @column = mock(:name => 'user_id', :references => 'users')
52
52
  @table.stub(:reference_columns).and_return([@column])
53
53
  @no_sql_connection.should_receive(:get_id_using_pre_mongified_id).with('users', 1).once.and_return(500)
@@ -65,7 +65,7 @@ describe Mongify::Translation::Process do
65
65
 
66
66
  @table = mock(:translate => {'first_name'=> 'Timmy', 'last_name' => 'Zuza', 'preference_id' => 1},
67
67
  :name => 'users',
68
- :embed? => false,
68
+ :embedded? => false,
69
69
  :sql_name => 'users')
70
70
 
71
71
  @translation.stub(:tables).and_return([@table])
@@ -85,8 +85,8 @@ describe Mongify::Translation::Process do
85
85
 
86
86
  context "copy_embed_tables" do
87
87
  before(:each) do
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')
88
+ @target_table = mock(:name => 'posts', :embedded? => false, :sql_name => 'posts')
89
+ @embed_table = mock(:translate => {}, :name => 'comments', :embedded? => true, :embed_on => 'post_id', :embed_in => 'posts', :embedded_as_object? => false, :sql_name => 'comments')
90
90
  @no_sql_connection.stub(:find_one).and_return({'_id' => 500})
91
91
  @translation.stub(:tables).and_return([@target_table, @embed_table])
92
92
  @translation.stub(:fetch_reference_ids).and_return({})
@@ -98,25 +98,25 @@ describe Mongify::Translation::Process do
98
98
  @translation.send(:copy_embedded_tables)
99
99
  end
100
100
  it "should remove the pre_mongified_id before embedding" do
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)
101
+ @embed_table = mock(:translate => {'first_name' => 'bob', 'pre_mongified_id' => 1}, :name => 'comments', :sql_name => 'comments', :embedded? => true, :embed_on => 'post_id', :embed_in => 'posts', :embedded_as_object? => false)
102
102
  @translation.stub(:tables).and_return([@target_table, @embed_table])
103
103
  @no_sql_connection.should_receive(:update).with("posts", 500, {"$addToSet"=>{"comments"=>{'first_name' => 'bob'}}})
104
104
  @translation.send(:copy_embedded_tables)
105
105
  end
106
106
  it "should remove the parent_id from the embedding row" do
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)
107
+ @embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'comments', :sql_name => 'comments', :embedded? => true, :embed_on => 'post_id', :embed_in => 'posts', :embedded_as_object? => false)
108
108
  @translation.stub(:tables).and_return([@target_table, @embed_table])
109
109
  @no_sql_connection.should_receive(:update).with("posts", 500, {"$addToSet"=>{"comments"=>{'first_name' => 'bob'}}})
110
110
  @translation.send(:copy_embedded_tables)
111
111
  end
112
112
  it "should call $addToSet on update of an embed_as_object table" do
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)
113
+ @embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'comments', :sql_name => 'comments', :embedded? => true, :embed_on => 'post_id', :embed_in => 'posts', :embedded_as_object? => true)
114
114
  @translation.stub(:tables).and_return([@target_table, @embed_table])
115
115
  @no_sql_connection.should_receive(:update).with("posts", 500, {"$set"=>{"comments"=>{'first_name' => 'bob'}}})
116
116
  @translation.send(:copy_embedded_tables)
117
117
  end
118
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)
119
+ @embed_table = mock(:translate => {'first_name' => 'bob', 'post_id' => 1}, :name => 'notes', :sql_name => 'comments', :embedded? => true, :embed_on => 'post_id', :embed_in => 'posts', :embedded_as_object? => true)
120
120
  @translation.stub(:tables).and_return([@target_table, @embed_table])
121
121
  @no_sql_connection.should_receive(:update).with("posts", 500, {"$set"=>{"notes"=>{'first_name' => 'bob'}}})
122
122
  @translation.send(:copy_embedded_tables)
@@ -140,6 +140,55 @@ describe Mongify::Translation::Process do
140
140
  end
141
141
  end
142
142
 
143
+ context "copy_polymorphic_tables" do
144
+ before(:each) do
145
+ @ref_table = mock(:name => 'user_accounts',
146
+ :embedded? => false,
147
+ :ignored? => false,
148
+ :sql_name => 'user_accounts')
149
+ @translation.stub(:find).with('user_accounts').and_return([@ref_table])
150
+
151
+ @sql_connection.should_receive(:select_rows).with('comments').and_return([{'commentable_id' => 1, 'commentable_type' => 'UserAccount', 'data' => 'good'}])
152
+ @no_sql_connection.should_receive(:get_id_using_pre_mongified_id).with('user_accounts', 1).and_return(500)
153
+ end
154
+ context "embedded" do
155
+ it "should work correctly" do
156
+ @table = mock(:translate => {'data' => 123},
157
+ :name => 'comments',
158
+ :embedded? => true,
159
+ :polymorphic_as => 'commentable',
160
+ :polymorphic? => true,
161
+ :ignored? => false,
162
+ :embedded_as_object? => false,
163
+ :sql_name => 'comments',
164
+ :reference_columns => [])
165
+
166
+ @translation.stub(:all_tables).and_return([@table])
167
+
168
+ @no_sql_connection.should_receive(:update).with('user_accounts', 500, {'$addToSet' => {'comments' => {'data' => 123}}})
169
+ @translation.send(:copy_polymorphic_tables)
170
+ end
171
+ end
172
+ context "not embedded" do
173
+ it "should work" do
174
+ @table = mock(:translate => {'data' => 123, 'commentable_type' => 'UserAccount', 'commentable_id' => 1},
175
+ :name => 'comments',
176
+ :embedded? => false,
177
+ :polymorphic_as => 'commentable',
178
+ :polymorphic? => true,
179
+ :ignored? => false,
180
+ :embedded_as_object? => false,
181
+ :sql_name => 'comments',
182
+ :reference_columns => [])
183
+
184
+ @translation.stub(:all_tables).and_return([@table])
185
+
186
+ @no_sql_connection.should_receive(:insert_into).with('comments', {'data' => 123, 'commentable_type' => 'UserAccount', 'commentable_id' => 500})
187
+ @translation.send(:copy_polymorphic_tables)
188
+ end
189
+ end
190
+ end
191
+
143
192
  context "remove_pre_mongified_ids" do
144
193
  it "should remove_pre_mongified_ids on no_sql_connection" do
145
194
  @no_sql_connection.should_receive(:remove_pre_mongified_ids).with(anything)
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Mongify::Translation do
4
4
  before(:all) do
5
- @file_path = File.expand_path(File.dirname(__FILE__) + '/../files/simple_translation.rb')
5
+ @file_path = File.expand_path(File.dirname(__FILE__) + '/../files/translation.rb')
6
6
  @translation = Mongify::Translation.parse(@file_path)
7
7
  end
8
8
 
@@ -48,17 +48,35 @@ describe Mongify::Translation do
48
48
  end
49
49
  end
50
50
 
51
+ context "find" do
52
+ before(:each) do
53
+ @user_table = mock(:name => 'users')
54
+ @translation.stub(:all_tables).and_return([mock(:name => 'comments'),
55
+ @user_table,
56
+ mock(:name => 'posts')])
57
+ end
58
+ it "should work" do
59
+
60
+ @translation.find('users').should == @user_table
61
+ end
62
+ it "should return nil if nothing is found" do
63
+ @translation.find('apples').should be_nil
64
+ end
65
+ end
66
+
51
67
  context "tables reference" do
52
68
  before(:each) do
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)
69
+ @copy_table = mock(:name => 'users', :ignored? => false, :embedded? => false, :polymorphic? => false)
70
+ @embed_table = mock(:name => 'comments', :ignored? => false, :embedded? => true, :polymorphic? => false)
71
+ @ignored_table = mock(:name => 'apples', :ignored? => true, :embedded? => false, :polymorphic? => false)
72
+ @polymorphic_table = mock(:name => 'comments', :ignored? => false, :embedded? => false, :polymorphic? => true)
56
73
  @translation = Mongify::Translation.new()
57
- @translation.stub(:all_tables).and_return([@copy_table, @embed_table, @ignored_table])
74
+ @all_tables = [@copy_table, @embed_table, @ignored_table, @polymorphic_table]
75
+ @translation.stub(:all_tables).and_return(@all_tables)
58
76
  end
59
77
  context "tables" do
60
78
  it "should not show ignored" do
61
-
79
+ @translation.tables.count == @all_tables.count - 1
62
80
  end
63
81
  end
64
82
  context "copy_tables" do
@@ -71,6 +89,9 @@ describe Mongify::Translation do
71
89
  @translation.embed_tables.should == [@embed_table]
72
90
  end
73
91
  end
92
+ it "should return only polymorphic tables" do
93
+ @translation.polymorphic_tables.should == [@polymorphic_table]
94
+ end
74
95
  end
75
96
 
76
97
  context "add_table" do
@@ -1,5 +1,5 @@
1
1
  # Used during testing to generate and load database information and connection strings
2
- class GenerateDatabase
2
+ class DatabaseGenerator
3
3
  # Returns a mysql connection (using the database.yml)
4
4
  def self.mysql_connection
5
5
  @sql_connection ||= Mongify::Database::SqlConnection.new( :adapter => CONNECTION_CONFIG.mysql['adapter'],
@@ -10,6 +10,7 @@ class GenerateDatabase
10
10
  :database => CONNECTION_CONFIG.mysql['database']
11
11
  )
12
12
  end
13
+
13
14
  # Returns a sqlite connection (using the database.yml )
14
15
  def self.sqlite_connection
15
16
  @db_path = File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), CONNECTION_CONFIG.sqlite['database'])
@@ -50,6 +51,14 @@ class GenerateDatabase
50
51
  t.timestamps
51
52
  end
52
53
 
54
+ conn.create_table(:notes) do |t|
55
+ t.integer :user_id
56
+ t.integer :notable_id
57
+ t.string :notable_type
58
+ t.text :body
59
+ t.timestamps
60
+ end
61
+
53
62
  if include_data
54
63
 
55
64
  #Users
@@ -75,21 +84,33 @@ class GenerateDatabase
75
84
  [
76
85
  {:post_id => 1, :user_id => 1, :body => 'First Comment Body'},
77
86
  {:post_id => 2, :user_id => 1, :body => 'Second Comment Body'},
78
- {:post_id => 2, :user_id => 2, :body => 'Thrid Comment Body'}
87
+ {:post_id => 2, :user_id => 2, :body => 'Thrid Comment Body'},
79
88
  ].each do |v|
80
89
  conn.insert("INSERT INTO comments (body, post_id, user_id, created_at, updated_at)
81
90
  VALUES ('#{v[:body]}', #{v[:post_id]}, #{v[:user_id]}, '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
82
91
  end
83
92
 
93
+ #Preferences
84
94
  [
85
95
  {:user_id => 1, :notify_by_email => true},
86
96
  {:user_id => 2, :notify_by_email => true},
87
- {:user_id => 3, :notify_by_email => false}
97
+ {:user_id => 3, :notify_by_email => false},
88
98
  ].each do |v|
89
99
  conn.insert("INSERT INTO preferences (user_id, notify_by_email, created_at, updated_at)
90
100
  VALUES (#{v[:user_id]}, '#{v[:notify_by_email]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
91
101
  end
92
102
 
103
+ #Notes
104
+ [
105
+ {:user_id => 1, :body => 'note 1', :notable_id => 1, :notable_type => 'Post'},
106
+ {:user_id => 2, :body => 'note 2', :notable_id => 2, :notable_type => 'Post'},
107
+ {:user_id => 2, :body => 'note 3', :notable_id => 1, :notable_type => 'User'},
108
+ {:user_id => 3, :body => 'note 4', :notable_id => 2, :notable_type => 'User'},
109
+ ].each do |v|
110
+ conn.insert("INSERT INTO notes (user_id, body, notable_id, notable_type, created_at, updated_at)
111
+ VALUES (#{v[:user_id]}, '#{v[:body]}', #{v[:notable_id]}, '#{v[:notable_type]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
112
+ end
113
+
93
114
  end
94
115
 
95
116
  sqlite_connection.database
@@ -33,3 +33,13 @@ table "preferences" do
33
33
  column "updated_at", :datetime
34
34
  end
35
35
 
36
+ table "notes" do
37
+ column "id", :key
38
+ column "user_id", :integer, :references => "users"
39
+ column "notable_id", :integer, :references => "notables"
40
+ column "notable_type", :string
41
+ column "body", :text
42
+ column "created_at", :datetime
43
+ column "updated_at", :datetime
44
+ end
45
+
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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
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-21 00:00:00 -05:00
18
+ date: 2011-01-28 00:00:00 -05:00
19
19
  default_executable: mongify
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -284,8 +284,7 @@ files:
284
284
  - mongify.gemspec
285
285
  - spec/default.watch
286
286
  - spec/files/base_configuration.rb
287
- - spec/files/empty_translation.rb
288
- - spec/files/simple_translation.rb
287
+ - spec/files/translation.rb
289
288
  - spec/mongify/cli/application_spec.rb
290
289
  - spec/mongify/cli/help_command_spec.rb
291
290
  - spec/mongify/cli/options_spec.rb
@@ -307,8 +306,8 @@ files:
307
306
  - spec/spec_helper.rb
308
307
  - spec/support/config_reader.rb
309
308
  - spec/support/database.example
309
+ - spec/support/database_generator.rb
310
310
  - spec/support/database_output.txt
311
- - spec/support/generate_database.rb
312
311
  - spec/tmp/.gitignore
313
312
  has_rdoc: true
314
313
  homepage: http://github.com/anlek/mongify
@@ -345,7 +344,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
345
344
  requirements: []
346
345
 
347
346
  rubyforge_project:
348
- rubygems_version: 1.4.1
347
+ rubygems_version: 1.4.2
349
348
  signing_key:
350
349
  specification_version: 3
351
350
  summary: Translate your SQL data to MongoDB
@@ -358,8 +357,7 @@ test_files:
358
357
  - features/support/env.rb
359
358
  - spec/default.watch
360
359
  - spec/files/base_configuration.rb
361
- - spec/files/empty_translation.rb
362
- - spec/files/simple_translation.rb
360
+ - spec/files/translation.rb
363
361
  - spec/mongify/cli/application_spec.rb
364
362
  - spec/mongify/cli/help_command_spec.rb
365
363
  - spec/mongify/cli/options_spec.rb
@@ -381,6 +379,6 @@ test_files:
381
379
  - spec/spec_helper.rb
382
380
  - spec/support/config_reader.rb
383
381
  - spec/support/database.example
382
+ - spec/support/database_generator.rb
384
383
  - spec/support/database_output.txt
385
- - spec/support/generate_database.rb
386
384
  - spec/tmp/.gitignore
File without changes