How to generate index migration queries from resource at runtime?

I have a system that needs to bulk load a bunch of data from time to time.

One way to speed that load process is to delete some of the indexes for that table an then, after the load is done, re-create it.

I can do that manually via psql, but an Ash Resource already has all the information about the index, so I was wondering if there is some way for me to generate, at runtime, the same queries ash_postgres.generate_migrations generates for the resource indexes.

In other words, I want to generate create index ... and drop index ... queries for each index and identity I have in my resource so I can execute it at runtime using Repo.query

:thinking: I have no idea if this is really what you’ll end up wanting, but we do have some tooling around this kind of thing. What you can do is this:

snapshot =
  |> AshPostgres.MigrationGenerator.take_snapshots(YourRepo, [TheResource])

snapshot_without_indices =

operations = AshPostgres.MigrationGenerator.get_operations_from_snapshots([snapshot], [snapshot_without_indices])

{up, down} = AshPostgres.MigrationGenerator.build_up_and_down(operations)

# do something with up/down here

Thinking about this, I don’t see how you could really do what you want that way…we generate ecto migration code. You’d have to define an ecto migration module and run it. If thats on the table then you could use up to remove everything and down to add them back :person_shrugging:

1 Like