public function run($sArgument) { if (!isset($sArgument) || !isset($sArgument[0]) || !isset($sArgument[1]) || !isset($sArgument[2]) || !isset($sArgument[3])) { die('You have to set admin/password/full name and email address on the command line like this: php starter.php adminname mypassword fullname emailaddress'); } Yii::import('application.helpers.common_helper', true); try { $this->connection = App()->getDb(); $this->connection->active = true; } catch (CDbException $e) { $this->createDatabase(); } $this->connection->charset = 'utf8'; switch ($this->connection->driverName) { case 'mysql': case 'mysqli': $this->connection->createCommand("ALTER DATABASE " . $this->connection->quoteTableName($this->getDBConnectionStringProperty('dbname')) . " DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;")->execute(); $sql_file = 'mysql'; break; case 'pgsql': $sql_file = 'pgsql'; break; case 'dblib': case 'mssql': case 'sqlsrv': $sql_file = 'mssql'; break; default: throw new Exception(sprintf('Unknown database type "%s".', $this->connection->driverName)); } $this->_executeSQLFile(dirname(Yii::app()->basePath) . '/installer/sql/create-' . $sql_file . '.sql'); $this->connection->createCommand()->insert($this->connection->tablePrefix . 'users', array('users_name' => $sArgument[0], 'password' => hash('sha256', $sArgument[1]), 'full_name' => $sArgument[2], 'parent_id' => 0, 'lang' => 'auto', 'email' => $sArgument[3])); $this->connection->createCommand()->insert($this->connection->tablePrefix . 'permissions', array('entity' => 'global', 'entity_id' => 0, 'uid' => 1, 'permission' => 'superadmin', 'create_p' => 0, 'read_p' => 1, 'update_p' => 0, 'delete_p' => 0, 'import_p' => 0, 'export_p' => 0)); }
public function testCreateDb() { $step1 = new Step1(); $step1->setAttributes($this->data, false); $this->assertTrue($step1->createDb()); $conn_string = 'mysql:host=' . $step1->db_host . ';dbname=' . $step1->db_name; $con = new CDbConnection($conn_string, $step1->db_login, $step1->db_pass); try { $con->init(); } catch (Exception $e) { $this->assert('Db was not created'); } $con->createCommand('DROP DATABASE IF EXISTS ' . $con->quoteTableName($this->data['db_name']))->execute(); }
/** * Installer::_setup_tables() * Function that actually modify the database. Read $sqlfile and execute it. * @param string $sqlfile * @return Empty string if everything was okay - otherwise the error messages */ function _setup_tables($sFileName, $aDbConfig = array(), $sDatabasePrefix = '') { extract(empty($aDbConfig) ? self::_getDatabaseConfig() : $aDbConfig); try { switch ($sDatabaseType) { case 'mysql': case 'mysqli': $this->connection->createCommand("ALTER DATABASE " . $this->connection->quoteTableName($sDatabaseName) . " DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;")->execute(); break; } } catch (Exception $e) { return array($e->getMessage()); } return $this->_executeSQLFile($sFileName, $sDatabasePrefix); }
/** * Installer::_setup_tables() * Function that actually modify the database. Read $sqlfile and execute it. * @param string $sqlfile * @return Empty string if everything was okay - otherwise the error messages */ function _setup_tables($sFileName, $aDbConfig = array(), $sDatabasePrefix = '') { extract(empty($aDbConfig) ? self::_getDatabaseConfig() : $aDbConfig); switch ($sDatabaseType) { case 'mysql': case 'mysqli': $this->connection->createCommand("ALTER DATABASE " . $this->connection->quoteTableName($sDatabaseName) . " DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;")->execute(); break; case 'pgsql': if (version_compare($this->connection->getServerVersion(), '9', '>=')) { $this->connection->createCommand("ALTER DATABASE " . $this->connection->quoteTableName($sDatabaseName) . " SET bytea_output='escape';")->execute(); } break; } return $this->_executeSQLFile($sFileName, $sDatabasePrefix); }