Skip to content

Commit

Permalink
stats-updater: Simplifyenv handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bperel committed Aug 22, 2024
1 parent b29af56 commit e89d7a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
14 changes: 3 additions & 11 deletions apps/stats-updater/.env
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
MYSQL_HOST=127.0.0.1
MYSQL_PORT=64999
MYSQL_ROOT_PASSWORD=changeme

MYSQL_DM_DATABASE=dm
MYSQL_DM_STATS_DATABASE=dm_stats
MYSQL_COA_DATABASE=coa

DATABASE_URL_COA=mysql://root:changeme@localhost:64999/coa
DATABASE_URL_DM=mysql://root:changeme@localhost:64999/dm
DATABASE_URL_DM_STATS=mysql://root:changeme@localhost:64999/dm_stats
DATABASE_NAME_COA=coa
DATABASE_NAME_DM_STATS=dm_stats
DATABASE_URL_DM_STATS=mysql://root:changeme@localhost:64999/dm_stats_new

DM_STATS_DDL_PATH=../../packages/prisma-schemas/schemas/dm_stats/migrations/0_init/migration.sql
11 changes: 4 additions & 7 deletions apps/stats-updater/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ Provisioning is executed when the container starts.

Required environment variables :

- `DATABASE_URL_COA` : The connection string to the COA DB
- `DATABASE_URL_DM` : The connection string to the DM DB
- `DATABASE_URL_DM_STATS` : The connection string to the DM stats DB
- `MYSQL_DM_STATS_DATABASE` : The name of the DM stats DB
- `MYSQL_HOST` : The hostname of the database
- `MYSQL_ROOT_PASSWORD` : The password to the database
- `MYSQL_PORT` : The port to the database
- `DATABASE_URL_DM_STATS` : The connection string to the temporary DM stats DB
- `DATABASE_NAME_COA` : The name of the coa schema
- `DATABASE_NAME_DM_STATS` : The name of the DM stats schema
- `DM_STATS_DDL_PATH` : The path to a file containing the DDL for the DM stats DB
44 changes: 28 additions & 16 deletions apps/stats-updater/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,30 @@ import { prismaClient as prismaDmStats } from "~prisma-schemas/schemas/dm_stats/
dotenv.config();

for (const envKey of [
"MYSQL_HOST",
"MYSQL_PORT",
"MYSQL_ROOT_PASSWORD",
"DATABASE_URL_DM_STATS",
"DATABASE_NAME_COA",
"DATABASE_NAME_DM_STATS",
"DM_STATS_DDL_PATH",
]) {
if (!process.env[envKey]) {
console.error(`Environment variable not found, aborting: ${envKey}`);
process.exit(1);
}
}

const { DATABASE_URL_DM_STATS,
DATABASE_NAME_COA,
DATABASE_NAME_DM_STATS,
DM_STATS_DDL_PATH } = process.env as {
DATABASE_URL_DM_STATS: string,
DATABASE_NAME_COA: string,
DATABASE_NAME_DM: string,
DATABASE_NAME_DM_STATS: string,
DM_STATS_DDL_PATH: string,
}

const [_protocol, _username, MYSQL_ROOT_PASSWORD, MYSQL_HOST, MYSQL_PORT, DATABASE_NAME_DM_STATS_NEW] = (DATABASE_URL_DM_STATS).split(/\W+/)

const tables = [
"auteurs_histoires",
"histoires_publications",
Expand All @@ -43,10 +56,10 @@ const tables = [
let connection: PoolConnection;

const pool = createPool({
host: process.env.MYSQL_HOST,
port: parseInt(process.env.MYSQL_PORT!),
host: MYSQL_HOST,
port: parseInt(MYSQL_PORT),
user: "root",
password: process.env.MYSQL_ROOT_PASSWORD,
password: MYSQL_ROOT_PASSWORD,
multipleStatements: true,
});

Expand Down Expand Up @@ -75,11 +88,10 @@ const runQueryFile = async (dbName: string, sqlFile: string) =>
runQuery(`USE ${dbName};` + readFileSync(sqlFile).toString());

connect().then(async () => {
const dbName = process.env.MYSQL_DM_STATS_DATABASE;
await runQuery(`DROP DATABASE IF EXISTS ${dbName}_new`);
await runQuery(`CREATE DATABASE ${dbName}_new`);
await runQuery(`DROP DATABASE IF EXISTS ${DATABASE_NAME_DM_STATS_NEW}`);
await runQuery(`CREATE DATABASE ${DATABASE_NAME_DM_STATS_NEW}`);

await runQueryFile(`${dbName}_new`, process.env.DM_STATS_DDL_PATH);
await runQueryFile(DATABASE_NAME_DM_STATS_NEW, DM_STATS_DDL_PATH);

const authorUsers = await prismaDm.authorUser.findMany({
where: {
Expand Down Expand Up @@ -202,19 +214,19 @@ connect().then(async () => {
"Adding oldestdate; adding publicationcode and issuenumber for WTD < 3",
);
await runQuery(`
UPDATE ${dbName}_new.utilisateurs_publications_suggerees
JOIN coa.inducks_issue i using (issuecode)
UPDATE ${DATABASE_NAME_DM_STATS_NEW}.utilisateurs_publications_suggerees
JOIN ${DATABASE_NAME_COA}.inducks_issue i using (issuecode)
SET utilisateurs_publications_suggerees.publicationcode = i.publicationcode
, utilisateurs_publications_suggerees.issuenumber = i.issuenumber
, utilisateurs_publications_suggerees.oldestdate = i.oldestdate`);

await runQuery(`DROP DATABASE IF EXISTS ${dbName}_old`);
await runQuery(`CREATE DATABASE ${dbName}_old`);
await runQuery(`DROP DATABASE IF EXISTS ${DATABASE_NAME_DM_STATS}_old`);
await runQuery(`CREATE DATABASE ${DATABASE_NAME_DM_STATS}_old`);

for (const table of tables) {
await runQuery(`RENAME TABLE
${dbName}.${table} TO ${dbName}_old.${table},
${dbName}_new.${table} TO ${dbName}.${table}`);
${DATABASE_NAME_DM_STATS}.${table} TO ${DATABASE_NAME_DM_STATS}_old.${table},
${DATABASE_NAME_DM_STATS_NEW}.${table} TO ${DATABASE_NAME_DM_STATS}.${table}`);
}

await disconnect();
Expand Down

0 comments on commit e89d7a3

Please sign in to comment.