Skip to content

Commit

Permalink
Merge pull request #2 from romainneutron/nlegoff-replace_users-fixes
Browse files Browse the repository at this point in the history
Some adjustments
  • Loading branch information
nlegoff committed Feb 20, 2014
2 parents 11178d7 + a986227 commit ad34bfa
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 711 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\Entities\FtpCredential;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
Expand All @@ -32,21 +33,26 @@ public function apply(EntityManager $em, \appbox $appbox, Configuration $conf)
if (false === $version->isMigrated()) {
$version->execute('up');
}
$version = $conf->getVersion('ftp-credential');
if (false === $version->isMigrated()) {
$version->execute('up');
}
$this->alterTablesUp($em);

try {
$em->getConnection()->beginTransaction();
$em->getConnection()->query('SET FOREIGN_KEY_CHECKS=0');
// Creates user entities
$this->updateUsers($em);
$this->updateFtpSettings($em);
// Creates user model references
$this->updateTemplateOwner($em);
$this->updateLastAppliedModels($em);
$this->cleanForeignKeyReferences($em);
$em->getConnection()->query('SET FOREIGN_KEY_CHECKS=1');
$em->getConnection()->commit();
$this->renameTable($em, 'up');
} catch (Exception $e) {
} catch (\Exception $e) {
$em->getConnection()->rollback();
$em->close();
throw $e;
Expand All @@ -71,6 +77,10 @@ public function rollback(EntityManager $em, \appbox $appbox, Configuration $conf
$this->emptyTables($em);
// rollback schema
$this->alterTablesDown($em);
$version = $conf->getVersion('ftp-credential');
if ($version->isMigrated()) {
$version->execute('down');
}
$version = $conf->getVersion('user');
if ($version->isMigrated()) {
$version->execute('down');
Expand Down Expand Up @@ -351,6 +361,50 @@ private function updateUsers(EntityManager $em)
$em->getConnection()->executeQuery('UPDATE Users SET deleted=1, login=SUBSTRING(login, 11) WHERE login LIKE "(#deleted_%"');
}

private function updateFtpSettings(EntityManager $em)
{
$offset = 0;
$perBatch = 100;

do {
$sql = 'SELECT usr_id, activeFTP, addrFTP, loginFTP,
retryFTP, passifFTP, pwdFTP, destFTP, prefixFTPfolder
FROM usr
WHERE usr_login NOT LIKE "(#deleted_%" AND model_of = 0'
.sprintf(' LIMIT %d, %d', $offset, $perBatch);

$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$rs = $stmt->fetchAll();
$stmt->closeCursor();

foreach ($rs as $row) {
if (null === $user = $em->getPartialReference('Phraseanet:User', $row['usr_id'])) {
continue;
}
$credential = new FtpCredential();
$credential->setActive($row['activeFTP']);
$credential->setAddress($row['addrFTP']);
$credential->setLogin($row['loginFTP']);
$credential->setMaxRetry((Integer) $row['retryFTP']);
$credential->setPassive($row['passifFTP']);
$credential->setPassword($row['pwdFTP']);
$credential->setReceptionFolder($row['destFTP']);
$credential->setRepositoryPrefixName($row['prefixFTPfolder']);
$credential->setUser($user);

$em->persist($credential);
}

$em->flush();
$em->clear();

$offset += $perBatch;
} while (count($rs) > 0);

return true;
}

/**
* Sets last_model from usr table.
*/
Expand Down
76 changes: 58 additions & 18 deletions lib/classes/patch/390alpha1a.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*/

use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Model\Entities\FtpCredential;
use Alchemy\Phrasea\Model\Entities\Order;
use Alchemy\Phrasea\Model\Entities\OrderElement;
use Gedmo\Timestampable\TimestampableListener;

class patch_390alpha1a implements patchInterface
{
Expand Down Expand Up @@ -49,42 +51,78 @@ public function concern()
*/
public function getDoctrineMigrations()
{
return ['user', 'ftp-credential'];
return ['user', 'order'];
}

/**
* {@inheritdoc}
*/
public function apply(base $appbox, Application $app)
{
$sql = 'DELETE FROM Orders';
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute();
$stmt->closeCursor();

$sql = 'DELETE FROM OrderElements';
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute();
$stmt->closeCursor();

$conn = $app['phraseanet.appbox']->get_connection();
$sql = 'SELECT usr_id, activeFTP, addrFTP, loginFTP,
retryFTP, passifFTP, pwdFTP, destFTP, prefixFTPfolder
FROM usr';
$sql = 'SELECT id, usr_id, created_on, `usage`, deadline, ssel_id FROM `order`';
$stmt = $conn->prepare($sql);
$stmt->execute();
$rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$stmt->closeCursor();

$n = 0;
$em = $app['EM'];
$em->getEventManager()->removeEventSubscriber(new TimestampableListener());

foreach ($rs as $row) {
$user = $em->getPartialReference('Phraseanet:User', $row['usr_id']);
$credential = new FtpCredential();
$credential->setActive($row['activeFTP']);
$credential->setAddress($row['addrFTP']);
$credential->setLogin($row['loginFTP']);
$credential->setMaxRetry((Integer) $row['retryFTP']);
$credential->setPassive($row['passifFTP']);
$credential->setPassword($row['pwdFTP']);
$credential->setReceptionFolder($row['destFTP']);
$credential->setRepositoryPrefixName($row['prefixFTPfolder']);
$credential->setUser($user);
$sql = 'SELECT count(id) as todo
FROM order_elements
WHERE deny = NULL
AND order_id = :id';

$em->persist($credential);
$stmt = $conn->prepare($sql);
$stmt->execute([':id' => $row['id']]);
$todo = $stmt->fetch(\PDO::FETCH_ASSOC);
$stmt->closeCursor();

$n++;
$user = $em->getPartialReference('Phraseanet:User', $row['usr_id']);

$order = new Order();
$order->setUser($user)
->setTodo($todo['todo'])
->setOrderUsage($row['usage'])
->setDeadline(new \DateTime($row['deadline']))
->setCreatedOn(new \DateTime($row['created_on']))
->setBasket($em->getPartialReference('Phraseanet:Basket', $row['ssel_id']));

$em->persist($order);

$sql = 'SELECT base_id, record_id, order_master_id, deny
FROM order_elements
WHERE order_id = :id';

$stmt = $conn->prepare($sql);
$stmt->execute([':id' => $row['id']]);
$elements = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$stmt->closeCursor();

foreach ($elements as $element) {
$orderElement = new OrderElement();
$orderElement->setBaseId($element['base_id'])
->setDeny($element['deny'] === null ? null : (Boolean) $element['deny'])
->setOrder($order)
->setOrderMaster($em->getPartialReference('Phraseanet:User',$element['order_master_id']))
->setRecordId($element['record_id']);

$order->addElement($orderElement);
$em->persist($orderElement);
}

if ($n % 100 === 0) {
$em->flush();
Expand All @@ -95,6 +133,8 @@ public function apply(base $appbox, Application $app)
$em->flush();
$em->clear();

$em->getEventManager()->addEventSubscriber(new TimestampableListener());

return true;
}
}
140 changes: 0 additions & 140 deletions lib/classes/patch/390alpha1b.php

This file was deleted.

Loading

0 comments on commit ad34bfa

Please sign in to comment.