-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
237 additions
and
132 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,66 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { main } from '../main' | ||
import { DATABASE_URL } from '../../__tests__/utils' | ||
|
||
describe('cli', () => { | ||
it('DATABASE_URL is required', async () => { | ||
delete process.env.DATABASE_URL | ||
beforeEach(() => { | ||
vi.spyOn(console, 'error').mockImplementation((...args) => { | ||
process.stderr.write(`${args.join(' ')}\n`) | ||
}) | ||
vi.spyOn(console, 'log').mockImplementation((...args) => { | ||
process.stdout.write(`${args.join(' ')}\n`) | ||
}) | ||
}) | ||
|
||
it('should throw error on failed database connection', async () => { | ||
process.env.DATABASE_URL = 'postgres://invalid' | ||
|
||
await main() | ||
|
||
expect(console.error).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'Error connecting to database, please check your DATABASE_URL' | ||
) | ||
) | ||
|
||
expect(console.error).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'getaddrinfo ENOTFOUND invalid' | ||
) | ||
) | ||
}) | ||
|
||
it('should log success on successful connection', async () => { | ||
process.env.DATABASE_URL = DATABASE_URL | ||
|
||
await main() | ||
|
||
expect(console.log).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'Connected to database successfully' | ||
) | ||
) | ||
}) | ||
|
||
it('status', async () => { | ||
process.env.DATABASE_URL = DATABASE_URL | ||
|
||
await main('status') | ||
|
||
await expect(async () => main()).rejects.toThrow('DATABASE_URL not set, please run `DATABASE_URL=postgres://<your pg url> typed-pg`') | ||
expect(console.log).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'Status:' | ||
) | ||
) | ||
expect(console.log).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'Lock:' | ||
) | ||
) | ||
expect(console.log).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
'Migrations:' | ||
) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
import { main } from './main' | ||
|
||
main() | ||
main().finally(() => process.exit(0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,50 @@ | ||
import postgres from "postgres" | ||
import postgres, { type Sql } from "postgres" | ||
import { Logger } from '@faasjs/logger' | ||
|
||
export async function main() { | ||
export async function main(operation = process.argv[2] as string) { | ||
const logger = new Logger('TypedPg') | ||
const connection = process.env.DATABASE_URL as string | ||
|
||
if (!connection) | ||
throw Error('DATABASE_URL not set, please run `DATABASE_URL=postgres://<your pg url> typed-pg`') | ||
if (!connection) { | ||
logger.error('DATABASE_URL not set, please run `DATABASE_URL=postgres://<your pg url> typed-pg`') | ||
return | ||
} | ||
|
||
const sql = postgres(connection) | ||
let sql: Sql | ||
|
||
try { | ||
sql = postgres(connection) | ||
await sql`SELECT 1` | ||
logger.info('Connected to database successfully') | ||
} catch (error) { | ||
console.error(error) | ||
throw Error('Error connecting to database, please check your DATABASE_URL') | ||
logger.error('Error connecting to database, please check your DATABASE_URL\n', error) | ||
return | ||
} | ||
|
||
if (!operation) { | ||
logger.error('Please provide a operation to run: `typed-pg <operation>`, e.g. `typed-pg status`') | ||
return | ||
} | ||
|
||
console.log('Connected to database') | ||
switch (operation) { | ||
case 'status': { | ||
await sql`CREATE TABLE IF NOT EXISTS typed_pg_migrations_lock ( | ||
"index" serial4 NOT NULL,is_locked int4 NULL,CONSTRAINT typed_pg_migrations_lock_pkey PRIMARY KEY (index) | ||
)` | ||
await sql`CREATE TABLE IF NOT EXISTS typed_pg_migrations ( | ||
id serial4 NOT NULL, | ||
"name" varchar(255) NULL, | ||
batch int4 NULL, | ||
migration_time timestamptz NULL, | ||
CONSTRAINT typed_pg_migrations_pkey PRIMARY KEY (id) | ||
)` | ||
const lock = await sql`SELECT * FROM typed_pg_migrations_lock` | ||
const migrations = await sql`SELECT * FROM typed_pg_migrations` | ||
|
||
logger.info('Status:') | ||
logger.info('Lock:', lock) | ||
logger.info('Migrations:', migrations) | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.