Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
corps-directory
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fhurtado14
corps-directory
Commits
e63dae39
Commit
e63dae39
authored
4 months ago
by
Federico Hurtado
Browse files
Options
Downloads
Patches
Plain Diff
Move table init to DatabaseInitializer
parent
4206f367
No related branches found
No related tags found
1 merge request
!11
Basic functionality achieved of generalized search function. Need to define...
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend/config/database/database.config.js
+0
-42
0 additions, 42 deletions
backend/config/database/database.config.js
backend/config/database/databaseInitializer.js
+32
-0
32 additions, 0 deletions
backend/config/database/databaseInitializer.js
with
32 additions
and
42 deletions
backend/config/database/database.config.js
+
0
−
42
View file @
e63dae39
...
...
@@ -23,46 +23,4 @@ console.log("Database Host: ", process.env.DB_HOST);
console
.
log
(
"
Database user:
"
,
process
.
env
.
DB_USER
);
console
.
log
(
"
Database name:
"
,
process
.
env
.
DB_NAME
);
// Function to execute the SQL file at initilization
const
executeSQLFile
=
(
filePath
)
=>
{
const
sql
=
fs
.
readFileSync
(
filePath
,
"
utf8
"
);
// Split SQL into individual statements
const
sqlStatements
=
sql
.
split
(
/;
\s
*$/m
);
// connect to MySQL
pool
.
getConnection
((
err
,
connection
)
=>
{
if
(
err
)
{
console
.
error
(
"
Error connecting to MySQL:
"
,
err
.
message
);
return
;
}
// successfully connected to DB
console
.
log
(
"
connected to DB, executing schema...
"
);
// Execute each SQL statement in the file one by one
sqlStatements
.
forEach
((
statement
)
=>
{
if
(
statement
.
trim
())
{
// Ignore empty statements
connection
.
query
(
statement
,
(
err
,
result
)
=>
{
if
(
err
)
{
console
.
error
(
"
Error executing SQL statement:
"
,
err
.
message
);
// connection.release();
return
;
}
});
}
console
.
log
(
"
Just added:
"
,
statement
);
});
// release connection after all statements
connection
.
release
();
});
};
// Execute the SQL file during startup
const
schemaFilePath
=
path
.
join
(
__dirname
,
"
./schema.sql
"
);
executeSQLFile
(
schemaFilePath
);
module
.
exports
=
{
pool
:
pool
.
promise
()
};
This diff is collapsed.
Click to expand it.
backend/config/database/databaseInitializer.js
+
32
−
0
View file @
e63dae39
...
...
@@ -16,6 +16,8 @@ const {
checkChapterTableExistence
,
addChapterIfNotExists
,
}
=
require
(
"
../../repository/chapterRepository
"
);
const
fs
=
require
(
"
fs
"
).
promises
;
const
path
=
require
(
"
path
"
);
/*
Class responsible for populating the database
...
...
@@ -27,6 +29,32 @@ class DatabaseInitializer {
this
.
pool
=
pool
;
}
/*
Execute SQL schema file to create necessary tables on startup.
*/
async
executeSQLFile
(
filePath
)
{
try
{
const
sql
=
await
fs
.
readFile
(
filePath
,
"
utf8
"
);
const
sqlStatements
=
sql
.
split
(
/;
\s
*$/m
);
const
connection
=
await
this
.
pool
.
getConnection
();
console
.
log
(
"
Connected to DB, executing schema...
"
);
for
(
const
statement
of
sqlStatements
)
{
if
(
statement
.
trim
())
{
await
connection
.
query
(
statement
);
console
.
log
(
"
Executed statement:
"
,
statement
);
}
}
connection
.
release
();
}
catch
(
err
)
{
console
.
error
(
"
Error executing SQL schema file:
"
,
err
.
message
);
throw
err
;
}
}
/*
Method for ensuring database is accessible.
*/
...
...
@@ -374,6 +402,10 @@ class DatabaseInitializer {
async
initializeAll
()
{
await
this
.
testDBConnection
();
const
schemaFilePath
=
path
.
join
(
__dirname
,
"
./schema.sql
"
);
await
this
.
executeSQLFile
(
schemaFilePath
);
await
this
.
initializeRoles
();
// await this.initializeTestUsers(); // no not initialize test users anymore
await
this
.
initializeStates
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment