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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement