fuelen / ecto_dev_logger

An alternative logger for Ecto queries
Apache License 2.0
151 stars 17 forks source link

Issue with SQLite #35

Closed firesidewing closed 2 weeks ago

firesidewing commented 1 month ago

Hi,

I've hooked this up previously on one of my apps when using postgres but I'm using SQLite in another and can't seem to get things working. Is there anything extra i need to do or is SQLite not supported?

[error] Handler [:ecto_dev_logger, :revenue_generator, :repo] has failed and has been detached. Class=:error Reason=:function_clause Stacktrace=[ {Ecto.DevLogger, :inline_params, [ "SELECT u1.\"id\", u1.\"email\", u1.\"name\", u1.\"role\", u1.\"hashed_password\", u1.\"confirmed_at\", u1.\"inserted_at\", u1.\"updated_at\" FROM \"users_tokens\" AS u0 INNER JOIN \"users\" AS u1 ON u1.\"id\" = u0.\"user_id\" WHERE ((u0.\"token\" = ?) AND (u0.\"context\" = ?)) AND (u0.\"inserted_at\" > CAST (strftime('%Y-%m-%d %H:%M:%f000Z',?,-(60) || ' day') AS TEXT))", [ {:blob, <<40, 104, 14, 10, 236, 235, 76, 132, 167, 97, 240, 23, 135, 117, 156, 21, 60, 108, 141, 75, 70, 112, 229, 171, 175, 43, 223, 74, 166, 61, 142, 113>>}, "session", ~U[2024-09-11 16:44:11.867818Z] ], :cyan, Ecto.Adapters.SQLite3 ], [file: ~c"lib/ecto/dev_logger.ex", line: 108]}, {Ecto.DevLogger, :"-telemetry_handler/4-fun-0-", 8, [file: ~c"lib/ecto/dev_logger.ex", line: 99]}, {Logger, :__do_log__, 4, [file: ~c"lib/logger.ex", line: 931]}, {:telemetry, :"-execute/3-fun-0-", 4, [ file: ~c"/home/josh/revenue_generator/deps/telemetry/src/telemetry.erl", line: 160 ]}, {:lists, :foreach_1, 2, [file: ~c"lists.erl", line: 2310]}, {Ecto.Adapters.SQL, :log, 4, [file: ~c"lib/ecto/adapters/sql.ex", line: 1258]}, {DBConnection, :log, 5, [file: ~c"lib/db_connection.ex", line: 1704]}, {Ecto.Adapters.SQLite3.Connection, :prepare_execute, 5, [file: ~c"lib/ecto/adapters/sqlite3/connection.ex", line: 44]}, {Ecto.Adapters.SQL, :execute!, 5, [file: ~c"lib/ecto/adapters/sql.ex", line: 984]}, {Ecto.Adapters.SQL, :execute, 6, [file: ~c"lib/ecto/adapters/sql.ex", line: 976]}, {Ecto.Repo.Queryable, :execute, 4, [file: ~c"lib/ecto/repo/queryable.ex", line: 232]}, {Ecto.Repo.Queryable, :all, 3, [file: ~c"lib/ecto/repo/queryable.ex", line: 19]}, {Ecto.Repo.Queryable, :one, 3, [file: ~c"lib/ecto/repo/queryable.ex", line: 154]}, {Phoenix.LiveView.Utils, :assign_new, 3, [file: ~c"lib/phoenix_live_view/utils.ex", line: 79]}, {RevenueGeneratorWeb.UserAuth, :on_mount, 4, [file: ~c"lib/revenue_generator_web/user_auth.ex", line: 153]}, {Phoenix.LiveView.Lifecycle, :"-mount/3-fun-0-", 4, [file: ~c"lib/phoenix_live_view/lifecycle.ex", line: 158]}, {Phoenix.LiveView.Lifecycle, :reduce_socket, 3, [file: ~c"lib/phoenix_live_view/lifecycle.ex", line: 237]}, {Phoenix.LiveView.Utils, :"-maybe_call_live_view_mount!/5-fun-0-", 6, [file: ~c"lib/phoenix_live_view/utils.ex", line: 341]}, {:telemetry, :span, 3, [ file: ~c"/home/josh/revenue_generator/deps/telemetry/src/telemetry.erl", line: 321 ]}, {Phoenix.LiveView.Channel, :verified_mount, 8, [file: ~c"lib/phoenix_live_view/channel.ex", line: 1174]} ]

fuelen commented 1 month ago

Hi

Yes, SQLite is not supported yet. Different adapters use different placeholders in prepared statements. I see that ? is used in your query, the same placeholder as for MySQL. Try to add Ecto.Adapters.SQLite3 to this function clause https://github.com/fuelen/ecto_dev_logger/blob/42a4b29651db5a1534ce6881d8bae99ec0aa0b78/lib/ecto/dev_logger.ex#L142 similarly to https://github.com/fuelen/ecto_dev_logger/blob/42a4b29651db5a1534ce6881d8bae99ec0aa0b78/lib/ecto/dev_logger.ex#L112-L113 Perhaps, that's the only change that is needed to support SQLite

firesidewing commented 1 month ago

Yeah looks like that works, mind if I create a PR?

fuelen commented 1 month ago

PR is welcome