File tree 1 file changed +12
-10
lines changed
lib/redis_counters/dumpers
1 file changed +12
-10
lines changed Original file line number Diff line number Diff line change @@ -74,35 +74,37 @@ def initialize(engine)
74
74
75
75
def merge
76
76
target_fields = fields . join ( ', ' )
77
+ temp_source = "_source_#{ source_table } "
77
78
78
79
sql = <<-SQL
80
+ CREATE TEMP TABLE #{ temp_source } ON COMMIT DROP AS
81
+ SELECT #{ selected_fields_expression }
82
+ FROM #{ source_table }
83
+ #{ source_conditions_expression }
84
+ #{ group_by_expression } ;
85
+
79
86
WITH
80
- source AS
81
- (
82
- SELECT #{ selected_fields_expression }
83
- FROM #{ source_table }
84
- #{ source_conditions_expression }
85
- #{ group_by_expression }
86
- ),
87
87
updated AS
88
88
(
89
89
UPDATE #{ target_table } target
90
90
SET
91
91
#{ updating_expression }
92
- FROM source
92
+ FROM #{ temp_source } AS source
93
93
WHERE #{ matching_expression }
94
94
#{ extra_conditions }
95
95
RETURNING target.*
96
96
)
97
97
INSERT INTO #{ target_table } (#{ target_fields } )
98
98
SELECT #{ target_fields }
99
- FROM source
99
+ FROM #{ temp_source } as source
100
100
WHERE NOT EXISTS (
101
101
SELECT 1
102
102
FROM updated target
103
103
WHERE #{ matching_expression }
104
104
#{ extra_conditions }
105
- )
105
+ );
106
+
107
+ DROP TABLE #{ temp_source } ;
106
108
SQL
107
109
108
110
sql = model . send ( :sanitize_sql , [ sql , engine . common_params ] )
You can’t perform that action at this time.
0 commit comments