This repository was archived by the owner on Dec 22, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +30
-10
lines changed Expand file tree Collapse file tree 4 files changed +30
-10
lines changed Original file line number Diff line number Diff line change 1
1
PATH
2
2
remote: .
3
3
specs:
4
- mosql (0.1.0 )
4
+ mosql (0.1.1 )
5
5
bson_ext
6
6
json
7
7
log4r
17
17
bson (1.8.2 )
18
18
bson_ext (1.8.2 )
19
19
bson (~> 1.8.2 )
20
- json (1.7.6 )
20
+ json (1.7.7 )
21
21
log4r (1.1.10 )
22
22
metaclass (0.0.1 )
23
23
minitest (3.0.0 )
Original file line number Diff line number Diff line change @@ -253,11 +253,12 @@ def optail
253
253
end
254
254
255
255
def sync_object ( ns , _id )
256
+ sqlid = @sql . transform_one_ns ( ns , { '_id' => _id } ) [ '_id' ]
256
257
obj = collection_for_ns ( ns ) . find_one ( { :_id => _id } )
257
258
if obj
258
259
@sql . upsert_ns ( ns , obj )
259
260
else
260
- @sql . table_for_ns ( ns ) . where ( :_id => _id ) . delete ( )
261
+ @sql . table_for_ns ( ns ) . where ( :_id => sqlid ) . delete ( )
261
262
end
262
263
end
263
264
Original file line number Diff line number Diff line change @@ -25,22 +25,24 @@ def table_for_ns(ns)
25
25
@db [ @schema . table_for_ns ( ns ) . intern ]
26
26
end
27
27
28
-
29
- def upsert_ns ( ns , obj )
28
+ def transform_one_ns ( ns , obj )
30
29
h = { }
31
30
cols = @schema . all_columns ( @schema . find_ns ( ns ) )
32
31
row = @schema . transform ( ns , obj )
33
32
cols . zip ( row ) . each { |k , v | h [ k ] = v }
33
+ h
34
+ end
35
+
36
+ def upsert_ns ( ns , obj )
37
+ h = transform_one_ns ( ns , obj )
34
38
upsert ( table_for_ns ( ns ) , h )
35
39
end
36
40
37
41
# obj must contain an _id field. All other fields will be ignored.
38
42
def delete_ns ( ns , obj )
39
- cols = @schema . all_columns ( @schema . find_ns ( ns ) )
40
- row = @schema . transform ( ns , obj )
41
- sqlid = row [ cols . index ( "_id" ) ]
42
- raise "No _id found in transform of #{ obj } " if sqlid . nil?
43
- table_for_ns ( ns ) . where ( :_id => sqlid ) . delete
43
+ h = transform_one_ns ( ns , obj )
44
+ raise "No _id found in transform of #{ obj . inspect } " if h [ '_id' ] . nil?
45
+ table_for_ns ( ns ) . where ( :_id => h [ '_id' ] ) . delete
44
46
end
45
47
46
48
def upsert ( table , item )
Original file line number Diff line number Diff line change @@ -56,4 +56,21 @@ def fake_cli
56
56
} )
57
57
assert_equal ( 0 , sequel [ :sqltable ] . where ( :_id => o [ '_id' ] . to_s ) . count )
58
58
end
59
+
60
+ it 'handle "u" ops with $set and BSON::ObjectIDs' do
61
+ o = { '_id' => BSON ::ObjectId . new , 'var' => 17 }
62
+ @adapter . upsert_ns ( 'mosql_test.collection' , o )
63
+
64
+ # $set's are currently a bit of a hack where we read the object
65
+ # from the db, so make sure the new object exists in mongo
66
+ connect_mongo [ 'mosql_test' ] [ 'collection' ] . insert ( o . merge ( 'var' => 100 ) ,
67
+ :w => 1 )
68
+
69
+ @cli . handle_op ( { 'ns' => 'mosql_test.collection' ,
70
+ 'op' => 'u' ,
71
+ 'o2' => { '_id' => o [ '_id' ] } ,
72
+ 'o' => { '$set' => { 'var' => 100 } } ,
73
+ } )
74
+ assert_equal ( 100 , sequel [ :sqltable ] . where ( :_id => o [ '_id' ] . to_s ) . select . first [ :var ] )
75
+ end
59
76
end
You can’t perform that action at this time.
0 commit comments