Skip to content

Conversation

elliotb
Copy link
Contributor

@elliotb elliotb commented Oct 5, 2025

This PR fixes the resource generator to properly handle tables in non-public PostgreSQL schemas (e.g., inventory.products, ocloud.roles). Previously, the generator would fail when attempting to introspect tables outside the public schema. e.g.:

mix ash_postgres.gen.resources OgatApp.AshDomains.GrantedRoles --tables ocloud.roles --snapshots-only
Generating resources from [OgatApp.Repo]
** (Postgrex.Error) ERROR 42P01 (undefined_table) relation "roles" does not exist
    (ecto_sql 3.13.2) lib/ecto/adapters/sql.ex:1098: Ecto.Adapters.SQL.raise_sql_call_error/1
    (ash_postgres 2.6.20) lib/resource_generator/spec.ex:127: AshPostgres.ResourceGenerator.Spec.add_foreign_keys/1
    (ash_postgres 2.6.20) lib/resource_generator/spec.ex:113: anonymous fn/1 in AshPostgres.ResourceGenerator.Spec.tables/2
    (elixir 1.18.0) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
    (ash_postgres 2.6.20) lib/resource_generator/spec.ex:111: anonymous fn/2 in AshPostgres.ResourceGenerator.Spec.tables/2
    (ecto_sql 3.13.2) lib/ecto/migrator.ex:170: Ecto.Migrator.with_repo/3
    (ash_postgres 2.6.20) lib/resource_generator/spec.ex:86: AshPostgres.ResourceGenerator.Spec.tables/2
    (elixir 1.18.0) lib/enum.ex:4438: Enum.flat_map_list/2

Two issues have been fixed:

  1. When generating resources from tables like ocloud.roles, PostgreSQL queries would fail with
    ERROR 42P01 (undefined_table) relation "roles" does not exist because unqualified table names were used in introspection queries.

  2. After generating resources, the migration generator would crash with could not list directory "priv/resource_snapshots/repo/roles": no such file or directory because it tried to ls snapshot directories based on unqualified table names.

The following changes were made:

  • Use qualified table names (schema.table) in all PostgreSQL introspection queries for foreign keys, check constraints, and indexes
  • Add schema option to postgres configuration for generated resources when schema is not "public"
  • Guard against missing snapshot directories to handle new resources and schema-prefixed paths gracefully
  • Added test coverage for the resource generator for non-public schema support with foreign keys and indexes

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR. (Claude Code assisted in the development of this PR)
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

- Add schema field to generated resources when table is in non-public schema
- Fix SQL queries to use schema-qualified table names for foreign keys and constraints
- Update index queries to respect actual schema instead of hardcoded 'public'
- Add test coverage for tables in custom schemas with foreign keys and indexes
Fixes crash when generating migrations for resources in non-public schemas
where the snapshot directory doesn't exist yet.
@zachdaniel zachdaniel merged commit c45b336 into ash-project:main Oct 5, 2025
59 of 62 checks passed
@zachdaniel
Copy link
Contributor

🚀 Thank you for your contribution! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants