railsmechanic

railsmechanic OP

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

First 10 of 14 Posts Switch mode

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
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
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
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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 & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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

We're in Beta

About us Mission Statement