Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/inflection/__tests__/inflection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,34 @@ describe('inflection', () => {
{ name: '__leading', result: 'leading' },
{ name: 'trailing__', result: 'trailing' },
{ name: '---', result: '' },
{ name: 'x'.repeat(63), result: 'x'.repeat(63) },
// Over 63 chars: a 50-char head plus '-' and 12 hex of sha256(input).
// These vectors are pinned against toK8sName in
// compute/lib/module-loader/__tests__/k8s-name.test.ts.
{
name: 'a'.repeat(80),
result: 'a'.repeat(63)
result: `${'a'.repeat(50)}-0f45e858fbc4`
},
{
// truncation lands on a '-'; trailing non-alphanumerics are dropped
name: `${'a'.repeat(62)}-bcd`,
result: 'a'.repeat(62)
result: `${'a'.repeat(50)}-a215f248a9da`
},
{
// the head cut lands on a '-'; it is dropped before the digest joins
name: `${'a'.repeat(49)}_${'b'.repeat(20)}`,
result: `${'a'.repeat(49)}-f0bdb703600e`
},
{
name: 'constructive_database_80a2eaaf-1b6d-4a25-9d6f-2ff1d1a6e001_mail',
result: 'constructive-database-80a2eaaf-1b6d-4a25-9d6f-2ff1d1a6e001-mail'
},
{
name: 'constructive_database_80a2eaaf-1b6d-4a25-9d6f-2ff1d1a6e001_mailer',
result: 'constructive-database-80a2eaaf-1b6d-4a25-9d6f-2ff1-a6b33285965f'
},
{
name: 'constructive_database_80a2eaaf-1b6d-4a25-9d6f-2ff1d1a6e001_mailbox',
result: 'constructive-database-80a2eaaf-1b6d-4a25-9d6f-2ff1-5e6f3baa8a2e'
}
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

-- requires: schemas/inflection/schema

-- Normalizes a value into a Kubernetes DNS-1123 label (RFC 1123):
-- lowercase alphanumerics and '-', at most 63 characters, and no leading or
-- trailing '-'. Used to derive Knative/K8s object names from arbitrary slugs.
-- Kubernetes DNS-1123 label normalizer. Mirrors toK8sName in
-- compute/lib/module-loader/src/k8s-name.ts and must stay in lockstep with it:
-- lowercase, ':' -> '--', '_' -> '-', strip invalid chars, trim
-- leading/trailing hyphens. A result within 63 chars is returned as is; a
-- longer one is cut to a 50-char head and suffixed with '-' and the first 12
-- hex digits of sha256(original value), so two values that agree on their
-- first 63 mapped chars still get distinct labels.

BEGIN;

Expand Down Expand Up @@ -40,23 +44,21 @@ trimmed AS (
FROM
stripped
),
truncated AS (
digested AS (
SELECT
"left"(value, 63) AS value
regexp_replace("left"(value, 50), '-+$', '') || '-' || "left"(encode(sha256(convert_to(dns_1123.value, 'UTF8')), 'hex'), 12) AS value
Comment thread
tenki-reviewer[bot] marked this conversation as resolved.
FROM
trimmed
),
-- truncation can leave a dangling '-'; drop any trailing non-alphanumerics
final AS (
SELECT
regexp_replace(value, '[^a-z0-9]+$', '') AS value
FROM
truncated
)
SELECT
value
CASE WHEN length(trimmed.value) <= 63 THEN
trimmed.value
ELSE
digested.value
END
FROM
final;
trimmed,
digested;
$$
LANGUAGE SQL
STRICT IMMUTABLE;
Expand Down
Binary file modified packages/inflection/sql/pgpm-inflection--0.46.0.bundle.tar.gz
Binary file not shown.
20 changes: 9 additions & 11 deletions packages/inflection/sql/pgpm-inflection--0.46.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -584,21 +584,19 @@ trimmed AS (
FROM
stripped
),
truncated AS (
digested AS (
SELECT
"left"(value, 63) AS value
regexp_replace("left"(value, 50), '-+$', '') || '-' || "left"(encode(sha256(convert_to(dns_1123.value, 'UTF8')), 'hex'), 12) AS value
FROM
trimmed
),
-- truncation can leave a dangling '-'; drop any trailing non-alphanumerics
final AS (
SELECT
regexp_replace(value, '[^a-z0-9]+$', '') AS value
FROM
truncated
)
SELECT
value
CASE WHEN length(trimmed.value) <= 63 THEN
trimmed.value
ELSE
digested.value
END
FROM
final;
trimmed,
digested;
$EOFCODE$ LANGUAGE sql STRICT IMMUTABLE;
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ exports[`db_meta_modules should verify module table structures have database_id

exports[`db_meta_modules should verify module tables have proper foreign key relationships 1`] = `
{
"constraintCount": 567,
"constraintCount": 570,
"foreignTables": [
"catalog_module",
"database",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ CREATE TABLE metaschema_modules_public.billing_provider_module (
billing_disputes_table_id uuid NOT NULL DEFAULT uuid_nil(),
billing_disputes_table_name text NOT NULL DEFAULT '',

-- Operation ledger: one row per idempotent provider mutation an entity asked
-- for (checkout, plan change, cancel), with its recovery deadline.
billing_operations_table_id uuid NOT NULL DEFAULT uuid_nil(),
billing_operations_table_name text NOT NULL DEFAULT '',

-- Provider state: one row per entity holding the provider-observed status,
-- the derived lifecycle (grace/suspended/review), checkout binding and any
-- scheduled plan change.
billing_provider_state_table_id uuid NOT NULL DEFAULT uuid_nil(),
billing_provider_state_table_name text NOT NULL DEFAULT '',

-- Billing health: one row per database_id holding the latest billing:doctor
-- verdict (ready, checks, next_step, checked_at). Written only by the
-- system-only record_billing_health seam; read by the enable_billing gate.
billing_health_table_id uuid NOT NULL DEFAULT uuid_nil(),
billing_health_table_name text NOT NULL DEFAULT '',

-- Generated functions
process_billing_event_function text NOT NULL DEFAULT '',
record_refund_function text NOT NULL DEFAULT '',
Expand Down Expand Up @@ -82,6 +99,17 @@ CREATE TABLE metaschema_modules_public.billing_provider_module (
get_billing_subscription_by_entity_function text NOT NULL DEFAULT '',
get_billing_subscription_by_external_id_function text NOT NULL DEFAULT '',
get_plan_pricing_by_external_price_function text NOT NULL DEFAULT '',
-- Operation ledger + provider state seams: the system-only path the billing
-- functions use to reserve/settle mutations, apply provider observations,
-- claim bounded reconciliation batches and own scheduled plan changes.
reserve_billing_operation_function text NOT NULL DEFAULT '',
finish_billing_operation_function text NOT NULL DEFAULT '',
apply_provider_observation_function text NOT NULL DEFAULT '',
list_due_reconciliations_function text NOT NULL DEFAULT '',
prepare_scheduled_change_function text NOT NULL DEFAULT '',
clear_scheduled_change_function text NOT NULL DEFAULT '',
get_billing_provider_state_function text NOT NULL DEFAULT '',
record_billing_health_function text NOT NULL DEFAULT '',

prefix text NULL,

Expand All @@ -100,6 +128,9 @@ CREATE TABLE metaschema_modules_public.billing_provider_module (
CONSTRAINT billing_refunds_table_fkey FOREIGN KEY (billing_refunds_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
CONSTRAINT billing_invoices_table_fkey FOREIGN KEY (billing_invoices_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
CONSTRAINT billing_disputes_table_fkey FOREIGN KEY (billing_disputes_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
CONSTRAINT billing_operations_table_fkey FOREIGN KEY (billing_operations_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
CONSTRAINT billing_provider_state_table_fkey FOREIGN KEY (billing_provider_state_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
CONSTRAINT billing_health_table_fkey FOREIGN KEY (billing_health_table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
CONSTRAINT products_table_fkey FOREIGN KEY (products_table_id) REFERENCES metaschema_public.table (id) ON DELETE SET NULL,
CONSTRAINT prices_table_fkey FOREIGN KEY (prices_table_id) REFERENCES metaschema_public.table (id) ON DELETE SET NULL,
CONSTRAINT subscriptions_table_fkey FOREIGN KEY (subscriptions_table_id) REFERENCES metaschema_public.table (id) ON DELETE SET NULL,
Expand All @@ -114,6 +145,9 @@ CREATE INDEX billing_provider_module_billing_webhook_events_table_id_idx ON meta
CREATE INDEX billing_provider_module_billing_refunds_table_id_idx ON metaschema_modules_public.billing_provider_module ( billing_refunds_table_id );
CREATE INDEX billing_provider_module_billing_invoices_table_id_idx ON metaschema_modules_public.billing_provider_module ( billing_invoices_table_id );
CREATE INDEX billing_provider_module_billing_disputes_table_id_idx ON metaschema_modules_public.billing_provider_module ( billing_disputes_table_id );
CREATE INDEX billing_provider_module_billing_operations_table_id_idx ON metaschema_modules_public.billing_provider_module ( billing_operations_table_id );
CREATE INDEX billing_provider_module_billing_provider_state_table_id_idx ON metaschema_modules_public.billing_provider_module ( billing_provider_state_table_id );
CREATE INDEX billing_provider_module_billing_health_table_id_idx ON metaschema_modules_public.billing_provider_module ( billing_health_table_id );
CREATE INDEX billing_provider_module_prices_table_id_idx ON metaschema_modules_public.billing_provider_module ( prices_table_id );
CREATE INDEX billing_provider_module_products_table_id_idx ON metaschema_modules_public.billing_provider_module ( products_table_id );
CREATE INDEX billing_provider_module_subscriptions_table_id_idx ON metaschema_modules_public.billing_provider_module ( subscriptions_table_id );
Expand All @@ -126,9 +160,12 @@ CREATE INDEX billing_provider_module_schema_id_idx ON metaschema_modules_public.
-- install, keyed by the role name in the column.
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_customers_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_disputes_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_health_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_invoices_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_operations_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_prices_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_products_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_provider_state_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_refunds_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_subscriptions_table_id IS '@module_table';
COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_webhook_events_table_id IS '@module_table';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ CREATE TABLE metaschema_modules_public.user_auth_module (
revoke_session_tree_function text NOT NULL DEFAULT 'revoke_session_tree',
sweep_expired_sessions_function text NOT NULL DEFAULT 'sweep_expired_sessions',

-- identifiers:unverified_sweep maintenance function (TTL for unverified email/phone claims)
sweep_unverified_identifiers_function text NOT NULL DEFAULT 'sweep_unverified_identifiers',

-- UNIQUE(api_id),

-- API routing (configurable per-module)
Expand Down
Binary file not shown.
39 changes: 39 additions & 0 deletions packages/metaschema-modules/sql/metaschema-modules--0.46.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,7 @@ CREATE TABLE metaschema_modules_public.user_auth_module (
extend_token_expires text NOT NULL DEFAULT 'extend_token_expires',
revoke_session_tree_function text NOT NULL DEFAULT 'revoke_session_tree',
sweep_expired_sessions_function text NOT NULL DEFAULT 'sweep_expired_sessions',
sweep_unverified_identifiers_function text NOT NULL DEFAULT 'sweep_unverified_identifiers',
api_name text DEFAULT 'auth',
private_api_name text DEFAULT NULL,
CONSTRAINT db_fkey
Expand Down Expand Up @@ -2885,6 +2886,12 @@ CREATE TABLE metaschema_modules_public.billing_provider_module (
billing_invoices_table_name text NOT NULL DEFAULT '',
billing_disputes_table_id uuid NOT NULL DEFAULT uuid_nil(),
billing_disputes_table_name text NOT NULL DEFAULT '',
billing_operations_table_id uuid NOT NULL DEFAULT uuid_nil(),
billing_operations_table_name text NOT NULL DEFAULT '',
billing_provider_state_table_id uuid NOT NULL DEFAULT uuid_nil(),
billing_provider_state_table_name text NOT NULL DEFAULT '',
billing_health_table_id uuid NOT NULL DEFAULT uuid_nil(),
billing_health_table_name text NOT NULL DEFAULT '',
process_billing_event_function text NOT NULL DEFAULT '',
record_refund_function text NOT NULL DEFAULT '',
upsert_invoice_function text NOT NULL DEFAULT '',
Expand All @@ -2907,6 +2914,14 @@ CREATE TABLE metaschema_modules_public.billing_provider_module (
get_billing_subscription_by_entity_function text NOT NULL DEFAULT '',
get_billing_subscription_by_external_id_function text NOT NULL DEFAULT '',
get_plan_pricing_by_external_price_function text NOT NULL DEFAULT '',
reserve_billing_operation_function text NOT NULL DEFAULT '',
finish_billing_operation_function text NOT NULL DEFAULT '',
apply_provider_observation_function text NOT NULL DEFAULT '',
list_due_reconciliations_function text NOT NULL DEFAULT '',
prepare_scheduled_change_function text NOT NULL DEFAULT '',
clear_scheduled_change_function text NOT NULL DEFAULT '',
get_billing_provider_state_function text NOT NULL DEFAULT '',
record_billing_health_function text NOT NULL DEFAULT '',
prefix text NULL,
api_name text DEFAULT NULL,
private_api_name text DEFAULT NULL,
Expand Down Expand Up @@ -2954,6 +2969,18 @@ CREATE TABLE metaschema_modules_public.billing_provider_module (
FOREIGN KEY(billing_disputes_table_id)
REFERENCES metaschema_public.table (id)
ON DELETE CASCADE,
CONSTRAINT billing_operations_table_fkey
FOREIGN KEY(billing_operations_table_id)
REFERENCES metaschema_public.table (id)
ON DELETE CASCADE,
CONSTRAINT billing_provider_state_table_fkey
FOREIGN KEY(billing_provider_state_table_id)
REFERENCES metaschema_public.table (id)
ON DELETE CASCADE,
CONSTRAINT billing_health_table_fkey
FOREIGN KEY(billing_health_table_id)
REFERENCES metaschema_public.table (id)
ON DELETE CASCADE,
CONSTRAINT products_table_fkey
FOREIGN KEY(products_table_id)
REFERENCES metaschema_public.table (id)
Expand Down Expand Up @@ -2986,6 +3013,12 @@ CREATE INDEX billing_provider_module_billing_invoices_table_id_idx ON metaschema

CREATE INDEX billing_provider_module_billing_disputes_table_id_idx ON metaschema_modules_public.billing_provider_module (billing_disputes_table_id);

CREATE INDEX billing_provider_module_billing_operations_table_id_idx ON metaschema_modules_public.billing_provider_module (billing_operations_table_id);

CREATE INDEX billing_provider_module_billing_provider_state_table_id_idx ON metaschema_modules_public.billing_provider_module (billing_provider_state_table_id);

CREATE INDEX billing_provider_module_billing_health_table_id_idx ON metaschema_modules_public.billing_provider_module (billing_health_table_id);

CREATE INDEX billing_provider_module_prices_table_id_idx ON metaschema_modules_public.billing_provider_module (prices_table_id);

CREATE INDEX billing_provider_module_products_table_id_idx ON metaschema_modules_public.billing_provider_module (products_table_id);
Expand All @@ -3000,12 +3033,18 @@ COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_cust

COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_disputes_table_id IS '@module_table';

COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_health_table_id IS '@module_table';

COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_invoices_table_id IS '@module_table';

COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_operations_table_id IS '@module_table';

COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_prices_table_id IS '@module_table';

COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_products_table_id IS '@module_table';

COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_provider_state_table_id IS '@module_table';

COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_refunds_table_id IS '@module_table';

COMMENT ON COLUMN metaschema_modules_public.billing_provider_module.billing_subscriptions_table_id IS '@module_table';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ SELECT
get_billing_subscription_by_entity_function,
get_billing_subscription_by_external_id_function,
get_plan_pricing_by_external_price_function,
billing_operations_table_id,
billing_operations_table_name,
billing_provider_state_table_id,
billing_provider_state_table_name,
billing_health_table_id,
billing_health_table_name,
reserve_billing_operation_function,
finish_billing_operation_function,
apply_provider_observation_function,
list_due_reconciliations_function,
prepare_scheduled_change_function,
clear_scheduled_change_function,
get_billing_provider_state_function,
record_billing_health_function,
prefix
FROM metaschema_modules_public.billing_provider_module
WHERE FALSE;
Expand Down
Loading