elixir-sqlite / ecto_sqlite3

An Ecto SQLite3 adapter.
https://hexdocs.pm/ecto_sqlite3
MIT License
288 stars 43 forks source link

Fix bad init file loading in dump_cmd/3 #111

Closed joeljuca closed 1 year ago

joeljuca commented 1 year ago

A test over dump_cmd/3 was failing with a strange error:

$ mix test
Compiling 2 files (.ex)
.........................warning: missing `:on` in join, defaulting to `on: true`.
  test/ecto/adapters/sqlite3/connection_test.exs:307: Ecto.Adapters.SQLite3.ConnectionTest."test parent binding subquery and CTE"/1

warning: missing `:on` in join on Ecto.Adapters.SQLite3.ConnectionTest.Schema2, defaulting to `on: true`.
  test/ecto/adapters/sqlite3/connection_test.exs:1412: Ecto.Adapters.SQLite3.ConnectionTest."test join with hints"/1

warning: missing `:on` in join, defaulting to `on: true`.
  test/ecto/adapters/sqlite3/connection_test.exs:1478: Ecto.Adapters.SQLite3.ConnectionTest."test join with subquery"/1

warning: missing `:on` in join, defaulting to `on: true`.
  test/ecto/adapters/sqlite3/connection_test.exs:1515: Ecto.Adapters.SQLite3.ConnectionTest."test join with fragment"/1

warning: missing `:on` in join, defaulting to `on: true`.
  test/ecto/adapters/sqlite3/connection_test.exs:1548: Ecto.Adapters.SQLite3.ConnectionTest."test join with query interpolation"/1

warning: missing `:on` in join, defaulting to `on: true`.
  test/ecto/adapters/sqlite3/connection_test.exs:1557: Ecto.Adapters.SQLite3.ConnectionTest."test lateral join with fragment"/1

...................................................................................................................................................................................

  1) test dump_cmd/3 runs command (Ecto.Adapters.SQLite3ConnTest)
     test/ecto/adapters/sqlite3_test.exs:86
     match (=) failed
     code:  assert {"CREATE TABLE test (id INTEGER PRIMARY KEY);\n", 0} = SQLite3.dump_cmd([".schema"], [], opts)
     left:  {"CREATE TABLE test (id INTEGER PRIMARY KEY);\n", 0}
     right: {".auth off\n.mode column\n.headers on\n.prompt \"> \"\nCREATE TABLE test (id INTEGER PRIMARY KEY);\n", 0}
     stacktrace:
       test/ecto/adapters/sqlite3_test.exs:98: (test)

.........................
Finished in 0.8 seconds (0.7s async, 0.09s sync)
230 tests, 1 failure

Randomized with seed 760049

The reason? Well, it's probably my .sqliterc:

.echo on
.auth off
.mode column
.headers on
.prompt "> "

The sqlite3 program loads an initialization file ~/.sqliterc, if present. Since I do have one, it'll be used during dump_cmd/3 operations. It's quite a dangerous behavior since any destructive command present in ~/.sqliterc will be executed against a project's database, possibly destroying data, etc.

I couldn't find a way to make sqlite3 not load my .sqliterc, but after setting /dev/null as -init argument, test passes again (and my .sqliterc is not being loaded/used).

warmwaffles commented 1 year ago

Weird I didn't realize sqlite had an init file it could use similar to psql's

joeljuca commented 1 year ago

I personally love it! I can easily configure and apply pre-defined queries in any database I open locally.