function getDbStructure($schema, $model_name = null, $verbose_output = true) { //set active connection to schema and stop if there is no matching schema if (!DinklyDataConfig::setActiveConnection($schema)) { echo "\nNo such schema in config/db.yml\n"; return false; } //Connect to database $db = DinklyDataConnector::fetchDB(); $creds = DinklyDataConfig::getDBCreds(); $db_name = $creds["name"]; $db_name = DinklyBuilder::sanitize($db, $db_name); $model_name = DinklyBuilder::sanitize($db, $model_name); //get columns from specified table or all tables if ($model_name) { $stmt = $db->prepare("SHOW COLUMNS FROM " . $model_name . ""); $stmt->execute(); $table_schema = $stmt->fetchAll(); echo "\nRead table " . $model_name . " columns...\n"; sqlToYml($table_schema, $model_name, $db_name); } else { $stmt = $db->prepare("SHOW TABLES"); $stmt->execute(); $table_names = $stmt->fetchAll(); foreach ($table_names as $table_array) { $table_name = $table_array[0]; $stmt = $db->prepare("SHOW COLUMNS FROM " . $table_name . ""); $stmt->execute(); $table_schema = $stmt->fetchAll(); echo "\nRead table " . $table_name . " columns...\n"; sqlToYml($table_schema, $table_name, $db_name); } } }
protected function setUp() { date_default_timezone_set("Europe/Paris"); //Prepulate database and load with test users DinklyDataConfig::setActiveConnection('unit_test'); DinklyBuilder::buildTable('unit_test', 'TestUser', null, false); DinklyBuilder::loadAllFixtures('unit_test', false); }
public function setUp() { date_default_timezone_set("Europe/Paris"); $this->dsn = 'mysql:dbname=dinkly_unit_test;host=localhost;port=3306'; $this->username = '******'; $this->password = '******'; //Prepulate database and load with test users DinklyDataConfig::setActiveConnection('unit_test'); DinklyBuilder::buildTable('unit_test', 'TestUser', null, false); DinklyBuilder::loadAllFixtures('unit_test', false); }
protected function setUp() { date_default_timezone_set("Europe/Paris"); $this->dsn = 'mysql:dbname=dinkly_unit_test;host=localhost;port=3306'; $this->username = '******'; $this->password = '******'; //Prepulate database and load with test users DinklyDataConfig::setActiveConnection('unit_test'); DinklyBuilder::buildTable('unit_test', 'TestUser', null, false); DinklyBuilder::loadAllFixtures('unit_test', false); $this->user = new TestUser(); $this->user->init(1); $this->valid_array = array('id' => $this->user->getId(), 'created_at' => $this->user->getCreatedAt(), 'updated_at' => $this->user->getUpdatedAt(), 'username' => $this->user->getUserName(), 'password' => $this->user->getPassword(), 'first_name' => $this->user->getFirstName(), 'last_name' => $this->user->getLastName(), 'title' => $this->user->getTitle(), 'last_login_at' => $this->user->getLastLoginAt(), 'login_count' => $this->user->getLoginCount()); }
/** * Test function to make check for successful DB connection * * @param string $schema defaults to first found in config * @return bool true on successful connection false otherwise * @throws Exception if connection failed */ public static function testDB($schema = null) { try { if ($schema) { if (DinklyDataConfig::hasConnection($schema)) { DinklyDataConfig::setActiveConnection($schema); } else { echo "Connection failed: Schema does not exist\n"; return false; } } if (self::fetchDB()) { return true; } } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage() . "\n"; return false; } return false; }
public function testLoadDBCreds() { $this->assertTrue(DinklyDataConfig::setActiveConnection($this->db_creds)); }
/** * Load a specific fixture to populate DB table * * @param string $set: folder name of fixtures you would like to load * @param string $model_name: name model fixture to be parsed * @param bool $truncate (optional): truncate the table if set to true, or append records if false * @param bool $verbose_output (optional): how chatty would you like the build to be? * @param string $override_database_name (optional): if passed, this will override the name of the database as it appears in config/db.yml * * @return bool true if loaded successfully, false if load fails */ public static function loadFixture($set, $model_name, $plugin_name = null, $truncate = true, $verbose_output = true, $override_database_name = null) { //Use the proper DB credentials, or apply a passed-in override $creds = DinklyDataConfig::getDBCreds(); if ($override_database_name) { $creds['name'] = $override_database_name; DinklyDataConfig::setActiveConnection($creds); } //Create database if it doesn't exist $db = self::fetchDB($creds); $file_path = null; if ($plugin_name) { $file_path = $_SERVER['APPLICATION_ROOT'] . "plugins/" . $plugin_name . "/config/fixtures/" . $set . "/" . $model_name . ".yml"; } else { $file_path = $_SERVER['APPLICATION_ROOT'] . "config/fixtures/" . $set . "/" . $model_name . ".yml"; } if ($verbose_output) { echo "Attempting to parse '" . $model_name . "' fixture yaml..."; } $fixture = Yaml::parse($file_path); $model_name = $collection_name = null; if (isset($fixture['table_name'])) { $model_name = Dinkly::convertToCamelCase($fixture['table_name'], true); if ($verbose_output) { echo "success!\n"; } } else { return false; } if (isset($fixture['records'])) { if ($truncate) { if ($verbose_output) { echo "Truncating '" . $fixture['table_name'] . "'..."; } $db->exec("truncate table " . $fixture['table_name']); if ($verbose_output) { echo "success!\n"; } } $count = sizeof($fixture['records']); if ($verbose_output) { echo "Inserting " . $count . " record(s) into table '" . $fixture['table_name'] . "'"; } foreach ($fixture['records'] as $pos => $record) { if ($verbose_output) { echo "..."; } $model = new $model_name(); foreach ($record as $col_name => $value) { //Automatically set created date if none was passed if ($col_name == 'created_at' && $value == "") { $value = date('Y-m-d G:i:s'); } $set_field = 'set' . Dinkly::convertToCamelCase($col_name, true); $model->{$set_field}($value); } $model->save(); } if ($verbose_output) { echo "success!\n"; } return true; } }