miguelvillaresb

miguelvillaresb

Ecto and TDS to access MSSQL Server databases

I’m using Ecto and TDS to access a Microsoft SQL Server instance from an Elixir application with multiple databases, specifically one database per client. When configuring the TDS repo, I’m required to specify a specific database, but in the application I need to access different databases depending on the client making the request.

The databases are named following the <customer_id>_database naming convention.

I’m trying to access multiple databases using a single repository, as there are dozens or hundreds of clients. I’ve tried using query prefixes as well as Ecto syntax, but in all cases, I receive errors indicating that the <customer_id>_database.dbo.table objects are inaccessible. The only way I’ve managed to access different databases is by directly executing SQL queries like this:

  sql_query = """
  SELECT c.snombrecli FROM \"14637_database\".dbo.clientes AS c WHERE c.icodcli = 1;
  """
  {:ok, result} = Repo.query(sql_query, [])

Is there an alternative to using Ecto with Microsoft SQL Server to comfortably access N client databases?

Thanks in advance

First 4 of 4 Posts Switch mode

joddm

joddm

A possible point in the right direction might be dynamic repos

Replicas and dynamic repositories — Ecto v3.11.1 (hexdocs.pm)

Underjord | Ecto & Multi-tenancy - Dynamic Repos - Part 1 - Getting started

Edit:
Also one reason why you’re not getting query prefixes to work is maybe you haven’t configured connection prefixes correctly? Multi tenancy with query prefixes — Ecto v3.11.1 (hexdocs.pm)

Underjord | Ecto & Multi-tenancy - Prefixes - Part 3

(I don’t have any experience using MS SQL or TDS)

miguelvillaresb

miguelvillaresb

Thanks for the reply. I already checked these references but I am not getting good results making some tests. It seems that query prefix support is fully implemented on PostgreSQL and MySQL but not really sure about Microsoft SQL Server with TDS.

sbuttgereit

sbuttgereit

I’m doing some MS SQL server work right now (not Elixir based) and thought I’d poke around.

I saw something interesting in the ecto_sql TDS related code (which is where the idea of prefix will be implemented, not the Elixir tds driver).

https://github.com/elixir-ecto/ecto_sql/blob/da2b3b5f42186d70395464f19fabf50e7d9aa1b5/lib/ecto/adapters/tds/connection.ex#L1662C10-L1662C21

If you look at the these quote_table/2 definitions inside the TDS adapter, it looks like they may take the prefix in the form of a tuple. Specifically it looks like prefix could take the form {server, db, schema} or {db, schema}.

Here’s another snippet of code from ecto_sql where you can see the quote_table/2 function get called from insert/7 (ecto_sql/lib/ecto/adapters/tds/connection.ex at da2b3b5f42186d70395464f19fabf50e7d9aa1b5 · elixir-ecto/ecto_sql · GitHub):

["INSERT INTO ", quote_table(prefix, table), values]

Now some caveats: I’ve only done a cursory search of the source code over my morning coffee; I’ve not tested this; I’ve not done an exhaustive search over documentation; I haven’t looked to see if this was support that was started and not finished. All of that could apply and it could be that there is some parsing somewhere above these functions that is expecting a different form for the designations of db and schema. I could see issues for this in some representations.

Anyway, it’s a lead that might get you to a better answer.

miguelvillaresb

miguelvillaresb

Really good catch, many thanks for that. My code does not work as it is but I tried to include the prefix as a tuple in the Ecto query and now I am getting data from the desired database and schema, with something like this:

      query =
        from(c in "clientes",
          where: c.icodcli == 1,
          select: c.snombrecli
        )

      results = MNRepo.all(query, prefix: {"14637_advAboUsu1", "dbo"})

Thanks again for helping!!!

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement