-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/#181 add roles table
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getConnection } from "./index"; | ||
|
||
export async function createRolesTable() { | ||
const client = await getConnection(); | ||
|
||
const { | ||
rows: [{ exists: rolesExists }], | ||
} = await client.query(` | ||
SELECT EXISTS( SELECT 1 FROM pg_tables WHERE schemaname='public' and tablename='roles'); | ||
`); | ||
|
||
if (!rolesExists) { | ||
await client.query( | ||
` | ||
CREATE TABLE roles ( | ||
roleId uuid PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
roleName varchar (64) UNIQUE NOT NULL | ||
) | ||
` | ||
); | ||
return true; | ||
} | ||
return false; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Dotenv from "dotenv"; | ||
import { getConnection, pool } from "../db"; | ||
|
||
Dotenv.config({ path: ".env.test.local" }); | ||
|
||
import { createRolesTable } from "../db/roles"; | ||
|
||
beforeAll(async () => { | ||
const client = await getConnection(); | ||
await client.query("DROP TABLE IF EXISTS roles"); | ||
await createRolesTable(); | ||
}); | ||
|
||
test("Verify Table Created", async () => { | ||
expect(process.env.PGUSER).toBe("docker"); | ||
const client = await getConnection(); | ||
const result = await client.query(` | ||
SELECT EXISTS( SELECT 1 FROM pg_tables WHERE schemaname='public' and tablename='roles'); | ||
`); | ||
expect(result).toBeTruthy(); | ||
}); | ||
|
||
afterAll(async () => { | ||
const client = await getConnection(); | ||
await client.release(); | ||
await pool.end(); | ||
}); |