Austin Bonander 25cbeedab4
feat: create sqlx.toml format (#3383)
* feat: create `sqlx.toml` format

* feat: add support for ignored_chars config to sqlx_core::migrate

* chore: test ignored_chars with `U+FEFF` (ZWNBSP/BOM)

https://en.wikipedia.org/wiki/Byte_order_mark

* refactor: make `Config` always compiled

simplifies usage while still making parsing optional for less generated code

* refactor: add origin information to `Column`

* feat(macros): implement `type_override` and `column_override` from `sqlx.toml`

* refactor(sqlx.toml): make all keys kebab-case, create `macros.preferred-crates`

* feat: make macros aware of `macros.preferred-crates`

* feat: make `sqlx-cli` aware of `database-url-var`

* feat: teach macros about `migrate.table-name`, `migrations-dir`

* feat: teach macros about `migrate.ignored-chars`

* chore: delete unused source file `sqlx-cli/src/migration.rs`

* feat: teach `sqlx-cli` about `migrate.defaults`

* feat: teach `sqlx-cli` about `migrate.migrations-dir`

* feat: teach `sqlx-cli` about `migrate.table-name`

* feat: introduce `migrate.create-schemas`

* WIP feat: create multi-tenant database example

* fix(postgres): don't fetch `ColumnOrigin` for transparently-prepared statements

* feat: progress on axum-multi-tenant example

* feat(config): better errors for mislabeled fields

* WIP feat: filling out axum-multi-tenant example

* feat: multi-tenant example

No longer Axum-based because filling out the request routes would have distracted from the purpose of the example.

* chore(ci): test multi-tenant example

* fixup after merge

* fix(ci): enable `sqlx-toml` in CLI build for examples

* fix: CI, README for `multi-tenant`

* fix: clippy warnings

* fix: multi-tenant README

* fix: sequential versioning inference for migrations

* fix: migration versioning with explicit overrides

* fix: only warn on ambiguous crates if the invocation relies on it

* fix: remove unused imports

* fix: doctest

* fix: `sqlx mig add` behavior and tests

* fix: restore original type-checking order

* fix: deprecation warning in `tests/postgres/macros.rs`

* feat: create postgres/multi-database example

* fix: examples/postgres/multi-database

* fix: cargo fmt

* chore: add tests for config `migrate.defaults`

* fix: sqlx-cli/tests/add.rs

* feat(cli): add `--config` override to all relevant commands

* chore: run `sqlx mig add` test with `RUST_BACKTRACE=1`

* fix: properly canonicalize config path for `sqlx mig add` test

* fix: get `sqlx mig add` test passing

* fix(cli): test `migrate.ignored-chars`, fix bugs

* feat: create `macros.preferred-crates` example

* fix(examples): use workspace `sqlx`

* fix: examples

* fix(sqlite): unexpected feature flags in `type_checking.rs`

* fix: run `cargo fmt`

* fix: more example fixes

* fix(ci): preferred-crates setup

* fix(examples): enable default-features for workspace `sqlx`

* fix(examples): issues in `preferred-crates`

* chore: adjust error message for missing param type in `query!()`

* doc: mention new `sqlx.toml` configuration

* chore: add `CHANGELOG` entry

Normally I generate these when cutting the release, but I wanted to take time to editorialize this one.

* doc: fix new example titles

* refactor: make `sqlx-toml` feature non-default, improve errors

* refactor: eliminate panics in `Config` read path

* chore: remove unused `axum` dependency from new examples

* fix(config): restore fallback to default config for macros

* chore(config): remove use of `once_cell` (to match `main`)
2025-06-30 16:34:46 -07:00

60 lines
3.0 KiB
SQL

-- `payments::PaymentStatus`
--
-- Historically at LaunchBadge we preferred not to define enums on the database side because it can be annoying
-- and error-prone to keep them in-sync with the application.
-- Instead, we let the application define the enum and just have the database store a compact representation of it.
-- This is mostly a matter of taste, however.
--
-- For the purposes of this example, we're using an in-database enum because this is a common use-case
-- for needing type overrides.
create type payments.payment_status as enum (
'pending',
'created',
'success',
'failed'
);
create table payments.payment
(
payment_id uuid primary key default gen_random_uuid(),
-- This cross-schema reference means migrations for the `accounts` crate should be run first.
account_id uuid not null references accounts.account (account_id),
status payments.payment_status not null,
-- ISO 4217 currency code (https://en.wikipedia.org/wiki/ISO_4217#List_of_ISO_4217_currency_codes)
--
-- This *could* be an ENUM of currency codes, but constraining this to a set of known values in the database
-- would be annoying to keep up to date as support for more currencies is added.
--
-- Consider also if support for cryptocurrencies is desired; those are not covered by ISO 4217.
--
-- Though ISO 4217 is a three-character code, `TEXT`, `VARCHAR` and `CHAR(N)`
-- all use the same storage format in Postgres. Any constraint against the length of this field
-- would purely be a sanity check.
currency text not null,
-- There's an endless debate about what type should be used to represent currency amounts.
--
-- Postgres has the `MONEY` type, but the fractional precision depends on a C locale setting and the type is mostly
-- optimized for storing USD, or other currencies with a minimum fraction of 1 cent.
--
-- NEVER use `FLOAT` or `DOUBLE`. IEEE-754 rounding point has round-off and precision errors that make it wholly
-- unsuitable for representing real money amounts.
--
-- `NUMERIC`, being an arbitrary-precision decimal format, is a safe default choice that can support any currency,
-- and so is what we've chosen here.
amount NUMERIC not null,
-- Payments almost always take place through a third-party vendor (e.g. PayPal, Stripe, etc.),
-- so imagine this is an identifier string for this payment in such a vendor's systems.
--
-- For privacy and security reasons, payment and personally-identifying information
-- (e.g. credit card numbers, bank account numbers, billing addresses) should only be stored with the vendor
-- unless there is a good reason otherwise.
external_payment_id text,
created_at timestamptz not null default now(),
updated_at timestamptz
);
select payments.trigger_updated_at('payments.payment');