/** * {@inheritdoc} * @codeCoverageIgnore */ public static function getObsoleteColumns($logger, $schema, $database) { $obsoleteColumns = parent::getObsoleteColumns($logger, $schema, $database); // Preserve columns which were added through the user interface. $preserveColumns = array(); // accountinfo_config may not exist yet when populating an empty // database. In that case, there are no obsolete columns. if (in_array('accountinfo_config', $database->getTableNames())) { $fields = $database->query("SELECT id FROM accountinfo_config WHERE name_accountinfo IS NULL AND account_type = 'COMPUTERS'"); foreach ($fields as $field) { $preserveColumns[] = "fields_{$field['id']}"; } } return array_diff($obsoleteColumns, $preserveColumns); }
/** * Create/update all tables * * This method iterates over all JSON schema files in ./data, instantiates * table objects of the same name for each file and calls their setSchema() * method. * * @param bool $prune Drop obsolete tables/columns */ public function updateTables($prune) { $database = $this->_serviceLocator->get('Database\\Nada'); $handledTables = array(); $glob = new \GlobIterator(Module::getPath('data/Tables') . '/*.json'); foreach ($glob as $fileinfo) { $tableClass = $fileinfo->getBaseName('.json'); $table = $this->_serviceLocator->get('Database\\Table\\' . $tableClass); $table->setSchema($prune); $handledTables[] = $table->table; } // Views need manual invocation. $this->_serviceLocator->get('Database\\Table\\Clients')->setSchema(); $this->_serviceLocator->get('Database\\Table\\PackageDownloadInfo')->setSchema(); $this->_serviceLocator->get('Database\\Table\\WindowsInstallations')->setSchema(); $logger = $this->_serviceLocator->get('Library\\Logger'); // Server tables have no table class $glob = new \GlobIterator(Module::getPath('data/Tables/Server') . '/*.json'); foreach ($glob as $fileinfo) { $schema = \Zend\Config\Factory::fromFile($fileinfo->getPathname()); self::setSchema($logger, $schema, $database, \Database\AbstractTable::getObsoleteColumns($logger, $schema, $database), $prune); $handledTables[] = $schema['name']; } // SNMP tables have no table class $glob = new \GlobIterator(Module::getPath('data/Tables/Snmp') . '/*.json'); foreach ($glob as $fileinfo) { $schema = \Zend\Config\Factory::fromFile($fileinfo->getPathname()); $obsoleteColumns = \Database\AbstractTable::getObsoleteColumns($logger, $schema, $database); if ($schema['name'] == 'snmp_accountinfo') { // Preserve columns which were added through the user interface. $preserveColumns = array(); // accountinfo_config may not exist yet when populating an empty // database. In that case, there are no obsolete columns. if (in_array('accountinfo_config', $database->getTableNames())) { $customFieldConfig = $this->_serviceLocator->get('Database\\Table\\CustomFieldConfig'); $select = $customFieldConfig->getSql()->select(); $select->columns(array('id'))->where(array('name_accountinfo' => null, 'account_type' => 'SNMP')); foreach ($customFieldConfig->selectWith($select) as $field) { $preserveColumns[] = "fields_{$field['id']}"; } $obsoleteColumns = array_diff($obsoleteColumns, $preserveColumns); } } self::setSchema($logger, $schema, $database, $obsoleteColumns, $prune); $handledTables[] = $schema['name']; } // Detect obsolete tables that are present in the database but not in // any of the schema files. $obsoleteTables = array_diff($database->getTableNames(), $handledTables); foreach ($obsoleteTables as $table) { if ($prune) { $logger->notice("Dropping table {$table}..."); $database->dropTable($table); $logger->notice("Done."); } else { $logger->warn("Obsolete table {$table} detected."); } } }