/** * Setup the admin user. * * @return bool */ public function setupAdmin() { $answer = strtoupper($this->in('<question>Would you like to [c]reate a new user, or use an [e]xisting user?</question>', array('C', 'E'))); $userMap = Configure::read('Forum.userMap'); $statusMap = Configure::read('Forum.statusMap'); // New User if ($answer === 'C') { $this->install['username'] = $this->_newUser('username'); $this->install['password'] = $this->_newUser('password'); $this->install['email'] = $this->_newUser('email'); $result = $this->db->execute(sprintf("INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`) VALUES (%s, %s, %s, %s);", $this->install['table'], $userMap['username'], $userMap['password'], $userMap['email'], $userMap['status'], $this->db->value(Sanitize::clean($this->install['username'])), $this->db->value(Security::hash($this->install['password'], null, true)), $this->db->value($this->install['email']), $this->db->value($statusMap['active']))); if ($result) { $this->install['user_id'] = $this->db->lastInsertId(); } else { $this->out('<error>An error has occurred while creating the user</error>'); return $this->setupAdmin(); } // Old User } else { if ($answer === 'E') { $this->install['user_id'] = $this->_oldUser(); // Redo } else { return $this->setupAdmin(); } } // Give ACL $result = ClassRegistry::init('Forum.Access')->add(array('parent_id' => $this->install['acl_admin'], 'foreign_key' => $this->install['user_id'])); if (!$result) { $this->out('<error>An error occurred while granting administrator access</error>'); return $this->setupAdmin(); } return true; }
/** * test that execute runs queries. * * @return void */ public function testExecute() { $query = 'SELECT * FROM ' . $this->Dbo->fullTableName('articles') . ' WHERE 1 = 1'; $this->Dbo->took = null; $this->Dbo->affected = null; $result = $this->Dbo->execute($query, array('log' => false)); $this->assertNotNull($result, 'No query performed! %s'); $this->assertNull($this->Dbo->took, 'Stats were set %s'); $this->assertNull($this->Dbo->affected, 'Stats were set %s'); $result = $this->Dbo->execute($query); $this->assertNotNull($result, 'No query performed! %s'); $this->assertNotNull($this->Dbo->took, 'Stats were not set %s'); $this->assertNotNull($this->Dbo->affected, 'Stats were not set %s'); }
/** * Alter Indexes method * * @param array $indexes List of indexes * @param string $type Type of operation to be done * @param string $table table name * @throws MigrationException * @return void */ protected function _alterIndexes($indexes, $type, $table) { foreach ($indexes as $key => $index) { if (is_numeric($key)) { $key = $index; $index = array(); } $sql = $this->db->alterSchema(array($table => array($type => array('indexes' => array($key => $index))))); if ($this->_invokePrecheck('beforeAction', $type . '_index', array('table' => $table, 'index' => $key))) { $this->_invokeCallbacks('beforeAction', $type . '_index', array('table' => $table, 'index' => $key)); if (@$this->db->execute($sql) === false) { throw new MigrationException($this, sprintf(__d('migrations', 'SQL Error: %s'), $this->db->error)); } } $this->_invokeCallbacks('afterAction', $type . '_index', array('table' => $table, 'index' => $key)); } }
/** * Test that describe ignores `default current_timestamp` in timestamp columns. * * @return void */ public function testDescribeHandleCurrentTimestamp() { $name = $this->Dbo->fullTableName('timestamp_default_values'); $sql = <<<SQL CREATE TABLE {$name} ( \tid INT NOT NULL, \tphone VARCHAR(10), \tlimit_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, \tPRIMARY KEY (id) ); SQL; $this->Dbo->execute($sql); $model = new Model(array('table' => 'timestamp_default_values', 'ds' => 'test', 'alias' => 'TimestampDefaultValue')); $result = $this->Dbo->describe($model); $this->Dbo->execute('DROP TABLE ' . $name); $this->assertNull($result['limit_date']['default']); $schema = new CakeSchema(array('connection' => 'test', 'testdescribes' => $result)); $result = $this->Dbo->createSchema($schema); $this->assertContains('"limit_date" timestamp NOT NULL', $result); }
/** * Rename an existing index. * * @param string $table * @param string $oldName * @param string $newName * @throws MissingTableException if table does not exist in database * @throws MissingIndexException if the $oldName index does not exist on the table * @throws IndexAlreadyExistsException if the $newName index already exists on the table * @throws MigrationException if an sql error occurred * @return Migration */ public function renameIndex($table, $oldName, $newName) { if (!in_array($this->_db->fullTableName($table, false, false), $this->_db->listSources())) { throw new MissingTableException(__d('migration', 'Table "%s" does not exist in database.', $this->_db->fullTableName($table, false, false))); } $existingIndexes = $this->_db->index($table); if (get_class($this->_db) === 'Postgres') { $oldName = strtolower($oldName); $newName = strtolower($newName); } if (!array_key_exists($oldName, $existingIndexes)) { throw new MissingIndexException(__d('migration', 'Index "%s" does not exist on table "%s".', array($oldName, $table))); } if (array_key_exists($newName, $existingIndexes)) { throw new IndexAlreadyExistsException(__d('migration', 'Index "%s" already exists on table "%s".', array($newName, $table))); } try { $this->_db->execute($this->_db->alterSchema(array($table => array('drop' => array('indexes' => array($oldName => array())), 'add' => array('indexes' => array($newName => $existingIndexes[$oldName])))))); } catch (Exception $e) { throw new MigrationException(__d('migration', 'SQL Error: %s', $e->getMessage())); } return $this; }
/** * Run after all tests executed, should return SQL statement to drop table for this fixture. * * @param DboSource $db An instance of the database object used to create the fixture table * @return bool True on success, false on failure */ public function drop($db) { if (empty($this->fields)) { return false; } $this->Schema->build(array($this->table => $this->fields)); try { $db->execute($db->dropSchema($this->Schema), array('log' => false)); $this->created = array_diff($this->created, array($db->configKeyName)); } catch (Exception $e) { return false; } return true; }
/** * testLastError * * @return void * @access public */ function testLastError() { $debug = Configure::read('debug'); Configure::write('debug', 0); $this->db->simulate = false; $query = 'SELECT [name] FROM [categories]'; $this->assertTrue($this->db->execute($query) !== false); $this->assertNull($this->db->lastError()); $query = 'SELECT [inexistent_field] FROM [categories]'; $this->assertFalse($this->db->execute($query)); $this->assertNotNull($this->db->lastError()); $query = 'SELECT [name] FROM [categories]'; $this->assertTrue($this->db->execute($query) !== false); $this->assertNull($this->db->lastError()); Configure::write('debug', $debug); }
/** * Overrides DboSource::execute() to correctly handle query statistics * * @param string $sql * @return unknown */ function execute($sql) { $result = parent::execute($sql); $this->_queryStats = array(); return $result; }