// Single responsibility: how ONE table is spelled — the statement order a `serial` column forces, // the clause order inside a column, and the quoting of a name `identifier()` would refuse. import { describe, expect, test } from 'bun:test'; import type { CatalogColumn, CatalogSequence, CatalogTable } from './catalog'; import { createSequence, quoted, tableStatements } from './schema-dump-table'; const column = (name: string, overrides: Partial = {}): CatalogColumn => ({ name, type: 'text', notNull: false, default: null, generated: null, identity: null, collation: null, ...overrides, }); const sequence = (name: string): CatalogSequence => ({ name, dataType: 'bigint', start: '1', increment: '1', min: '1', max: '9223372036854775807', cache: '1', cycle: false, }); const table = (overrides: Partial = {}): CatalogTable => ({ name: 'posts', unlogged: false, options: null, columns: [column('id', { type: 'uuid', notNull: true })], constraints: [], sequences: [], replicaIdentity: { kind: 'default' }, ...overrides, }); describe('quoted', () => { test('a name carrying a double quote or a space is escaped, not refused', () => { expect(quoted('plain')).toBe('"plain"'); expect(quoted('odd "name" here')).toBe('"odd ""name"" here"'); }); }); describe('createSequence', () => { test('spells every option, the default ones included', () => { expect(createSequence({ ...sequence('ticket_seq'), start: '100', cycle: true })).toBe( 'create sequence "ticket_seq" as bigint start with 100 increment by 1 minvalue 1 ' + 'maxvalue 9223372036854775807 cache 1 cycle;', ); }); }); describe('tableStatements', () => { test('a column is type, collation, value, then nullability', () => { const [create] = tableStatements( table({ columns: [ column('title', { collation: 'C', default: "'x'::text", notNull: true }), column('search', { generated: { expression: 'lower(title)', storage: 'stored' } }), column('n', { type: 'integer', notNull: true, identity: { mode: 'by default', sequence: { ...sequence('posts_n_seq') } }, }), ], constraints: [{ name: 'posts_pkey', definition: 'PRIMARY KEY (n)' }], }), ); expect(create).toBe( [ 'create table "posts" (', ` "title" text collate "C" default 'x'::text not null,`, ' "search" text generated always as (lower(title)) stored,', ' "n" integer generated by default as identity (sequence name "posts_n_seq" start with 1 ' + 'increment by 1 minvalue 1 maxvalue 9223372036854775807 cache 1 no cycle) not null,', ' constraint "posts_pkey" PRIMARY KEY (n)', ');', ].join('\n'), ); }); test('a serial column: the sequence before the table, its ownership after', () => { const statements = tableStatements( table({ columns: [column('id', { type: 'bigint', default: "nextval('posts_id_seq'::regclass)" })], sequences: [{ ...sequence('posts_id_seq'), column: 'id' }], }), ); expect(statements.map((statement) => statement.split(' ').slice(0, 2).join(' '))).toEqual([ 'create sequence', 'create table', 'alter sequence', ]); expect(statements[2]).toBe('alter sequence "posts_id_seq" owned by "posts"."id";'); }); test('unlogged and storage options reach the statement', () => { const [create] = tableStatements(table({ unlogged: true, options: 'fillfactor=70' })); expect(create).toStartWith('create unlogged table "posts" ('); expect(create).toEndWith(') with (fillfactor=70);'); }); test('replica identity full and nothing are the table file’s; an index one is not', () => { const last = (kind: CatalogTable['replicaIdentity']): string | undefined => tableStatements(table({ replicaIdentity: kind })).at(-1); expect(last({ kind: 'full' })).toBe('alter table "posts" replica identity full;'); expect(last({ kind: 'nothing' })).toBe('alter table "posts" replica identity nothing;'); // The index it names does not exist until `05_indexes` — `schema-dump.ts` files it there. expect(last({ kind: 'index', index: 'posts_key' })).toStartWith('create table'); expect(last({ kind: 'default' })).toStartWith('create table'); }); }); describe('a partitioned table and its partitions', () => { test('the parent is an ordinary `create table` with `partition by` after its columns', () => { expect(tableStatements(table({ name: 'events', partitionBy: 'RANGE (at)' }))).toEqual([ 'create table "events" (\n "id" uuid not null\n) partition by RANGE (at);', ]); }); test('a partition names its parent and bound, never the columns it holds of the parent', () => { const partition = table({ name: 'events_2026_11', partitionOf: { table: 'events', bound: "FOR VALUES FROM ('2026-11-01') TO ('2026-12-01')" }, }); expect(tableStatements(partition)).toEqual([ 'create table "events_2026_11" partition of "events" ' + "FOR VALUES FROM ('2026-11-01') TO ('2026-12-01');", ]); }); test('a sub-partitioned partition carries its own key; its own constraint rides in parentheses', () => { const partition = table({ name: 'events_2026_12', partitionOf: { table: 'events', bound: 'DEFAULT' }, partitionBy: 'LIST (kind)', constraints: [{ name: 'only_known', definition: "CHECK ((kind <> ''::text))" }], }); expect(tableStatements(partition)).toEqual([ 'create table "events_2026_12" partition of "events" (\n' + ` constraint "only_known" CHECK ((kind <> ''::text))\n` + ') DEFAULT partition by LIST (kind);', ]); }); });