Moved database methods into own context class.

This commit is contained in:
flash 2025-02-09 17:39:48 +00:00
parent 21a730d189
commit bac889787a
14 changed files with 94 additions and 62 deletions

View file

@ -147,7 +147,7 @@ msz_sched_task_func('Resync statistics counters.', true, function() use ($msz) {
];
foreach($stats as $name => $query) {
$result = $msz->dbConn->query($query);
$result = $msz->dbCtx->conn->query($query);
$msz->counters->set($name, $result->next() ? $result->getInteger(0) : 0);
}
});
@ -166,7 +166,7 @@ msz_sched_task_func('Omega disliking comments...', true, function() use ($msz) {
if(!is_array($commentIds))
return;
$stmt = $msz->dbConn->prepare('REPLACE INTO msz_comments_votes (comment_id, user_id, comment_vote) SELECT ?, user_id, -1 FROM msz_users');
$stmt = $msz->dbCtx->conn->prepare('REPLACE INTO msz_comments_votes (comment_id, user_id, comment_vote) SELECT ?, user_id, -1 FROM msz_users');
foreach($commentIds as $commentId) {
$stmt->addParameter(1, $commentId);
$stmt->execute();
@ -178,9 +178,9 @@ msz_sched_task_func('Announcing random topics...', true, function() use ($msz) {
if(!is_array($categoryIds))
return;
$stmtRevert = $msz->dbConn->prepare('UPDATE msz_forum_topics SET topic_type = 0 WHERE forum_id = ? AND topic_type = 2');
$stmtRandom = $msz->dbConn->prepare('SELECT topic_id FROM msz_forum_topics WHERE forum_id = ? AND topic_deleted IS NULL ORDER BY RAND() LIMIT 1');
$stmtAnnounce = $msz->dbConn->prepare('UPDATE msz_forum_topics SET topic_type = 2 WHERE topic_id = ?');
$stmtRevert = $msz->dbCtx->conn->prepare('UPDATE msz_forum_topics SET topic_type = 0 WHERE forum_id = ? AND topic_type = 2');
$stmtRandom = $msz->dbCtx->conn->prepare('SELECT topic_id FROM msz_forum_topics WHERE forum_id = ? AND topic_deleted IS NULL ORDER BY RAND() LIMIT 1');
$stmtAnnounce = $msz->dbCtx->conn->prepare('UPDATE msz_forum_topics SET topic_type = 2 WHERE topic_id = ?');
foreach($categoryIds as $categoryId) {
$stmtRevert->addParameter(1, $categoryId);
$stmtRevert->execute();
@ -201,8 +201,8 @@ msz_sched_task_func('Changing category icons...', false, function() use ($msz) {
if(!is_array($categoryIds))
return;
$stmtIcon = $msz->dbConn->prepare('UPDATE msz_forum_categories SET forum_icon = COALESCE(?, forum_icon), forum_name = COALESCE(?, forum_name), forum_colour = ((ROUND(RAND() * 255) << 16) | (ROUND(RAND() * 255) << 8) | ROUND(RAND() * 255)) WHERE forum_id = ?');
$stmtUnlock = $msz->dbConn->prepare('UPDATE msz_forum_topics SET topic_locked = IF(?, NULL, COALESCE(topic_locked, NOW())) WHERE topic_id = ?');
$stmtIcon = $msz->dbCtx->conn->prepare('UPDATE msz_forum_categories SET forum_icon = COALESCE(?, forum_icon), forum_name = COALESCE(?, forum_name), forum_colour = ((ROUND(RAND() * 255) << 16) | (ROUND(RAND() * 255) << 8) | ROUND(RAND() * 255)) WHERE forum_id = ?');
$stmtUnlock = $msz->dbCtx->conn->prepare('UPDATE msz_forum_topics SET topic_locked = IF(?, NULL, COALESCE(topic_locked, NOW())) WHERE topic_id = ?');
foreach($categoryIds as $categoryId) {
$scoped = $msz->config->scopeTo(sprintf('forum.rngicon.fc%d', $categoryId));
@ -247,7 +247,7 @@ foreach($schedTasks as $task) {
try {
switch($task->type) {
case 'sql':
$affected = $msz->dbConn->execute($task->command);
$affected = $msz->dbCtx->conn->execute($task->command);
echo $affected . ' rows affected. ' . PHP_EOL;
break;

View file

@ -9,13 +9,13 @@ try {
chmod(MSZ_ROOT . '/.migrating', 0777);
echo 'Creating migration manager...' . PHP_EOL;
$manager = $msz->createMigrationManager();
$manager = $msz->dbCtx->createMigrationManager();
echo 'Preparing to run migrations...' . PHP_EOL;
$manager->init();
echo 'Creating migration repository...' . PHP_EOL;
$repo = $msz->createMigrationRepo();
$repo = $msz->dbCtx->createMigrationRepo();
echo 'Running migrations...' . PHP_EOL;
$completed = $manager->processMigrations($repo);

View file

@ -4,14 +4,14 @@ use Index\Db\Migration\FsDbMigrationRepo;
require_once __DIR__ . '/../misuzu.php';
$repo = $msz->createMigrationRepo();
$repo = $msz->dbCtx->createMigrationRepo();
if(!($repo instanceof FsDbMigrationRepo)) {
echo 'Migration repository type does not support creation of templates.' . PHP_EOL;
return;
}
$baseName = implode(' ', array_slice($argv, 1));
$manager = $msz->createMigrationManager();
$manager = $msz->dbCtx->createMigrationManager();
try {
$names = $manager->createNames($baseName);