mover 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mover.rb +52 -33
- data/lib/mover/version.rb +1 -1
- metadata +2 -2
data/lib/mover.rb
CHANGED
@@ -38,19 +38,38 @@ module Mover
|
|
38
38
|
|
39
39
|
# Columns
|
40
40
|
magic = options[:magic] || 'moved_at'
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
from = {
|
42
|
+
:columns => from_class.column_names,
|
43
|
+
:table => from_class.table_name
|
44
|
+
}
|
45
|
+
to = {
|
46
|
+
:columns => to_class.column_names,
|
47
|
+
:table => to_class.table_name
|
48
|
+
}
|
45
49
|
|
46
|
-
#
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
select << connection.quote_column_name(magic)
|
50
|
+
# insert[column] = value
|
51
|
+
insert = (from[:columns] & to[:columns]).inject({}) do |hash, column|
|
52
|
+
if column == magic && !options[:migrate]
|
53
|
+
hash[column] = Time.now.utc
|
51
54
|
else
|
52
|
-
|
55
|
+
hash[column] = column
|
53
56
|
end
|
57
|
+
hash
|
58
|
+
end
|
59
|
+
|
60
|
+
# Magic column not in "from" table
|
61
|
+
if to[:columns].include?(magic) && !from[:columns].include?(magic)
|
62
|
+
insert[magic] = Time.now.utc
|
63
|
+
end
|
64
|
+
|
65
|
+
# Quote everything
|
66
|
+
insert = insert.inject({}) do |hash, (column, value)|
|
67
|
+
if value.is_a?(Time)
|
68
|
+
hash[connection.quote_column_name(column)] = connection.quote(value)
|
69
|
+
else
|
70
|
+
hash[connection.quote_column_name(column)] = connection.quote_column_name(value)
|
71
|
+
end
|
72
|
+
hash
|
54
73
|
end
|
55
74
|
|
56
75
|
# Callbacks
|
@@ -89,44 +108,44 @@ module Mover
|
|
89
108
|
|
90
109
|
if options[:quick]
|
91
110
|
connection.execute(<<-SQL)
|
92
|
-
INSERT INTO #{
|
93
|
-
SELECT #{
|
94
|
-
FROM #{
|
111
|
+
INSERT INTO #{to[:table]} (#{insert.keys.join(', ')})
|
112
|
+
SELECT #{insert.values.join(', ')}
|
113
|
+
FROM #{from[:table]}
|
95
114
|
#{where}
|
96
115
|
SQL
|
97
116
|
elsif !options[:generic] && connection.class.to_s.include?('Mysql')
|
98
|
-
update = insert.collect do |
|
99
|
-
if
|
100
|
-
"#{
|
117
|
+
update = insert.collect do |column, value|
|
118
|
+
if value.include?("'")
|
119
|
+
"#{to[:table]}.#{column} = #{value}"
|
101
120
|
else
|
102
|
-
"#{
|
121
|
+
"#{to[:table]}.#{column} = #{from[:table]}.#{value}"
|
103
122
|
end
|
104
123
|
end
|
105
124
|
|
106
125
|
connection.execute(<<-SQL)
|
107
|
-
INSERT INTO #{
|
108
|
-
SELECT #{
|
109
|
-
FROM #{
|
126
|
+
INSERT INTO #{to[:table]} (#{insert.keys.join(', ')})
|
127
|
+
SELECT #{insert.values.join(', ')}
|
128
|
+
FROM #{from[:table]}
|
110
129
|
#{where}
|
111
130
|
ON DUPLICATE KEY
|
112
131
|
UPDATE #{update.join(', ')};
|
113
132
|
SQL
|
114
133
|
else
|
115
|
-
conditions.gsub!(
|
116
|
-
conditions.gsub!(
|
117
|
-
select.collect
|
118
|
-
set = insert.collect do |
|
119
|
-
if
|
120
|
-
"t.#{
|
134
|
+
conditions.gsub!(to[:table], 't')
|
135
|
+
conditions.gsub!(from[:table], 'f')
|
136
|
+
select = insert.values.collect { |i| i.include?("'") ? i : "f.#{i}" }
|
137
|
+
set = insert.collect do |column, value|
|
138
|
+
if value.include?("'")
|
139
|
+
"t.#{column} = #{value}"
|
121
140
|
else
|
122
|
-
"t.#{
|
141
|
+
"t.#{column} = f.#{value}"
|
123
142
|
end
|
124
143
|
end
|
125
144
|
|
126
145
|
connection.execute(<<-SQL)
|
127
|
-
UPDATE #{
|
146
|
+
UPDATE #{to[:table]}
|
128
147
|
AS t
|
129
|
-
INNER JOIN #{
|
148
|
+
INNER JOIN #{from[:table]}
|
130
149
|
AS f
|
131
150
|
ON f.id = t.id
|
132
151
|
AND #{conditions}
|
@@ -134,11 +153,11 @@ module Mover
|
|
134
153
|
SQL
|
135
154
|
|
136
155
|
connection.execute(<<-SQL)
|
137
|
-
INSERT INTO #{
|
156
|
+
INSERT INTO #{to[:table]} (#{insert.keys.join(', ')})
|
138
157
|
SELECT #{select.join(', ')}
|
139
|
-
FROM #{
|
158
|
+
FROM #{from[:table]}
|
140
159
|
AS f
|
141
|
-
LEFT OUTER JOIN #{
|
160
|
+
LEFT OUTER JOIN #{to[:table]}
|
142
161
|
AS t
|
143
162
|
ON f.id = t.id
|
144
163
|
WHERE (
|
@@ -149,7 +168,7 @@ module Mover
|
|
149
168
|
end
|
150
169
|
|
151
170
|
unless options[:copy]
|
152
|
-
connection.execute("DELETE FROM #{
|
171
|
+
connection.execute("DELETE FROM #{from[:table]} #{where}")
|
153
172
|
end
|
154
173
|
|
155
174
|
exec_callbacks.call after
|
data/lib/mover/version.rb
CHANGED