Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration fails with node-postgres' default connection configuration #148

Open
kputh opened this issue Mar 1, 2022 · 1 comment
Open

Comments

@kputh
Copy link

kputh commented Mar 1, 2022

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?

@kputh kputh changed the title Migration fails with seemingly wrong connection configuration Migration fails with node-postgres' default connection configuration Mar 1, 2022
@rickbergfalk
Copy link
Owner

@kputh Postgrator just calls the execQuery to do everything with latest version. I wondered if something was funky with the this context of things, but I don't understand why it'd work in some environments and not others.

Is there anything in your migrations that reference the role kaiputh?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants