mover 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/mover.rb +52 -33
  2. data/lib/mover/version.rb +1 -1
  3. metadata +2 -2
@@ -38,19 +38,38 @@ module Mover
38
38
 
39
39
  # Columns
40
40
  magic = options[:magic] || 'moved_at'
41
- insert = from_class.column_names & to_class.column_names
42
- insert -= [ magic ]
43
- insert.collect! { |col| connection.quote_column_name(col) }
44
- select = insert.clone
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
- # Magic columns
47
- if to_class.column_names.include?(magic)
48
- insert << connection.quote_column_name(magic)
49
- if options[:migrate]
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
- select << connection.quote(Time.now.utc)
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 #{to_class.table_name} (#{insert.join(', ')})
93
- SELECT #{select.join(', ')}
94
- FROM #{from_class.table_name}
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 |i|
99
- if i.include?("'")
100
- "#{to_class.table_name}.#{i} = #{i}"
117
+ update = insert.collect do |column, value|
118
+ if value.include?("'")
119
+ "#{to[:table]}.#{column} = #{value}"
101
120
  else
102
- "#{to_class.table_name}.#{i} = #{from_class.table_name}.#{i}"
121
+ "#{to[:table]}.#{column} = #{from[:table]}.#{value}"
103
122
  end
104
123
  end
105
124
 
106
125
  connection.execute(<<-SQL)
107
- INSERT INTO #{to_class.table_name} (#{insert.join(', ')})
108
- SELECT #{select.join(', ')}
109
- FROM #{from_class.table_name}
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!(to_class.table_name, 't')
116
- conditions.gsub!(from_class.table_name, 'f')
117
- select.collect! { |s| s.include?('`') ? "f.#{s}" : s }
118
- set = insert.collect do |i|
119
- if i.include?("'")
120
- "t.#{i} = #{i}"
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.#{i} = f.#{i}"
141
+ "t.#{column} = f.#{value}"
123
142
  end
124
143
  end
125
144
 
126
145
  connection.execute(<<-SQL)
127
- UPDATE #{to_class.table_name}
146
+ UPDATE #{to[:table]}
128
147
  AS t
129
- INNER JOIN #{from_class.table_name}
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 #{to_class.table_name} (#{insert.join(', ')})
156
+ INSERT INTO #{to[:table]} (#{insert.keys.join(', ')})
138
157
  SELECT #{select.join(', ')}
139
- FROM #{from_class.table_name}
158
+ FROM #{from[:table]}
140
159
  AS f
141
- LEFT OUTER JOIN #{to_class.table_name}
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 #{from_class.table_name} #{where}")
171
+ connection.execute("DELETE FROM #{from[:table]} #{where}")
153
172
  end
154
173
 
155
174
  exec_callbacks.call after
@@ -1,3 +1,3 @@
1
1
  module Mover
2
- VERSION = "0.3.2" unless defined?(::Mover::VERSION)
2
+ VERSION = "0.3.3" unless defined?(::Mover::VERSION)
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 2
9
- version: 0.3.2
8
+ - 3
9
+ version: 0.3.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Winton Welsh