mongify 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ == 0.0.6 / 16 Jan 2011
2
+ * Added the ability to embed tables into collections (currently only one to many relationship)
3
+ * Few tweaks and improvements in code
1
4
  == 0.0.5 / 15 Jan 2011
2
5
  * Improved description for mongify.
3
6
  == 0.0.4 / 15 Jan 2011
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongify (0.0.5)
4
+ mongify (0.0.6)
5
5
  activerecord (>= 2.3.10)
6
6
  activesupport (>= 2.3.10)
7
7
  bson_ext (>= 1.1.5)
@@ -11,7 +11,9 @@ 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 there should be 3 comments in mongodb
14
+ And the first post's owner_id should be first user
15
+ And there should be 0 comments in mongodb
16
+ And the first post should have 1 comment
15
17
 
16
18
 
17
19
 
@@ -5,3 +5,11 @@ end
5
5
  Then /^there should be (\d+) (.*) in mongodb$/ do |count, collection|
6
6
  GenerateDatabase.mongo_connection.db[collection].count.should == count.to_i
7
7
  end
8
+
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']
11
+ end
12
+
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
15
+ end
@@ -48,6 +48,10 @@ module Mongify
48
48
  db[colleciton_name].update({"_id" => id}, attributes)
49
49
  end
50
50
 
51
+ def find_one(collection_name, query)
52
+ db[collection_name].find_one(query)
53
+ end
54
+
51
55
  def get_id_using_pre_mongified_id(colleciton_name, pre_mongified_id)
52
56
  db[colleciton_name].find_one('pre_mongified_id' => pre_mongified_id).try(:[], '_id')
53
57
  end
@@ -55,6 +55,19 @@ module Mongify
55
55
  end
56
56
  new_row
57
57
  end
58
+
59
+ def embed_in
60
+ @options['embed_in'].to_s unless @options['embed_in'].nil?
61
+ end
62
+
63
+ def embed?
64
+ embed_in.present?
65
+ end
66
+
67
+ def embed_on
68
+ return nil unless embed?
69
+ (@options['on'] || "#{@options['embed_in'].singularize}_id").to_s
70
+ end
58
71
 
59
72
  #######
60
73
  private
@@ -19,7 +19,8 @@ module Mongify
19
19
  self.no_sql_connection = no_sql_connection
20
20
  raise "noSql Connection is not valid" unless self.no_sql_connection.valid?
21
21
 
22
- copy
22
+ copy_data
23
+ copy_embedded_tables
23
24
  update_reference_ids
24
25
  nil
25
26
  end
@@ -28,28 +29,45 @@ module Mongify
28
29
  private
29
30
  #######
30
31
 
31
- def copy
32
- self.tables.each do |t|
32
+ def copy_data
33
+ self.copy_tables.each do |t|
33
34
  sql_connection.select_rows(t.name).each do |row|
34
35
  no_sql_connection.insert_into(t.name, t.translate(row))
35
36
  end
36
37
  end
37
38
  end
38
39
 
40
+ def copy_embedded_tables
41
+ self.embed_tables.each do |t|
42
+ sql_connection.select_rows(t.name).each do |row|
43
+ target_row = no_sql_connection.find_one(t.embed_in, {:pre_mongified_id => row[t.embed_on]})
44
+ next unless target_row.present?
45
+ row = t.translate(row)
46
+ row.merge!(fetch_reference_ids(t, row))
47
+ no_sql_connection.update(t.embed_in, target_row['_id'], {'$addToSet' => {t.name => row}})
48
+ end
49
+ end
50
+ end
51
+
39
52
  def update_reference_ids
40
53
  self.tables.each do |t|
41
54
  no_sql_connection.select_rows(t.name).each do |row|
42
55
  id = row["_id"]
43
- attributes = {}
44
- t.reference_columns.each do |c|
45
- new_id = no_sql_connection.get_id_using_pre_mongified_id(c.references.to_s, row[c.name])
46
- attributes.merge!(c.name => new_id) unless new_id.nil?
47
- end
56
+ attributes = fetch_reference_ids(t, row)
48
57
  no_sql_connection.update(t.name, id, {"$set" => attributes}) unless attributes.blank?
