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

dev #56994 : introduce user data migration script

parent 1822735c
Branches
Tags
5 merge requests!2080Sandbox detach zf from storm,!2061Master,!2060Master,!2059Hotline master,!2056Dev#56994 migration des comptes lecteurs chambery
Pipeline #610 passed with stage
in 12 minutes and 45 seconds
<?php
require './console.php';
echo 'User Data Migration by Stl And Pat since 1854
\ . ./
\ .:";\'.:.." /
(M^^.^~~:.\'").
- (/ . . . \ \) -
O ((| :. ~ ^ :. .|))
|\\ - (\- | \ / | /) -
| T -\ \ / /-
/ \[_]..........................\ \ / /
';
$valid_count = Class_Users::countBy(['statut' => 0, 'role_level' => 2]);
echo $valid_count . ' valid users in database
';
if (0 == $valid_count) {
echo 'Oups, not enough valid users, aborting...
..-^~~~^-..
.~ ~.
(;: :;)
(: :)
\':._ _.:\'
| |
(=====)
| |
\O/ | |
\ | |
/\ ((/ \))
';
exit(255);
}
$for_real = ('--force' == $argv[1]);
echo $for_real
? 'OH MY, RUNNING FOR REAL, GOOD LUCK'
: 'pro tip: use --force option to run for real, with great powers come great responsibility
Simulation mode engaged...';
echo '
';
$without_datas = 0;
$without_target = 0;
$success = 0;
foreach(Class_Users::findAllBy(['statut' => 1, 'role_level' => 2]) as $user) {
if (!user_has_info($user)) {
//$user->delete();
$without_datas++;
continue;
}
if (!$target = get_target_for($user)) {
printf("This %s sucks\n", $user->getLogin());
$without_target++;
continue;
}
$success++;
give_data_to_target($user, $target, $for_real);
//$user->delete();
}
/* foreach(Class_Users::findAllBy(['statut' => 1, 'role_level' => 2]) as $user) { */
/* $write_failed_user_in_csv; */
/* } */
printf('
%s without datas
%s without target
%s success
', $without_datas, $without_target, $success);
function user_has_info($user) {
$adapter = Zend_Db_Table::getDefaultAdapter();
$queries = ["select count(id) from notices_paniers where id_user=" . $user->getId() . " and notices != ''",
"select count(id) from notices_avis where id_user=" . $user->getId(),
"select count(id) from cms_avis where id_user=" . $user->getId(),
"select count(id) from suggestion_achat where user_id=" . $user->getId(),
"select count(id) from session_formation_inscriptions where stagiaire_id=" . $user->getId(),
"select count(id) from user_group_memberships where user_id=" . $user->getId(),
"select count(id) from formulaires where id_user=" . $user->getId(),
"select count(id) from multimedia_devicehold where id_user=" . $user->getId(),
];
foreach($queries as $query) {
$result = $adapter->query($query)->fetch();
if (0 < current($result))
return true;
}
return false;
}
function get_target_for($user) {
$target_login = 'CHAM' . $user->getLogin(); // to replace
$targets = Class_Users::findAllBy(['login' => $target_login]);
if (!$targets)
return;
if (1 < count($targets))
return;
return current($targets);
}
function give_data_to_target($from, $to, $for_real) {
if (!$for_real)
return;
$adapter = Zend_Db_Table::getDefaultAdapter();
$queries = ["update notices_paniers set id_user=" . $to->getId() . " where id_user=" . $from->getId() . " and notices != ''",
"update notices_avis set id_user=" . $to->getId() . " where id_user=" . $from->getId(),
"update cms_avis set id_user=" . $to->getId() ." where id_user=" . $from->getId(),
"update suggestion_achat set user_id=" . $to->getId() . " where user_id=" . $from->getId(),
"update session_formation_inscriptions set stagiaire_id=" . $to->getId() . " where stagiaire_id=" . $from->getId(),
"update user_group_memberships set user_id=" . $to->getId() . " where user_id=" . $from->getId(),
"update formulaires set id_user=" . $to->getId() . " where id_user=" . $from->getId(),
"update multimedia_devicehold set id_user=" . $to->getId() . " where id_user=" . $from->getId(),
];
foreach($queries as $query)
$adapter->query($query);
}
\ 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