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); }
protected function setUp() { date_default_timezone_set("Europe/Paris"); $this->db_creds = array('host' => 'localhost', 'user' => 'root', 'pass' => 'root', 'name' => 'dinkly_unit_test'); //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 testForMySQLKeywords() { //Let's make sure to start without a table DinklyBuilder::dropTable('unit_test', 'TestUser'); //Grab a model and turn it into an array to be manipulated in tests that follow $model_yaml = DinklyBuilder::parseModelYaml('unit_test', 'TestUser', false); //Create a table with a column named after a MySQL keyword $model_yaml['registry'][] = array('key' => array('type' => 'int', 'allow_null' => true)); $this->assertTrue(DinklyBuilder::buildTable('unit_test', 'TestUser', $model_yaml, 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()); }
<?php require_once 'config/bootstrap.php'; $options = getopt("a:"); if (!isset($options['a'])) { echo "\nPlease use the -a flag to indicate the desired application name to use.\nExample: php tools/gen_app.php -a=admin\n\n"; } else { DinklyBuilder::buildApp($options['a']); }
echo " you may re-run this script with the insert flag to update your tables."; echo "\n"; echo " Usage: php tools/gen_models.php [args]\n\n"; echo " The available arguments are:\n"; echo " -h Show this help\n"; echo " -s Schema name, in underscore format (required)\n"; echo " -p Plugin name, in underscore format (optional)\n"; echo " -e Environment, corresponding to a database connection entry in config.yml (defaults to 'dev')\n"; echo " -i Insert SQL (optional)\n"; echo "\n"; echo " Example: php tools/gen_models.php -s=monkey_tail -p=tail_extensions -i\n"; echo "\n =======================================================================================\n\n"; exit; } if (!isset($options['s'])) { echo "\nPlease use the -s flag to indicate which schema set to use.\n\n"; die; } $plugin_name = null; if (isset($options['p'])) { $plugin_name = $options['p']; } $insert_sql = false; if (isset($options['i'])) { $insert_sql = true; } if (isset($options['e'])) { $Dinkly = new Dinkly($options['e'], true); } DinklyBuilder::buildAllModels($options['s'], $insert_sql, $plugin_name);
echo " -s Schema name, in underscore format (required)\n"; echo " -m Model name, in camel-case format (optional)\n"; echo " -p Plugin name, in underscore format (optional)\n"; echo " -e Environment, corresponding to a database connection entry in config.yml (defaults to 'dev')\n"; echo " -t Truncate table before loading fixture (optional)\n"; echo "\n"; echo " Example: php tools/load_fixtures.php -s=monkey_tail -m=Banana -t\n"; echo "\n =======================================================================================\n\n"; exit; } if (!isset($options['s'])) { echo "\nPlease use the -s flag to indicate which fixture set to use.\nExample: php tools/load_fixtures.php -s=dinkly\n\n"; echo "You may optionally use the -m flag to indicate a single fixture to load.\nExample: php php tools/load_fixtures.php -s=dinkly -m=fubar\n\n"; die; } $truncate = false; if (isset($options['t'])) { $truncate = true; } $plugin_name = false; if (isset($options['p'])) { $plugin_name = $options['p']; } if (isset($options['m'])) { if (isset($options['e'])) { $Dinkly = new Dinkly($options['e']); } DinklyBuilder::loadFixture($options['s'], $options['m'], $plugin_name, $truncate); } else { DinklyBuilder::loadAllFixtures($options['s'], $plugin_name, $truncate); }
<?php require_once 'config/bootstrap.php'; $options = getopt("a:m:"); if (!isset($options['a'])) { echo "\nPlease use the -a flag to indicate which application this module will be contained in.\nExample: php tools/gen_module.php -a=dinkly -m=test_module\n\n"; } else { if (!isset($options['m'])) { echo "\nPlease use the -m flag to indicate the desired module name to use.\nExample: php tools/gen_module.php -a=dinkly -m=test_module\n\n"; } else { DinklyBuilder::buildModule($options['a'], $options['m']); echo "\nModule created! You'll want to clear the session to access it in your browser using /?nocache=1\n\n"; } }
echo " -h Show this help\n"; echo " -m Model name, in camel-case format (required)\n"; echo " -s Schema name, in underscore format (required)\n"; echo " -p Plugin name, in underscore format (optional)\n"; echo " -e Environment, corresponding to a database connection entry in config.yml (defaults to 'dev')\n"; echo " -i Insert SQL (optional)\n"; echo "\n"; echo " Example: php tools/gen_model.php -s=monkey_tail -m=FunkyNugget -i\n"; echo "\n =======================================================================\n\n"; exit; } if (!isset($options['s'])) { echo "\nPlease use the -s flag to indicate which schema set to use.\n\n"; die; } if (!isset($options['m'])) { echo "\nPlease use the -m flag to indicate the desired model name to use.\n\n"; die; } $plugin_name = null; if (isset($options['p'])) { $plugin_name = $options['p']; } if (isset($options['e'])) { $Dinkly = new Dinkly($options['e'], true); } if (DinklyBuilder::buildModel($options['s'], $options['m'], $plugin_name)) { if (isset($options['i'])) { DinklyBuilder::buildTable($options['s'], $options['m'], $plugin_name); } }
/** * Add missing fields to existing database table * * @param string $schema String name of schema to use to get fields * @param string $verbose_output string used to log added fields * * @return bool true if fields added successfully, false otherwise */ public static function addMissingModelFieldsToDb($schema, $plugin_name = null, $verbose_output = null) { if (!DinklyDataConfig::setActiveConnection($schema)) { return false; } //If no DB exists, create one try { $db = DinklyDataConnector::fetchDB(); } catch (PDOException $e) { if ($e->getCode() == 1049) { self::createDb($schema, DinklyDataConfig::getDBCreds()); $db = DinklyDataConnector::fetchDB(); } } $model_names = DinklyBuilder::getAllModels($schema); //Gather up yaml configs for each model $model_yaml = $table_names = array(); foreach ($model_names as $model) { $model_yaml[$model] = self::parseModelYaml($schema, $model, $plugin_name, false); $table_names[$model] = $model_yaml[$model]['table_name']; } //Create index for matching tables and columns within the yaml config $yaml_fields = array(); foreach ($model_yaml as $y) { $yaml_fields[$y['table_name']] = array(); foreach ($y['registry'] as $field_name) { $name = null; if (is_array($field_name)) { $name = key($field_name); } else { $name = $field_name; } $yaml_fields[$y['table_name']][] = $name; } } //Create a very similar index, for matching tables and columns within the existing database $db_table_fields = array(); foreach ($table_names as $table_name) { $query = "show columns from " . $table_name; $results = $db->query($query)->fetchAll(); $db_table_fields[$table_name] = array(); foreach ($results as $k => $v) { $db_table_fields[$table_name][] = $v['Field']; } } //Find any fields that are in the yaml, but are not in the database currently $fields_to_add = array(); foreach ($yaml_fields as $table_name => $yaml_field_list) { foreach ($yaml_field_list as $field_name) { if (!in_array($field_name, $db_table_fields[$table_name])) { if (!isset($fields_to_add[$table_name])) { $fields_to_add[$table_name] = array(); } $fields_to_add[$table_name][] = $field_name; } } } //For each field missing from the database, but present in the yaml, run an alter query foreach ($fields_to_add as $table => $field_list) { foreach ($field_list as $field) { $registry = $model_yaml[Dinkly::convertToCamelCase($table, true)]['registry']; foreach ($registry as $field_config) { $sql = null; if (is_array($field_config)) { if (key($field_config) == $field) { $sql = self::genTableAlterQuery($db, $table, $field_config); } } else { if ($field == $field_config) { $sql = self::genTableAlterQuery($db, $table, $field_config); } } if ($sql) { if ($verbose_output) { echo "Adding field " . $field . " to " . $table . "...\n"; } $db->exec($sql); } } } } }