Skip to content

Commit

Permalink
Merge pull request #195 from ZumDeWald/Feature/#181-RolesTable
Browse files Browse the repository at this point in the history
Feature/#181 add roles table
  • Loading branch information
ZumDeWald authored Oct 4, 2021
2 parents 74ca30d + 48144b1 commit 82f8e4a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions db-setup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Dotenv from "dotenv";
import { getConnection, pool } from "./db";
import { createUsersTable } from "./db/users";
import { createRolesTable } from "./db/roles";

Dotenv.config({ path: ".env.db-setup" });

async function main() {
await createUsersTable();
await createRolesTable();
const client = await getConnection();
await client.release();
await pool.end();
Expand Down
24 changes: 24 additions & 0 deletions db/roles.ts
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;
}
27 changes: 27 additions & 0 deletions test/roles.test.ts
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();
});

0 comments on commit 82f8e4a

Please sign in to comment.