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

dbConn() creates a new connection pool each time it's called #1

Open
jedwards1211 opened this issue Jan 6, 2019 · 2 comments
Open

Comments

@jedwards1211
Copy link

I think you meant for it to only create one connection pool.

jedwards1211 added a commit to jedwards1211/umzug-postgres-storage that referenced this issue Jan 6, 2019
@jedwards1211
Copy link
Author

actually I see that you're ending each pool you use...it seems pointless to use a pool every time, why not just create a Client?

@jedwards1211
Copy link
Author

jedwards1211 commented Jan 6, 2019

Here is how I adapted it for my own code, using modern ES:

import { Client } from 'pg'

export default class UmzugPostgresStorage {
  constructor(config) {
    if (config.storageOptions) {
      config = config.storageOptions
    }

    //establish the dbconnection and store in a promise.
    this.config = config
  }

  async query(sql) {
    const client = new Client(this.config.database)
    try {
      await client.connect()
      await client.query(`
        CREATE TABLE IF NOT EXISTS ${this.config.relation} (
            "${this.config.column}" character varying(255)
        );
      `)
      return await client.query(sql)
    } finally {
      await client.end()
    }
  }

  async logMigration(migrationName) {
    await this.query(`
      INSERT INTO ${this.config.relation}
        ("${this.config.column}")
      SELECT '${migrationName}'
        WHERE NOT EXISTS (
          SELECT "${this.config.column}" FROM ${this.config.relation}
          WHERE "${this.config.column}" = '${migrationName}'
        );
    `)
  }

  async unlogMigration(migrationName) {
    await this.query(`
      DELETE FROM ${this.config.relation}
      WHERE "${this.config.column}" = '${migrationName}'
    `)
  }

  async executed() {
    const { rows } = await this.query(`
      SELECT "${this.config.column}"
      FROM ${this.config.relation}
      ORDER BY "${this.config.column}" ASC;
    `)
    return rows.map(row => row[this.config.column])
  }
}

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

Successfully merging a pull request may close this issue.

1 participant