elixir-sqlite/sqlite_ecto2

Add documentation from sqlite_ecto

taufiqkh opened this issue ยท 14 comments

Documentation needs to be added from sqlite_ecto, and updated to reflect the changes made for Ecto 2.

I'm happy to help with the update as I'm currently going through the tutorial, though I'm quite new to Elixir. I currently have the docs as .md files in a top-level docs directory.

Tutorial is all done except the last step, which attempts to test a uniqueness constraint. The step adds a migration with the following:

  def change do
    create index(:users, [:name], unique: true)
  end

and the following test:

    assert_raise Sqlite.Ecto.Error, "constraint: UNIQUE constraint failed: users.name", fn ->
      %User{name: "ludwig_wittgenstein", password: "NOT_THE_REAL_USER"} |> Repo.insert
    end

Instead, I get Ecto.StaleEntryError, and I'm not familiar enough to know whether this is a mistake on my part or a bug elsewhere:

     Expected exception Sqlite.Ecto.Error but got Ecto.StaleEntryError (attempted to insert a stale struct:
     
     %Blog.User{__meta__: #Ecto.Schema.Metadata<:built, "users">, email: nil, id: nil, inserted_at: nil, name: "ludwig_wittgenstein", password: "NOT_THE_REAL_USER", posts: #Ecto.Association.NotLoaded<association :posts is not loaded>, updated_at: nil}
     )
     stacktrace:
       (ecto) lib/ecto/repo/schema.ex:473: Ecto.Repo.Schema.apply/4
       (ecto) lib/ecto/repo/schema.ex:205: anonymous fn/13 in Ecto.Repo.Schema.do_insert/4
       (ecto) lib/ecto/repo/schema.ex:684: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/6
       (ecto) lib/ecto/adapters/sql.ex:620: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
       (db_connection) lib/db_connection.ex:1275: DBConnection.transaction_run/4
       (db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
       (db_connection) lib/db_connection.ex:790: DBConnection.transaction/3
       test/blog_test.exs:54: (test)

Perhaps I should read a tutorial...

You can follow the tutorial here to reproduce. Unfortunately the test doesn't clean up after itself and will need some tweaking to make it able to be re-run.

@taufiqkh: Looks good. A few things I see that might need adjusting:

  • Please update the sqlite_ecto2 version reference to use 2.0.0-dev.2. There were some important API changes since -dev.0.
  • One of those changes is that the module name Sqlite.Ecto is no longer used. Please update the references to point to Sqlite.Ecto2.
  • When we get to the first unit test ("The models are getting more complicated, so let's write a test โ€ฆ"), that test fails as follows:
  1) test that everything works as it should (BlogTest)
     test/blog_test.exs:15
     Assertion with == failed
     code: ["ludwig_wittgenstein"] == User |> select([user], user.name()) |> Repo.all()
     lhs:  ["ludwig_wittgenstein"]
     rhs:  ["jazzyb", "ludwig_wittgenstein", "ludwig_wittgenstein"]
     stacktrace:
       test/blog_test.exs:18: (test)

I'll investigate that and continue from there as I'm able.

FWIW I've placed a copy of the tutorial based on my exploration so far at https://github.com/scouten/sqlite_ecto2_blog_tutorial.

As for the problem with the test not cleaning up after itself, I think the tutorial should be updated to use Ecto.Adapters.SQL.Sandbox, at least in the test configuration.

I wasn't able to get to a point where I could reproduce the problem with Ecto.StaleEntryError. Could you post a repo that demonstrates that?

Thanks for that, have updated to use Sqlite.Ecto2 and version 2.0.0-dev.2. Added a post-test delete_all but I'll take a look at the sandbox.

Example blog repo posted at https://github.com/taufiqkh/sqlite_ecto2_blog_tutorial

Thanks for the example code. Can now confirm the Ecto.StaleEntryError. Investigating โ€ฆ

It looks like this is related to the issue I described in https://github.com/scouten/sqlite_ecto2#incomplete-ecto-constraint-implementation. I'm out of time tonight, but I'll see if I can find a way to improve the error reporting.

Found it. The root cause was that the constraint-violation error was getting dropped. This was in code I added to sqlitex a month or so ago to support getting IDs back from queries.

Root cause is fixed here: elixir-sqlite/sqlitex#59.

Working on pulling this through to sqlite_ecto2.

@taufiqkh please bump your example to use the just-released sqlite_ecto2 version 2.0.0-dev.3 and sqlitex 1.3.2. I ran your repo and the test passes (modulo a minor change to wording in the expected exception).

Thank you very much for alerting me to that issue! ๐Ÿ’™๐Ÿ’š๐Ÿ’›๐Ÿ’œ

When you feel ready, I look forward to a PR with the updated tutorial content!

Oh, sorry, didn't properly read your comment about the version update. Thanks for your feedback.