oguimbal / pg-mem

An in memory postgres DB instance for your unit tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Insert statement fails after column gets renamed

krumft opened this issue Β· comments

Our application is using typeorm version 0.3.16 against a Postgres database, and we maintain our DB schema in TypeScript migrations. These are getting executed successfully by the ORM framework against the 'real' Postgres db.

We are using pg-mem version 2.6.9 to create some unit and integration tests for our application.

Our test code executes the following function definition:

import { v4 } from 'uuid';
....
db.registerExtension('uuid-ossp', (schema) => {
			schema.registerFunction({
				name: 'uuid_generate_v4',
				returns: DataType.uuid,
				implementation: v4,
				impure: true,
			});
		});

We have recently renamed a column in one of our tables and hit the following issue.

Scenario 1: No renaming, inserting into the existing table schema, table has no primary key::

CREATE TABLE test("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "col" character varying);
INSERT INTO test(id, col) VALUES (DEFAULT, '1'), (DEFAULT, '2') RETURNING "id";

Result: execution is successful.

Scenario 2: No renaming, inserting into the existing table schema, table has primary key constraint:

CREATE TABLE test("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "col" character varying, CONSTRAINT "PK" PRIMARY KEY ("id"));
INSERT INTO test(id, col) VALUES (DEFAULT, '1'), (DEFAULT, '2') RETURNING "id";

Result: execution is successful.

Scenario 3: Renaming a column, and then inserting into the new table schema, table has no primary key:

CREATE TABLE test("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "col" character varying);
ALTER TABLE test RENAME COLUMN "col" TO "newcol";
INSERT INTO test(id, newcol) VALUES (DEFAULT, '1'), (DEFAULT, '2') RETURNING "id";

Result: execution is successful.

Scenario 4: Renaming a column, and then inserting into the new table schema, table has primary key constraint:

CREATE TABLE test("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "col" character varying, CONSTRAINT "PK" PRIMARY KEY ("id"));
ALTER TABLE test RENAME COLUMN "col" TO "newcol";
INSERT INTO test(id, newcol) VALUES (DEFAULT, '1'), (DEFAULT, '2') RETURNING "id";

Result:


Error: Corrupted alias

πŸ’₯ This is a nasty error, which was unexpected by pg-mem. Also known "a bug" 😁 Please file an issue !

*️⃣ Reconsituted failed SQL statement: INSERT INTO test  (id, newcol) VALUES (( DEFAULT ), ('1')), (( DEFAULT ), ('2'))  RETURNING id

πŸ‘‰ You can file an issue at https://github.com/oguimbal/pg-mem along with a way to reproduce this error (if you can), and  the stacktrace:



    at Alias._getColumn (...node_modules/pg-mem/src/transforms/alias.ts:135:19)
    at Alias.getColumn (...node_modules/pg-mem/src/transforms/alias.ts:104:26)
    at ...node_modules/pg-mem/src/execution/records-mutations/insert.ts:53:53
    at Array.map (<anonymous>)
    at Insert.visit (...node_modules/pg-mem/src/execution/records-mutations/insert.ts:51:56)
    at ...node_modules/pg-mem/src/execution/records-mutations/insert.ts:37:40
    at StackOf.usingValue (...node_modules/pg-mem/src/parser/context.ts:11:20)
    at new Insert (...node_modules/pg-mem/src/execution/records-mutations/insert.ts:37:22)
    at buildWithable (...node_modules/pg-mem/src/execution/select.ts:41:20)
    at new SelectExec (...node_modules/pg-mem/src/execution/select.ts:270:67)

Raised #341 to address this