ash-project / ash-project/ash_postgres

Cant rollback migration for tenants

Open
#642 5 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Elixir
Stars
189
Forks
168
Avg merge
13h 1m
Merged PRs (30d)
22

Description

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

### AI Policy

- [x] I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue.

### Versions

Erlang/OTP 27 [erts-15.2] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit]
Elixir 1.18.1 (compiled with Erlang/OTP 27)

### Operating system

Mac OS Sequoia Version 15.6.1 (24G90)

### Current Behavior

when trying to rollback a tenant migration i run into
```
Generated my_menu app
** (RuntimeError) MyMenu.Repo.all_tenants/0 was called, but was not defined. In order to migrate tenants, you must define this function.
For example, you might say:

def all_tenants do
for org <- MyApp.Accounts.all_organizations!() do
org.schema
end
end
```

then I added this into repo.ex ` @impl true
def all_tenants do
MyMenu.Orgs.Org
|> Ash.read!()
|> Enum.map(fn org -> "org_#{org.slug}" end)
end`

#### running: `mix ash.migrate --tenants` worked fine, but `mix ash_postgres.rollback --tenants` is failing

```mix ash_postgres.rollback --tenants

** (Ash.Error.Unknown)
Bread Crumbs:
> Exception raised in: MyMenu.Orgs.Org.read

Unknown Error

* ** (ArgumentError) errors were found at the given arguments:

* 1st argument: the table identifier does not refer to an existing ETS table

(stdlib 6.2) :ets.match_object(:telemetry_handler_table, {:handler, :_, [:ash, :orgs, :read | :_], :_, :_})
(telemetry 1.3.0) /Users/icaro/Projects/icaro/my_menu/deps/telemetry/src/telemetry_handler_table.erl:59: :telemetry_handler_table.list_by_prefix/1
(telemetry 1.3.0) /Users/icaro/Projects/icaro/my_menu/deps/telemetry/src/telemetry.erl:379: :telemetry.list_handlers/1
(ash 3.7.2) lib/ash/actions/read/read.ex:84: Ash.Actions.Read.run/3
(ash 3.7.2) lib/ash.ex:2776: Ash.read/2
(ash 3.7.2) lib/ash.ex:2711: Ash.read!/2
(my_menu 0.1.0) lib/my_menu/repo.ex:26: MyMenu.Repo.all_tenants/0
(ash_postgres 2.6.23) lib/mix/helpers.ex:171: AshPostgres.Mix.Helpers.tenants/2
(stdlib 6.2) :ets.match_object(:telemetry_handler_table, {:handler, :_, [:ash, :orgs, :read | :_], :_, :_})
(telemetry 1.3.0) /Users/icaro/Projects/icaro/my_menu/deps/telemetry/src/telemetry_handler_table.erl:59: :telemetry_handler_table.list_by_prefix/1
(telemetry 1.3.0) /Users/icaro/Projects/icaro/my_menu/deps/telemetry/src/telemetry.erl:379: :telemetry.list_handlers/1
(ash 3.7.2) lib/ash/actions/read/read.ex:84: Ash.Actions.Read.run/3
(ash 3.7.2) lib/ash.ex:2776: Ash.read/2
(ash 3.7.2) lib/ash.ex:2711: Ash.read!/2
(my_menu 0.1.0) lib/my_menu/repo.ex:26: MyMenu.Repo.all_tenants/0
(ash_postgres 2.6.23) lib/mix/helpers.ex:171: AshPostgres.Mix.Helpers.tenants/2
```

### Reproduction

```
defmodule MyMenu.Repo do
use AshPostgres.Repo,
otp_app: :my_menu

@impl true
def installed_extensions do
# Add extensions here, and the migration generator will install them.
["ash-functions", "citext", AshMoney.AshPostgresExtension]
end

# Don't open unnecessary transactions
# will default to `false` in 4.0
@impl true
def prefer_transaction? do
false
end

@impl true
def min_pg_version do
%Version{major: 16, minor: 0, patch: 0}
end

@impl true
def all_tenants do
MyMenu.Orgs.Org
|> Ash.read!()
|> Enum.map(fn org -> "org_#{org.slug}" end)
end
end
```

```defmodule MyMenu.Orgs.Org do
require Ash.Resource.Change.Builtins

use Ash.Resource,
domain: MyMenu.Orgs,
data_layer: AshPostgres.DataLayer,
extensions: [AshAuthentication]

postgres do
table "orgs"
repo MyMenu.Repo

# Automatic tenant schema creation
manage_tenant do
template ["org_", :slug]
create? true
update? false
end
end

actions do
defaults [:read]

create :create do
accept [:name]

change fn changeset, _context ->
name = Ash.Changeset.get_attribute(changeset, :name)
slug =
name
|> String.downcase()
|> String.replace(" ", "-")

Ash.Changeset.force_change_attribute(changeset, :slug, slug)
end
end

end

attributes do
uuid_v7_primary_key :id

attribute :name, :string do
allow_nil? false
public? true
end

attribute :slug, :string do
allow_nil? false
public? true
end

create_timestamp :created_at
update_timestamp :updated_at
end

identities do
identity :unique_slug, [:slug]
end
end
```

### Expected Behavior

`mix ash_postgres.rollback --tenants` run down function of the last migration created

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.