/** * Implements \SiteAudit\Check\Abstract\calculateScore(). */ public function calculateScore() { if (version_compare(DRUSH_VERSION, 7, '>=')) { $sql = drush_sql_get_class(); $db_spec = $sql->db_spec(); } else { $db_spec = _drush_sql_get_db_spec(); } try { $sql_query = 'SELECT SUM(TABLES.data_length + TABLES.index_length) '; $sql_query .= 'FROM information_schema.TABLES '; $sql_query .= 'WHERE TABLES.table_schema = :dbname '; $sql_query .= 'GROUP BY TABLES.table_schema '; $this->registry['rows_by_table'] = db_query($sql_query, array(':dbname' => $db_spec['database']))->fetchField(); if (!$this->registry['rows_by_table']) { $this->abort = TRUE; return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL; } return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO; } catch (PDOException $e) { // Error executing the query. $this->abort = TRUE; return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL; } // Empty database. $this->abort = TRUE; return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL; }
public function db_exists() { $database = $this->db_spec['database']; // Get a new class instance that has no 'database'. $db_spec_no_db = $this->db_spec; unset($db_spec_no_db['database']); $sql_no_db = drush_sql_get_class($db_spec_no_db); $query = "SELECT 1 AS result FROM pg_database WHERE datname='{$database}'"; drush_shell_exec($sql_no_db->connect() . ' -t -c %s', $query); $output = drush_shell_exec_output(); return (bool) $output[0]; }
public function db_exists() { // TODO: untested, but the gist is here. $database = $this->db_spec['database']; // Get a new class instance that has no 'database'. $db_spec_no_db = $this->db_spec; unset($db_spec_no_db['database']); $sql_no_db = drush_sql_get_class($db_spec_no_db); $query = "if db_id('{$database}') IS NOT NULL print 1"; drush_shell_exec($sql_no_db->connect() . ' -Q %s', $query); $output = drush_shell_exec_output(); return $output[0] == 1; }
/** * Implements \SiteAudit\Check\Abstract\calculateScore(). */ public function calculateScore() { if (version_compare(DRUSH_VERSION, 7, '>=')) { $sql = drush_sql_get_class(); $db_spec = $sql->db_spec(); } else { $db_spec = _drush_sql_get_db_spec(); } $sql_query = 'SELECT TABLE_NAME AS name '; $sql_query .= ', TABLE_COLLATION AS collation '; $sql_query .= 'FROM information_schema.TABLES '; $sql_query .= 'WHERE TABLES.table_schema = :dbname '; $sql_query .= 'AND TABLE_COLLATION NOT IN (:collation) '; $result = db_query($sql_query, array(':dbname' => $db_spec['database'], ':collation' => array('utf8_general_ci', 'utf8_unicode_ci', 'utf8_bin'))); if (!$result->rowCount()) { return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS; } $warn = FALSE; foreach ($result as $row) { $this->registry['collation_tables'][$row->name] = $row->collation; // Special case for old imports. if ($row->collation == 'latin1_swedish_ci') { $warn = TRUE; } } if ($warn) { return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN; } return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO; }
/** * Implements \SiteAudit\Check\Abstract\calculateScore(). */ public function calculateScore() { if (version_compare(DRUSH_VERSION, 7, '>=')) { $sql = drush_sql_get_class(); $db_spec = $sql->db_spec(); } else { $db_spec = _drush_sql_get_db_spec(); } $this->registry['rows_by_table'] = array(); $warning = FALSE; $sql_query = 'SELECT TABLE_NAME AS table_name, TABLE_ROWS AS rows '; $sql_query .= 'FROM information_schema.TABLES '; $sql_query .= 'WHERE TABLES.TABLE_SCHEMA = :dbname '; $sql_query .= 'AND TABLE_ROWS >= :count '; $sql_query .= 'ORDER BY TABLE_ROWS desc '; $result = db_query($sql_query, array(':count' => drush_get_option('min_rows', SiteAuditCheckDatabaseRowCount::AUDIT_CHECK_DB_ROW_MIN_DEFAULT), ':dbname' => $db_spec['database'])); foreach ($result as $row) { if ($row->rows > drush_get_option('min_rows', SiteAuditCheckDatabaseRowCount::AUDIT_CHECK_DB_ROW_MIN_DEFAULT)) { $warning = TRUE; } $this->registry['rows_by_table'][$row->table_name] = $row->rows; } if ($warning) { return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN; } return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO; }
/** * Test to see if the Drupal database has a specified * table or tables. * * This is a bootstrap helper function designed to be called * from the bootstrap_drupal_database_validate() methods of * derived DrupalBoot classes. If a database exists, but is * empty, then the Drupal database bootstrap will fail. To * prevent this situation, we test for some table that is needed * in an ordinary bootstrap, and return FALSE from the validate * function if it does not exist, so that we do not attempt to * start the database bootstrap. * * Note that we must manually do our own prefix testing here, * because the existing wrappers we have for handling prefixes * depend on bootstrapping to the "database" phase, and therefore * are not available to validate this same phase. * * @param $required_tables * Array of table names, or string with one table name * * @return TRUE if all tables in input parameter exist in * the database. */ function bootstrap_drupal_database_has_table($required_tables) { try { $sql = drush_sql_get_class(); $spec = $sql->db_spec(); $prefix = isset($spec['prefix']) ? $spec['prefix'] : NULL; if (!is_array($prefix)) { $prefix = array('default' => $prefix); } $tables = $sql->listTables(); foreach ((array) $required_tables as $required_table) { $prefix_key = array_key_exists($required_table, $prefix) ? $required_table : 'default'; if (!in_array($prefix[$prefix_key] . $required_table, $tables)) { return FALSE; } } } catch (Exception $e) { // Usually the checks above should return a result without // throwing an exception, but we'll catch any that are // thrown just in case. return FALSE; } return TRUE; }
/** * Implements \SiteAudit\Check\Abstract\calculateScore(). */ public function calculateScore() { if (version_compare(DRUSH_VERSION, 7, '>=')) { $sql = drush_sql_get_class(); $db_spec = $sql->db_spec(); } else { $db_spec = _drush_sql_get_db_spec(); } $sql_query = 'SELECT TABLE_NAME AS name '; $sql_query .= ', ENGINE '; $sql_query .= 'FROM information_schema.TABLES '; $sql_query .= 'WHERE TABLES.table_schema = :dbname '; $sql_query .= 'AND ENGINE != :engine '; $result = db_query($sql_query, array(':dbname' => $db_spec['database'], ':engine' => 'InnoDB')); if (!$result->rowCount()) { return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS; } foreach ($result as $row) { $this->registry['engine_tables'][$row->name] = $row->ENGINE; } return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL; }