spicychickensauce

spicychickensauce

I’m evaluating migrating an existing application to ash and it’s looking pretty good so far.

However, I got stuck at this: My app currently supports both sqlite and postgres, configured via a compile time setting. (I need sqlite support because my app is also bundled on android, postgres is used on servers)

I cannot figure out how to make that work with ash.
The problem is in the resources. I tried this:

  if App.Config.use_sqlite?() do
    use Ash.Resource,
      domain: App,
      data_layer: AshSqlite.DataLayer
  else
    use Ash.Resource,
      domain: App,
      data_layer: AshPostgres.DataLayer
  end

  # same conditional for the `postgres` and `sqlite` expressions

  actions do
  ...

However, I get a compile error:

    error: undefined function actions/1 (there is no such import)
    │
 12 │   actions do

This is very weird, since when I manually comment out the appropriate use it compiles just fine.

What is happening here? Why does this conditional compilation logic not work with ash?

It used to work just fine when I did that in my ecto repo to configure the appropriate adapter…

Showing Posts 1 to 9

zachdaniel

zachdaniel

Creator of Ash

:thinking: try this instead:

use mix ash.gen.base_resource MyApp.Resource and then modify it to look something like this:

  # use it in the resources like this
  use MyApp.Resource

  defmodule MyApp.Resource do
    defmacro __using__(opts) do
       data_layer = 
         if App.Config.use_sqlite?() do
           AshSqlite.DataLayer
         else
           AshPostgres.DataLayer
         end

       opts = Keyword.put_new(opts, :data_layer, data_layer)

        quote do
          use Ash.Resource, unquote(opts)
        end
      end
    end
spicychickensauce

spicychickensauce OP

Thank you!
This got me one step closer.
However, now I’m getting a similar error a bit later here:

  if App.Config.use_sqlite?() do
    sqlite do
       ...
    end
  else
    postgres do
       ....
    end
  end
    error: undefined function sqlite/1 (there is no such import)
    │
  9 │     sqlite do
    │     ^

This is with use_sqlite?() == false, with true I get the error on postgres do.

Maybe I need some more macro magic? I just don’t understand how this unreachable code path can cause a compile error, that function doesn’t need to be defined in that branch!

spicychickensauce

spicychickensauce OP

I got it working by doing:

  if App.Config.use_sqlite?() do
    import AshSqlite.DataLayer

    sqlite do
      # ...
    end
  else
    import AshPostgres.DataLayer

    postgres do
      # ...
    end
  end

A bit weird, since the use MyApp.Resource should already have imported those definitions, at least for the branch that is compiled.

Anyway, that seems to work and isn’t too bad..

spicychickensauce

spicychickensauce OP

Now ideally I could also get rid of the duplication inside the postgres and the sqlite block.
They will have to be kept in sync and as far as I can tell the options that each support is pretty much identical.

I’m not that familiar with macros, but I’m guessing this could be done via a macro?

spicychickensauce

spicychickensauce OP

Ok, this was easier than anticipated.
I created a macro like this:

  defmacro sqlite_or_postgres(do: code) do
    quote do
      if unquote(App.Config.use_sqlite?()) do
        import AshSqlite.DataLayer

        sqlite do
          unquote(code)
        end
      else
        import AshPostgres.DataLayer

        postgres do
          unquote(code)
        end
      end
    end
  end

And can use it like this:

sqlite_or_postgres do
    table "things"
    repo App.Repo
    # ...
  end

I’m sure this will shoot me in the foot at some point when I need to use a feature that only exist in postgres, but for now it seems to work nicely :crossed_fingers:

zachdaniel

zachdaniel

Creator of Ash

Simpler would be this:

 defmodule MyApp.Resource do
    defmacro __using__(opts) do
       {data_layer, extension_to_add} = 
         if App.Config.use_sqlite?() do
           {AshSqlite.DataLayer, AshPostgres.DataLayer}
         else
           {AshPostgres.DataLayer, AshSqlite.DataLayer}
         end

      {table, opts} = Keyword.pop(opts, :table)

       opts = 
         opts
         |> Keyword.put_new(:data_layer, data_layer)
         |> Keyword.update(:extensions, [extension_to_add], &[extension_to_add | &1])

        quote do
          use Ash.Resource, unquote(opts)
          
           postgres do
              table unquote(table)
              repo YourRepo
           end

           sqlite do
             table unquote(table)
             repo YourRepo
           end
        end
      end
    end

You can use the postgres and sqlite extensions multiple times, and can add it as an extension without making it the data layer so that when you have special sqlite or postgres specific things in a resource, you can just do postgres do ... and not include table.

spicychickensauce

spicychickensauce OP

Thanks, the extension_to_add part might be useful!

However, I’ve also got references blocks, identity_index_names and custom_indexes etc. in those sqlite_or_postgres blocks.

So with your proposed approach, it seems like I’d have to define all of these inside the use MyApp.Resource, table: ..., etc. That doesn’t seem like a good approach?

zachdaniel

zachdaniel

Creator of Ash

Yeah, that’s a good point :thinking:. You could do it your way, and then with the addition of adding both extensions, you could do custom things in postgres and sqlite blocks respectively.

spicychickensauce

spicychickensauce OP

Great, then I’ll go with that. Seems the most flexible and DRY approach.
Thank you very much for the help!

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews