-
Notifications
You must be signed in to change notification settings - Fork 69
Description
Hi,
we've been using Postgrator for a while now, and ran into a bug after a recent upgrade. This is a simplified version of the function we run:
import pg from 'pg';
import Postgrator from 'postgrator';
export async function migrateDb(): Promise<void> {
const client = new pg.Client({
host: '127.0.0.1',
port: 5432,
database: 'postgres',
user: 'postgres',
password: 'the password',
});
try {
await client.connect();
// connection test – added for demonstration purposes
await client.query(`CREATE TABLE persons (personid INT,lastname VARCHAR(255));`);
await client.query(`INSERT INTO persons (personid,lastname) VALUES (1,'Doe');`);
const result = await client.query('SELECT lastname FROM persons;');
console.debug('connection test query result', result.rows);
const postgrator = new Postgrator({
migrationPattern: 'migrations/*',
driver: 'pg',
database: 'postgres',
schemaTable: 'schemaversion',
execQuery: query => client.query(query),
});
postgrator.on('migration-started', m => console.debug(`migration-started\t #${m.version} ${m.name}`));
postgrator.on('migration-finished', m => console.debug(`migration-finished\t #${m.version} ${m.name}`));
console.debug('before migration');
await postgrator.migrate();
console.debug('after migration');
await client.end();
} catch (error) {
console.error('migration failed', error);
await client.end();
throw error;
}
}I run this code on/with
- node v12.22.10
- pg 8.7.3
- postgrator 5.0.1
- Postgres 9.6
and get the following output:
connection test query result [ { lastname: 'Doe' } ]
before migration
migration failed error: role "kaiputh" does not exist
at Connection.parseE (/Users/kaiputh/code/project-name/backend/out/dist/node_modules/pg/lib/connection.js:555:11)
at Connection.parseMessage (/Users/kaiputh/code/project-name/backend/out/dist/node_modules/pg/lib/connection.js:380:19)
at Socket.<anonymous> (/Users/kaiputh/code/project-name/backend/out/dist/node_modules/pg/lib/connection.js:120:22)
at Socket.emit (events.js:314:20)
at addChunk (_stream_readable.js:297:12)
at readableAddChunk (_stream_readable.js:272:9)
at Socket.Readable.push (_stream_readable.js:213:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
at TCP.callbackTrampoline (internal/async_hooks.js:126:14) {
length: 99,
severity: 'FATAL',
code: '28000',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'miscinit.c',
line: '510',
routine: 'InitializeSessionUserId',
appliedMigrations: []
}
For some reason, this error only occures during development, and neither in our automated tests nor on the server. It's unclear to me what triggers it.
I modified the code to make it easy to reproduce and debug the issue. As far as I can tell, the connection to the Postgres server works as it should. I can change the role name in the error message by setting the environment variable PGUSER (see pg documentation). It seems like some part or dependency of Postgrator is trying to open a separate database connection with the default values of pg. Is this a bug in Postgrator?