Skip to content
Snippets Groups Projects
Commit 790ae2c3 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

dev #56994 : logging

parent 3ffc6e86
5 merge requests!2080Sandbox detach zf from storm,!2061Master,!2060Master,!2059Hotline master,!2056Dev#56994 migration des comptes lecteurs chambery
Pipeline #620 passed with stage
in 11 minutes and 53 seconds
......@@ -21,6 +21,15 @@
class Class_User_Datas extends Class_Entity {
protected $_relations = [['notices_paniers', 'id_user'],
['notices_avis', 'id_user'],
['cms_avis', 'id_user'],
['suggestion_achat', 'user_id'],
['session_formation_inscriptions', 'stagiaire_id'],
['user_group_memberships', 'user_id'],
['formulaires', 'id_user'],
['multimedia_devicehold', 'id_user']];
public function __construct($user) {
$this
->setUser($user)
......@@ -29,22 +38,10 @@ class Class_User_Datas extends Class_Entity {
public function hasDatas() {
$id = $this->getUser()->getId();
$queries = [['notices_paniers', 'id_user=' . $id . ' and notices != \'\''],
['notices_avis', 'id_user=' . $id],
['cms_avis', 'id_user=' . $id],
['suggestion_achat', 'user_id=' . $id],
['session_formation_inscriptions', 'stagiaire_id=' . $id],
['user_group_memberships', 'user_id=' . $id],
['formulaires', 'id_user=' . $id],
['multimedia_devicehold', 'id_user=' . $id]
];
foreach($queries as $query) {
$pattern = 'select count(id) from %s where %s=' . $this->getUser()->getId();
foreach($this->_relations as $relation) {
$result = $this->getSql()
->fetchOne(sprintf('select count(id) from %s where %s',
$query[0], $query[1]));
->fetchOne(sprintf($pattern, $relation[0], $relation[1]));
if (0 < $result)
return true;
}
......@@ -58,23 +55,13 @@ class Class_User_Datas extends Class_Entity {
return false;
if (!$for_real)
return true;
$queries = [['notices_paniers', 'id_user'],
['notices_avis', 'id_user'],
['cms_avis', 'id_user'],
['suggestion_achat', 'user_id'],
['session_formation_inscriptions', 'stagiaire_id'],
['user_group_memberships', 'user_id'],
['formulaires', 'id_user'],
['multimedia_devicehold', 'id_user']
];
return $target;
$pattern = 'update %s set %s=' . $target->getId() . ' where %s=' . $this->getUser()->getId();
foreach($queries as $query)
$this->getSql()->query(sprintf($pattern, $query[0], $query[1], $query[1]));
foreach($this->_relations as $relation)
$this->getSql()->query(sprintf($pattern, $relation[0], $relation[1], $relation[1]));
return true;
return $target;
}
......
......@@ -12,8 +12,14 @@ echo 'User Data Migration by Stl And Pat since 1854
| T -\ \ / /-
/ \[_]..........................\ \ / /
This will detect user marked for deletion and
try to give their datas to another user
based on a rule before deleting them.
';
readline('press enter to continue...');
$valid_count = Class_Users::countBy(['statut' => 0, 'role_level' => 2]);
$invalid_count = Class_Users::countBy(['statut' => 1, 'role_level' => 2]);
echo $valid_count . ' valid users in database
......@@ -39,7 +45,33 @@ if (0 == $valid_count) {
exit(255);
}
class UserDataMigrationLog extends Class_Entity {
public function __construct($path) {
$this->setPath($path)
->reset();
}
public function write($content) {
file_put_contents($this->getPath(), implode('|', $content) . "\n", FILE_APPEND);
return $this;
}
public function reset() {
if (file_exists($this->getPath()))
unlink($this->getPath());
return $this;
}
}
$for_real = ('--force' == $argv[1]);
if ($for_real)
readline('CAUTION : you asked to run for real, users may be deleted !');
echo $for_real
? 'OH MY, RUNNING FOR REAL, GOOD LUCK'
: 'pro tip: use --force option to run for real, with great powers comes great responsibility
......@@ -52,32 +84,72 @@ $without_datas = 0;
$without_target = 0;
$success = 0;
echo date('c') . "\n";
$no_datas_log = (new UserDataMigrationLog('deleted_without_datas.txt'))
->write(['id_bokeh', 'carte', 'nom', 'prenom', 'naissance']);
$no_target_log = (new UserDataMigrationLog('not_deleted_target_problem.txt'))
->write(['id_bokeh', 'carte', 'nom', 'prenom', 'naissance', 'cause']);
$success_log = (new UserDataMigrationLog('deleted_with_datas.txt'))
->write(['id_bokeh_source', 'carte_source', 'nom_source', 'prenom_source', 'naissance_source',
'id_bokeh_dest', 'carte_dest', 'nom_dest', 'prenom_dest', 'naissance_dest',]);
echo 'Started at ' . date('c') . "\n";
foreach(Class_Users::findAllBy(['statut' => 1, 'role_level' => 2]) as $user) {
$datas = new Class_User_Datas($user);
if (!$datas->hasDatas()) {
//$user->delete();
$no_datas_log->write([$user->getId(),
$user->getLogin(),
$user->getNom(),
$user->getPrenom(),
$user->getNaissance()]);
if ($for_real)
$user->delete();
$without_datas++;
continue;
}
if (!$datas->giveTo(['login' => 'CHAM' . $user->getLogin()], $for_real)) {
if (!$target = $datas->giveTo(['login' => 'CHAM' . $user->getLogin(),
'statut' => 0,
'role_level' => 2],
$for_real)) {
$no_target_log->write([$user->getId(),
$user->getLogin(),
$user->getNom(),
$user->getPrenom(),
$user->getNaissance(),
$datas->getError()]);
$without_target++;
echo 'F';
continue;
}
$success_log->write([$user->getId(),
$user->getLogin(),
$user->getNom(),
$user->getPrenom(),
$user->getNaissance(),
$target->getId(),
$target->getLogin(),
$target->getNom(),
$target->getPrenom(),
$target->getNaissance()]);
if ($for_real)
$user->delete();
$success++;
echo '.';
//$user->delete();
}
echo "\n" . date('c') . "\n";
echo "\nEnded at " . date('c') . "\n";
printf('
%s without datas
%s without target
%s success
%s without datas, log in deleted_without_datas.txt
%s without target, log in not_deleted_target_problem.txt
%s success, log in deleted_with_datas.txt
', $without_datas, $without_target, $success);
\ No newline at end of file
......@@ -108,12 +108,14 @@ class UserDatasTest extends UserDatasTestCase {
$query[0], $query[1], $query[1]))
->answers(1);
$this->assertTrue($this->_datas->giveTo(['login' => 'actarus'], true));
$this->assertEquals(45,
$this->_datas->giveTo(['login' => 'actarus'], true)->getId());
}
/** @test */
public function givingToActarusNotForRealShouldDoNothing() {
$this->assertTrue($this->_datas->giveTo(['login' => 'actarus'], false));
$this->assertEquals(45,
$this->_datas->giveTo(['login' => 'actarus'], false)->getId());
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment