diff --git a/package.json b/package.json index 1ec095c..294e5eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@analtools/jsonormalize", - "version": "0.0.15", + "version": "0.0.16", "description": "JSONormalize — Transform any JSON into a relational database schema. Automatically normalizes nested structures, detects relationships, and generates SQLite migrations. Perfect for rapid prototyping, data migrations, and structured data workflows.", "keywords": [ "json-normalize", diff --git a/src/postgres/__snapshots__/create-initial-migration.test.ts.snap b/src/postgres/__snapshots__/create-initial-migration.test.ts.snap new file mode 100644 index 0000000..757bce3 --- /dev/null +++ b/src/postgres/__snapshots__/create-initial-migration.test.ts.snap @@ -0,0 +1,18 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`postgres/create-initial-migration > should handle with schemaName 1`] = ` +"CREATE TABLE schema_test.test ( + normalize_id INTEGER NOT NULL, + value TEXT NOT NULL, + PRIMARY KEY (normalize_id) +); + +CREATE TABLE schema_test.test_items ( + normalize_id INTEGER NOT NULL, + normalize_parent_id INTEGER NOT NULL, + value TEXT NOT NULL, + PRIMARY KEY (normalize_id), + FOREIGN KEY (normalize_parent_id) REFERENCES schema_test.test (normalize_id) +); +CREATE INDEX idx_test_items_normalize_parent_id ON schema_test.test_items (normalize_parent_id);" +`; diff --git a/src/postgres/create-initial-migration.test.ts b/src/postgres/create-initial-migration.test.ts new file mode 100644 index 0000000..74bec23 --- /dev/null +++ b/src/postgres/create-initial-migration.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; + +import { createRelationalStructure } from "../create-relational-structure"; +import { normalize } from "../normalize"; +import { createInitialMigration } from "./create-initial-migration"; + +describe("postgres/create-initial-migration", () => { + it("should handle with schemaName", () => { + expect( + createInitialMigration({ + tables: createRelationalStructure( + "test", + normalize([ + { value: "a", items: [{ value: "a.1" }, { value: "a.2" }] }, + { value: "b", items: [{ value: "b.1" }] }, + ]), + ), + schemaName: "schema_test", + }), + ).toMatchSnapshot(); + }); +}); diff --git a/src/postgres/create-initial-migration.ts b/src/postgres/create-initial-migration.ts index c793277..3188bef 100644 --- a/src/postgres/create-initial-migration.ts +++ b/src/postgres/create-initial-migration.ts @@ -26,14 +26,17 @@ export function createInitialMigration({ ...(foreignKeys.length ? foreignKeys.map( ({ key, reference }) => - ` FOREIGN KEY (${key}) REFERENCES ${reference.table} (${reference.key})`, + ` FOREIGN KEY (${key}) REFERENCES ${getFullTableName({ tableName: reference.table, schemaName })} (${reference.key})`, ) : []), ].join(`,${EOL}`), `);`, ...foreignKeys.map( ({ key }) => - `CREATE INDEX idx_${table.name}_${key} ON ${table.name} (${key});`, + `CREATE INDEX idx_${table.name}_${key} ON ${getFullTableName({ + tableName: table.name, + schemaName, + })} (${key});`, ), ].join(EOL); })