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

Marked As Solved

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!!!

Also Liked

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.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement