Skip to content

Commit

Permalink
MySQL fix timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
stakovicz committed Feb 11, 2025
1 parent 7c77159 commit 5e759fc
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DATABASE_PORT=3306
DATABASE_NAME=web
DATABASE_USER=afup
DATABASE_PASSWORD=afup
DATABASE_TIMEZONE=+01:00

SECRET=ThisTokenIsNotSoSecretChangeIt

Expand Down
45 changes: 45 additions & 0 deletions app/Resources/views/admin/healthcheck.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% extends 'admin/base_with_header.html.twig' %}

{% block content %}
<h1>Healthcheck</h1>
<div class="ui grid">
<div class="eight wide column">
<div class="ui segment">
<h2 class="ui header">Dates</h2>
<div class="ui clearing divider"></div>
<dl>
<dt>PHP</dt>
<dd>{{ dates.php }}</dd>
<dt>MySQL Base_De_Donnees</dt>
<dd>{{ dates.mysql_bdd }}</dd>
<dt>MySQL Ting</dt>
<dd>{{ dates.mysql_ting }}</dd>
<dt>Différence MySQL et PHP</dt>
<dd>
{{ dates.diff ? 'Les timezones sont différentes' : 'Pas de différence de timezones' }}
</dd>
</dl>
</div>
</div>
<div class="eight wide column">
<div class="ui segment">
<h2 class="ui header">Versions</h2>
<div class="ui clearing divider"></div>
<dl>
<dt>PHP</dt>
<dd>{{ versions.php }}</dd>
<dt>Symfony</dt>
<dd>{{ versions.symfony }}</dd>
</dl>
</div>
</div>
</div>
<style>
dt {
font-weight: bold;
}
dd {
font-family: "Noto Sans Mono", monospace;
}
</style>
{% endblock %}
9 changes: 9 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ parameters:
forum_partenaire:
nom: 'Sponsors/Partenaires'
niveau: 'ROLE_ADMIN'
healthcheck:
nom: 'Healthcheck'
niveau: 'ROLE_ADMIN'
url: '/admin/healthcheck'
connexion:
nom: 'Connexion'
masquee: true
Expand Down Expand Up @@ -357,6 +361,11 @@ ting:
port: "%database_port%"
user: "%database_user%"
password: "%database_password%"

databases_options:
web:
timezone: "%database_timezone%"

repositories:
event:
namespace : AppBundle\Event\Model\Repository
Expand Down
4 changes: 4 additions & 0 deletions app/config/routing/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ admin_site:
admin_github_user_routes:
resource: "admin_github_user.yml"
prefix: /event/github-user

admin_healthcheck:
path: /healthcheck
defaults: {_controller: AppBundle\Controller\Admin\HealthcheckController}
1 change: 1 addition & 0 deletions docker/dockerfiles/mysql/my.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ skip-host-cache
skip-name-resolve
innodb_file_per_table=1
sql-mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
default-time-zone = "+00:00"
1 change: 1 addition & 0 deletions docker/dockerfiles/mysqltest/my.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ skip-host-cache
skip-name-resolve
innodb_file_per_table=1
sql-mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
default-time-zone = "+00:00"
1 change: 1 addition & 0 deletions sources/Afup/Utils/Base_De_Donnees.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getDbLink()
if ($this->link === null) {
$this->link = mysqli_connect($this->config['host'], $this->config['user'], $this->config['password'], null, (int) $this->config['port']) or die('Connexion à la base de données impossible');
mysqli_set_charset($this->link, "utf8mb4");
mysqli_query($this->link, "SET time_zone = '" . getenv('DATABASE_TIMEZONE') . "'");
$this->selectionnerBase($this->config['database']);
}
return $this->link;
Expand Down
54 changes: 54 additions & 0 deletions sources/AppBundle/Controller/Admin/HealthcheckController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace AppBundle\Controller\Admin;

use Afup\Site\Corporate\_Site_Base_De_Donnees;
use AppBundle\Event\Model\Repository\EventRepository;
use CCMBenchmark\TingBundle\Repository\RepositoryFactory;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;

class HealthcheckController extends AbstractController
{
private RepositoryFactory $ting;

public function __construct(RepositoryFactory $ting)
{
$this->ting = $ting;
}

public function __invoke(): Response
{
$php = new DateTime();

$bdd = new _Site_Base_De_Donnees();
$mysqlBdd = $bdd->obtenirUn('SELECT CURRENT_TIMESTAMP');
$mysqlBdd = new DateTime($mysqlBdd);

$repo = $this->ting->get(EventRepository::class);
$mysqlTing = $repo->getQuery('SELECT CURRENT_TIMESTAMP')->execute()['CURRENT_TIMESTAMP'];
$mysqlTing = new DateTime($mysqlTing);

$hasDiff = $php->getTimestamp() !== $mysqlBdd->getTimestamp();
if (!$hasDiff) {
$hasDiff = $php->getTimestamp() !== $mysqlTing->getTimestamp();
}

return $this->render('admin/healthcheck.html.twig', [
'dates' => [
'php' => $php->format(\DateTime::ATOM),
'mysql_bdd' => $mysqlBdd->format(\DateTime::ATOM),
'mysql_ting' => $mysqlTing->format(\DateTime::ATOM),
'diff' => $hasDiff
],
'versions' => [
'php' => phpversion(),
'symfony' => Kernel::VERSION
]
]);
}
}
9 changes: 9 additions & 0 deletions tests/behat/features/Admin/Divers/Healthcheck.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Administration - Healthcheck

@reloadDbWithTestData
Scenario: Vérification des dates
Given I am logged in as admin and on the Administration
And I follow "Healthcheck"
Then I should see "Healthcheck"
And I should see "Pas de différence de timezones"
And I should not see "Les timezones sont différentes"

0 comments on commit 5e759fc

Please sign in to comment.