Skip to content

Commit

Permalink
stats-updater: Merge index.ts and db.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
bperel committed Aug 19, 2024
1 parent f07dfd1 commit 02c93f4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 88 deletions.
73 changes: 0 additions & 73 deletions apps/stats-updater/db.ts

This file was deleted.

80 changes: 65 additions & 15 deletions apps/stats-updater/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
#!/usr/bin/env bun

import "~prisma-schemas/util/groupBy";
import * as dotenv from "dotenv";
import { readFileSync } from "fs";
import { PoolConnection } from "mariadb";
import { createPool } from "mariadb";

import * as process from "process";

const originalConnectionString = process.env.DATABASE_URL_DM_STATS!;
process.env.DATABASE_URL_DM_STATS = originalConnectionString.replace(
dotenv.config();

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


process.env.DATABASE_URL_DM_STATS = process.env.DATABASE_URL_DM_STATS!.replace(
"dm_stats",
"dm_stats_new",
);
Expand All @@ -22,8 +41,6 @@ import {
import type { authorUser } from "~prisma-schemas/schemas/dm";
import type { authorStory } from "~prisma-schemas/schemas/dm_stats";

import * as db from "./db";

const tables = [
"auteurs_histoires",
"histoires_publications",
Expand All @@ -32,12 +49,47 @@ const tables = [
"utilisateurs_publications_suggerees",
];

db.connect().then(async () => {

let connection: PoolConnection;

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

const connect = async () => {
try {
connection = await pool.getConnection();
} catch (err: unknown) {
console.error(err);
}
};

const disconnect = async () => {
if (connection) {
await connection.end();
}
return pool.end();
};

const runQuery = async (sql: string) => {
console.log(new Date().toISOString());
console.debug(sql);
return await connection.query(sql);
};

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 db.runQuery(`DROP DATABASE IF EXISTS ${dbName}_new`);
await db.runQuery(`CREATE DATABASE ${dbName}_new`);
await runQuery(`DROP DATABASE IF EXISTS ${dbName}_new`);
await runQuery(`CREATE DATABASE ${dbName}_new`);

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

const authorUsers = await prismaDm.authorUser.findMany({
where: {
Expand Down Expand Up @@ -160,24 +212,22 @@ db.connect().then(async () => {
await prismaDmStats.$executeRaw`OPTIMIZE TABLE utilisateurs_publications_suggerees`;

console.log("Adding publicationcode and issuenumber for WTD < 3");
await db.runQuery(`
await runQuery(`
UPDATE ${dbName}_new.utilisateurs_publications_suggerees
JOIN coa.inducks_issue i using (issuecode)
SET utilisateurs_publications_suggerees.publicationcode = i.publicationcode
, utilisateurs_publications_suggerees.issuenumber = i.issuenumber`);

process.env.DATABASE_URL_DM_STATS = originalConnectionString;
await db.runMigrations();

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

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

await db.disconnect();
await disconnect();
process.exit(0);
});

0 comments on commit 02c93f4

Please sign in to comment.