add env params to connect for migration

This commit is contained in:
Tykayn 2021-04-17 15:30:07 +02:00 committed by tykayn
parent ae7fc029d5
commit 56126c6649
3 changed files with 35 additions and 16 deletions

4
.env
View File

@ -51,7 +51,7 @@ MAILER_DSN=smtp://localhost
###< symfony/mailer ### ###< symfony/mailer ###
## Migration from v1 ## Migration from v1
OLD_DATABASE_NAME=framadate OLD_DATABASE_NAME=framadate
OLD_DATABASE_USER=framadate OLD_DATABASE_USER=framadate-admin
OLD_DATABASE_PASS=framadate OLD_DATABASE_PASS=framadate-admin-password
### UNIQ_INSTALL_KEY must be really unique ### UNIQ_INSTALL_KEY must be really unique
UNIQ_INSTALL_KEY=superCaliFragilistiExpialidousiousse UNIQ_INSTALL_KEY=superCaliFragilistiExpialidousiousse

View File

@ -4,7 +4,10 @@
# Put parameters here that don't need to change on each machine where the app is deployed # Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration # https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters: parameters:
OLD_DATABASE_NAME: '%env(OLD_DATABASE_NAME)%'
OLD_DATABASE_USER: '%env(OLD_DATABASE_USER)%'
OLD_DATABASE_PASS: '%env(OLD_DATABASE_PASS)%'
UNIQ_INSTALL_KEY: '%env(UNIQ_INSTALL_KEY)%'
services: services:
# default configuration for services in *this* file # default configuration for services in *this* file
_defaults: _defaults:

View File

@ -3,9 +3,8 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Poll; use App\Entity\Poll;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\JsonResponse;
use FOS\RestBundle\Controller\Annotations\Get;
/** /**
* Class DefaultController * Class DefaultController
* @package App\Controller * @package App\Controller
@ -17,35 +16,52 @@ class MigrationController extends FramadateController {
* name = "_migrate_from_v1") * name = "_migrate_from_v1")
*/ */
public function indexAction( $unique_key ) { public function indexAction( $unique_key ) {
// get env vars // get env vars
if ( $unique_key !== $this->getParameter( 'UNIQ_INSTALL_KEY' ) ) {
return new JsonResponse( [
'error' => 'NOPE! veuillez vérifier votre fichier .env',
] );
};
// check uniq key is good // check uniq key is good
// fetch old Database // fetch old Database
// connec // connec
$debug = ''; $debug = '';
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$pollsByID = [];
$pdo_options[ \PDO::ATTR_ERRMODE ] = \PDO::ERRMODE_EXCEPTION; $pdo_options[ \PDO::ATTR_ERRMODE ] = \PDO::ERRMODE_EXCEPTION;
$bdd = new \PDO( 'mysql:host=localhost;dbname=framadate', $bdd = new \PDO( 'mysql:host=localhost;dbname=' . $this->getParameter( 'OLD_DATABASE_NAME' ),
'useur', $this->getParameter( 'OLD_DATABASE_USER' ),
'le_pass', $this->getParameter( 'OLD_DATABASE_PASS' ),
$pdo_options ); $pdo_options );
$reponse = $bdd->query( 'SELECT * FROM les_sondages ORDER BY id ASC LIMIT 0, 450' ); $res_Poll = $bdd->query( 'SELECT * FROM fd_poll' );
//boucler pour ranger //boucler pour ranger
while ( $d = $reponse->fetch( \PDO::FETCH_OBJ ) ) { while ( $d = $res_Poll->fetch( \PDO::FETCH_OBJ ) ) {
echo "<br> migration du sondage $d->title , $d->id";
$u = new Poll(); $u = new Poll();
if ( $d->pseudo == 'PSEUDOOOOOO' ) { $u
$d->pseudo = ""; ->setCustomURL( $d->id )
} ->setAdminKey( $d->admin_id )
->setTitle( $d->title )
->setDescription( $d->description )
->setCreationDate( date_create( $d->creation_date ) );
$pollsByID[ $d->id ] = $u;
// $u->setPseudo($d->pseudo) // $u->setPseudo($d->pseudo)
// ->setIp($d->ip) // ->setIp($d->ip)
// ->setAge($d->age) // ->setAge($d->age)
// ->setsondage($s); // ->setsondage($s);
$em->persist( $s ); // $em->persist( $u );
} }
$em->flush(); // $em->flush();
// gather objects // gather objects
// create new polls // create new polls