@@ -2,7 +2,7 @@ import { promises as fs } from "fs";
22import { dirname } from "path" ;
33import { fileURLToPath } from "url" ;
44
5- import { FastifyInstance , FastifyRequest } from "fastify" ;
5+ import { FastifyInstance } from "fastify" ;
66import { StatusCodes } from "http-status-codes" ;
77import { env } from "../env" ;
88import { createCustomError } from "../error/customError" ;
@@ -16,53 +16,6 @@ const connectionString = env.POSTGRES_CONNECTION_URL;
1616const DATABASE_NAME =
1717 new URL ( connectionString ) . pathname . split ( "/" ) [ 1 ] || "postgres" ;
1818
19- export const checkTablesExistence = async (
20- server : FastifyInstance ,
21- ) : Promise < void > => {
22- try {
23- const knex = await connectToDatabase ( ) ;
24- // Check if the tables Exists
25- const tablesList : string [ ] = env . DB_TABLES_LIST . split ( "," ) . map ( function (
26- item : any ,
27- ) {
28- return item . trim ( ) ;
29- } ) ;
30-
31- if ( ! tablesList ) {
32- const error = createCustomError (
33- "DB_TABLES_LIST ENV variable is empty" ,
34- StatusCodes . NOT_FOUND ,
35- "DB_TABLES_LIST_NOT_FOUND" ,
36- ) ;
37- throw error ;
38- }
39-
40- for ( const tableName of tablesList ) {
41- const schemaSQL = await fs . readFile (
42- `${ __dirname } /sql-schemas/${ tableName } .sql` ,
43- "utf-8" ,
44- ) ;
45- // Create Table using schema
46- await knex . schema . raw ( schemaSQL ) ;
47-
48- server . log . info (
49- `SQL for ${ tableName } processed successfully on start-up` ,
50- ) ;
51- }
52-
53- // Disconnect from DB
54- await knex . destroy ( ) ;
55- } catch ( error : any ) {
56- server . log . error ( error ) ;
57- const customError = createCustomError (
58- "Error while executing Table SQLs on startup" ,
59- StatusCodes . INTERNAL_SERVER_ERROR ,
60- "SERVER_STARTUP_TABLES_CREATION_ERROR" ,
61- ) ;
62- throw customError ;
63- }
64- } ;
65-
6619export const implementTriggerOnStartUp = async (
6720 server : FastifyInstance ,
6821) : Promise < void > => {
@@ -118,49 +71,3 @@ export const implementTriggerOnStartUp = async (
11871 throw customError ;
11972 }
12073} ;
121-
122- export const ensureDatabaseExists = async (
123- server : FastifyInstance | FastifyRequest ,
124- ) : Promise < void > => {
125- try {
126- // Creating KNEX Config
127- let modifiedConnectionString = connectionString ;
128- if ( DATABASE_NAME !== "postgres" ) {
129- // This is required if the Database mentioned in the connection string is not postgres
130- // as we need to connect to the postgres database to create the user provied database
131- // and then connect to the user provided database
132- modifiedConnectionString = connectionString . replace (
133- `/${ DATABASE_NAME } ` ,
134- "/postgres" ,
135- ) ;
136- }
137-
138- let knex = await connectToDatabase ( modifiedConnectionString ) ;
139-
140- // Check if Database Exists & create if it doesn't
141- let hasDatabase : any ;
142- switch ( dbClient ) {
143- case "pg" :
144- server . log . debug ( "checking if pg database exists" ) ;
145- hasDatabase = await knex . raw (
146- `SELECT 1 from pg_database WHERE datname = '${ DATABASE_NAME } '` ,
147- ) ;
148- server . log . info ( `CHECKING for Database ${ DATABASE_NAME } ...` ) ;
149- if ( ! hasDatabase . rows . length ) {
150- await knex . raw ( `CREATE DATABASE ${ DATABASE_NAME } ` ) ;
151- } else {
152- server . log . info ( `Database ${ DATABASE_NAME } already exists` ) ;
153- }
154- break ;
155- default :
156- throw new Error (
157- `Unsupported database client: ${ dbClient } . Cannot create database ${ DATABASE_NAME } ` ,
158- ) ;
159- }
160-
161- await knex . destroy ( ) ;
162- } catch ( error ) {
163- server . log . error ( error ) ;
164- throw new Error ( `Error creating database ${ DATABASE_NAME } ` ) ;
165- }
166- } ;
0 commit comments