diff --git a/src/db/db.ts b/src/db/db.ts index 5a862a98..b11824c0 100644 --- a/src/db/db.ts +++ b/src/db/db.ts @@ -28,5 +28,6 @@ export abstract class SQLDatabase { public abstract getIndicesForTable(tableName: string): Promise; public abstract getAllTriggers(): Promise; + public abstract getAllTableNames(): Promise; public abstract getAllViews(): Promise; } diff --git a/src/db/postgres-db.ts b/src/db/postgres-db.ts index 1a5fa04c..3ec6864e 100644 --- a/src/db/postgres-db.ts +++ b/src/db/postgres-db.ts @@ -173,4 +173,12 @@ ${chalk.magentaBright('>>')} ${highlight(query.trim(), { sql`select viewname as name from pg_catalog.pg_views;` )).map((result: any) => result.name as string); } + public async getAllTableNames(): Promise { + return (await this.all( + sql`SELECT table_name as name + FROM information_schema.tables + WHERE table_schema='public' + AND table_type='BASE TABLE';` + )).map((result: any) => result.name as string); + } } diff --git a/src/db/sqlite-db.ts b/src/db/sqlite-db.ts index 6eab3a64..bf8c2737 100644 --- a/src/db/sqlite-db.ts +++ b/src/db/sqlite-db.ts @@ -58,7 +58,7 @@ export default class SQLiteDB extends SQLDatabase { return setupPreparedStatements< sqlite.Statement, SQLDatabase - >(db); + >(db); }) .then(statements => { db.statements = statements; @@ -125,4 +125,9 @@ ${chalk.magentaBright('>>')} ${highlight(sql.trim(), { sql`select * from sqlite_master where type = 'view';` )).map((i: any) => i.name as string); } + public async getAllTableNames(): Promise { + return (await this.all( + sql`SELECT name FROM sqlite_master WHERE type='table';` + )).map((i: any) => i.name as string); + } } diff --git a/tests/ex010.transactions-table.test.ts b/tests/ex010.transactions-table.test.ts index 644b73de..ae7c9423 100644 --- a/tests/ex010.transactions-table.test.ts +++ b/tests/ex010.transactions-table.test.ts @@ -54,9 +54,7 @@ class TransactionsTableTest { @test('Transactions table exists') public async transactionsExists() { let db = await getDb('dev'); - let allTables = (await db.all( - sql`SELECT name FROM sqlite_master WHERE type='table'` - )).map(i => i.name); + let allTables = await db.getAllTableNames(); assert.includeMembers( allTables.map(t => t.toLowerCase()), ['transaction'], @@ -67,13 +65,13 @@ class TransactionsTableTest { @test('Inserting a new transaction completes successfully') public async insertTransaction() { let db = await getDb('dev'); - let beforeTransactions = await db.all(sql`SELECT * from "Transaction"`); + let beforeTransactions = await db.all(sql`SELECT * from "transaction"`); let transaction = await db.get( - sql`INSERT INTO "Transaction" (Authorization, OrderId) VALUES (?, ?)`, + sql`INSERT INTO "transaction" ("authorization", orderid) VALUES ($1, $2)`, 'lk1hdklh12ld', 10264 ); - let afterTransactions = await db.all(sql`SELECT * from "Transaction"`); + let afterTransactions = await db.all(sql`SELECT * from "transaction"`); assert.equal( beforeTransactions.length + 1, afterTransactions.length, @@ -88,7 +86,7 @@ class TransactionsTableTest { try { let transaction = await db.run( sql` - INSERT INTO "Transaction" (Authorization, OrderId) VALUES (?, ?)`, + INSERT INTO "transaction" ("authorization", orderid) VALUES ($1, $2)`, 'lk1hdklh12ld', 191927158 ); @@ -105,9 +103,9 @@ class TransactionsTableTest { 'At least one error (foreign key constraint violation) should have been thrown' ); assert.include( - errors[0], - 'FOREIGN KEY', - 'Error message says something about "FOREIGN KEY' + errors[0].toLowerCase(), + 'foreign key', + 'Error message says something about "foreign key' ); } }