create task for reloaded database

This commit is contained in:
Manuel Raynaud
2013-09-03 11:07:32 +02:00
parent 1e6a31d1a3
commit 9f96b830e7
3 changed files with 174 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Command;
use Propel\Runtime\Propel;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Thelia\Install\Database;
/**
* Class ReloadDatabasesCommand
* @package Thelia\Command
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class ReloadDatabaseCommand extends BaseModuleGenerate
{
public function configure()
{
$this
->setName("thelia:dev:reloadDB")
->setDescription("erase current database and create new one")
/* ->addOption(
"load-fixtures",
null,
InputOption::VALUE_NONE,
"load fixtures in databases"
)*/
;
}
public function execute(InputInterface $input, OutputInterface $output)
{
$connection = Propel::getConnection(\Thelia\Model\Map\ProductTableMap::DATABASE_NAME);
$connection = $connection->getWrappedConnection();
$database = new Database($connection);
$output->writeln(array(
'',
'<info>starting reloaded database, please wait</info>'
));
$database->insertSql();
$output->writeln(array(
'',
'<info>Database reloaded with success</info>',
''
));
}
}

View File

@@ -64,6 +64,7 @@
<command class="Thelia\Command\ModuleGenerateModelCommand"/> <command class="Thelia\Command\ModuleGenerateModelCommand"/>
<command class="Thelia\Command\ModuleGenerateSqlCommand"/> <command class="Thelia\Command\ModuleGenerateSqlCommand"/>
<command class="Thelia\Command\CreateAdminUser"/> <command class="Thelia\Command\CreateAdminUser"/>
<command class="Thelia\Command\ReloadDatabaseCommand"/>
</commands> </commands>
<services> <services>

View File

@@ -0,0 +1,103 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Install;
/**
* Class Database
* @package Thelia\Install
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class Database
{
public $connection;
public function __construct(\PDO $connection)
{
$this->connection = $connection;
}
/**
* Insert all sql needed in database
*
* @param $dbName
*/
public function insertSql($dbName = null)
{
if($dbName) {
$this->connection->query(sprintf("use %s", $dbName));
}
$sql = array();
$sql = array_merge(
$sql,
$this->prepareSql(file_get_contents(THELIA_ROOT . "/install/thelia.sql")),
$this->prepareSql(file_get_contents(THELIA_ROOT . "/install/insert.sql"))
);
for ($i = 0; $i < count($sql); $i ++) {
if (!empty($sql[$i])) {
$this->connection->query($sql[$i]);
}
}
}
/**
* Separate each sql instruction in an array
*
* @param $sql
* @return array
*/
protected function prepareSql($sql)
{
$sql = str_replace(";',", "-CODE-", $sql);
$sql = trim($sql);
$query = array();
$tab = explode(";", $sql);
for ($i=0; $i<count($tab); $i++) {
$queryTemp = str_replace("-CODE-", ";',", $tab[$i]);
$queryTemp = str_replace("|", ";", $queryTemp);
$query[] = $queryTemp;
}
return $query;
}
/**
* create database if not exists
*
* @param $dbName
*/
public function createDatabase($dbName)
{
$this->connection->query(
sprintf(
"CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8",
$dbName
)
);
}
}