gongxiangp2/scripts/fixtures/ai-model-registry-evidence.mjs

42 lines
4.5 KiB
JavaScript

// Schema-only fixture captured from the two additive migrations in disposable PostgreSQL.
export function aiModelRegistryEvidence() {
const column = (table_name, column_name, data_type, is_nullable = 'NO', column_default = null, udt_name = null) => ({ table_name, column_name, data_type, is_nullable, column_default, udt_name })
const provider = 'ai_model_providers', catalog = 'ai_model_catalog', config = 'ai_model_configuration', audit = 'ai_model_audit'
const unique = (tablename, indexname, columns, predicate = '') => ({ tablename, indexname, columns, predicate, indisunique: true, indisprimary: false, indisvalid: true, indisready: true, access_method: 'btree' })
const foreign = (tablename, name, referenced_table) => ({ tablename, constraint_name: `${tablename}_${name}_fkey`, contype: 'f', convalidated: true, delete_action: 'r', columns: [name], referenced_table, referenced_columns: ['id'] })
const check = (tablename, constraint_name, columns, expression) => ({ tablename, constraint_name, columns, expression, contype: 'c', convalidated: true })
return {
columns: [
...['id', 'name', 'base_url', 'credential_source'].map(name => column(provider, name, 'text')),
...['secret_ciphertext', 'secret_nonce', 'secret_auth_tag'].map(name => column(provider, name, 'text', 'YES')),
...['enabled', 'legacy_binding'].map(name => column(provider, name, 'boolean', 'NO', 'false')),
column(provider, 'revision', 'integer', 'NO', '1'), column(provider, 'synced_at', 'timestamp with time zone', 'YES'),
...['created_at', 'updated_at'].map(name => column(provider, name, 'timestamp with time zone', 'NO', 'now()')),
...['id', 'provider_id', 'model_id'].map(name => column(catalog, name, 'text')),
column(catalog, 'compatibility', 'text', 'NO', "'unconfirmed'::text"), column(catalog, 'stages', 'ARRAY', 'NO', 'ARRAY[]::text[]', '_text'),
column(catalog, 'capability_metadata', 'jsonb', 'YES'), column(catalog, 'enabled', 'boolean', 'NO', 'false'), column(catalog, 'present', 'boolean', 'NO', 'true'),
...['created_at', 'updated_at'].map(name => column(catalog, name, 'timestamp with time zone', 'NO', 'now()')),
column(config, 'id', 'text'), ...['default_analysis', 'default_copy', 'default_image'].map(name => column(config, name, 'text', 'YES')),
column(config, 'revision', 'integer', 'NO', '1'), column(config, 'updated_at', 'timestamp with time zone', 'NO', 'now()'),
...['id', 'actor_id', 'action'].map(name => column(audit, name, 'text')), column(audit, 'provider_id', 'text', 'YES'),
column(audit, 'details', 'jsonb', 'NO', "'{}'::jsonb"), column(audit, 'created_at', 'timestamp with time zone', 'NO', 'now()'),
],
indexes: [
unique(provider, 'ai_model_providers_legacy_binding_key', ['legacy_binding'], '(legacy_binding = true)'),
unique(catalog, 'ai_model_catalog_provider_model_key', ['provider_id', 'model_id']),
{ tablename: audit, indexname: 'ai_model_audit_created_idx', indexdef: 'CREATE INDEX ai_model_audit_created_idx ON public.ai_model_audit USING btree (created_at)' },
],
constraints: [
foreign(catalog, 'provider_id', provider), ...['default_analysis', 'default_copy', 'default_image'].map(name => foreign(config, name, catalog)),
check(provider, 'ai_model_providers_credential_source_check', ['credential_source'], "credential_source = ANY (ARRAY['environment'::text, 'encrypted'::text])"),
check(provider, 'ai_model_providers_revision_check', ['revision'], 'revision > 0'),
check(provider, 'ai_model_credential_complete', ['credential_source', 'secret_ciphertext', 'secret_nonce', 'secret_auth_tag'], "credential_source = 'environment'::text AND secret_ciphertext IS NULL AND secret_nonce IS NULL AND secret_auth_tag IS NULL OR credential_source = 'encrypted'::text AND secret_ciphertext IS NOT NULL AND secret_nonce IS NOT NULL AND secret_auth_tag IS NOT NULL"),
check(catalog, 'ai_model_catalog_stages_check', ['stages'], "stages <@ ARRAY['analysis'::text, 'copy'::text, 'image'::text]"),
check(catalog, 'ai_model_catalog_compatibility_check', ['compatibility'], "compatibility = ANY (ARRAY['confirmed'::text, 'unconfirmed'::text])"),
check(catalog, 'ai_model_enabled_compatible', ['enabled', 'stages', 'compatibility'], "NOT enabled OR cardinality(stages) > 0 AND compatibility = 'confirmed'::text"),
check(config, 'ai_model_configuration_id_check', ['id'], "id = 'detail-generator'::text"),
check(config, 'ai_model_configuration_revision_check', ['revision'], 'revision > 0'),
],
}
}