Cant rollback migration for tenants

未关闭
#642 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
elixir, postgresql
领域
databases

调研方向

首先,使用 issue 中的 MyMenu.Repo.all_tenants/0 示例重现租户回滚。跟踪从 lib/mix/helpers.ex:171 开始的租户发现路径,并将其与 lib/my_menu/repo.ex:26 中所示的 Ash.read! 调用进行比较,包括 telemetry 失败。完成的标准是 mix ash_postgres.rollback --tenants 运行最后一个迁移的 down 函数。

由索引模型根据 Issue 内容生成。

描述

bug
Code of Conduct
  • I agree to follow this project's Code of Conduct
AI Policy
  • 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

** (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
  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

主要语言
Elixir
星标
191
派生
186
平均合并
16 小时 4 分钟
30 天内合并 PR
17

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ash-project/ash_postgres 的其他 Issue

查看 ash-project/ash_postgres 的全部 Issue

相似的 Issue

更多 Elixir Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。