Skip to content

postgres: boss_db:delete() in transaction always rollbacks transaction if no 'counters' table exists in the db #135

@brigadier

Description

@brigadier

Postgresql
The following code never succeeds unless there's a 'counters' table in the database:

F = fun() ->
   %% some other things that will allways be rolled back
   boss_db:delete("model-1"); %%never gets deleted, gets rolled back, but returns ok
   boss_db:delete("model-2"); %%throws an exception about rolled back transaction
end,
boss_db:transaction(F)

the same code outside of tranaction works

boss_db:delete("model-1"); %%succeeds

The reason is the function delete:

delete(Conn, Id) when is_list(Id) ->
    {_, TableName, IdColumn, TableId} = boss_sql_lib:infer_type_from_id(Id),
    Res = pgsql:equery(Conn, ["DELETE FROM ", TableName, " WHERE ", IdColumn, " = $1"], [TableId]),
    case Res of
        {ok, _Count} -> 
            pgsql:equery(Conn, "DELETE FROM counters WHERE name = $1", [Id]),
            ok;
        {error, Reason} -> {error, Reason}
    end.

This functions executes the second SQL query which fails and rollbacks the whole transaction.
A result of the second pgsql:equery is ignored so it returns ok every time even then the query fails.

I'm not sure if this should be considered a bug or not.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions