From 9f96b830e7de06c2e7d0e064875ef1dcf60a2965 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 3 Sep 2013 11:07:32 +0200 Subject: [PATCH] create task for reloaded database --- .../Thelia/Command/ReloadDatabaseCommand.php | 70 ++++++++++++ core/lib/Thelia/Config/Resources/config.xml | 1 + core/lib/Thelia/Install/Database.php | 103 ++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 core/lib/Thelia/Command/ReloadDatabaseCommand.php create mode 100644 core/lib/Thelia/Install/Database.php diff --git a/core/lib/Thelia/Command/ReloadDatabaseCommand.php b/core/lib/Thelia/Command/ReloadDatabaseCommand.php new file mode 100644 index 000000000..d25add644 --- /dev/null +++ b/core/lib/Thelia/Command/ReloadDatabaseCommand.php @@ -0,0 +1,70 @@ +. */ +/* */ +/*************************************************************************************/ + +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 + */ +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( + '', + 'starting reloaded database, please wait' + )); + $database->insertSql(); + $output->writeln(array( + '', + 'Database reloaded with success', + '' + )); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 4c1bf61f8..df4ca0049 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -64,6 +64,7 @@ + diff --git a/core/lib/Thelia/Install/Database.php b/core/lib/Thelia/Install/Database.php new file mode 100644 index 000000000..34ca97dae --- /dev/null +++ b/core/lib/Thelia/Install/Database.php @@ -0,0 +1,103 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Install; + + +/** + * Class Database + * @package Thelia\Install + * @author Manuel Raynaud + */ +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; $iconnection->query( + sprintf( + "CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8", + $dbName + ) + ); + } +} \ No newline at end of file