diff --git a/apps/bender/priv/migrations/sequence/1770994589112-copy-sequence.erl b/apps/bender/priv/migrations/sequence/1770994589112-copy-sequence.erl index 0aae543..5599bcd 100644 --- a/apps/bender/priv/migrations/sequence/1770994589112-copy-sequence.erl +++ b/apps/bender/priv/migrations/sequence/1770994589112-copy-sequence.erl @@ -15,12 +15,20 @@ perform_batch(Connection, Offset, Limit) -> {ok, _, Rows} -> Values = lists:foldl( fun - ({ID, AuxState}, "") -> - #{value := Value} = binary_to_term(AuxState), - " ('" ++ unicode:characters_to_list(ID) ++ "', " ++ integer_to_list(Value) ++ ") "; + ({ID, AuxState}, "" = Acc) -> + case value(ID, AuxState) of + {ok, Value} -> + " ('" ++ unicode:characters_to_list(ID) ++ "', " ++ integer_to_list(Value) ++ ") "; + {error, _} -> + Acc + end; ({ID, AuxState}, Acc) -> - #{value := Value} = binary_to_term(AuxState), - " ('" ++ unicode:characters_to_list(ID) ++ "', " ++ integer_to_list(Value) ++ "), " ++ Acc + case value(ID, AuxState) of + {ok, Value} -> + " ('" ++ unicode:characters_to_list(ID) ++ "', " ++ integer_to_list(Value) ++ "), " ++ Acc; + {error, _} -> + Acc + end end, "", Rows @@ -31,3 +39,19 @@ perform_batch(Connection, Offset, Limit) -> ), perform_batch(Connection, Offset + erlang:length(Rows), Limit) end. + +value(ID, null) -> + logger:warning("migrations. sequence ~p state is null", [ID]), + {error, state_is_null}; +value(ID, AuxState) when is_binary(AuxState) -> + try binary_to_term(AuxState) of + #{value := Value} -> + {ok, Value}; + BadState -> + logger:warning("migrations. sequence ~p bad state: ~p", [ID, BadState]), + {error, bad_state} + catch + _Error:_Term:_Stack -> + logger:warning("migrations. sequence ~p bad state: ~p", [ID, AuxState]), + {error, bad_state} + end.