| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493 |
- import { afterAll, beforeAll, expect, test } from 'vitest'
- import pgMeta from '../src/index'
- import { cleanupRoot, createTestDatabase } from './db/utils'
- beforeAll(async () => {
- // Any global setup if needed
- })
- afterAll(async () => {
- await cleanupRoot()
- })
- const cleanNondet = (x: any) => {
- const { columns, primary_keys, relationships, ...rest2 } = x
- return {
- columns: columns.map(({ id, table_id, ...rest }: any) => rest),
- primary_keys: primary_keys.map(({ table_id, ...rest }: any) => rest),
- relationships: relationships.map(({ id, ...rest }: any) => rest),
- ...rest2,
- }
- }
- type TestDb = Awaited<ReturnType<typeof createTestDatabase>>
- const withTestDatabase = (name: string, fn: (db: TestDb) => Promise<void>) => {
- test(name, async () => {
- const db = await createTestDatabase()
- try {
- await fn(db)
- } finally {
- await db.cleanup()
- }
- })
- }
- /** Original tests ported from postgres-meta */
- withTestDatabase('list tables', async ({ executeQuery }) => {
- const { sql, zod } = await pgMeta.tables.list()
- const res = zod.parse(await executeQuery(sql))
- const { columns, primary_keys, relationships, ...rest } = res.find(
- ({ name }) => name === 'users'
- )!
- expect({
- columns: columns!.map(({ id, table_id, ...rest }) => rest),
- primary_keys: primary_keys.map(({ table_id, ...rest }) => rest),
- relationships: relationships.map(({ id, ...rest }) => rest),
- ...rest,
- }).toMatchInlineSnapshot(
- {
- bytes: expect.any(Number),
- dead_rows_estimate: expect.any(Number),
- id: expect.any(Number),
- live_rows_estimate: expect.any(Number),
- size: expect.any(String),
- },
- `
- {
- "bytes": Any<Number>,
- "columns": [
- {
- "check": null,
- "comment": null,
- "data_type": "bigint",
- "default_value": null,
- "enums": [],
- "format": "int8",
- "format_schema": "pg_catalog",
- "identity_generation": "BY DEFAULT",
- "is_generated": false,
- "is_identity": true,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "id",
- "ordinal_position": 1,
- "schema": "public",
- "table": "users",
- },
- {
- "check": null,
- "comment": null,
- "data_type": "text",
- "default_value": null,
- "enums": [],
- "format": "text",
- "format_schema": "pg_catalog",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "name",
- "ordinal_position": 2,
- "schema": "public",
- "table": "users",
- },
- {
- "check": null,
- "comment": null,
- "data_type": "USER-DEFINED",
- "default_value": "'ACTIVE'::user_status",
- "enums": [
- "ACTIVE",
- "INACTIVE",
- ],
- "format": "user_status",
- "format_schema": "public",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "status",
- "ordinal_position": 3,
- "schema": "public",
- "table": "users",
- },
- ],
- "comment": null,
- "dead_rows_estimate": Any<Number>,
- "id": Any<Number>,
- "live_rows_estimate": Any<Number>,
- "name": "users",
- "primary_keys": [
- {
- "name": "id",
- "schema": "public",
- "table_name": "users",
- },
- ],
- "relationships": [
- {
- "constraint_name": "todos_user-id_fkey",
- "source_column_name": "user-id",
- "source_schema": "public",
- "source_table_name": "todos",
- "target_column_name": "id",
- "target_table_name": "users",
- "target_table_schema": "public",
- },
- {
- "constraint_name": "user_details_user_id_fkey",
- "source_column_name": "user_id",
- "source_schema": "public",
- "source_table_name": "user_details",
- "target_column_name": "id",
- "target_table_name": "users",
- "target_table_schema": "public",
- },
- ],
- "replica_identity": "DEFAULT",
- "rls_enabled": false,
- "rls_forced": false,
- "schema": "public",
- "size": Any<String>,
- }
- `
- )
- })
- withTestDatabase('list tables without columns', async ({ executeQuery }) => {
- const { sql, zod } = await pgMeta.tables.list({ includeColumns: false })
- const res = zod.parse(await executeQuery(sql))
- //@ts-expect-error columns doesn't exist at type level if includeColumns is false
- const { columns, primary_keys, relationships, ...rest } = res.find(
- ({ name }) => name === 'users'
- )!
- expect({
- primary_keys: primary_keys.map(({ table_id, ...rest }) => rest),
- relationships: relationships.map(({ id, ...rest }) => rest),
- ...rest,
- }).toMatchInlineSnapshot(
- {
- bytes: expect.any(Number),
- dead_rows_estimate: expect.any(Number),
- id: expect.any(Number),
- live_rows_estimate: expect.any(Number),
- size: expect.any(String),
- },
- `
- {
- "bytes": Any<Number>,
- "comment": null,
- "dead_rows_estimate": Any<Number>,
- "id": Any<Number>,
- "live_rows_estimate": Any<Number>,
- "name": "users",
- "primary_keys": [
- {
- "name": "id",
- "schema": "public",
- "table_name": "users",
- },
- ],
- "relationships": [
- {
- "constraint_name": "todos_user-id_fkey",
- "source_column_name": "user-id",
- "source_schema": "public",
- "source_table_name": "todos",
- "target_column_name": "id",
- "target_table_name": "users",
- "target_table_schema": "public",
- },
- {
- "constraint_name": "user_details_user_id_fkey",
- "source_column_name": "user_id",
- "source_schema": "public",
- "source_table_name": "user_details",
- "target_column_name": "id",
- "target_table_name": "users",
- "target_table_schema": "public",
- },
- ],
- "replica_identity": "DEFAULT",
- "rls_enabled": false,
- "rls_forced": false,
- "schema": "public",
- "size": Any<String>,
- }
- `
- )
- })
- withTestDatabase('list tables with included schemas', async ({ executeQuery }) => {
- const { sql, zod } = await pgMeta.tables.list({
- includedSchemas: ['public'],
- })
- const res = zod.parse(await executeQuery(sql))
- expect(res.length).toBeGreaterThan(0)
- res.forEach((table) => {
- expect(table.schema).toBe('public')
- })
- })
- withTestDatabase('list tables with excluded schemas', async ({ executeQuery }) => {
- const { sql, zod } = await pgMeta.tables.list({
- excludedSchemas: ['public'],
- })
- const res = zod.parse(await executeQuery(sql))
- res.forEach((table) => {
- expect(table.schema).not.toBe('public')
- })
- })
- withTestDatabase(
- 'list tables with excluded schemas and include System Schemas',
- async ({ executeQuery }) => {
- const { sql, zod } = await pgMeta.tables.list({
- excludedSchemas: ['public'],
- includeSystemSchemas: true,
- })
- const res = zod.parse(await executeQuery(sql))
- expect(res.length).toBeGreaterThan(0)
- res.forEach((table) => {
- expect(table.schema).not.toBe('public')
- })
- }
- )
- withTestDatabase('create, retrieve, update, and delete table', async ({ executeQuery }) => {
- // Create table
- const { sql: createSql } = await pgMeta.tables.create({
- name: 'test',
- comment: 'foo',
- })
- await executeQuery(createSql)
- // Retrieve the created table
- const { sql: retrieveSql, zod: retrieveZod } = await pgMeta.tables.retrieve({
- name: 'test',
- schema: 'public',
- })
- const retrieveRes = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- expect(retrieveRes).toMatchInlineSnapshot(
- {
- bytes: expect.any(Number),
- dead_rows_estimate: expect.any(Number),
- id: expect.any(Number),
- live_rows_estimate: expect.any(Number),
- size: expect.any(String),
- },
- `
- {
- "bytes": Any<Number>,
- "columns": [],
- "comment": "foo",
- "dead_rows_estimate": Any<Number>,
- "id": Any<Number>,
- "live_rows_estimate": Any<Number>,
- "name": "test",
- "primary_keys": [],
- "relationships": [],
- "replica_identity": "DEFAULT",
- "rls_enabled": false,
- "rls_forced": false,
- "schema": "public",
- "size": Any<String>,
- }
- `
- )
- // Update table
- const { sql: updateSql } = await pgMeta.tables.update(retrieveRes!, {
- name: 'test a',
- rls_enabled: true,
- rls_forced: true,
- replica_identity: 'NOTHING',
- comment: 'foo',
- })
- await executeQuery(updateSql)
- // Retrieve the updated table
- const { sql: retrieveUpdatedSql, zod: retrieveUpdatedZod } = await pgMeta.tables.retrieve({
- name: 'test a',
- schema: 'public',
- })
- const updateRes = retrieveUpdatedZod.parse((await executeQuery(retrieveUpdatedSql))[0])
- expect(updateRes).toMatchInlineSnapshot(
- {
- bytes: expect.any(Number),
- dead_rows_estimate: expect.any(Number),
- id: expect.any(Number),
- live_rows_estimate: expect.any(Number),
- size: expect.any(String),
- },
- `
- {
- "bytes": Any<Number>,
- "columns": [],
- "comment": "foo",
- "dead_rows_estimate": Any<Number>,
- "id": Any<Number>,
- "live_rows_estimate": Any<Number>,
- "name": "test a",
- "primary_keys": [],
- "relationships": [],
- "replica_identity": "NOTHING",
- "rls_enabled": true,
- "rls_forced": true,
- "schema": "public",
- "size": Any<String>,
- }
- `
- )
- // Remove table
- const { sql: removeSql } = await pgMeta.tables.remove(updateRes!)
- await executeQuery(removeSql)
- // Verify table is deleted
- const { sql: verifyDeleteSql } = await pgMeta.tables.retrieve(updateRes!)
- const verifyDeleteRes = await executeQuery(verifyDeleteSql)
- expect(verifyDeleteRes).toHaveLength(0)
- })
- withTestDatabase('update with name unchanged', async ({ executeQuery }) => {
- // Create table
- const { sql: createSql } = await pgMeta.tables.create({ name: 't' })
- await executeQuery(createSql)
- // Get the created table
- const { sql: retrieveSql, zod: retrieveZod } = await pgMeta.tables.retrieve({
- name: 't',
- schema: 'public',
- })
- const table = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- // Update table with same name
- const { sql: updateSql } = await pgMeta.tables.update(table!, { name: 't' })
- await executeQuery(updateSql)
- // Verify update
- const { sql: verifySQL, zod: verifyZod } = await pgMeta.tables.retrieve({
- name: 't',
- schema: 'public',
- })
- const res = verifyZod.parse((await executeQuery(verifySQL))[0])
- expect(res).toMatchInlineSnapshot(
- {
- bytes: expect.any(Number),
- dead_rows_estimate: expect.any(Number),
- id: expect.any(Number),
- live_rows_estimate: expect.any(Number),
- size: expect.any(String),
- },
- `
- {
- "bytes": Any<Number>,
- "columns": [],
- "comment": null,
- "dead_rows_estimate": Any<Number>,
- "id": Any<Number>,
- "live_rows_estimate": Any<Number>,
- "name": "t",
- "primary_keys": [],
- "relationships": [],
- "replica_identity": "DEFAULT",
- "rls_enabled": false,
- "rls_forced": false,
- "schema": "public",
- "size": Any<String>,
- }
- `
- )
- })
- withTestDatabase("allow ' in comments", async ({ executeQuery }) => {
- // Create table with single quote in comment
- const { sql: createSql } = await pgMeta.tables.create({
- name: 't',
- comment: "'",
- })
- await executeQuery(createSql)
- // Verify creation
- const { sql: retrieveSql, zod: retrieveZod } = await pgMeta.tables.retrieve({
- name: 't',
- schema: 'public',
- })
- const res = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- expect(res).toMatchInlineSnapshot(
- {
- bytes: expect.any(Number),
- dead_rows_estimate: expect.any(Number),
- id: expect.any(Number),
- live_rows_estimate: expect.any(Number),
- size: expect.any(String),
- },
- `
- {
- "bytes": Any<Number>,
- "columns": [],
- "comment": "'",
- "dead_rows_estimate": Any<Number>,
- "id": Any<Number>,
- "live_rows_estimate": Any<Number>,
- "name": "t",
- "primary_keys": [],
- "relationships": [],
- "replica_identity": "DEFAULT",
- "rls_enabled": false,
- "rls_forced": false,
- "schema": "public",
- "size": Any<String>,
- }
- `
- )
- })
- withTestDatabase('primary keys', async ({ executeQuery }) => {
- // Create table with columns
- const { sql: createSql } = await pgMeta.tables.create({ name: 't' })
- await executeQuery(createSql)
- await executeQuery(`
- ALTER TABLE t
- ADD COLUMN c bigint,
- ADD COLUMN cc text
- `)
- // Get the created table
- const { sql: retrieveSql, zod: retrieveZod } = await pgMeta.tables.retrieve({
- name: 't',
- schema: 'public',
- })
- const table = retrieveZod.parse((await executeQuery(retrieveSql))[0])
- // Update table with primary keys
- const { sql: updateSql } = await pgMeta.tables.update(table!, {
- primary_keys: [{ name: 'c' }, { name: 'cc' }],
- })
- await executeQuery(updateSql)
- // Verify update
- const { sql: verifySQL, zod: verifyZod } = await pgMeta.tables.retrieve({
- name: 't',
- schema: 'public',
- })
- const res = verifyZod.parse((await executeQuery(verifySQL))[0])
- expect(cleanNondet(res)).toMatchInlineSnapshot(
- {
- bytes: expect.any(Number),
- dead_rows_estimate: expect.any(Number),
- id: expect.any(Number),
- live_rows_estimate: expect.any(Number),
- size: expect.any(String),
- },
- `
- {
- "bytes": Any<Number>,
- "columns": [
- {
- "check": null,
- "comment": null,
- "data_type": "bigint",
- "default_value": null,
- "enums": [],
- "format": "int8",
- "format_schema": "pg_catalog",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "c",
- "ordinal_position": 1,
- "schema": "public",
- "table": "t",
- },
- {
- "check": null,
- "comment": null,
- "data_type": "text",
- "default_value": null,
- "enums": [],
- "format": "text",
- "format_schema": "pg_catalog",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "cc",
- "ordinal_position": 2,
- "schema": "public",
- "table": "t",
- },
- ],
- "comment": null,
- "dead_rows_estimate": Any<Number>,
- "id": Any<Number>,
- "live_rows_estimate": Any<Number>,
- "name": "t",
- "primary_keys": [
- {
- "name": "c",
- "schema": "public",
- "table_name": "t",
- },
- {
- "name": "cc",
- "schema": "public",
- "table_name": "t",
- },
- ],
- "relationships": [],
- "replica_identity": "DEFAULT",
- "rls_enabled": false,
- "rls_forced": false,
- "schema": "public",
- "size": Any<String>,
- }
- `
- )
- })
- // /** Additional tests */
- withTestDatabase('retrieve table by id', async ({ executeQuery }) => {
- const { sql, zod } = await pgMeta.tables.retrieve({ id: 16510 })
- const res = zod.parse((await executeQuery(sql))[0])
- expect(res).toMatchInlineSnapshot(
- {
- bytes: expect.any(Number),
- dead_rows_estimate: expect.any(Number),
- id: expect.any(Number),
- live_rows_estimate: expect.any(Number),
- size: expect.any(String),
- },
- `
- {
- "bytes": Any<Number>,
- "columns": [
- {
- "check": null,
- "comment": null,
- "data_type": "text",
- "default_value": null,
- "enums": [],
- "format": "text",
- "format_schema": "pg_catalog",
- "id": "16510.2",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "name",
- "ordinal_position": 2,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "integer",
- "default_value": null,
- "enums": [],
- "format": "int4",
- "format_schema": "pg_catalog",
- "id": "16510.3",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "category",
- "ordinal_position": 3,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "jsonb",
- "default_value": null,
- "enums": [],
- "format": "jsonb",
- "format_schema": "pg_catalog",
- "id": "16510.4",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "metadata",
- "ordinal_position": 4,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "timestamp without time zone",
- "default_value": null,
- "enums": [],
- "format": "timestamp",
- "format_schema": "pg_catalog",
- "id": "16510.5",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "created_at",
- "ordinal_position": 5,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "integer",
- "default_value": "nextval('memes_id_seq'::regclass)",
- "enums": [],
- "format": "int4",
- "format_schema": "pg_catalog",
- "id": "16510.1",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "id",
- "ordinal_position": 1,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "USER-DEFINED",
- "default_value": "'old'::meme_status",
- "enums": [
- "new",
- "old",
- "retired",
- ],
- "format": "meme_status",
- "format_schema": "public",
- "id": "16510.6",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "status",
- "ordinal_position": 6,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- ],
- "comment": null,
- "dead_rows_estimate": Any<Number>,
- "id": Any<Number>,
- "live_rows_estimate": Any<Number>,
- "name": "memes",
- "primary_keys": [
- {
- "name": "id",
- "schema": "public",
- "table_id": 16510,
- "table_name": "memes",
- },
- ],
- "relationships": [
- {
- "constraint_name": "memes_category_fkey",
- "id": 16519,
- "source_column_name": "category",
- "source_schema": "public",
- "source_table_name": "memes",
- "target_column_name": "id",
- "target_table_name": "category",
- "target_table_schema": "public",
- },
- ],
- "replica_identity": "DEFAULT",
- "rls_enabled": false,
- "rls_forced": false,
- "schema": "public",
- "size": Any<String>,
- }
- `
- )
- })
- withTestDatabase('retrieve table by name and schema', async ({ executeQuery }) => {
- const { sql, zod } = await pgMeta.tables.retrieve({ name: 'memes', schema: 'public' })
- const res = zod.parse((await executeQuery(sql))[0])
- expect(res).toMatchInlineSnapshot(
- {
- bytes: expect.any(Number),
- dead_rows_estimate: expect.any(Number),
- id: expect.any(Number),
- live_rows_estimate: expect.any(Number),
- size: expect.any(String),
- },
- `
- {
- "bytes": Any<Number>,
- "columns": [
- {
- "check": null,
- "comment": null,
- "data_type": "text",
- "default_value": null,
- "enums": [],
- "format": "text",
- "format_schema": "pg_catalog",
- "id": "16510.2",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "name",
- "ordinal_position": 2,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "integer",
- "default_value": null,
- "enums": [],
- "format": "int4",
- "format_schema": "pg_catalog",
- "id": "16510.3",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "category",
- "ordinal_position": 3,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "jsonb",
- "default_value": null,
- "enums": [],
- "format": "jsonb",
- "format_schema": "pg_catalog",
- "id": "16510.4",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "metadata",
- "ordinal_position": 4,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "timestamp without time zone",
- "default_value": null,
- "enums": [],
- "format": "timestamp",
- "format_schema": "pg_catalog",
- "id": "16510.5",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "created_at",
- "ordinal_position": 5,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "integer",
- "default_value": "nextval('memes_id_seq'::regclass)",
- "enums": [],
- "format": "int4",
- "format_schema": "pg_catalog",
- "id": "16510.1",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": false,
- "is_unique": false,
- "is_updatable": true,
- "name": "id",
- "ordinal_position": 1,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- {
- "check": null,
- "comment": null,
- "data_type": "USER-DEFINED",
- "default_value": "'old'::meme_status",
- "enums": [
- "new",
- "old",
- "retired",
- ],
- "format": "meme_status",
- "format_schema": "public",
- "id": "16510.6",
- "identity_generation": null,
- "is_generated": false,
- "is_identity": false,
- "is_nullable": true,
- "is_unique": false,
- "is_updatable": true,
- "name": "status",
- "ordinal_position": 6,
- "schema": "public",
- "table": "memes",
- "table_id": 16510,
- },
- ],
- "comment": null,
- "dead_rows_estimate": Any<Number>,
- "id": Any<Number>,
- "live_rows_estimate": Any<Number>,
- "name": "memes",
- "primary_keys": [
- {
- "name": "id",
- "schema": "public",
- "table_id": 16510,
- "table_name": "memes",
- },
- ],
- "relationships": [
- {
- "constraint_name": "memes_category_fkey",
- "id": 16519,
- "source_column_name": "category",
- "source_schema": "public",
- "source_table_name": "memes",
- "target_column_name": "id",
- "target_table_name": "category",
- "target_table_schema": "public",
- },
- ],
- "replica_identity": "DEFAULT",
- "rls_enabled": false,
- "rls_forced": false,
- "schema": "public",
- "size": Any<String>,
- }
- `
- )
- })
- withTestDatabase('retrieve error if missing identifiers', async ({}) => {
- await expect(async () => {
- //@ts-expect-error use with missing params
- await pgMeta.tables.retrieve({ name: 'memes' })
- }).rejects.toThrow('Must provide either id or name and schema')
- })
- withTestDatabase('remove table by id', async ({ executeQuery }) => {
- // First create a test table
- await executeQuery(`
- CREATE TABLE test_remove_table (
- id SERIAL PRIMARY KEY,
- name TEXT
- );
- `)
- // Get the table's id
- const { sql: listSql, zod: listZod } = await pgMeta.tables.list()
- const tables = listZod.parse(await executeQuery(listSql))
- const tableId = tables.find((t) => t.name === 'test_remove_table')!
- // Remove the table
- const { sql } = await pgMeta.tables.remove(tableId)
- await executeQuery(sql)
- // Verify the table is gone
- const tablesAfter = listZod.parse(await executeQuery(listSql))
- expect(tablesAfter.find((t) => t.name === 'test_remove_table')).toBeUndefined()
- })
- withTestDatabase('remove table by name and schema', async ({ executeQuery }) => {
- // First create a test table
- await executeQuery(`
- CREATE TABLE test_remove_table_2 (
- id SERIAL PRIMARY KEY,
- name TEXT
- );
- `)
- // Remove the table
- const { sql } = await pgMeta.tables.remove({
- name: 'test_remove_table_2',
- schema: 'public',
- })
- await executeQuery(sql)
- // Verify the table is gone
- const { sql: listSql, zod: listZod } = await pgMeta.tables.list()
- const tables = listZod.parse(await executeQuery(listSql))
- expect(tables.find((t) => t.name === 'test_remove_table_2')).toBeUndefined()
- })
- withTestDatabase('remove throws error for non-existent table', async ({ executeQuery }) => {
- const { sql } = await pgMeta.tables.remove({
- name: 'non_existent_table',
- schema: 'public',
- })
- // With schema and name
- await expect(executeQuery(sql)).rejects.toThrow(
- `Failed to execute query: table "non_existent_table" does not exist`
- )
- })
- withTestDatabase('remove throws error with missing identifiers', async ({}) => {
- await expect(async () => {
- //@ts-expect-error use with missing params
- await pgMeta.tables.remove({ name: 'some_table' })
- }).rejects.toThrow('SQL identifier cannot be null or undefined')
- })
- withTestDatabase('update table - rename', async ({ executeQuery }) => {
- // Create test table
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_rename' })
- await executeQuery(createSql)
- // Update table name
- const { sql: updateSql } = await pgMeta.tables.update(
- { id: 0, name: 'test_rename', schema: 'public' },
- { name: 'test_renamed' }
- )
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_renamed',
- schema: 'public',
- })
- const res = zod.parse((await executeQuery(retrieveSql))[0])
- expect(res!.name).toBe('test_renamed')
- })
- withTestDatabase('update table - change schema', async ({ executeQuery }) => {
- // Create test schema and table
- await executeQuery('CREATE SCHEMA test_schema')
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_schema_move' })
- await executeQuery(createSql)
- // Move table to new schema
- const { sql: updateSql } = await pgMeta.tables.update(
- { id: 0, name: 'test_schema_move', schema: 'public' },
- { schema: 'test_schema' }
- )
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_schema_move',
- schema: 'test_schema',
- })
- const res = zod.parse((await executeQuery(retrieveSql))[0])
- expect(res!.schema).toBe('test_schema')
- })
- withTestDatabase('update table - row level security', async ({ executeQuery }) => {
- // Create test table
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_rls' })
- await executeQuery(createSql)
- // Enable RLS
- const { sql: updateSql } = await pgMeta.tables.update(
- { id: 0, name: 'test_rls', schema: 'public' },
- { rls_enabled: true, rls_forced: true }
- )
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_rls',
- schema: 'public',
- })
- const res = zod.parse((await executeQuery(retrieveSql))[0])
- expect(res!.rls_enabled).toBe(true)
- expect(res!.rls_forced).toBe(true)
- })
- withTestDatabase('update table - replica identity', async ({ executeQuery }) => {
- // Create test table
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_replica' })
- await executeQuery(createSql)
- // Change replica identity
- const { sql: updateSql } = await pgMeta.tables.update(
- { id: 0, name: 'test_replica', schema: 'public' },
- { replica_identity: 'NOTHING' }
- )
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_replica',
- schema: 'public',
- })
- const res = zod.parse((await executeQuery(retrieveSql))[0])
- expect(res!.replica_identity).toBe('NOTHING')
- })
- withTestDatabase('update table - replica identity INDEX requires index name', async () => {
- expect(() =>
- pgMeta.tables.update({ id: 1, name: 'test', schema: 'public' }, { replica_identity: 'INDEX' })
- ).toThrow('replica_identity_index is required')
- })
- withTestDatabase('update table - primary keys', async ({ executeQuery }) => {
- // Create test table with a column
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_pk' })
- await executeQuery(createSql)
- await executeQuery('ALTER TABLE test_pk ADD COLUMN id INT')
- // Add primary key
- const { sql: updateSql } = await pgMeta.tables.update(
- { id: 0, name: 'test_pk', schema: 'public' },
- { primary_keys: [{ name: 'id' }] }
- )
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_pk',
- schema: 'public',
- })
- const res = zod.parse((await executeQuery(retrieveSql))[0])
- expect(res!.primary_keys).toHaveLength(1)
- expect(res!.primary_keys[0].name).toBe('id')
- })
- withTestDatabase('update table - remove primary keys', async ({ executeQuery }) => {
- // Create test table with primary key
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_pk_remove' })
- await executeQuery(createSql)
- await executeQuery('ALTER TABLE test_pk_remove ADD COLUMN id INT PRIMARY KEY')
- const { sql: tableSql, zod: tableZod } = await pgMeta.tables.retrieve({
- name: 'test_pk_remove',
- schema: 'public',
- })
- const table = tableZod.parse((await executeQuery(tableSql))[0])
- // Remove primary key
- const { sql: updateSql } = await pgMeta.tables.update(table!, { primary_keys: [] })
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_pk_remove',
- schema: 'public',
- })
- const res = zod.parse((await executeQuery(retrieveSql))[0])
- expect(res!.primary_keys).toHaveLength(0)
- })
- withTestDatabase('update table - comment', async ({ executeQuery }) => {
- // Create test table
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_comment' })
- await executeQuery(createSql)
- // Add comment
- const { sql: updateSql } = await pgMeta.tables.update(
- { id: 0, name: 'test_comment', schema: 'public' },
- { comment: 'Test comment' }
- )
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_comment',
- schema: 'public',
- })
- const res = zod.parse((await executeQuery(retrieveSql))[0])
- expect(res!.comment).toBe('Test comment')
- })
- withTestDatabase('update table - multiple changes', async ({ executeQuery }) => {
- // Create test table
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_multiple' })
- await executeQuery(createSql)
- await executeQuery('ALTER TABLE test_multiple ADD COLUMN id INT')
- // Make multiple changes
- const { sql: updateSql } = await pgMeta.tables.update(
- { id: 0, name: 'test_multiple', schema: 'public' },
- {
- name: 'test_multiple_updated',
- comment: 'Updated table',
- rls_enabled: true,
- primary_keys: [{ name: 'id' }],
- replica_identity: 'FULL',
- }
- )
- await executeQuery(updateSql)
- // Verify all updates
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_multiple_updated',
- schema: 'public',
- })
- const res = zod.parse((await executeQuery(retrieveSql))[0])
- expect(res).toMatchObject({
- name: 'test_multiple_updated',
- comment: 'Updated table',
- rls_enabled: true,
- replica_identity: 'FULL',
- primary_keys: [expect.objectContaining({ name: 'id' })],
- })
- })
- withTestDatabase('update table - by id', async ({ executeQuery }) => {
- // Create test table
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_by_id' })
- await executeQuery(createSql)
- // Get table id
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_by_id',
- schema: 'public',
- })
- const table = zod.parse((await executeQuery(retrieveSql))[0])
- // Update by id
- const { sql: updateSql } = await pgMeta.tables.update(table!, { name: 'test_by_id_updated' })
- await executeQuery(updateSql)
- // Verify update
- const { sql: verifySQL, zod: verifyZod } = await pgMeta.tables.retrieve({
- name: 'test_by_id_updated',
- schema: 'public',
- })
- const res = verifyZod.parse((await executeQuery(verifySQL))[0])
- expect(res!.name).toBe('test_by_id_updated')
- })
- withTestDatabase('update table - error on non-existent table', async ({ executeQuery }) => {
- const { sql: updateSql } = await pgMeta.tables.update(
- { id: 0, name: 'non_existent', schema: 'public' },
- { name: 'new_name' }
- )
- await expect(executeQuery(updateSql)).rejects.toThrow()
- })
- withTestDatabase('update table - rename with schema change', async ({ executeQuery }) => {
- // Create test schema and table
- await executeQuery('CREATE SCHEMA test_schema')
- const { sql: createSql } = await pgMeta.tables.create({ name: 'test_rename_schema' })
- await executeQuery(createSql)
- // Update both name and schema
- const { sql: updateSql } = await pgMeta.tables.update(
- { id: 0, name: 'test_rename_schema', schema: 'public' },
- {
- name: 'test_renamed_schema',
- schema: 'test_schema',
- }
- )
- await executeQuery(updateSql)
- // Verify update
- const { sql: retrieveSql, zod } = await pgMeta.tables.retrieve({
- name: 'test_renamed_schema',
- schema: 'test_schema',
- })
- const res = zod.parse((await executeQuery(retrieveSql))[0])
- expect(res!.name).toBe('test_renamed_schema')
- expect(res!.schema).toBe('test_schema')
- })
- // Table creation test cases
- const tableCreationTests = [
- {
- name: 'create table with default schema',
- input: {
- name: 'test_table_1',
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- {
- name: 'create table with explicit public schema',
- input: {
- name: 'test_table_2',
- schema: 'public',
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- {
- name: 'create table with custom schema',
- input: {
- name: 'test_table_3',
- schema: 'custom_schema',
- },
- expectedSchema: 'custom_schema',
- expectedComment: null,
- beforeTest: async (
- executeQuery: Awaited<ReturnType<typeof createTestDatabase>>['executeQuery']
- ) => {
- await executeQuery('CREATE SCHEMA IF NOT EXISTS custom_schema')
- },
- },
- {
- name: 'create table with comment',
- input: {
- name: 'test_table_4',
- comment: 'Test comment',
- },
- expectedSchema: 'public',
- expectedComment: 'Test comment',
- },
- {
- name: 'create table with empty string comment',
- input: {
- name: 'test_table_5',
- comment: '',
- },
- expectedSchema: 'public',
- expectedComment: null, // PostgreSQL treats empty string comments as NULL
- },
- {
- name: 'create table with null comment',
- input: {
- name: 'test_table_6',
- comment: null,
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- {
- name: 'create table with uppercase name',
- input: {
- name: 'UPPERCASE_TABLE',
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- {
- name: 'create table with mixed case name',
- input: {
- name: 'MixedCase_Table_Name',
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- {
- name: 'create table with quoted name',
- input: {
- name: 'table "with" quotes',
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- {
- name: 'create table with special characters',
- input: {
- name: 'table$with#special@chars',
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- {
- name: 'create table with name at maximum length',
- input: {
- name: 'a'.repeat(63), // PostgreSQL has a 63-byte limit for identifiers
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- {
- name: 'create table with schema containing special characters',
- input: {
- name: 'normal_table',
- schema: 'Special.Schema$Name',
- },
- expectedSchema: 'Special.Schema$Name',
- expectedComment: null,
- beforeTest: async (executeQuery: TestDb['executeQuery']) => {
- await executeQuery('CREATE SCHEMA IF NOT EXISTS "Special.Schema$Name"')
- },
- },
- {
- name: 'create table with very long name',
- input: {
- name: 'a'.repeat(63),
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- {
- name: 'create table with special characters in name',
- input: {
- name: 'table,name',
- },
- expectedSchema: 'public',
- expectedComment: null,
- },
- ]
- // SQL injection test cases
- const sqlInjectionTests = [
- {
- name: 'prevent SQL injection in table name',
- input: {
- name: "table_name'; DROP TABLE users; --",
- },
- },
- {
- name: 'prevent SQL injection in comment',
- input: {
- name: 'safe_table',
- comment: "normal comment'; DROP TABLE users; --",
- },
- },
- ]
- // Error test cases
- const errorTests = [
- {
- name: 'fail on duplicate table name',
- input: { name: 'duplicate_table' },
- setup: async (executeQuery: TestDb['executeQuery']) => {
- await executeQuery(pgMeta.tables.create({ name: 'duplicate_table' }).sql)
- },
- expectedError: /relation.*already exists/,
- },
- {
- name: 'fail on invalid schema',
- input: { name: 'test_table', schema: 'nonexistent_schema' },
- expectedError: /schema.*does not exist/,
- },
- {
- name: 'fail on empty table name',
- input: {
- name: '',
- },
- expectedError: /zero-length delimited identifier/,
- },
- {
- name: 'fail on schema name exceeding maximum length',
- input: {
- name: 'table',
- schema: 'a'.repeat(64),
- },
- expectedError: /schema.*does not exist/,
- },
- ]
- // Generate individual test cases for successful table creation
- for (const testCase of tableCreationTests) {
- withTestDatabase(testCase.name, async ({ executeQuery }) => {
- if (testCase.beforeTest) {
- await testCase.beforeTest(executeQuery)
- }
- const { sql } = pgMeta.tables.create(testCase.input)
- await executeQuery(sql)
- const { sql: listSql, zod: listZod } = await pgMeta.tables.list()
- const tables = listZod.parse(await executeQuery(listSql))
- const createdTable = tables.find((t) => t.name === testCase.input.name)
- expect(createdTable).toBeDefined()
- expect(createdTable?.schema).toBe(testCase.expectedSchema)
- expect(createdTable?.comment).toBe(testCase.expectedComment)
- })
- }
- // Generate individual test cases for SQL injection prevention
- for (const testCase of sqlInjectionTests) {
- withTestDatabase(`create - ${testCase.name}`, async ({ executeQuery }) => {
- const { sql } = pgMeta.tables.create(testCase.input)
- await executeQuery(sql)
- // Verify table was created with correct name
- const { sql: listSql, zod: listZod } = await pgMeta.tables.list()
- const tables = listZod.parse(await executeQuery(listSql))
- expect(tables.find((t) => t.name === testCase.input.name)).toBeDefined()
- // Verify users table still exists (wasn't dropped)
- expect(tables.find((t) => t.name === 'users')).toBeDefined()
- })
- }
- // Generate individual test cases for error conditions
- for (const testCase of errorTests) {
- withTestDatabase(`create - ${testCase.name}`, async ({ executeQuery }) => {
- if (testCase.setup) {
- await testCase.setup(executeQuery)
- }
- const { sql } = pgMeta.tables.create(testCase.input)
- await expect(executeQuery(sql)).rejects.toThrow(testCase.expectedError)
- })
- }
|