kardiojack

kardiojack

Hey everyone,

I am designing an app with Phoenix and am running into a mental block with how to architect importing and exporting user data. I need to allow users to import and export their data from a competitor app by uploading a SQLite file. I would like to validate and process this file for each user and then add their data to my app’s main Postgres database.

The Postgres database is easy–I will just design its schema with Ecto with the docs as my guide. I also have been able to manually add the SQLite schema and that works fine with Ecto if I hardcode the db path.

I have run into two issues:

  1. the standard way in the docs for Ecto and SQLite is to hard code the database path. I need to use a dynamic path at run time after the user uploads their SQLite database via the web app.

  2. As far as I can tell Phoenix is set up to work with one database. How can I use the SQLite AND Postgres in the same app?

Hopefully this makes sense.

Thanks! :smiley:

Jack

Showing Posts 1 to 3

mindok

mindok

Using two repos in the same application is pretty easy, particularly if one of them is read-only so you don’t have to worry about migrations. In your config.exs, search for ecto_repos: - you will see it points to a list rather than a single value, so you can add extra repos in there. Your main application.ex reads this to figure out which repos to start on system startup.

The bigger problem will be dynamically connecting to different SQLite files.

To be honest, I’d bypass Ecto on the SQLite site and use Exqlite directly. The read api using Exqlite.Basic is pretty straightforward and likely all you need. It goes along the lines of:

  1. Get a connection to the file:
{:ok, db} = Exqlite.Connection.connect(database: <path to database file>)
  1. Execute your fave select queries:
    {:ok, rows, cols} =
      db
      |> Exqlite.Basic.exec("SELECT stuff, more_stuff FROM table_of_stuff WHERE owner=?", ["jack"])
      |> Exqlite.Basic.rows()
  1. Close the connection when you’re finished (it probably falls out of scope and cleans up itself, but better safe than sorry)
Exqlite.Basic.close(db)
  1. Do what you want with the data in rows
rows |> Enum.map(...mangle_data_to_fit_new...) |> Enum.each(...insert into new repo...)

Writing is pretty similar. You may want to set a few different parameters on the initial connection to ensure the SQLite file is creating with the right settings, then you will use CREATE & INSERT statements to create the destination tables and add data.

The only drama I have had with this approach is execution (i.e. Exqlite.Basic.exec(...)) failing silently sometimes - e.g. if you have 5 parameter placeholders and only supply 4 parameters. I haven’t dug into it, but make sure you build in some testing!

kardiojack

kardiojack OP

Thanks so much for your help! People weren’t exaggerating about the Elixir community being super welcoming.

Makes sense about bypassing Ecto–that was my plan B, unless someone suggested it as a working okay for a plan A lol.

MRdotB

MRdotB

I did something similar I needed to export a part of our postgres database to a sqlite a year ago.

The dynamic repo and the proper cleanup took me some time to get right but it work well.

I got a dynamic repo to start a sqlite and stop and rm the sqlite database after I upload it on s3

defmodule YourApp.DynamicRepo do
  use Ecto.Repo, otp_app: :evoluflor_api, adapter: Ecto.Adapters.SQLite3

  require Logger

  @doc """
  Create a dynamic sqlite repo.
  Check https://hexdocs.pm/ecto/replicas-and-dynamic-repositories.html for more details
  """
  def open_database(db_path) do
    __MODULE__.start_link(
      name: nil,
      otp_app: :your_app,
      adapter: Ecto.Adapters.SQLite3,
      database: db_path,
      # important we can copy the app at any time otherwise some data can be missing
      journal_mode: :off
    )
  end

  def cleanup_database(%{repo_pid: repo_pid, db_path: db_path}) do
    :ok = Supervisor.stop(repo_pid)
    :ok = cleanup_database(db_path)
    Logger.debug("✔️ cleanup successfully stopped and cleaned up the sqlite database")
    :ok
  end

  def cleanup_database(db_path) do
    File.rm!(db_path)
  end
end

In my case the sqlite database I open already have the tables created
I have a lot of code to map the data from postgres to sqlite change the boolean to 1 or 2, handle the timestamp, the jsonb …
the file look like that
Then I insert the list result in the sqlite database

{:ok, repo_pid} = DynamicRepo.open_database(db_path)
# Set the repo_pid for the current process
DynamicRepo.put_dynamic_repo(repo_pid)
# Query and Map data from postgres to sqlite
data = [[id: 1, count: 1, type: "ticket"]]
# Insert the data in my sqlite database
DynamicRepo.insert_all(table_name, data, on_conflict: :raise)
# I upload the sqlite database to s3 and use my cleanup function that stop the process and delete the local database
DynamicRepo.cleanup_database(%{repo_pid: repo_pid, db_path: db_path})

The only problem we got is when the postgres database got new migration and our mapper is not updated.

— All posts loaded —

Where Next? Top

Trending in Questions Top

axelson
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
yasoob
Hi all! I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
caioaao
Hey, I know this has been discussed ad nauseum, but I couldn’t find a definitive answer or at least some consensus around this, so let me...
New
Liam_or
I don’t suppose you can lazy load a library depending on runtime data? For example if it’s a certain user they can lazy load ‘library A’ ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
PJUllrich
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews