elbasti

elbasti

Generating `schema_migrations` table automatically in Livebook with sqlite3

Hi everyone,

I’m trying to run a LiveBook that uses ecto and sqlite.

When I trun to run a migration, I get the following error: ** (Exqlite.Error) no such table: schema_migrations.

I suppose I could create the table manually, but I was wondering if there’s a way it can be created automatically. Since I’m using livebook, I can’t really run the mix tasks.

This is the content of my livebook:

Get Sqlite to work

Mix.install(
  [
    {:ecto, "~> 3.11"},
    {:ecto_sqlite3, "~> 0.13"},
    {:ecto_sql, "~> 3.11"},
    {:kino, "~> 0.12.3"}
  ],
  config: [
    my_app: [
      {:ecto_repos, [MyApp.Repo]},
      {:"Elixir.MyApp.Repo", [database: :memory]}
    ]
  ]
)

Setup Repo

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.SQLite3
end

alias MyApp.Repo
{:ok, repo_pid} = Kino.start_child(Repo)
# Ecto.Adapters.SQL.query!(MyApp.Repo, "create table users (id int)")
defmodule Migrations.AddPositionsTable do
  use Ecto.Migration

  def up do
    create table("positions") do
      add(:city, :string, size: 40)
      add(:temp_lo, :integer)
      add(:temp_hi, :integer)
      add(:prcp, :float)

      timestamps()
    end
  end

  def down do
    drop(table("positions"))
  end
end

Ecto.Migrator.run(Repo, [{0, Migrations.AddPositionsTable}], :up, all: true)

Whith this being the entirety of the error message when I try to run the migration:

** (Exqlite.Error) no such table: schema_migrations
SELECT s0."version" FROM "schema_migrations" AS s0
    (ecto_sql 3.11.1) lib/ecto/adapters/sql.ex:1054: Ecto.Adapters.SQL.raise_sql_call_error/1
    (ecto_sql 3.11.1) lib/ecto/adapters/sql.ex:952: Ecto.Adapters.SQL.execute/6
    (ecto 3.11.2) lib/ecto/repo/queryable.ex:232: Ecto.Repo.Queryable.execute/4
    (ecto 3.11.2) lib/ecto/repo/queryable.ex:19: Ecto.Repo.Queryable.all/3
    (ecto_sql 3.11.1) lib/ecto/migrator.ex:586: anonymous fn/5 in Ecto.Migrator.lock_for_migrations/4
    (ecto_sql 3.11.1) lib/ecto/migrator.ex:585: Ecto.Migrator.lock_for_migrations/4
    (ecto_sql 3.11.1) lib/ecto/migrator.ex:432: Ecto.Migrator.run/4

Any help on how to create the schema_migrations table would be greatly appreciated!

Marked As Solved

D4no0

D4no0

Sadly, by the nature of how ecto migrations work, you will not be able to use the in-memory option.

Just change the configuration to point to a local file and everything should work as expected.

Also Liked

D4no0

D4no0

When you start sqlite with in-memory option, the database exists only as long as the process is alive. In this case, ecto currently doesn’t guarantee that migrations will be done in the same process, so when a new process is started there are no migrations left as the database is in memory.

This is a limitation of ecto design as it was more oriented in interacting with persistent databases, maybe it will be fixed in the future, however from what I understand is not that trivial.

DreamEmulator

DreamEmulator

Seb here, long time lurker and budding fan of this community..
Just wanted to drop in and says thanks for these kinds of answers :folded_hands:

It’s so refreshing to see real people really helping each other learn.
Specially now amongst all the AI hype I appreciate it so much more to join a community of really nice real people taking time for each other :handshake:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement