railsmechanic

railsmechanic

Hi Alchemists,

we’re investigating whether the migration of our (simple) Rails API to Phoenix is a viable step for us.
As we can’t migrate the Rails database schema to ecto, I’m interested in how to use Phoenix just with raw SQL queries. Is it recommended to drop ecto and setup the database connection pooling etc. from scratch, or do you recommend to keep ecto because of all the great things like connection pooling etc.?

Long story short:
How to use Phoenix just with raw SQL queries? Are there any examples about that subject?

Many thanks

Showing Posts 1 to 10

sashaafm

sashaafm

This Stackoverflow thread might be of interested and quite simple.

dgamidov

dgamidov

@railsmechanic, you will loose a lot of cool things without ecto.
Check this video:

andre1sk

andre1sk

If you opt for stored procedures as opposed to using raw queries you will actually gain a lot of flexibility compared to using ORM.

railsmechanic

railsmechanic OP

I know, but a complete migration to ecto is not possible at the moment.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

It’s also worth noting that Phoenix has nothing to do with this at all, it is merely a web framework.

What prevents you from migrating the rails schema to ecto?

railsmechanic

railsmechanic OP

I know that Phoenix is “just” a web framework, but I don’t want to reinvent the wheel by writing my own database connection pool and stuff etc. Before doing this, I just wanted to know whether a default Phoenix respectively ecto installation is able to execute raw SQL queries without the need to setup models/schemas.

The reason for all this is, as we cannot migrate the rails schema to ecto at the moment, because our customer is using the underlying database from other systems which don’t belong to us. Otherwise we’ve already migrated the service. :slight_smile:

themarlzy

themarlzy

As someone who hasn’t really used Rails / ORM before, I don’t understand the benefits. Whenever I see the convoluted Ecto code, I just think “isn’t it easier to just write the SQL?”. I don’t buy the portability argument - if you want to change from mySQL to postgres, you’re in for some pain, no matter the abstraction.

I’m admittedly naive to the pros of this, given the lack of experience with the tools.

Curious to hear how one would utilise raw SQL.

M

michalmuskala

michalmuskala

You can look at the drivers: GitHub - elixir-ecto/postgrex: PostgreSQL driver for Elixir · GitHub and
GitHub - xerions/mariaex: Pure Elixir database driver for MariaDB / MySQL · GitHub - they are built on top of db_connection
so they have pooling, transactions support, etc. For postgres you could
also look at GitHub - robconery/moebius: A functional query tool for Elixir · GitHub for postgres that is
closer to SQL.

That said, you don’t need to change anything in your database to be able to
use ecto. You can simply define your schema modules and skip migrations.
I’ve heard couple stories of ecto working fine with a rails database.

michalmuskala

michalmuskala

I agree that the argument of changing the database is completely
nonsensical. I have yet to see a system that does not use any
database-specific queries.

I see the strength of ecto in different areas - changesets are extremely
powerful and convenient way to work with data in general, not necessarily
database-originating data. Custom type definitions and automatic casting
make it a breeze to build systems. There are also some additional features
around handling associations, properly parametrizing queries, etc. Making
queries composeable is also a powerful idea that simplifies code
tremendously.

While I could definitely live without queries and write raw SQL,
abstractions such as changesets, multi and types are definitely a huge
improvement.

railsmechanic

railsmechanic OP

Many thanks, moebius looks surprising…:+1:

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New
adr
Hello all. I have a question as to best practices for code organization when implementing a liveview with a lot of responsibilities. Sup...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
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
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews