mirror of
https://github.com/nowcommunity/nowdatabase.git
synced 2026-10-12 19:13:52 +03:00
implemented non-working draft-version of database reset
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Router } from 'express'
|
||||
import { createTestUsers } from '../services/user'
|
||||
import { resetTestDb } from '../utils/db'
|
||||
const router = Router()
|
||||
|
||||
router.get('/create-test-users', async (_req, res) => {
|
||||
@@ -11,4 +12,9 @@ router.get('/ping', (_req, res) => {
|
||||
return res.status(200).send()
|
||||
})
|
||||
|
||||
router.get("/reset-test-database", async (_req, res) => {
|
||||
await resetTestDb();
|
||||
res.status(200).send()
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
@@ -5,6 +5,10 @@ import { PrismaClient as LogClient } from '../../prisma/generated/now_log_test_c
|
||||
import mariadb from 'mariadb'
|
||||
import { MARIADB_HOST, MARIADB_PASSWORD, DB_CONNECTION_LIMIT, MARIADB_USER } from './config'
|
||||
|
||||
import { readFile } from "fs/promises"
|
||||
import { PathLike } from "fs"
|
||||
import { RUNNING_ENV } from "../utils/config"
|
||||
|
||||
export const logDb = new LogClient()
|
||||
export const nowDb = new NowClient()
|
||||
|
||||
@@ -67,3 +71,43 @@ export const testDbConnection = async () => {
|
||||
}
|
||||
logger.error(`Attempted ${maxAttempts} times, but database connection could not be established`)
|
||||
}
|
||||
|
||||
export const resetTestDb = async () => {
|
||||
if (RUNNING_ENV === "prod")
|
||||
throw new Error(`Trying to reset test database with RUNNING_ENV ${RUNNING_ENV}`)
|
||||
const testPool = mariadb.createPool({
|
||||
host: MARIADB_HOST,
|
||||
password: MARIADB_PASSWORD,
|
||||
user: MARIADB_USER,
|
||||
connectionLimit: parseInt(DB_CONNECTION_LIMIT),
|
||||
checkDuplicate: false,
|
||||
multipleStatements: true
|
||||
})
|
||||
|
||||
const conn = await testPool.getConnection()
|
||||
|
||||
logger.info(`Current dir: ${process.cwd()}`)
|
||||
const fileContentsNowTest = await readSqlFile("../test_data/sqlfiles/now_test.sql")
|
||||
const fileContentsNowLogTest = await readSqlFile("../test_data/sqlfiles/now_log_test.sql")
|
||||
|
||||
if (!fileContentsNowTest || !fileContentsNowLogTest) {
|
||||
logger.error("Couldn't open sqlfiles")
|
||||
return
|
||||
}
|
||||
|
||||
await conn.query(fileContentsNowTest);
|
||||
await conn.query(fileContentsNowLogTest);
|
||||
return "";
|
||||
}
|
||||
|
||||
const readSqlFile = async (filename: PathLike): Promise<string | undefined> => {
|
||||
try{
|
||||
const fileContents = await readFile(filename, "utf8");
|
||||
// logger.info(`Opened file and got: ${fileContents}`)
|
||||
return fileContents
|
||||
|
||||
} catch (e) {
|
||||
logger.error(`Couldn't open file ${filename}: ${e}`);
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ services:
|
||||
context: .
|
||||
dockerfile: ./backend/Dockerfile
|
||||
target: node-run
|
||||
volumes:
|
||||
- ./test_data:/usr/src/app/test_data
|
||||
healthcheck:
|
||||
test: curl -f http://localhost:4000/test/ping || exit 1
|
||||
start_period: 10s
|
||||
|
||||
@@ -19,6 +19,7 @@ services:
|
||||
volumes:
|
||||
- ./backend/src:/usr/src/app/backend/src
|
||||
- ./frontend/src/:/usr/src/app/frontend/src/
|
||||
- ./test_data:/usr/src/app/test_data
|
||||
ports:
|
||||
- 127.0.0.1:4000:4000
|
||||
env_file: .dev.env
|
||||
|
||||
@@ -25,6 +25,8 @@ services:
|
||||
context: .
|
||||
dockerfile: ./backend/Dockerfile
|
||||
target: node-run
|
||||
volumes:
|
||||
- ./test_data:/usr/src/app/test_data
|
||||
healthcheck:
|
||||
test: curl -f http://localhost:4000/test/ping || exit 1
|
||||
start_period: 10s
|
||||
|
||||
Reference in New Issue
Block a user