When two plan logs overlap current window

Stephen Paul Weber created

Extend the later one

Change summary

lib/customer_plan.rb | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

Detailed changes

lib/customer_plan.rb 🔗

@@ -164,13 +164,14 @@ protected
 			UPDATE plan_log SET date_range=range_merge(
 				date_range,
 				tsrange(
-					LOCALTIMESTAMP,
-					GREATEST(upper(date_range), LOCALTIMESTAMP) + '1 month'
+					LOWER(date_range),
+					GREATEST(UPPER(date_range), LOCALTIMESTAMP) + '1 month'
 				)
 			)
 			WHERE
 				customer_id=$1 AND
-				date_range && tsrange(LOCALTIMESTAMP, LOCALTIMESTAMP + '1 month')
+				UPPER(date_range) = (SELECT MAX(UPPER(date_range)) FROM plan_log WHERE
+				customer_id=$1 AND date_range && tsrange(LOCALTIMESTAMP, LOCALTIMESTAMP + '1 month'))
 		SQL
 	end
 end