ash-project / ash_postgres

The PostgreSQL data layer for Ash Framework
https://hexdocs.pm/ash_postgres
MIT License
140 stars 73 forks source link

Allow `:string` attributes to generate a `varchar` column by default #422

Closed moxley closed 2 days ago

moxley commented 2 weeks ago

Is your feature request related to a problem? Please describe.

For string database columns that store a smallish size string (i.e., most string columns), I prefer to use the varchar type, because that type has a good default maximum size (255), and that type has more predictable behavior across different database engines. I want to restrict the column size by default to prevent DOS attacks that try to flood the database with large amounts of data, or simply to reject too-long user input strings that the application doesn't have proper "max length" validation for. AshPostgres prefers the text type, and I'm not sure why.

It would be useful if I didn't have to add an entry in migration_types every time I add a smallish string attribute.

Describe the solution you'd like

Could AshPostgres provide application configuration to make varchar a default for string attributes? It could work similarly to how ecto_sql provides configuration for primary key columns.

Configuration example:

config :my_app, MyApp.MyDomain, data_layer: [migration_types: [string: :varchar]]

Describe alternatives you've considered

Add an entry in migration_types every time I add a smallish string attribute.

migration_types utm_campaign: :string, utm_source: :string, utm_medium: :string, utm_content: :string, utm_term: :string
zachdaniel commented 2 days ago

I would suggest making a custom type, which can define the migration type callback in AshPostgres.Type. Then you can override the :string type using this config: https://hexdocs.pm/ash/Ash.Type.html#module-short-names

moxley commented 2 days ago

Thanks for the suggestion @zachdaniel. I will try that.