49
58
  end
50
59
  end
51
60
  end
52
61
 
62
+ def fetch_reference_ids(table, row)
63
+ attributes = {}
64
+ table.reference_columns.each do |c|
65
+ new_id = no_sql_connection.get_id_using_pre_mongified_id(c.references.to_s, row[c.name])
66
+ attributes.merge!(c.name => new_id) unless new_id.nil?
67
+ end
68
+ attributes
69
+ end
70
+
53
71
  end
54
72
  end
55
73
  end
@@ -45,6 +45,14 @@ module Mongify
45
45
  @tables << table
46
46
  end
47
47
 
48
+ def copy_tables
49
+ tables.reject{|t| t.embed?}
50
+ end
51
+
52
+ def embed_tables
53
+ tables.reject{|t| !t.embed?}
54
+ end
55
+
48
56
  #######
49
57
  private
50
58
  #######
@@ -1,3 +1,3 @@
1
1
  module Mongify
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -109,6 +109,14 @@ describe Mongify::Database::NoSqlConnection do
109
109
  @mongodb_connection.update('users', 1, attributes)
110
110
  end
111
111
  end
112
+
113
+ context "find_one" do
114
+ it "should call find_one on collection" do
115
+ query= {'pre_mongified_id' => 1}
116
+ @collection.should_receive(:find_one).with(query)
117
+ @mongodb_connection.find_one('users', query)
118
+ end
119
+ end
112
120
  end
113
121
 
114
122
 
@@ -96,6 +96,37 @@ describe Mongify::Database::Table do
96
96
  end
97
97
  end
98
98
 
99
+ context "dealing with embedding," do
100
+ context "embed_on" do
101
+ it "should return embed_on option" do
102
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id').embed_in.should == 'posts'
103
+ end
104
+ it "should be nil when not embedded table" do
105
+ Mongify::Database::Table.new('users').embed_in.should be_nil
106
+ end
107
+ end
108
+ context "embed?" do
109
+ it "should be true" do
110
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id').should be_embed
111
+ end
112
+ it "should be false" do
113
+ Mongify::Database::Table.new('users').should_not be_embed
114
+ end
115
+ end
116
+
117
+ context "embed_on" do
118
+ it "should be post_id" do
119
+ Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id').embed_on.should == 'post_id'
120
+ end
121
+ it "should be nil when not embed?" do
122
+ Mongify::Database::Table.new('users', :on => 'test').embed_on.should be_nil
123
+ end
124
+ it "should calculate embed_on from embed_in" do
125
+ Mongify::Database::Table.new('comments', :embed_in => 'posts').embed_on.should == 'post_id'
126
+ end
127
+ end
128
+ end
129
+
99
130
  context "translate" do
100
131
  before(:each) do
101
132
  @column1 = mock(:translate => {'first_name' => 'Timmy'}, :name => 'first_name')
@@ -16,35 +16,61 @@ describe Mongify::Translation::Process do
16
16
  lambda { @translation.process(@sql_connection, 'bad param2') }.should raise_error(Mongify::NoSqlConnectionRequired)
17
17
  end
18
18
 
19
- it "should call copy" do
20
- @translation.should_receive(:copy)
19
+ it "should call copy_data" do
20
+ @translation.should_receive(:copy_data)
21
21
  @translation.stub(:update_reference_ids)
22
22
  @translation.process(@sql_connection, @no_sql_connection)
23
23
  end
24
24
  it "should call update_reference_ids" do
25
25
  @translation.should_receive(:update_reference_ids)
26
- @translation.stub(:copy)
26
+ @translation.stub(:copy_data)
27
27
  @translation.process(@sql_connection, @no_sql_connection)
28
28
  end
29
29
 
30
+ context "fetch_reference_ids" do
31
+ it "should get correct information" do
32
+ @no_sql_connection = mock()
33
+ @translation.stub(:no_sql_connection).and_return(@no_sql_connection)
34
+ @table = mock(:translate => {}, :name => 'users', :embed? => false)
35
+ @column = mock(:name => 'user_id', :references => 'users')
36
+ @table.stub(:reference_columns).and_return([@column])
37
+ @no_sql_connection.should_receive(:get_id_using_pre_mongified_id).with('users', 1).once.and_return(500)
38
+ @translation.send(:fetch_reference_ids, @table, {'user_id' => 1}).should == {'user_id' => 500}
39
+ end
40
+ end
30
41
 
31
42
  context "processing actions" do
32
43
  before(:each) do
33
- @sql_connection = mock(:select_rows => [{'first_name'=> 'Timmy', 'last_name' => 'Zuza'}])
44
+ @sql_connection = mock(:select_rows => [{'first_name'=> 'Timmy', 'last_name' => 'Zuza', 'preference_id' => 1}])
34
45
  @translation.stub(:sql_connection).and_return(@sql_connection)
35
46
 
36
47
  @no_sql_connection = mock()
37
48
  @translation.stub(:no_sql_connection).and_return(@no_sql_connection)
38
49
 
39
- @table = mock(:translate => {}, :name => 'users')
50
+ @table = mock(:translate => {}, :name => 'users', :embed? => false)
40
51
  @translation.stub(:tables).and_return([@table])
41
52
  end
42
53
 
43
- context "copy" do
54
+ context "copy_data" do
44
55
  it "should call translate on the tables" do
45
56
  @no_sql_connection.should_receive(:insert_into).and_return(true)
46
57
  @table.should_receive(:translate).once.and_return({})
47
- @translation.send(:copy)
58
+ @translation.send(:copy_data)
59
+ end
60
+ end
61
+
62
+ context "copy_embed_tables" do
63
+ before(:each) do
64
+ @target_table = mock(:name => 'posts', :embed? => false)
65
+ @embed_table = mock(:translate => {}, :name => 'comments', :embed? => true, :embed_on => 'post_id', :embed_in => 'posts')
66
+ @translation.stub(:tables).and_return([@target_table, @embed_table])
67
+ @translation.stub(:fetch_reference_ids).and_return({})
68
+ end
69
+ it "should loop through embedded tables" do
70
+ @translation.should_receive(:embed_tables).and_return([@embed_table])
71
+ @no_sql_connection.should_receive(:find_one).and_return({'_id' => 500})
72
+ @no_sql_connection.should_receive(:update)
73
+ @translation.send(:copy_embedded_tables)
48
74
  end
49
75
  end
50
76
 
@@ -47,6 +47,25 @@ describe Mongify::Translation do
47
47
  end
48
48
  end
49
49
 
50
+ context "tables reference" do
51
+ before(:each) do
52
+ @copy_table = mock(:name => 'users', :embed? => false)
53
+ @embed_table = mock(:name => 'comments', :embed? => true)
54
+ @translation = Mongify::Translation.new()
55
+ @translation.stub(:tables).and_return([@copy_table, @embed_table])
56
+ end
57
+ context "copy_tables" do
58
+ it "should return tables that are not embeded" do
59
+ @translation.copy_tables.should == [@copy_table]
60
+ end
61
+ end
62
+ context "embed_tables" do
63
+ it "should return only tables for embedding" do
64
+ @translation.embed_tables.should == [@embed_table]
65
+ end
66
+ end
67
+ end
68
+
50
69
  context "add_table" do
51
70
  before(:each) do
52
71
  @table = Mongify::Database::Table.new("users")
@@ -15,8 +15,7 @@ class GenerateDatabase
15
15
  @sqlite_connecton ||= Mongify::Database::SqlConnection.new(:adapter => CONNECTION_CONFIG.sqlite['adapter'], :database => @db_path)
16
16
  end
17
17
  def self.sqlite(include_data=true)
18
- puts ">\n\n>>sqlite_connection.database: #{sqlite_connection.database} :: Exists? #{File.exists?(sqlite_connection.database)}"
19
- puts ("DELETING>>>>") && File.delete(sqlite_connection.database) if File.exists?(sqlite_connection.database)
18
+ File.delete(sqlite_connection.database) if File.exists?(sqlite_connection.database)
20
19
 
21
20
  conn = sqlite_connection.connection
22
21
 
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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
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-15 00:00:00 -05:00
18
+ date: 2011-01-16 00:00:00 -05:00
19
19
  default_executable: mongify
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency