From 4386009895f17af6ff0ff99639855d34a79c4699 Mon Sep 17 00:00:00 2001 From: Stephen Mathieson Date: Thu, 18 Feb 2021 11:55:41 -0500 Subject: [PATCH 1/6] Setup GitHub action for running tests This patch adds a GitHub action for running the tests and updates the `docker-compose` configuration (which enables the tests to run/pass). --- .github/workflows/tests.yml | 15 +++++++++++++++ __tests__/index.test.ts | 2 +- docker-compose.yml | 24 ++++++++++++++---------- 3 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..1eea69a --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,15 @@ +name: Tests + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: '12' + - run: yarn + - run: yarn lint + - run: yarn test diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 57428a8..aa9d530 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -2,7 +2,7 @@ import { Postgres } from '../src/pg-client' import { inferTable, inferSchema } from '../src' import { SQL as sql } from 'sql-template-strings' -const connectionString = 'postgresql://postgres@localhost:5433/db?currentSchema=public' +const connectionString = 'postgresql://postgres:password@localhost:5433/db?currentSchema=public' const pg = new Postgres(connectionString) const account = sql` diff --git a/docker-compose.yml b/docker-compose.yml index cf93a19..8c56e2e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,15 @@ -postgres: - image: postgres:9.6 - ports: - - '5433:5432' - environment: - POSTGRES_DB: 'db' +version: '3.1' +services: + postgres: + image: postgres:9.6 + ports: + - '5433:5432' + environment: + POSTGRES_DB: 'db' + POSTGRES_PASSWORD: 'password' + POSTGRES_USER: 'postgres' -musicbrainz: - image: arey/musicbrainz-database - ports: - - '5434:5432' + musicbrainz: + image: arey/musicbrainz-database + ports: + - '5434:5432' From 7064492eee97fc5f5b0226a3890ee89eef94cf87 Mon Sep 17 00:00:00 2001 From: Stephen Mathieson Date: Sun, 21 Feb 2021 01:00:30 -0500 Subject: [PATCH 2/6] Refactor test script, add wait-port --- docker-compose.yml | 8 ++++---- package.json | 6 ++++-- yarn.lock | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index f8d256f..c83abfe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,7 @@ postgres: POSTGRES_DB: 'db' POSTGRES_PASSWORD: 'password' - musicbrainz: - image: arey/musicbrainz-database - ports: - - '5434:5432' +musicbrainz: + image: arey/musicbrainz-database + ports: + - '5434:5432' diff --git a/package.json b/package.json index 28f1f1a..b0ad4e1 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "license": "MIT", "scripts": { "lint": "eslint src/**/*.ts", - "test": "docker-compose up -d && jest --runInBand --forceExit", + "pretest": "docker-compose up -d && wait-port 5433", + "test": "jest --runInBand --forceExit", "pub": "tsc && np", "cli": "ts-node --transpile-only ./bin/index.ts" }, @@ -68,6 +69,7 @@ "lint-staged": "^9.5.0", "np": "^5.2.1", "ts-jest": "^24.2.0", - "typescript": "^3.7.3" + "typescript": "^3.7.3", + "wait-port": "^0.2.9" } } diff --git a/yarn.lock b/yarn.lock index d62562e..0c853f6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1143,6 +1143,11 @@ commander@^2.20.0, commander@~2.20.3: resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ== +commander@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/commander/-/commander-3.0.2.tgz#6837c3fb677ad9933d1cfba42dd14d5117d6b39e" + integrity sha512-Gar0ASD4BDyKC4hl4DwHqDrmvjoxWKZigVnAbn5H1owvm4CxCPdb0HQDehwNYMJpla5+M2tPmPARzhtYuwpHow== + component-emitter@^1.2.1: version "1.3.0" resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0" @@ -5517,6 +5522,15 @@ w3c-hr-time@^1.0.1: dependencies: browser-process-hrtime "^0.1.2" +wait-port@^0.2.9: + version "0.2.9" + resolved "https://registry.yarnpkg.com/wait-port/-/wait-port-0.2.9.tgz#3905cf271b5dbe37a85c03b85b418b81cb24ee55" + integrity sha512-hQ/cVKsNqGZ/UbZB/oakOGFqic00YAMM5/PEj3Bt4vKarv2jWIWzDbqlwT94qMs/exAQAsvMOq99sZblV92zxQ== + dependencies: + chalk "^2.4.2" + commander "^3.0.2" + debug "^4.1.1" + walker@^1.0.7, walker@~1.0.5: version "1.0.7" resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb" From 4832356c70a5e15027591914bfa0591aa3ae782a Mon Sep 17 00:00:00 2001 From: Stephen Mathieson Date: Sun, 21 Feb 2021 01:10:25 -0500 Subject: [PATCH 3/6] Disconnect from DB To avoid seeing tons of "_Creating a duplicate database object for the same connection_" warnings, we're now disconnecting from the database after generating code. --- __tests__/index.test.ts | 3 ++- src/index.ts | 8 ++++++-- src/pg-client.ts | 4 ++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 55108f7..a63c8eb 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -3,7 +3,6 @@ import { inferTable, inferSchema } from '../src' import { SQL as sql } from 'sql-template-strings' const connectionString = 'postgresql://postgres:password@localhost:5433/db?currentSchema=public' -const pg = new Postgres(connectionString) const account = sql` DROP TABLE IF EXISTS account; @@ -43,9 +42,11 @@ const complex = sql` ` beforeAll(async () => { + const pg = new Postgres(connectionString) await pg.query(account) await pg.query(requests) await pg.query(complex) + pg.disconnect() }) describe('inferTable', () => { diff --git a/src/index.ts b/src/index.ts index c9365d9..7acaacd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,7 +27,9 @@ export async function inferTable(connectionString: string, table: string): Promi ${header(code.includes('JSONValue'))} ${code} ` - return pretty(fullCode) + const result = pretty(fullCode) + db.disconnect() + return result } export async function inferSchema(connectionString: string): Promise { @@ -35,5 +37,7 @@ export async function inferSchema(connectionString: string): Promise { const tables = await db.allTables() const interfaces = tables.map(table => tableToTS(table.name, table.table)) const code = [header(interfaces.some(i => i.includes('JSONValue'))), ...interfaces].join('\n') - return pretty(code) + const result = pretty(code) + db.disconnect() + return result } diff --git a/src/pg-client.ts b/src/pg-client.ts index cc8009b..aabf4b0 100644 --- a/src/pg-client.ts +++ b/src/pg-client.ts @@ -18,6 +18,10 @@ export class Postgres { this.defaultSchema = database } + public disconnect(): void { + pgp.end() + } + public async table(tableName: string): Promise { const enumTypes = await this.enums(tableName) const table = await this.getTable(tableName, this.schema()) From e3e187649c3f977c54f462961b05d94ac1729968 Mon Sep 17 00:00:00 2001 From: Stephen Mathieson Date: Sun, 21 Feb 2021 01:12:45 -0500 Subject: [PATCH 4/6] Revert "Disconnect from DB" This reverts commit 4832356c70a5e15027591914bfa0591aa3ae782a. While the change prevented the warnings locally, it had no effect in CI :shrug: --- __tests__/index.test.ts | 3 +-- src/index.ts | 8 ++------ src/pg-client.ts | 4 ---- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index a63c8eb..55108f7 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -3,6 +3,7 @@ import { inferTable, inferSchema } from '../src' import { SQL as sql } from 'sql-template-strings' const connectionString = 'postgresql://postgres:password@localhost:5433/db?currentSchema=public' +const pg = new Postgres(connectionString) const account = sql` DROP TABLE IF EXISTS account; @@ -42,11 +43,9 @@ const complex = sql` ` beforeAll(async () => { - const pg = new Postgres(connectionString) await pg.query(account) await pg.query(requests) await pg.query(complex) - pg.disconnect() }) describe('inferTable', () => { diff --git a/src/index.ts b/src/index.ts index 7acaacd..c9365d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,9 +27,7 @@ export async function inferTable(connectionString: string, table: string): Promi ${header(code.includes('JSONValue'))} ${code} ` - const result = pretty(fullCode) - db.disconnect() - return result + return pretty(fullCode) } export async function inferSchema(connectionString: string): Promise { @@ -37,7 +35,5 @@ export async function inferSchema(connectionString: string): Promise { const tables = await db.allTables() const interfaces = tables.map(table => tableToTS(table.name, table.table)) const code = [header(interfaces.some(i => i.includes('JSONValue'))), ...interfaces].join('\n') - const result = pretty(code) - db.disconnect() - return result + return pretty(code) } diff --git a/src/pg-client.ts b/src/pg-client.ts index aabf4b0..cc8009b 100644 --- a/src/pg-client.ts +++ b/src/pg-client.ts @@ -18,10 +18,6 @@ export class Postgres { this.defaultSchema = database } - public disconnect(): void { - pgp.end() - } - public async table(tableName: string): Promise
{ const enumTypes = await this.enums(tableName) const table = await this.getTable(tableName, this.schema()) From ed25e3ffc4992a67a7662ff39f8f7fc168cf835f Mon Sep 17 00:00:00 2001 From: Stephen Mathieson Date: Sun, 21 Feb 2021 01:18:49 -0500 Subject: [PATCH 5/6] Refactor query method Wait until we've connected before running a query --- src/pg-client.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/pg-client.ts b/src/pg-client.ts index cc8009b..71165f9 100644 --- a/src/pg-client.ts +++ b/src/pg-client.ts @@ -35,7 +35,10 @@ export class Postgres { } public async query(query: SQLStatement): Promise { - return this.connection.query(query.text, query.values) + const client = await this.connection.connect() + const result = await client.query(query.text, query.values) + client.done() + return result } private async tableNames(): Promise { From b1ab79bcd596222de06c192b2e64c17aabbea513 Mon Sep 17 00:00:00 2001 From: Stephen Mathieson Date: Sun, 21 Feb 2021 01:20:33 -0500 Subject: [PATCH 6/6] Revert "Refactor query method" This reverts commit ed25e3ffc4992a67a7662ff39f8f7fc168cf835f. I have no idea what's going on --- src/pg-client.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/pg-client.ts b/src/pg-client.ts index 71165f9..cc8009b 100644 --- a/src/pg-client.ts +++ b/src/pg-client.ts @@ -35,10 +35,7 @@ export class Postgres { } public async query(query: SQLStatement): Promise { - const client = await this.connection.connect() - const result = await client.query(query.text, query.values) - client.done() - return result + return this.connection.query(query.text, query.values) } private async tableNames(): Promise {