From 70c26869327dfbbac8ff7a85b22f8d3f876682f9 Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 27 May 2019 17:25:04 +0200 Subject: [PATCH 001/106] - Added new controller system/DBSkel to call DBSkel procedure form CLI - Removed libraries/MigrationLib.php - Removed MigrationLib from libraries/CallerLib.php --- application/controllers/system/DBSkel.php | 24 ++ application/libraries/CallerLib.php | 1 - application/libraries/MigrationLib.php | 466 ---------------------- 3 files changed, 24 insertions(+), 467 deletions(-) create mode 100644 application/controllers/system/DBSkel.php delete mode 100644 application/libraries/MigrationLib.php diff --git a/application/controllers/system/DBSkel.php b/application/controllers/system/DBSkel.php new file mode 100644 index 000000000..071cfaa9e --- /dev/null +++ b/application/controllers/system/DBSkel.php @@ -0,0 +1,24 @@ +load->library('DBSkelLib'); + } + + /** + * Starts the migration procedure + */ + public function start() + { + $this->dbskellib->start(); + } +} diff --git a/application/libraries/CallerLib.php b/application/libraries/CallerLib.php index 0b46cf0c6..82883cac4 100644 --- a/application/libraries/CallerLib.php +++ b/application/libraries/CallerLib.php @@ -19,7 +19,6 @@ class CallerLib private static $RESOURCES_BLACK_LIST = array( 'CallerLib', // disabled self loading 'LogLib', // hardly usefull and virtually dangerous - 'MigrationLib', // virtually dangerous, DB manipulation 'FilesystemLib', // virtually dangerous, direct access to file system 'PermissionLib', // usefull? 'PersonLogLib' diff --git a/application/libraries/MigrationLib.php b/application/libraries/MigrationLib.php deleted file mode 100644 index a461d7a82..000000000 --- a/application/libraries/MigrationLib.php +++ /dev/null @@ -1,466 +0,0 @@ -load->library('EPrintfLib'); - } - - /** - * Check if a column exists in a table and schema - */ - private function columnExists($name, $schema, $table) - { - $query = sprintf("SELECT %s FROM %s.%s LIMIT 1", $name, $schema, $table); - - if (@$this->db->simple_query($query)) - { - return true; - } - - return false; - } - - /** - * Print an info about the starting of method up - */ - protected function startUP() - { - $this->eprintflib->printInfo( - sprintf("%s Start method up of class %s %s", EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) - ); - } - - /** - * Print an info about the ending of method up - */ - protected function endUP() - { - $this->eprintflib->printInfo( - sprintf("%s End method up of class %s %s", EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) - ); - } - - /** - * Print an info about the starting of method down - */ - protected function startDown() - { - $this->eprintflib->printInfo( - sprintf("%s Start method down of class %s %s", EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) - ); - } - - /** - * Print an info about the ending of method down - */ - protected function endDown() - { - $this->eprintflib->printInfo( - sprintf("%s End method down of class %s %s", EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) - ); - } - - /** - * Adds a column, with attributes, to a table and schema - */ - protected function addColumn($schema, $table, $fields) - { - foreach ($fields as $name => $definition) - { - if (!$this->columnExists($name, $schema, $table)) - { - if ($this->dbforge->add_column($schema.'.'.$table, array($name => $definition))) - { - $this->eprintflib->printMessage(sprintf("Column %s.%s.%s of type %s added", $schema, $table, $name, $definition["type"])); - } - else - { - $this->eprintflib->printError(sprintf("Error while adding column %s.%s.%s of type %s", $schema, $table, $name, $definition["type"])); - } - } - else - { - $this->eprintflib->printInfo(sprintf("Column %s.%s.%s already exists", $schema, $table, $name)); - } - } - } - - /** - * Modifies a column, and its attributes, of a table and schema - */ - protected function modifyColumn($schema, $table, $fields) - { - foreach ($fields as $name => $definition) - { - if ($this->columnExists($name, $schema, $table)) - { - if ($this->dbforge->modify_column($schema.'.'.$table, array($name => $definition))) - { - $this->eprintflib->printMessage(sprintf("Column %s.%s.%s has been modified", $schema, $table, $name)); - } - else - { - $this->eprintflib->printError(sprintf("Error while modifying column %s.%s.%s", $schema, $table, $name)); - } - } - else - { - $this->eprintflib->printInfo(sprintf("Column %s.%s.%s doesn't exist", $schema, $table, $name)); - } - } - } - - /** - * Drops a column from a table and schema - */ - protected function dropColumn($schema, $table, $field) - { - if ($this->columnExists($field, $schema, $table)) - { - if ($this->dbforge->drop_column($schema.'.'.$table, $field)) - { - $this->eprintflib->printMessage(sprintf("Column %s.%s.%s has been dropped", $schema, $table, $field)); - } - else - { - $this->eprintflib->printError(sprintf("Error while dropping column %s.%s.%s", $schema, $table, $field)); - } - } - else - { - $this->eprintflib->printInfo(sprintf("Column %s.%s.%s doesn't exist", $schema, $table, $field)); - } - } - - /** - * Sets a column as primary key of a table and schema - */ - protected function addPrimaryKey($schema, $table, $name, $fields) - { - $stringFields = null; - - if (is_array($fields)) - { - if (count($fields) > 0) - { - $stringFields = ""; - for ($i = 0; $i < count($fields); $i++) - { - $stringFields .= $fields[$i]; - if ($i != count($fields) - 1) - { - $stringFields .= ", "; - } - } - $query = sprintf("ALTER TABLE %s.%s ADD CONSTRAINT %s PRIMARY KEY (%s)", $schema, $table, $name, $stringFields); - } - } - else - { - $query = sprintf("ALTER TABLE %s.%s ADD CONSTRAINT %s PRIMARY KEY (%s)", $schema, $table, $name, $fields); - } - - if (@$this->db->simple_query($query)) - { - $this->eprintflib->printMessage(sprintf("Added primary key %s on table %s.%s", $name, $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf("Adding primary key %s on table %s.%s", $name, $schema, $table)); - } - } - - /** - * Sets a column as foreign key of a table and schema - */ - protected function addForeingKey($schema, $table, $name, $field, $schemaDest, $tableDest, $fieldDest, $attributes) - { - $query = sprintf( - "ALTER TABLE %s.%s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s.%s (%s) %s", - $schema, - $table, - $name, - $field, - $schemaDest, - $tableDest, - $fieldDest, - $attributes - ); - - if (@$this->db->simple_query($query)) - { - $this->eprintflib->printMessage(sprintf("Added foreign key %s on table %s.%s", $name, $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf("Adding foreign key %s on table %s.%s", $name, $schema, $table)); - } - } - - /** - * Sets a column as unique key of a table and schema - */ - protected function addUniqueKey($schema, $table, $name, $fields) - { - $stringFields = null; - - if (is_array($fields)) - { - if (count($fields) > 0) - { - $stringFields = ""; - for ($i = 0; $i < count($fields); $i++) - { - $stringFields .= $fields[$i]; - if ($i != count($fields) - 1) - { - $stringFields .= ", "; - } - } - $query = sprintf("CREATE UNIQUE INDEX %s ON %s.%s (%s)", $name, $schema, $table, $stringFields); - } - } - else - { - $query = sprintf("CREATE UNIQUE INDEX %s ON %s.%s (%s)", $name, $schema, $table, $fields); - } - - if (@$this->db->simple_query($query)) - { - $this->eprintflib->printMessage(sprintf("Added unique key %s on table %s.%s", $name, $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf("Adding unique key %s on table %s.%s", $name, $schema, $table)); - } - } - - /** - * Grants permissions to a user on a table and schema - */ - protected function grantTable($permissions, $schema, $table, $user) - { - $stringPermission = null; - - if (is_array($permissions)) - { - if (count($permissions) > 0) - { - $stringPermission = ""; - for ($i = 0; $i < count($permissions); $i++) - { - $stringPermission .= $permissions[$i]; - if ($i != count($permissions) - 1) - { - $stringPermission .= ", "; - } - } - $query = sprintf("GRANT %s ON TABLE %s.%s TO %s", $stringPermission, $schema, $table, $user); - } - } - else - { - $query = sprintf("GRANT %s ON TABLE %s.%s TO %s", $permissions, $schema, $table, $user); - } - - if (@$this->db->simple_query($query)) - { - $this->eprintflib->printMessage( - sprintf( - "Granted permissions %s on table %s.%s to user %s", - is_null($stringPermission) ? $permissions : $stringPermission, - $schema, - $table, - $user - ) - ); - } - else - { - $this->eprintflib->printError( - sprintf( - "Granting permissions %s on table %s.%s to user %s", - is_null($stringPermission) ? $permissions : $stringPermission, - $schema, - $table, - $user - ) - ); - } - } - - /** - * Creates a table in a schema with columns - */ - protected function createTable($schema, $table, $fields) - { - $this->dbforge->add_field($fields); - - if ($this->dbforge->create_table($schema.'.'.$table, true)) - { - $this->eprintflib->printMessage(sprintf("Table %s.%s created or existing", $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf("Creating table %s.%s", $schema, $table)); - } - } - - /** - * Drops a table from a schema - */ - protected function dropTable($schema, $table) - { - if ($this->dbforge->drop_table($schema.".".$table)) - { - $this->eprintflib->printMessage(sprintf("Table %s.%s has been dropped", $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf("Dropping table %s.%s", $schema, $table)); - } - } - - /** - * Initializes a sequence with the max value of a column - */ - protected function initializeSequence($schemaSrc, $sequence, $schemaDst, $table, $field) - { - $query = sprintf("SELECT SETVAL('%s.%s', (SELECT MAX(%s) FROM %s.%s))", $schemaSrc, $sequence, $field, $schemaDst, $table); - - if (@$this->db->simple_query($query)) - { - $this->eprintflib->printMessage(sprintf("Sequence %s.%s has been initialized", $schemaSrc, $sequence)); - } - else - { - $this->eprintflib->printError(sprintf("Initializing sequence %s.%s", $schemaSrc, $sequence)); - } - } - - /** - * Add comment to a column - */ - protected function addCommentToColumn($schema, $table, $field, $comment) - { - $query = sprintf("COMMENT ON COLUMN %s.%s.%s IS ?", $schema, $table, $field); - - if (@$this->db->query($query, array($comment))) - { - $this->eprintflib->printMessage(sprintf("Comment added to %s.%s.%s", $schema, $table, $field)); - } - else - { - $this->eprintflib->printError(sprintf("Error while adding comment to %s.%s.%s", $schema, $table, $field)); - } - } - - /** - * Add comment to a table - */ - protected function addCommentToTable($schema, $table, $comment) - { - $query = sprintf("COMMENT ON TABLE %s.%s IS ?", $schema, $table, $field); - - if (@$this->db->query($query, array($comment))) - { - $this->eprintflib->printMessage(sprintf("Comment added to %s.%s", $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf("Error while adding comment to %s.%s", $schema, $table)); - } - } - /** - * Grants permissions to a user on a sequence - */ - protected function grantSequence($permissions, $schema, $sequence, $user) - { - $stringPermission = null; - - if (is_array($permissions)) - { - if (count($permissions) > 0) - { - $stringPermission = ""; - for ($i = 0; $i < count($permissions); $i++) - { - $stringPermission .= $permissions[$i]; - if ($i != count($permissions) - 1) - { - $stringPermission .= ", "; - } - } - $query = sprintf("GRANT %s ON SEQUENCE %s.%s TO %s", $stringPermission, $schema, $sequence, $user); - } - } - else - { - $query = sprintf("GRANT %s ON SEQUENCE %s.%s TO %s", $permissions, $schema, $sequence, $user); - } - - if (@$this->db->simple_query($query)) - { - $this->eprintflib->printMessage( - sprintf( - "Granted permissions %s on sequence %s.%s to user %s", - is_null($stringPermission) ? $permissions : $stringPermission, - $schema, - $sequence, - $user - ) - ); - } - else - { - $this->eprintflib->printError( - sprintf( - "Granting permissions %s on sequence %s.%s to user %s", - is_null($stringPermission) ? $permissions : $stringPermission, - $schema, - $sequence, - $user - ) - ); - } - } - - /** - * Executes the given query - */ - protected function execQuery($query) - { - if (! @$this->db->simple_query($query)) - { - $error = $this->db->error(); - - if (is_array($error) && isset($error["message"])) - { - $this->eprintflib->printError($error["message"]); - } - else - { - $this->eprintflib->printError("Error while executing a query"); - } - } - - $this->eprintflib->printInfo( - "Query correctly executed: ". - substr(preg_replace("/\s+/", " ", trim($query)), 0, EPrintfLib::PRINT_QUERY_LEN). - (strlen($query) > EPrintfLib::PRINT_QUERY_LEN ? "..." : "") - ); - } -} From e8aeefd966915f3962a690db0ac2e67163536f28 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 28 May 2019 17:10:30 +0200 Subject: [PATCH 002/106] - Added configuration file for DBSkel - Added library for DBSkel logic --- application/config/dbskel.php | 27 + application/libraries/DBSkelLib.php | 1146 +++++++++++++++++++++++++++ 2 files changed, 1173 insertions(+) create mode 100644 application/config/dbskel.php create mode 100644 application/libraries/DBSkelLib.php diff --git a/application/config/dbskel.php b/application/config/dbskel.php new file mode 100644 index 000000000..dafdc0d31 --- /dev/null +++ b/application/config/dbskel.php @@ -0,0 +1,27 @@ +_ci =& get_instance(); // get code igniter instance + + // Loads DB conns and confs using system settings + $this->_ci->load->database('system'); + + // Loads dbskel configs + $this->_ci->config->load('dbskel'); + + // Loads library EPrintfLib + $this->_ci->load->library('EPrintfLib'); + } + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + /** + * Starts the DBSkel procedure + * Returns false on failure and true on success + * All errors/warnings/infos are printed here using EPrintfLib + */ + public function start() + { + $start = false; + + // Checks if DBSkel is enabled + if ($this->_ci->config->item(self::CONF_ENABLED) === true) + { + // Gets all the directories in application/dbskel + $start = $this->_processDirectories(glob(self::DBSKEL_DIR.'*', GLOB_ONLYDIR)); + + $this->_printSchemaSeparator(); + } + else + { + $this->_printMessage('DBSkel is NOT enabled'); + } + + return $start; + } + + //------------------------------------------------------------------------------------------------------------------ + // Private methods + + /** + * Process every single directory present in dbskel directory + */ + private function _processDirectories($directories) + { + $processDirectories = false; // failure by default + + // For each directory + foreach ($directories as $directory) + { + $processDirectories = false; // Reset to false at the beginning of each loop + + $this->_printSchemaSeparator(); + $this->_printInfo('Found directory >>> '.basename($directory).' <<<'); + + // NOTE: the order in which these methods are called has a meaning! + // If a step fails then the loop is stopped + + // 1 - Checks file naming convention in current directory + // NOTE: no need to check a failure! NOT a blocking check!! + $this->_checkFilenaming($directory); + + $this->_printFileSeparator(); + + // 2 - Process schema file + if (!$this->_processSchemaFile($directory)) break; + + $this->_printFileSeparator(); + + // 3 - Process sequence file + if (!$this->_processSequencesFile($directory)) break; + + $this->_printFileSeparator(); + + // 4 - Process table files + if (!$this->_processTableFiles($directory)) break; + + $this->_printFileSeparator(); + + // 5 - Process views file + if (!$this->_processViewsFile($directory)) break; + + $this->_printFileSeparator(); + + // 6 - Process functions file + if (!$this->_processFunctionsFile($directory)) break; + + $this->_printFileSeparator(); + + // 7 - Process grants file + if (!$this->_processGrantsFile($directory)) break; + + $this->_printFileSeparator(); + + // 7 - Process extra file + if (!$this->_processExtraFile($directory)) break; + + $processDirectories = true; // If all the steps ends successfully + + $this->_printSchemaSeparator(); + } + + return $processDirectories; + } + + /** + * Checks file names are compliant with the file naming convention + */ + private function _checkFilenaming($directory) + { + $files = array_filter(glob($directory.'/*'), 'is_file'); + + // For each file + foreach ($files as $file) + { + $fileName = basename($file); // File name + // If the file name is NOT compliant with the file naming convention + if (!$this->_isFilenameValid($fileName)) + { + $this->_printInfo('Not valid file name, it is going to be ignored: '.$fileName); + } + } + } + + /** + * Checks if the file name is compliant with the file naming convention + */ + private function _isFilenameValid($fileName) + { + return $fileName == self::SCHEMA_FILENAME // Schema file + || $fileName == self::SEQUENCES_FILENAME // Sequences file + || (substr($fileName, 0, strlen(self::TABLE_PREFIX)) == self::TABLE_PREFIX + && substr($fileName, -5, strlen(self::JSON_EXT)) == self::JSON_EXT) // Table files + || $fileName == self::VIEWS_FILENAME // Views file + || $fileName == self::FUNCTIONS_FILENAME // Function file + || $fileName == self::GRANTS_FILENAME // Grants file + || $fileName == self::EXTRA_FILENAME; // Extra file + } + + /** + * Process the schema file + */ + private function _processSchemaFile($directory) + { + // Looks for a schema file + $files = array_filter(glob($directory.'/'.self::SCHEMA_FILENAME), 'is_file'); + + // If a schema file is found... + if (count($files) > 0) + { + $this->_printMessage('Found schema file: '.$files[0]); + + //...process it! + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> would be executed in new and diff mode'); + } + else // new or diff mode + { + return $this->_execSQLFile($files[0]); + } + } + else + { + $this->_printMessage('No schema file found'); + } + + return true; // If no files are found then go forward -> is a success + } + + /** + * Process sequences file + * - Looks for sequences present in current schema, then sequences that are not present in php file are dropped + * - Looks for sequences present in php files, then sequences that are not present in database are installed + */ + private function _processSequencesFile($directory) + { + // Looks for a sequences file + $files = array_filter(glob($directory.'/'.self::SEQUENCES_FILENAME), 'is_file'); + + // If a sequences file is found... + if (count($files) > 0) + { + $this->_printMessage('Found sequences file: '.$files[0]); + + //...process it! + require_once($files[0]); // Read sequences file + $schema = basename($directory); // retrieves schema name from directory path + $dbSequencesArray = $this->_listSequencesBySchema($schema); // get list of sequences currently present in DB + + // Loops through list of sequences currently present in database + foreach ($dbSequencesArray as $dbSequence) + { + // If NOT in new mode and if the sequence present in database is NOT present in the list of sequences from php file + if (!$this->_isNewMode() && !array_key_exists($dbSequence, $sequencesArray)) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> sequence '.$dbSequence.' not found in sequences file >> would be removed in diff mode'); + } + elseif ($this->_isDiffMode()) // only if in diff mode + { + // Then drop it and objects that depends on it from database! If it fails then ends execution + if (!$this->_execQuery(sprintf('DROP SEQUENCE %s.%s CASCADE', $schema, $dbSequence))) + { + $this->_printError('Error occurred while dropping sequence: '.$dbSequence); + return false; + } + else + { + $this->_printMessage('Sequence dropped successfully: '.$dbSequence); + } + } + } + } + + // Loops through list of sequences from php file + foreach ($sequencesArray as $sequenceName => $sequenceSQL) + { + // If the sequence from php file is NOT present in database + if (!in_array($sequenceName, $dbSequencesArray)) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> sequence '.$sequenceName.' not found database >> would be added in new and diff mode'); + } + else + { + // Then install it! If it fails then ends execution + if (!$this->_execQuery($sequenceSQL)) + { + $this->_printError('Error occurred while adding sequence: '.$sequenceName); + return false; + } + else + { + $this->_printMessage('Sequence added successfully: '.$sequenceName); + } + } + } + } + } + else + { + $this->_printMessage('No sequences file found'); + } + + return true; // If ends of procedure with no failures or no files are found then is a success + } + + /** + * Process table files + */ + private function _processTableFiles($directory) + { + // Looks for table files + $files = array_filter(glob($directory.'/'.self::TABLE_PREFIX.'*'.self::JSON_EXT), 'is_file'); + + // If table files are found... + if (count($files) > 0) + { + //...process them! + } + else + { + $this->_printMessage('No table files found'); + } + + return true; // If no files are found then go forward -> is a success + } + + /** + * Process views file + * - Looks for views present in current schema, then views that are not present in php file are dropped + * - Looks for views present in php files and install them all + */ + private function _processViewsFile($directory) + { + // Looks for a views file + $files = array_filter(glob($directory.'/'.self::VIEWS_FILENAME), 'is_file'); + + // If a views file is found... + if (count($files) > 0) + { + $this->_printMessage('Found views file: '.$files[0]); + + //...process it! + require_once($files[0]); // Read views file + $schema = basename($directory); // retrieves schema name from directory path + $dbViewsArray = $this->_listViewsBySchema($schema); // get list of views currently present in DB + + // Loops through list of views currently present in database + foreach ($dbViewsArray as $dbView) + { + // If NOT in new mode and if the view present in database is NOT present in the list of views from php file + if (!$this->_isNewMode() && !array_key_exists($dbView, $viewsArray)) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> view '.$dbView.' not found in views file >> would be removed in diff mode'); + } + elseif ($this->_isDiffMode()) // only if in diff mode + { + // Then drop it and objects that depends on it from database! If it fails then ends execution + if (!$this->_execQuery(sprintf('DROP VIEW %s.%s CASCADE', $schema, $dbView))) + { + $this->_printError('Error occurred while dropping view: '.$dbView); + return false; + } + else + { + $this->_printMessage('View dropped successfully: '.$dbView); + } + } + } + } + + // Loops through list of views from php file + foreach ($viewsArray as $viewName => $viewSQL) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> view '.$viewName.' would be added in new and diff mode'); + } + else + { + // Then install it! If it fails then ends execution + if (!$this->_execQuery($viewSQL)) + { + $this->_printError('Error occurred while adding view: '.$viewName); + return false; + } + else + { + $this->_printMessage('View added successfully: '.$viewName); + } + } + } + } + else + { + $this->_printMessage('No views file found'); + } + + return true; // If ends of procedure with no failures or no files are found then is a success + } + + /** + * Process functions file + */ + private function _processFunctionsFile($directory) + { + // Looks for a functions file + $files = array_filter(glob($directory.'/'.self::FUNCTIONS_FILENAME), 'is_file'); + + // If a functions file is found... + if (count($files) > 0) + { + $this->_printMessage('Found functions file: '.$files[0]); + + //...process it! + require_once($files[0]); // Read functions file + $schema = basename($directory); // retrieves schema name from directory path + $dbFunctionsArray = $this->_listFunctionsBySchema($schema); // get list of functions currently present in DB + + // Loops through list of functions currently present in database + foreach ($dbFunctionsArray as $dbFunction) + { + // If NOT in new mode and if the function present in database is NOT present in the list of functions from php file + if (!$this->_isNewMode() && !array_key_exists($dbFunction, $functionsArray)) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> function '.$dbFunction.' not found in fucntions file >> would be removed in diff mode'); + } + elseif ($this->_isDiffMode()) // only if in diff mode + { + // Then drop it and objects that depends on it from database! If it fails then ends execution + if (!$this->_execQuery(sprintf('DROP FUNCTION %s.%s CASCADE', $schema, $dbFunction))) + { + $this->_printError('Error occurred while dropping function: '.$dbFunction); + return false; + } + else + { + $this->_printMessage('Function dropped successfully: '.$dbFunction); + } + } + } + } + + // Loops through list of functions from php file + foreach ($functionsArray as $functionName => $functionSQL) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> view '.$functionName.' would be added in new and diff mode'); + } + else + { + // Then install it! If it fails then ends execution + if (!$this->_execQuery($functionSQL)) + { + $this->_printError('Error occurred while adding function: '.$functionName); + return false; + } + else + { + $this->_printMessage('Function added successfully: '.$functionName); + } + } + } + } + else + { + $this->_printMessage('No functions file found'); + } + + return true; // If ends of procedure with no failures or no files are found then is a success + } + + /** + * Process grants file + */ + private function _processGrantsFile($directory) + { + // Looks for a grants file + $files = array_filter(glob($directory.'/'.self::GRANTS_FILENAME), 'is_file'); + + // If a grants file is found... + if (count($files) > 0) + { + $this->_printMessage('Found grants file: '.$files[0]); + + //...process it! + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> would be executed in new and diff mode'); + } + else // new or diff mode + { + return $this->_execSQLFile($files[0]); + } + } + else + { + $this->_printMessage('No grants file found'); + } + + return true; // If no files are found then go forward -> is a success + } + + /** + * Process extra file + */ + private function _processExtraFile($directory) + { + // Looks for an extra file + $files = array_filter(glob($directory.'/'.self::EXTRA_FILENAME), 'is_file'); + + // If an extra file is found... + if (count($files) > 0) + { + $this->_printMessage('Found extra file: '.$files[0]); + + //...process it! + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> would be executed in new and diff mode'); + } + else // new or diff mode + { + return $this->_execSQLFile($files[0]); + } + } + else + { + $this->_printMessage('No extra file found'); + } + + return true; // If no files are found then go forward -> is a success + } + + /** + * Load SQL from a file and then execute such SQL + */ + private function _execSQLFile($file) + { + $sql = file_get_contents($file); // Read the file content + if ($sql === false) // If failed + { + $this->_printError('Error occurred while reading file: '.$file); + } + else // otherwise + { + // Exec query + if ($this->_execQuery($sql) == false) // if failed + { + $this->_printError('Error occurred while executing SQL from file: '.$file); + } + else // otherwise + { + $this->_printMessage('Successfully executed SQL from file: '.$file); + return true; + } + } + + return false; + } + + /** + * + */ + private function _isDryrunMode() + { + return $this->_ci->config->item(self::CONF_MODE) == self::RUN_MODE_DRYRUN; + } + + /** + * + */ + private function _isDiffMode() + { + return $this->_ci->config->item(self::CONF_MODE) == self::RUN_MODE_DIFF; + } + + /** + * + */ + private function _isNewMode() + { + return $this->_ci->config->item(self::CONF_MODE) == self::RUN_MODE_NEW; + } + + //------------------------------------------------------------------------------------------------------------------ + // Private database methods + + /** + * Executes the given query + */ + private function _execQuery($query) + { + if (!@$this->_ci->db->simple_query($query)) + { + $error = $this->_ci->db->error(); + if (is_array($error) && isset($error['message'])) + { + $this->_printError($error['message']); + } + + return false; + } + + return true; + } + + /** + * + */ + private function _listSequencesBySchema($schema) + { + $sequencesArray = array(); + $query = sprintf('SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema = \'%s\'', $schema); + + if ($sequences = @$this->_ci->db->query($query)) + { + foreach ($sequences->result() as $sequence) + { + $sequencesArray[] = $sequence->sequence_name; + } + } + + return $sequencesArray; + } + + /** + * + */ + private function _listViewsBySchema($schema) + { + $viewsArray = array(); + $query = sprintf('SELECT table_name FROM information_schema.views WHERE table_schema = \'%s\'', $schema); + + if ($views = @$this->_ci->db->query($query)) + { + foreach ($views->result() as $view) + { + $viewsArray[] = $view->table_name; + } + } + + return $viewsArray; + } + + /** + * + */ + private function _listFunctionsBySchema($schema) + { + $functionsArray = array(); + $query = sprintf('SELECT p.proname as "view_name" + FROM pg_catalog.pg_proc p + LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace + WHERE n.nspname <> \'pg_catalog\' + AND n.nspname <> \'information_schema\' + AND n.nspname = \'%s\'', $schema); + + if ($functions = @$this->_ci->db->query($query)) + { + foreach ($functions->result() as $function) + { + $functionsArray[] = $function->view_name; + } + } + + return $functionsArray; + } + + + + + + + + + + + + + + + + + + + + /** + * Check if a column exists in a table and schema + */ + private function _columnExists($name, $schema, $table) + { + $query = sprintf('SELECT %s FROM %s.%s LIMIT 1', $name, $schema, $table); + + if (@$this->_ci->db->simple_query($query)) + { + return true; + } + + return false; + } + + /** + * Print an info about the starting of method up + */ + private function _startUP() + { + $this->eprintflib->printMessage( + sprintf('%s Start method up of class %s %s', EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) + ); + } + + /** + * Print an info about the ending of method up + */ + private function _endUP() + { + $this->eprintflib->printMessage( + sprintf('%s End method up of class %s %s', EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) + ); + } + + /** + * Print an info about the starting of method down + */ + private function _startDown() + { + $this->eprintflib->printMessage( + sprintf('%s Start method down of class %s %s', EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) + ); + } + + /** + * Print an info about the ending of method down + */ + private function _endDown() + { + $this->eprintflib->printMessage( + sprintf('%s End method down of class %s %s', EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) + ); + } + + /** + * Adds a column, with attributes, to a table and schema + */ + private function _addColumn($schema, $table, $fields) + { + foreach ($fields as $name => $definition) + { + if (!$this->columnExists($name, $schema, $table)) + { + if ($this->_ci->dbforge->add_column($schema.'.'.$table, array($name => $definition))) + { + $this->eprintflib->printMessage(sprintf('Column %s.%s.%s of type %s added', $schema, $table, $name, $definition['type'])); + } + else + { + $this->eprintflib->printError(sprintf('Error while adding column %s.%s.%s of type %s', $schema, $table, $name, $definition['type'])); + } + } + else + { + $this->eprintflib->printMessage(sprintf('Column %s.%s.%s already exists', $schema, $table, $name)); + } + } + } + + /** + * Modifies a column, and its attributes, of a table and schema + */ + private function _modifyColumn($schema, $table, $fields) + { + foreach ($fields as $name => $definition) + { + if ($this->columnExists($name, $schema, $table)) + { + if ($this->_ci->dbforge->modify_column($schema.'.'.$table, array($name => $definition))) + { + $this->eprintflib->printMessage(sprintf('Column %s.%s.%s has been modified', $schema, $table, $name)); + } + else + { + $this->eprintflib->printError(sprintf('Error while modifying column %s.%s.%s', $schema, $table, $name)); + } + } + else + { + $this->eprintflib->printMessage(sprintf('Column %s.%s.%s does NOTt exist', $schema, $table, $name)); + } + } + } + + /** + * Drops a column from a table and schema + */ + private function _dropColumn($schema, $table, $field) + { + if ($this->columnExists($field, $schema, $table)) + { + if ($this->_ci->dbforge->drop_column($schema.'.'.$table, $field)) + { + $this->eprintflib->printMessage(sprintf('Column %s.%s.%s has been dropped', $schema, $table, $field)); + } + else + { + $this->eprintflib->printError(sprintf('Error while dropping column %s.%s.%s', $schema, $table, $field)); + } + } + else + { + $this->eprintflib->printMessage(sprintf('Column %s.%s.%s does NOT t exist', $schema, $table, $field)); + } + } + + /** + * Sets a column as primary key of a table and schema + */ + private function _addPrimaryKey($schema, $table, $name, $fields) + { + $stringFields = null; + + if (is_array($fields)) + { + if (count($fields) > 0) + { + $stringFields = ''; + for ($i = 0; $i < count($fields); $i++) + { + $stringFields .= $fields[$i]; + if ($i != count($fields) - 1) + { + $stringFields .= ', '; + } + } + $query = sprintf('ALTER TABLE %s.%s ADD CONSTRAINT %s PRIMARY KEY (%s)', $schema, $table, $name, $stringFields); + } + } + else + { + $query = sprintf('ALTER TABLE %s.%s ADD CONSTRAINT %s PRIMARY KEY (%s)', $schema, $table, $name, $fields); + } + + if (@$this->_ci->db->simple_query($query)) + { + $this->eprintflib->printMessage(sprintf('Added primary key %s on table %s.%s', $name, $schema, $table)); + } + else + { + $this->eprintflib->printError(sprintf('Adding primary key %s on table %s.%s', $name, $schema, $table)); + } + } + + /** + * Sets a column as foreign key of a table and schema + */ + private function _addForeingKey($schema, $table, $name, $field, $schemaDest, $tableDest, $fieldDest, $attributes) + { + $query = sprintf( + 'ALTER TABLE %s.%s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s.%s (%s) %s', + $schema, + $table, + $name, + $field, + $schemaDest, + $tableDest, + $fieldDest, + $attributes + ); + + if (@$this->_ci->db->simple_query($query)) + { + $this->eprintflib->printMessage(sprintf('Added foreign key %s on table %s.%s', $name, $schema, $table)); + } + else + { + $this->eprintflib->printError(sprintf('Adding foreign key %s on table %s.%s', $name, $schema, $table)); + } + } + + /** + * Sets a column as unique key of a table and schema + */ + private function _addUniqueKey($schema, $table, $name, $fields) + { + $stringFields = null; + + if (is_array($fields)) + { + if (count($fields) > 0) + { + $stringFields = ''; + for ($i = 0; $i < count($fields); $i++) + { + $stringFields .= $fields[$i]; + if ($i != count($fields) - 1) + { + $stringFields .= ', '; + } + } + $query = sprintf('CREATE UNIQUE INDEX %s ON %s.%s (%s)', $name, $schema, $table, $stringFields); + } + } + else + { + $query = sprintf('CREATE UNIQUE INDEX %s ON %s.%s (%s)', $name, $schema, $table, $fields); + } + + if (@$this->_ci->db->simple_query($query)) + { + $this->eprintflib->printMessage(sprintf('Added unique key %s on table %s.%s', $name, $schema, $table)); + } + else + { + $this->eprintflib->printError(sprintf('Adding unique key %s on table %s.%s', $name, $schema, $table)); + } + } + + /** + * Grants permissions to a user on a table and schema + */ + private function _grantTable($permissions, $schema, $table, $user) + { + $stringPermission = null; + + if (is_array($permissions)) + { + if (count($permissions) > 0) + { + $stringPermission = ''; + for ($i = 0; $i < count($permissions); $i++) + { + $stringPermission .= $permissions[$i]; + if ($i != count($permissions) - 1) + { + $stringPermission .= ', '; + } + } + $query = sprintf('GRANT %s ON TABLE %s.%s TO %s', $stringPermission, $schema, $table, $user); + } + } + else + { + $query = sprintf('GRANT %s ON TABLE %s.%s TO %s', $permissions, $schema, $table, $user); + } + + if (@$this->_ci->db->simple_query($query)) + { + $this->eprintflib->printMessage( + sprintf( + 'Granted permissions %s on table %s.%s to user %s', + is_null($stringPermission) ? $permissions : $stringPermission, + $schema, + $table, + $user + ) + ); + } + else + { + $this->eprintflib->printError( + sprintf( + 'Granting permissions %s on table %s.%s to user %s', + is_null($stringPermission) ? $permissions : $stringPermission, + $schema, + $table, + $user + ) + ); + } + } + + /** + * Creates a table in a schema with columns + */ + private function _createTable($schema, $table, $fields) + { + $this->_ci->dbforge->add_field($fields); + + if ($this->_ci->dbforge->create_table($schema.'.'.$table, true)) + { + $this->eprintflib->printMessage(sprintf('Table %s.%s created or existing', $schema, $table)); + } + else + { + $this->eprintflib->printError(sprintf('Creating table %s.%s', $schema, $table)); + } + } + + /** + * Drops a table from a schema + */ + private function _dropTable($schema, $table) + { + if ($this->_ci->dbforge->drop_table($schema.'.'.$table)) + { + $this->eprintflib->printMessage(sprintf('Table %s.%s has been dropped', $schema, $table)); + } + else + { + $this->eprintflib->printError(sprintf('Dropping table %s.%s', $schema, $table)); + } + } + + /** + * Initializes a sequence with the max value of a column + */ + private function _initializeSequence($schemaSrc, $sequence, $schemaDst, $table, $field) + { + $query = sprintf('SELECT SETVAL(\'%s.%s\', (SELECT MAX(%s) FROM %s.%s))', $schemaSrc, $sequence, $field, $schemaDst, $table); + + if (@$this->_ci->db->simple_query($query)) + { + $this->eprintflib->printMessage(sprintf('Sequence %s.%s has been initialized', $schemaSrc, $sequence)); + } + else + { + $this->eprintflib->printError(sprintf('Initializing sequence %s.%s', $schemaSrc, $sequence)); + } + } + + /** + * Add comment to a column + */ + private function _commentOnColumn($schema, $table, $field, $comment) + { + $query = sprintf('COMMENT ON COLUMN %s.%s.%s IS ?', $schema, $table, $field); + + if (@$this->_ci->db->query($query, array($comment))) + { + $this->eprintflib->printMessage(sprintf('Comment added to %s.%s.%s', $schema, $table, $field)); + } + else + { + $this->eprintflib->printError(sprintf('Error while adding comment to %s.%s.%s', $schema, $table, $field)); + } + } + + /** + * Add comment to a table + */ + private function _commentOnTable($schema, $table, $comment) + { + $query = sprintf('COMMENT ON TABLE %s.%s IS ?', $schema, $table, $field); + + if (@$this->_ci->db->query($query, array($comment))) + { + $this->eprintflib->printMessage(sprintf('Comment added to %s.%s', $schema, $table)); + } + else + { + $this->eprintflib->printError(sprintf('Error while adding comment to %s.%s', $schema, $table)); + } + } + /** + * Grants permissions to a user on a sequence + */ + private function _grantSequence($permissions, $schema, $sequence, $user) + { + $stringPermission = null; + + if (is_array($permissions)) + { + if (count($permissions) > 0) + { + $stringPermission = ''; + for ($i = 0; $i < count($permissions); $i++) + { + $stringPermission .= $permissions[$i]; + if ($i != count($permissions) - 1) + { + $stringPermission .= ', '; + } + } + $query = sprintf('GRANT %s ON SEQUENCE %s.%s TO %s', $stringPermission, $schema, $sequence, $user); + } + } + else + { + $query = sprintf('GRANT %s ON SEQUENCE %s.%s TO %s', $permissions, $schema, $sequence, $user); + } + + if (@$this->_ci->db->simple_query($query)) + { + $this->eprintflib->printMessage( + sprintf( + 'Granted permissions %s on sequence %s.%s to user %s', + is_null($stringPermission) ? $permissions : $stringPermission, + $schema, + $sequence, + $user + ) + ); + } + else + { + $this->eprintflib->printError( + sprintf( + 'Granting permissions %s on sequence %s.%s to user %s', + is_null($stringPermission) ? $permissions : $stringPermission, + $schema, + $sequence, + $user + ) + ); + } + } + + //------------------------------------------------------------------------------------------------------------------ + // Private output methods + + /** + * + */ + private function _printInfo($string) + { + $this->_ci->eprintflib->printInfo($string); + } + + /** + * + */ + private function _printError($string) + { + $this->_ci->eprintflib->printError($string); + } + + /** + * + */ + private function _printMessage($string) + { + $this->_ci->eprintflib->printMessage($string); + } + + /** + * + */ + private function _printSchemaSeparator() + { + $this->_printInfo('--------------------------------------------------------------------------------------------'); + } + + /** + * + */ + private function _printFileSeparator() + { + $this->_printMessage('--------------------------------------------------------------------------------------------'); + } +} From 0f61a9e814a4062c0f6a9be9426e7ab16fd0547e Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 5 Jun 2019 10:22:14 +0200 Subject: [PATCH 003/106] - Added parameters steps and selectedDirectories to controller controllers/system/DBSkel - Added exit status to controller controllers/system/DBSkel - Removed old code from libraries/DBSkelLib - Added steps parameter to libraries/DBSkelLib to select the step/s to perform (colon separated) - Added selectedDirectories parameter to libraries/DBSkelLib to select the directory/ies to process (colon separated) - All DBSkel structure files are in php and SQL formats - The steps order is the following: schema, sequences, tables, constraints, views, functions, grants and extras - Current running modes are: dry run, new and diff - Diff mode is still under construction --- application/controllers/system/DBSkel.php | 8 +- application/libraries/DBSkelLib.php | 968 ++++++++++++---------- 2 files changed, 528 insertions(+), 448 deletions(-) diff --git a/application/controllers/system/DBSkel.php b/application/controllers/system/DBSkel.php index 071cfaa9e..94ba18cf6 100644 --- a/application/controllers/system/DBSkel.php +++ b/application/controllers/system/DBSkel.php @@ -15,10 +15,12 @@ class DBSkel extends CLI_Controller } /** - * Starts the migration procedure + * Starts the DBSkel procedure */ - public function start() + public function start($step = null, $selectedDirectories = null) { - $this->dbskellib->start(); + // If the DBSkel procedure fails then exit with an error + // In this way it's possible to undestand from console what is the exit status of the procedure + $this->dbskellib->start($step, $selectedDirectories) === true ? exit(0) : exit(1); } } diff --git a/application/libraries/DBSkelLib.php b/application/libraries/DBSkelLib.php index ed9be19f1..df8a71a6c 100644 --- a/application/libraries/DBSkelLib.php +++ b/application/libraries/DBSkelLib.php @@ -11,29 +11,45 @@ class DBSkelLib const CONF_ENABLED = 'dbskel_enabled'; const CONF_MODE = 'dbskel_mode'; + const SEPARATOR = ':'; // Step parameter character separator + // Run modes const RUN_MODE_DRYRUN = 'dryrun'; // run without changing the database, useful for testing const RUN_MODE_NEW = 'new'; // build a new database or if database is already present creates only new objects const RUN_MODE_DIFF = 'diff'; // like new, but it also remove object from database that are NOT present in configuration files + const STEP_SCHEMA = 1; + const STEP_SEQUENCES = 2; + const STEP_TABLES = 3; + const STEP_CONSTRAINTS = 4; + const STEP_VIEWS = 5; + const STEP_FUNCTIONS = 6; + const STEP_GRANTS = 7; + const STEP_EXTRA = 8; + + const MAX_STEPS = 8; // Maximum number of steps + // Configuration file names const SCHEMA_FILENAME = 'schema.sql'; // File name that contains schema creation SQL and SQL to comment a schema const SEQUENCES_FILENAME = 'sequences.php'; // PHP file that contains all the sequences const TABLE_PREFIX = 'TBL-'; // Table file prefix + const CONSTRAINTS_FILENAME = 'constraints.php'; // PHP file that contains all the constraints const VIEWS_FILENAME = 'views.php'; // PHP file that contains all the views const FUNCTIONS_FILENAME = 'functions.php'; // PHP file that contains all the functions const GRANTS_FILENAME = 'grants.sql'; // Grants SQL file name const EXTRA_FILENAME = 'extra.sql'; // Extra SQL file name - // JSON file extension - const JSON_EXT = '.json'; + // PHP file extension + const PHP_EXT = '.php'; - // Directory that containts the database skel + // Directory that contains the database skel const DBSKEL_DIR = APPPATH.'dbskel/'; - // Tables JSON properties name - const JSON_NAME = 'name'; - const JSON_SQL = 'sql'; + // Table properties + const T_COMMENT = 'comment'; + const T_TYPE = 'type'; + const T_NULL = 'null'; + const T_DEFAULT = 'default'; private $_ci; // Code igniter instance @@ -61,22 +77,42 @@ class DBSkelLib * Starts the DBSkel procedure * Returns false on failure and true on success * All errors/warnings/infos are printed here using EPrintfLib + * Accept the step parameter that can be used to run only a wanted step */ - public function start() + public function start($steps, $selectedDirectories) { $start = false; // Checks if DBSkel is enabled if ($this->_ci->config->item(self::CONF_ENABLED) === true) { - // Gets all the directories in application/dbskel - $start = $this->_processDirectories(glob(self::DBSKEL_DIR.'*', GLOB_ONLYDIR)); + // Checks if the given steps parameter is fine + if ($this->_checkParameterStep($steps)) + { + $this->_printSchemaSeparator(); - $this->_printSchemaSeparator(); + $this->_printRunningMode(); + + // By default perform all steps + $stepsArray = range(1, self::MAX_STEPS); + // If steps parameter is given then use it to select the steps to be performed + if ($steps != null) $stepsArray = explode(self::SEPARATOR, $steps); + + // Gets all the directories in application/dbskel + $directories = glob(self::DBSKEL_DIR.'*', GLOB_ONLYDIR); + + // Checks if the selectedDirectories parameter is fine + if ($this->_checkParameterSelectedDirectories($selectedDirectories, $directories)) + { + $start = $this->_processDirectories($directories, $stepsArray); + + $this->_printSchemaSeparator(); + } + } } else { - $this->_printMessage('DBSkel is NOT enabled'); + $this->_printInfo('DBSkel is NOT enabled'); } return $start; @@ -88,7 +124,7 @@ class DBSkelLib /** * Process every single directory present in dbskel directory */ - private function _processDirectories($directories) + private function _processDirectories($directories, $stepsArray) { $processDirectories = false; // failure by default @@ -103,44 +139,73 @@ class DBSkelLib // NOTE: the order in which these methods are called has a meaning! // If a step fails then the loop is stopped - // 1 - Checks file naming convention in current directory - // NOTE: no need to check a failure! NOT a blocking check!! + // 0 - Checks file naming convention in current directory + // NOTE: no need to check a failure! NOT a blocking check!! Always performed!!! $this->_checkFilenaming($directory); $this->_printFileSeparator(); - // 2 - Process schema file - if (!$this->_processSchemaFile($directory)) break; + // 1 - Process schema file + if (in_array(self::STEP_SCHEMA, $stepsArray)) + { + if (!$this->_processSchemaFile($directory)) break; - $this->_printFileSeparator(); + $this->_printFileSeparator(); + } - // 3 - Process sequence file - if (!$this->_processSequencesFile($directory)) break; + // 2 - Process sequence file + if (in_array(self::STEP_SEQUENCES, $stepsArray)) + { + if (!$this->_processSequencesFile($directory)) break; - $this->_printFileSeparator(); + $this->_printFileSeparator(); + } - // 4 - Process table files - if (!$this->_processTableFiles($directory)) break; + // 3 - Process table files + if (in_array(self::STEP_TABLES, $stepsArray)) + { + if (!$this->_processTableFiles($directory)) break; - $this->_printFileSeparator(); + $this->_printFileSeparator(); + } + + // 4 - Process constraints + if (in_array(self::STEP_CONSTRAINTS, $stepsArray)) + { + if (!$this->_processConstraintsFile($directory)) break; + + $this->_printFileSeparator(); + } // 5 - Process views file - if (!$this->_processViewsFile($directory)) break; + if (in_array(self::STEP_VIEWS, $stepsArray)) + { + if (!$this->_processViewsFile($directory)) break; - $this->_printFileSeparator(); + $this->_printFileSeparator(); + } // 6 - Process functions file - if (!$this->_processFunctionsFile($directory)) break; + if (in_array(self::STEP_FUNCTIONS, $stepsArray)) + { + if (!$this->_processFunctionsFile($directory)) break; - $this->_printFileSeparator(); + $this->_printFileSeparator(); + } // 7 - Process grants file - if (!$this->_processGrantsFile($directory)) break; + if (in_array(self::STEP_GRANTS, $stepsArray)) + { + if (!$this->_processGrantsFile($directory)) break; - $this->_printFileSeparator(); + $this->_printFileSeparator(); + } - // 7 - Process extra file - if (!$this->_processExtraFile($directory)) break; + // 8 - Process extra file + if (in_array(self::STEP_EXTRA, $stepsArray)) + { + if (!$this->_processExtraFile($directory)) break; + } $processDirectories = true; // If all the steps ends successfully @@ -177,7 +242,8 @@ class DBSkelLib return $fileName == self::SCHEMA_FILENAME // Schema file || $fileName == self::SEQUENCES_FILENAME // Sequences file || (substr($fileName, 0, strlen(self::TABLE_PREFIX)) == self::TABLE_PREFIX - && substr($fileName, -5, strlen(self::JSON_EXT)) == self::JSON_EXT) // Table files + && substr($fileName, -4, strlen(self::PHP_EXT)) == self::PHP_EXT) // Table files + || $fileName == self::CONSTRAINTS_FILENAME // Constraints file || $fileName == self::VIEWS_FILENAME // Views file || $fileName == self::FUNCTIONS_FILENAME // Function file || $fileName == self::GRANTS_FILENAME // Grants file @@ -217,8 +283,8 @@ class DBSkelLib /** * Process sequences file - * - Looks for sequences present in current schema, then sequences that are not present in php file are dropped - * - Looks for sequences present in php files, then sequences that are not present in database are installed + * - Looks for sequences present in current schema, then sequences that are NOT present in php file are dropped (diff mode only) + * - Looks for sequences present in php files, then sequences that are NOT present in database are installed */ private function _processSequencesFile($directory) { @@ -233,7 +299,7 @@ class DBSkelLib //...process it! require_once($files[0]); // Read sequences file $schema = basename($directory); // retrieves schema name from directory path - $dbSequencesArray = $this->_listSequencesBySchema($schema); // get list of sequences currently present in DB + $dbSequencesArray = $this->_listSequencesBySchema($schema); // get list of sequences currently present in DB schema // Loops through list of sequences currently present in database foreach ($dbSequencesArray as $dbSequence) @@ -243,7 +309,7 @@ class DBSkelLib { if ($this->_isDryrunMode()) // If dry run mode enabled { - $this->_printInfo('Dry run >> sequence '.$dbSequence.' not found in sequences file >> would be removed in diff mode'); + $this->_printInfo('Dry run >> sequence '.$dbSequence.' NOT found in sequences file >> would be removed in diff mode'); } elseif ($this->_isDiffMode()) // only if in diff mode { @@ -269,7 +335,7 @@ class DBSkelLib { if ($this->_isDryrunMode()) // If dry run mode enabled { - $this->_printInfo('Dry run >> sequence '.$sequenceName.' not found database >> would be added in new and diff mode'); + $this->_printInfo('Dry run >> sequence '.$sequenceName.' NOT found database >> would be added in new and diff mode'); } else { @@ -285,6 +351,10 @@ class DBSkelLib } } } + else + { + $this->_printMessage('Sequence already present in database: '.$sequenceName); + } } } else @@ -301,12 +371,101 @@ class DBSkelLib private function _processTableFiles($directory) { // Looks for table files - $files = array_filter(glob($directory.'/'.self::TABLE_PREFIX.'*'.self::JSON_EXT), 'is_file'); + $files = array_filter(glob($directory.'/'.self::TABLE_PREFIX.'*'.self::PHP_EXT), 'is_file'); // If table files are found... if (count($files) > 0) { //...process them! + $schema = basename($directory); // retrieves schema name from directory path + $dbTablesArray = $this->_listTablesBySchema($schema); // get list of tables currently present in DB schema + + // For each table file + foreach ($files as $file) + { + $this->_printMessage('Found table file: '.$file); + + require_once($file); // Read table file + + // Loops through list of tables currently present in database + foreach ($dbTablesArray as $dbTable) + { + // If NOT in new mode and if the table present in database is NOT present in the php table file + if (!$this->_isNewMode() && !array_key_exists($dbTable, $tableArray)) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> table '.$dbTable.' NOT found in table file >> would be removed in diff mode'); + } + elseif ($this->_isDiffMode()) // only if in diff mode + { + // Then drop it! If it fails then ends execution + if (!$this->_execQuery(sprintf('DROP TABLE %s.%s', $schema, $dbTable))) + { + $this->_printError('Error occurred while dropping table: '.$dbTable); + return false; + } + else + { + $this->_printMessage('Table dropped successfully: '.$dbTable); + } + } + } + } + + // Retrieves all the elements from the $tableArray except the element 'comment' + $tableElements = array_keys(array_diff_key($tableArray, array(self::T_COMMENT => null))); + if (is_array($tableElements) && count($tableElements) == 1) // If there is only one element left... + { + $tableName = $tableElements[0]; // ...then it is the name of the table + + // If the table from php file is NOT present in database + if (!in_array($tableName, $dbTablesArray)) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> table '.$tableName.' would be created in new and diff mode'); + } + else // new and diff mode + { + // Then create the new table! If it fails then ends execution + if ($this->_createTable($schema, $tableArray)) + { + $this->_printMessage('Table created successfully: '.$tableName); + } + else + { + $this->_printError('Error occurred while creating a new table: '.$tableName); + return false; + } + } + } + else // if table is already present in database + { + if ($this->_isNewMode()) // only if in new mode + { + $this->_printMessage('Table already present in database: '.$tableName); + } + elseif ($this->_isDiffMode()) // only if in diff mode + { + // Then diff the already present table with the one from php file! If it fails then ends execution + if ($this->_diffTable($schema, $tableArray)) + { + $this->_printMessage('Table diff success: '.$tableName); + } + else + { + $this->_printError('Error occurred while diff table: '.$tableName); + return false; + } + } + } + } + else // otherwise the array present in the php table file is not well formatted + { + $this->_printError('Table file with a bad format is going to be ignored: '.$file); + } + } } else { @@ -316,9 +475,96 @@ class DBSkelLib return true; // If no files are found then go forward -> is a success } + /** + * Process constraints file + * - Looks for constraints present in current schema, then constraints that are NOT present in php file are dropped (diff mode only) + * - Looks for constraints present in php files, then constraints that are NOT present in database are installed + */ + private function _processConstraintsFile($directory) + { + // Looks for a constraints file + $files = array_filter(glob($directory.'/'.self::CONSTRAINTS_FILENAME), 'is_file'); + + // If a constraints file is found... + if (count($files) > 0) + { + $this->_printMessage('Found constraints file: '.$files[0]); + + //...process it! + require_once($files[0]); // Read constraints file + $schema = basename($directory); // retrieves schema name from directory path + $dbConstraintsArray = $this->_listConstraintsBySchema($schema); // get list of constraints currently present in DB schema + $dbConstraintsNamesArray = array(); // Contains only the names of the constraints + + // Loops through list of constraints currently present in database + foreach ($dbConstraintsArray as $dbConstraint) + { + $dbConstraintsNamesArray[] = $dbConstraint['name']; // Copy only the name of the constraint + + // If NOT in new mode and if the constraint present in database is NOT present in the list of constraints from php file + if (!$this->_isNewMode() && !array_key_exists($dbConstraint['name'], $constraintsArray)) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> constraint '.$dbConstraint['name'].' NOT found in constraints file >> would be removed in diff mode'); + } + elseif ($this->_isDiffMode()) // only if in diff mode + { + // Then drop it and objects that depends on it from database! If it fails then ends execution + if (!$this->_execQuery(sprintf('ALTER TABLE %s.%s DROP CONSTRAINT %s', $schema, $dbConstraint['table'], $dbConstraint['name']))) + { + $this->_printError('Error occurred while dropping constraint: '.$dbConstraint['name']); + return false; + } + else + { + $this->_printMessage('Constraint dropped successfully: '.$dbConstraint['name']); + } + } + } + } + + // Loops through list of constraints from php file + foreach ($constraintsArray as $constraintName => $constraintSQL) + { + // If the constraint from php file is NOT present in database + if (!in_array($constraintName, $dbConstraintsNamesArray)) + { + if ($this->_isDryrunMode()) // If dry run mode enabled + { + $this->_printInfo('Dry run >> constraint '.$constraintName.' would be added in new and diff mode'); + } + else + { + // Then install it! If it fails then ends execution + if (!$this->_execQuery($constraintSQL)) + { + $this->_printError('Error occurred while adding constraint: '.$constraintName); + return false; + } + else + { + $this->_printMessage('Constraint added successfully: '.$constraintName); + } + } + } + else + { + $this->_printMessage('Constraint already present in database: '.$constraintName); + } + } + } + else + { + $this->_printMessage('No constraints file found'); + } + + return true; // If ends of procedure with no failures or no files are found then is a success + } + /** * Process views file - * - Looks for views present in current schema, then views that are not present in php file are dropped + * - Looks for views present in current schema, then views that are NOT present in php file are dropped (diff mode only) * - Looks for views present in php files and install them all */ private function _processViewsFile($directory) @@ -334,7 +580,7 @@ class DBSkelLib //...process it! require_once($files[0]); // Read views file $schema = basename($directory); // retrieves schema name from directory path - $dbViewsArray = $this->_listViewsBySchema($schema); // get list of views currently present in DB + $dbViewsArray = $this->_listViewsBySchema($schema); // get list of views currently present in DB schema // Loops through list of views currently present in database foreach ($dbViewsArray as $dbView) @@ -344,7 +590,7 @@ class DBSkelLib { if ($this->_isDryrunMode()) // If dry run mode enabled { - $this->_printInfo('Dry run >> view '.$dbView.' not found in views file >> would be removed in diff mode'); + $this->_printInfo('Dry run >> view '.$dbView.' NOT found in views file >> would be removed in diff mode'); } elseif ($this->_isDiffMode()) // only if in diff mode { @@ -394,6 +640,8 @@ class DBSkelLib /** * Process functions file + * - Looks for functions present in current schema, then functions that are NOT present in php file are dropped (diff mode only) + * - Looks for functions present in php files and install them all */ private function _processFunctionsFile($directory) { @@ -408,7 +656,7 @@ class DBSkelLib //...process it! require_once($files[0]); // Read functions file $schema = basename($directory); // retrieves schema name from directory path - $dbFunctionsArray = $this->_listFunctionsBySchema($schema); // get list of functions currently present in DB + $dbFunctionsArray = $this->_listFunctionsBySchema($schema); // get list of functions currently present in DB schema // Loops through list of functions currently present in database foreach ($dbFunctionsArray as $dbFunction) @@ -418,7 +666,7 @@ class DBSkelLib { if ($this->_isDryrunMode()) // If dry run mode enabled { - $this->_printInfo('Dry run >> function '.$dbFunction.' not found in fucntions file >> would be removed in diff mode'); + $this->_printInfo('Dry run >> function '.$dbFunction.' NOT found in fucntions file >> would be removed in diff mode'); } elseif ($this->_isDiffMode()) // only if in diff mode { @@ -556,7 +804,7 @@ class DBSkelLib } /** - * + * Checks if the running mode is 'dryrun' */ private function _isDryrunMode() { @@ -564,7 +812,7 @@ class DBSkelLib } /** - * + * Checks if the running mode is 'diff' */ private function _isDiffMode() { @@ -572,13 +820,80 @@ class DBSkelLib } /** - * + * Checks if the running mode is 'new' */ private function _isNewMode() { return $this->_ci->config->item(self::CONF_MODE) == self::RUN_MODE_NEW; } + /** + * Checks if the parameter step is correct + */ + private function _checkParameterStep($steps) + { + if ($steps != null) // if it was given + { + $stepsArray = explode(self::SEPARATOR, $steps); // split the string in an array + foreach ($stepsArray as $step) + { + if (!is_numeric($step)) // if it is not a number + { + $this->_ci->eprintflib->printError('The given parameter must be a number or a string in the following format: 1:3:5'); + return false; + } + elseif ($step > self::MAX_STEPS) // if it is a number but > MAX_STEPS + { + $this->_ci->eprintflib->printError('The maximun value fot this parameter is: '.self::MAX_STEPS); + return false; + } + elseif ($step < 1) // if it is a number but < 1 + { + $this->_ci->eprintflib->printError('The minimum value fot this parameter is 1'); + return false; + } + } + } + + return true; + } + + /** + * Checks if the parameter selectedDirectories is correct and stores the result in $directories + */ + private function _checkParameterSelectedDirectories($selectedDirectories, &$directories) + { + if ($selectedDirectories != null) + { + $selectedDirectoriesArray = explode(self::SEPARATOR, $selectedDirectories); + + $found = true; + + foreach ($selectedDirectoriesArray as $key => $value) + { + $selectedDirectoriesArray[$key] = self::DBSKEL_DIR.$value; + + if (!in_array(self::DBSKEL_DIR.$value, $directories)) + { + $found = false; + break; + } + } + + if ($found) + { + $directories = $selectedDirectoriesArray; + } + else + { + $this->_printError('One or more of the given directories does NOT exist'); + return false; + } + } + + return true; + } + //------------------------------------------------------------------------------------------------------------------ // Private database methods @@ -602,12 +917,14 @@ class DBSkelLib } /** - * + * Retrieves all the sequences present in the given database schema */ private function _listSequencesBySchema($schema) { $sequencesArray = array(); - $query = sprintf('SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema = \'%s\'', $schema); + $query = sprintf('SELECT sequence_name + FROM information_schema.sequences + WHERE sequence_schema = \'%s\'', $schema); if ($sequences = @$this->_ci->db->query($query)) { @@ -621,12 +938,63 @@ class DBSkelLib } /** - * + * Retrieves all the tables present in the given database schema + */ + private function _listTablesBySchema($schema) + { + $tablesArray = array(); + $query = sprintf('SELECT table_name + FROM information_schema.tables + WHERE table_type = \'BASE TABLE\' + AND table_schema = \'%s\'', $schema); + + if ($tables = @$this->_ci->db->query($query)) + { + foreach ($tables->result() as $table) + { + $tablesArray[] = $table->table_name; + } + } + + return $tablesArray; + } + + /** + * Retrieves all the constraints present in the given database schema + * Returns an array with all the constraints, each element of the array is an array with two elements: + * - name: the name of the constraint + * - table: the name of the table where the constraint is applied + * NOTE: does not retrieve NOT NULL constraints + */ + private function _listConstraintsBySchema($schema) + { + $constraintsArray = array(); + $query = sprintf('SELECT constraint_name, + table_name + FROM information_schema.table_constraints + WHERE table_schema = \'%s\' + AND constraint_name NOT LIKE \'%%_not_null\'', $schema); // avoid to retrieve NOT NULL constraints + + if ($constraints = @$this->_ci->db->query($query)) + { + foreach ($constraints->result() as $constraint) + { + $constraintsArray[] = array('name' => $constraint->constraint_name, 'table' => $constraint->table_name); + } + } + + return $constraintsArray; + } + + /** + * Retrieves all the views present in the given database schema */ private function _listViewsBySchema($schema) { $viewsArray = array(); - $query = sprintf('SELECT table_name FROM information_schema.views WHERE table_schema = \'%s\'', $schema); + $query = sprintf('SELECT table_name + FROM information_schema.views + WHERE table_schema = \'%s\'', $schema); if ($views = @$this->_ci->db->query($query)) { @@ -640,465 +1008,165 @@ class DBSkelLib } /** - * + * Retrieves all the functions present in the given database schema */ private function _listFunctionsBySchema($schema) { $functionsArray = array(); - $query = sprintf('SELECT p.proname as "view_name" - FROM pg_catalog.pg_proc p - LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace - WHERE n.nspname <> \'pg_catalog\' - AND n.nspname <> \'information_schema\' - AND n.nspname = \'%s\'', $schema); + $query = sprintf('SELECT routine_name + FROM information_schema.routines + WHERE specific_schema != \'pg_catalog\' + AND specific_schema != \'information_schema\' + AND routine_schema = \'%s\'', $schema); if ($functions = @$this->_ci->db->query($query)) { foreach ($functions->result() as $function) { - $functionsArray[] = $function->view_name; + $functionsArray[] = $function->routine_name; } } return $functionsArray; } - - - - - - - - - - - - - - - - - - /** - * Check if a column exists in a table and schema + * Retrieves all the columns from a database table */ - private function _columnExists($name, $schema, $table) + private function _listColumns($schema, $table) { - $query = sprintf('SELECT %s FROM %s.%s LIMIT 1', $name, $schema, $table); + $columnsArray = array(); + $query = sprintf('SELECT * + FROM information_schema.columns + WHERE table_schema = \'%s\' + AND table_name = \'%s\'', $schema, $table); - if (@$this->_ci->db->simple_query($query)) + if ($columns = @$this->_ci->db->query($query)) { - return true; - } - - return false; - } - - /** - * Print an info about the starting of method up - */ - private function _startUP() - { - $this->eprintflib->printMessage( - sprintf('%s Start method up of class %s %s', EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) - ); - } - - /** - * Print an info about the ending of method up - */ - private function _endUP() - { - $this->eprintflib->printMessage( - sprintf('%s End method up of class %s %s', EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) - ); - } - - /** - * Print an info about the starting of method down - */ - private function _startDown() - { - $this->eprintflib->printMessage( - sprintf('%s Start method down of class %s %s', EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) - ); - } - - /** - * Print an info about the ending of method down - */ - private function _endDown() - { - $this->eprintflib->printMessage( - sprintf('%s End method down of class %s %s', EPrintfLib::SEPARATOR, get_called_class(), EPrintfLib::SEPARATOR) - ); - } - - /** - * Adds a column, with attributes, to a table and schema - */ - private function _addColumn($schema, $table, $fields) - { - foreach ($fields as $name => $definition) - { - if (!$this->columnExists($name, $schema, $table)) + foreach ($columns->result() as $column) { - if ($this->_ci->dbforge->add_column($schema.'.'.$table, array($name => $definition))) - { - $this->eprintflib->printMessage(sprintf('Column %s.%s.%s of type %s added', $schema, $table, $name, $definition['type'])); - } - else - { - $this->eprintflib->printError(sprintf('Error while adding column %s.%s.%s of type %s', $schema, $table, $name, $definition['type'])); - } - } - else - { - $this->eprintflib->printMessage(sprintf('Column %s.%s.%s already exists', $schema, $table, $name)); + $columnsArray[] = $column->routine_name; } } + + return $columnsArray; } /** - * Modifies a column, and its attributes, of a table and schema + * Creates a new table in database using the given schema and an array that defines the table structure */ - private function _modifyColumn($schema, $table, $fields) + private function _createTable($schema, $tableArray) { - foreach ($fields as $name => $definition) + $tableName = ''; + $tableComment = ''; + $tableStructure = null; + + // For each element of the table array from the php file + foreach ($tableArray as $key => $value) { - if ($this->columnExists($name, $schema, $table)) + if ($key == self::T_COMMENT) // If it is the comment element { - if ($this->_ci->dbforge->modify_column($schema.'.'.$table, array($name => $definition))) - { - $this->eprintflib->printMessage(sprintf('Column %s.%s.%s has been modified', $schema, $table, $name)); - } - else - { - $this->eprintflib->printError(sprintf('Error while modifying column %s.%s.%s', $schema, $table, $name)); - } + $tableComment = $value; } - else + else // otherwise is the table structure element { - $this->eprintflib->printMessage(sprintf('Column %s.%s.%s does NOTt exist', $schema, $table, $name)); + $tableName = $key; + $tableStructure = $value; } } - } - /** - * Drops a column from a table and schema - */ - private function _dropColumn($schema, $table, $field) - { - if ($this->columnExists($field, $schema, $table)) + // Query to create a table + $query = sprintf('CREATE TABLE %s.%s (', $schema, $tableName); + // Query to comment the table and its columns + $queryComment = sprintf('COMMENT ON TABLE %s.%s IS \'%s\';', $schema, $tableName, $tableComment); + + // For each element of the table structure + foreach ($tableStructure as $colName => $colStructure) { - if ($this->_ci->dbforge->drop_column($schema.'.'.$table, $field)) + $notNull = ''; // by default the column could be null + if (isset($colStructure[self::T_NULL]) && $colStructure[self::T_NULL] === false) { - $this->eprintflib->printMessage(sprintf('Column %s.%s.%s has been dropped', $schema, $table, $field)); + $notNull = 'NOT NULL'; // set to NOT NULL } - else + + $default = ''; // by default there is no default for a column + if (isset($colStructure[self::T_DEFAULT])) { - $this->eprintflib->printError(sprintf('Error while dropping column %s.%s.%s', $schema, $table, $field)); + $default = 'DEFAULT '.$colStructure[self::T_DEFAULT]; // set as given by the table structure + } + + // Part of the query related to this column + $query .= sprintf('%s %s %s %s,', $colName, $colStructure[self::T_TYPE], $notNull, $default); + + // If a comment is present for this column then the query is built + if (isset($colStructure[self::T_COMMENT])) + { + $queryComment .= sprintf('COMMENT ON COLUMN %s.%s.%s IS \'%s\';', $schema, $tableName, $colName, $colStructure[self::T_COMMENT]); } } - else - { - $this->eprintflib->printMessage(sprintf('Column %s.%s.%s does NOT t exist', $schema, $table, $field)); - } + + // Removes the last comma from the query + $query = substr($query, 0, strlen($query) - 1); + + // Close the round bracket + $query .= ');'; + + return $this->_execQuery($query.$queryComment); // executes query and returns its result } /** - * Sets a column as primary key of a table and schema + * TODO + * Changes the structure of a table using the given schema and an array that defines the table structure */ - private function _addPrimaryKey($schema, $table, $name, $fields) + private function _diffTable($schema, $tableArray) { - $stringFields = null; + $tableName = ''; + $tableComment = ''; + $tableStructure = null; - if (is_array($fields)) + // For each element of the table array from the php file + foreach ($tableArray as $key => $value) { - if (count($fields) > 0) + if ($key == self::T_COMMENT) // If it is the comment element { - $stringFields = ''; - for ($i = 0; $i < count($fields); $i++) - { - $stringFields .= $fields[$i]; - if ($i != count($fields) - 1) - { - $stringFields .= ', '; - } - } - $query = sprintf('ALTER TABLE %s.%s ADD CONSTRAINT %s PRIMARY KEY (%s)', $schema, $table, $name, $stringFields); + $tableComment = $value; + } + else // otherwise is the table structure element + { + $tableName = $key; + $tableStructure = $value; } } - else - { - $query = sprintf('ALTER TABLE %s.%s ADD CONSTRAINT %s PRIMARY KEY (%s)', $schema, $table, $name, $fields); - } - if (@$this->_ci->db->simple_query($query)) - { - $this->eprintflib->printMessage(sprintf('Added primary key %s on table %s.%s', $name, $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf('Adding primary key %s on table %s.%s', $name, $schema, $table)); - } - } + // Query to alter the table + $query = ''; + // Query to comment the table and its columns + $queryComment = sprintf('COMMENT ON TABLE %s.%s IS \'%s\';', $schema, $tableName, $tableComment); - /** - * Sets a column as foreign key of a table and schema - */ - private function _addForeingKey($schema, $table, $name, $field, $schemaDest, $tableDest, $fieldDest, $attributes) - { - $query = sprintf( - 'ALTER TABLE %s.%s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s.%s (%s) %s', - $schema, - $table, - $name, - $field, - $schemaDest, - $tableDest, - $fieldDest, - $attributes - ); - - if (@$this->_ci->db->simple_query($query)) + // For each element of the table structure + foreach ($tableStructure as $colName => $colStructure) { - $this->eprintflib->printMessage(sprintf('Added foreign key %s on table %s.%s', $name, $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf('Adding foreign key %s on table %s.%s', $name, $schema, $table)); - } - } - - /** - * Sets a column as unique key of a table and schema - */ - private function _addUniqueKey($schema, $table, $name, $fields) - { - $stringFields = null; - - if (is_array($fields)) - { - if (count($fields) > 0) + $notNull = ''; // by default the column could be null + if (isset($colStructure[self::T_NULL]) && $colStructure[self::T_NULL] === false) { - $stringFields = ''; - for ($i = 0; $i < count($fields); $i++) - { - $stringFields .= $fields[$i]; - if ($i != count($fields) - 1) - { - $stringFields .= ', '; - } - } - $query = sprintf('CREATE UNIQUE INDEX %s ON %s.%s (%s)', $name, $schema, $table, $stringFields); + $notNull = 'NOT NULL'; // set to NOT NULL } - } - else - { - $query = sprintf('CREATE UNIQUE INDEX %s ON %s.%s (%s)', $name, $schema, $table, $fields); - } - if (@$this->_ci->db->simple_query($query)) - { - $this->eprintflib->printMessage(sprintf('Added unique key %s on table %s.%s', $name, $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf('Adding unique key %s on table %s.%s', $name, $schema, $table)); - } - } - - /** - * Grants permissions to a user on a table and schema - */ - private function _grantTable($permissions, $schema, $table, $user) - { - $stringPermission = null; - - if (is_array($permissions)) - { - if (count($permissions) > 0) + $default = ''; // by default there is no default for a column + if (isset($colStructure[self::T_DEFAULT])) { - $stringPermission = ''; - for ($i = 0; $i < count($permissions); $i++) - { - $stringPermission .= $permissions[$i]; - if ($i != count($permissions) - 1) - { - $stringPermission .= ', '; - } - } - $query = sprintf('GRANT %s ON TABLE %s.%s TO %s', $stringPermission, $schema, $table, $user); + $default = 'DEFAULT '.$colStructure[self::T_DEFAULT]; // set as given by the table structure } - } - else - { - $query = sprintf('GRANT %s ON TABLE %s.%s TO %s', $permissions, $schema, $table, $user); - } - if (@$this->_ci->db->simple_query($query)) - { - $this->eprintflib->printMessage( - sprintf( - 'Granted permissions %s on table %s.%s to user %s', - is_null($stringPermission) ? $permissions : $stringPermission, - $schema, - $table, - $user - ) - ); - } - else - { - $this->eprintflib->printError( - sprintf( - 'Granting permissions %s on table %s.%s to user %s', - is_null($stringPermission) ? $permissions : $stringPermission, - $schema, - $table, - $user - ) - ); - } - } + // Part of the query related to this column + $query .= sprintf('%s %s %s %s,', $colName, $colStructure[self::T_TYPE], $notNull, $default); - /** - * Creates a table in a schema with columns - */ - private function _createTable($schema, $table, $fields) - { - $this->_ci->dbforge->add_field($fields); - - if ($this->_ci->dbforge->create_table($schema.'.'.$table, true)) - { - $this->eprintflib->printMessage(sprintf('Table %s.%s created or existing', $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf('Creating table %s.%s', $schema, $table)); - } - } - - /** - * Drops a table from a schema - */ - private function _dropTable($schema, $table) - { - if ($this->_ci->dbforge->drop_table($schema.'.'.$table)) - { - $this->eprintflib->printMessage(sprintf('Table %s.%s has been dropped', $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf('Dropping table %s.%s', $schema, $table)); - } - } - - /** - * Initializes a sequence with the max value of a column - */ - private function _initializeSequence($schemaSrc, $sequence, $schemaDst, $table, $field) - { - $query = sprintf('SELECT SETVAL(\'%s.%s\', (SELECT MAX(%s) FROM %s.%s))', $schemaSrc, $sequence, $field, $schemaDst, $table); - - if (@$this->_ci->db->simple_query($query)) - { - $this->eprintflib->printMessage(sprintf('Sequence %s.%s has been initialized', $schemaSrc, $sequence)); - } - else - { - $this->eprintflib->printError(sprintf('Initializing sequence %s.%s', $schemaSrc, $sequence)); - } - } - - /** - * Add comment to a column - */ - private function _commentOnColumn($schema, $table, $field, $comment) - { - $query = sprintf('COMMENT ON COLUMN %s.%s.%s IS ?', $schema, $table, $field); - - if (@$this->_ci->db->query($query, array($comment))) - { - $this->eprintflib->printMessage(sprintf('Comment added to %s.%s.%s', $schema, $table, $field)); - } - else - { - $this->eprintflib->printError(sprintf('Error while adding comment to %s.%s.%s', $schema, $table, $field)); - } - } - - /** - * Add comment to a table - */ - private function _commentOnTable($schema, $table, $comment) - { - $query = sprintf('COMMENT ON TABLE %s.%s IS ?', $schema, $table, $field); - - if (@$this->_ci->db->query($query, array($comment))) - { - $this->eprintflib->printMessage(sprintf('Comment added to %s.%s', $schema, $table)); - } - else - { - $this->eprintflib->printError(sprintf('Error while adding comment to %s.%s', $schema, $table)); - } - } - /** - * Grants permissions to a user on a sequence - */ - private function _grantSequence($permissions, $schema, $sequence, $user) - { - $stringPermission = null; - - if (is_array($permissions)) - { - if (count($permissions) > 0) + // If a comment is present for this column then the query is built + if (isset($colStructure[self::T_COMMENT])) { - $stringPermission = ''; - for ($i = 0; $i < count($permissions); $i++) - { - $stringPermission .= $permissions[$i]; - if ($i != count($permissions) - 1) - { - $stringPermission .= ', '; - } - } - $query = sprintf('GRANT %s ON SEQUENCE %s.%s TO %s', $stringPermission, $schema, $sequence, $user); + $queryComment .= sprintf('COMMENT ON COLUMN %s.%s.%s IS \'%s\';', $schema, $tableName, $colName, $colStructure[self::T_COMMENT]); } } - else - { - $query = sprintf('GRANT %s ON SEQUENCE %s.%s TO %s', $permissions, $schema, $sequence, $user); - } - - if (@$this->_ci->db->simple_query($query)) - { - $this->eprintflib->printMessage( - sprintf( - 'Granted permissions %s on sequence %s.%s to user %s', - is_null($stringPermission) ? $permissions : $stringPermission, - $schema, - $sequence, - $user - ) - ); - } - else - { - $this->eprintflib->printError( - sprintf( - 'Granting permissions %s on sequence %s.%s to user %s', - is_null($stringPermission) ? $permissions : $stringPermission, - $schema, - $sequence, - $user - ) - ); - } } //------------------------------------------------------------------------------------------------------------------ @@ -1143,4 +1211,14 @@ class DBSkelLib { $this->_printMessage('--------------------------------------------------------------------------------------------'); } + + /** + * + */ + private function _printRunningMode() + { + if ($this->_isDryrunMode()) $this->_printInfo('>> DBSkel is running in dry run mode <<'); + elseif ($this->_isNewMode()) $this->_printInfo('>> DBSkel is running in new mode <<'); + elseif ($this->_isDiffMode()) $this->_printInfo('>> DBSkel is running in diff mode <<'); + } } From 962e91a108142e2157a6ac5ee50cdcb52ea61687 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 14 Jun 2019 12:58:43 +0200 Subject: [PATCH 004/106] Added first version dbskel structure files --- application/dbskel/fue/TBL-aktivitaet.php | 27 ++++++++++++ application/dbskel/fue/constraints.php | 8 ++++ application/dbskel/fue/extra.sql | 1 + application/dbskel/fue/functions.php | 19 +++++++++ application/dbskel/fue/grants.sql | 20 +++++++++ application/dbskel/fue/schema.sql | 4 ++ application/dbskel/fue/sequences.php | 11 +++++ application/dbskel/fue/views.php | 50 +++++++++++++++++++++++ application/dbskel/index.html | 11 +++++ 9 files changed, 151 insertions(+) create mode 100644 application/dbskel/fue/TBL-aktivitaet.php create mode 100644 application/dbskel/fue/constraints.php create mode 100644 application/dbskel/fue/extra.sql create mode 100644 application/dbskel/fue/functions.php create mode 100644 application/dbskel/fue/grants.sql create mode 100644 application/dbskel/fue/schema.sql create mode 100644 application/dbskel/fue/sequences.php create mode 100644 application/dbskel/fue/views.php create mode 100644 application/dbskel/index.html diff --git a/application/dbskel/fue/TBL-aktivitaet.php b/application/dbskel/fue/TBL-aktivitaet.php new file mode 100644 index 000000000..937ca3057 --- /dev/null +++ b/application/dbskel/fue/TBL-aktivitaet.php @@ -0,0 +1,27 @@ + array( + 'aktivitaet_kurzbz' => array( + 'comment' => 'I guess this is the PK', + 'type' => 'character varying(16)', + 'null' => false + ), + 'beschreibung' => array( + 'comment' => 'none', + 'type' => 'character varying(256)', + 'null' => false, + 'default' => "'Test string'" + ), + 'sort' => array( + 'comment' => 'nope', + 'type' => 'integer', + 'default' => 1 + ), + 'oe_kurzbz' => array( + 'comment' => 'uhm', + 'type' => 'character varying(32)' + ) + ), + 'comment' => 'Timesheet SLA Activity' +); diff --git a/application/dbskel/fue/constraints.php b/application/dbskel/fue/constraints.php new file mode 100644 index 000000000..a3e534cf1 --- /dev/null +++ b/application/dbskel/fue/constraints.php @@ -0,0 +1,8 @@ + 'ALTER TABLE fue.tbl_aktivitaet ADD CONSTRAINT pk_tbl_aktivitaet PRIMARY KEY (aktivitaet_kurzbz)', + 'fk_projekt_oe' => 'ALTER TABLE fue.tbl_aktivitaet ADD CONSTRAINT fk_test FOREIGN KEY (oe_kurzbz) REFERENCES public.tbl_organisationseinheit (oe_kurzbz)', + 'uk_beschreibung' => 'ALTER TABLE fue.tbl_aktivitaet ADD CONSTRAINT uk_beschreibung UNIQUE (beschreibung)', + 'testchk' => 'ALTER TABLE fue.tbl_aktivitaet ADD CONSTRAINT testchk CHECK (sort > 0)' +); diff --git a/application/dbskel/fue/extra.sql b/application/dbskel/fue/extra.sql new file mode 100644 index 000000000..3d826b40f --- /dev/null +++ b/application/dbskel/fue/extra.sql @@ -0,0 +1 @@ +SELECT 'Extra file' AS justatest; diff --git a/application/dbskel/fue/functions.php b/application/dbskel/fue/functions.php new file mode 100644 index 000000000..f3dab4498 --- /dev/null +++ b/application/dbskel/fue/functions.php @@ -0,0 +1,19 @@ + + 'CREATE OR REPLACE FUNCTION fue.get_highest_content_version(bigint) RETURNS smallint AS $$ + DECLARE i_content_id ALIAS FOR $1; + DECLARE rec RECORD; + BEGIN + + SELECT INTO rec version + FROM campus.tbl_contentsprache + WHERE content_id = i_content_id + ORDER BY version desc + LIMIT 1; + + RETURN rec.version; + END; + $$ LANGUAGE plpgsql;' +); diff --git a/application/dbskel/fue/grants.sql b/application/dbskel/fue/grants.sql new file mode 100644 index 000000000..cdc8c9c95 --- /dev/null +++ b/application/dbskel/fue/grants.sql @@ -0,0 +1,20 @@ +----------------------------------------------------- +-- Revokes all privileges from all granted users +----------------------------------------------------- +REVOKE ALL PRIVILEGES ON SCHEMA fue FROM vilesci; +REVOKE ALL ON ALL TABLES IN SCHEMA fue FROM vilesci; +REVOKE ALL ON ALL SEQUENCES IN SCHEMA fue FROM vilesci; +REVOKE ALL ON ALL FUNCTIONS IN SCHEMA fue FROM vilesci; + +---------------------------------------------------------------------------------------------------- +-- Gives the desired privileges to the chosen users (with great power comes great responsibility!) +---------------------------------------------------------------------------------------------------- + +-- Schema privileges +GRANT ALL ON SCHEMA fue TO vilesci; +GRANT USAGE ON SCHEMA fue TO web; +GRANT USAGE ON SCHEMA fue TO wawi; + +-- Sequences privileges +GRANT SELECT,UPDATE ON SEQUENCE fue.seq_projekt_dokument_projekt_dokument_id TO vilesci; +GRANT SELECT,UPDATE ON SEQUENCE fue.seq_projekt_dokument_projekt_dokument_id TO web; diff --git a/application/dbskel/fue/schema.sql b/application/dbskel/fue/schema.sql new file mode 100644 index 000000000..1dc28d796 --- /dev/null +++ b/application/dbskel/fue/schema.sql @@ -0,0 +1,4 @@ +-- Create the schema if not exists +CREATE SCHEMA IF NOT EXISTS fue; +-- Comment schema +COMMENT ON SCHEMA fue IS 'Projectmanagement'; diff --git a/application/dbskel/fue/sequences.php b/application/dbskel/fue/sequences.php new file mode 100644 index 000000000..98a1ca60b --- /dev/null +++ b/application/dbskel/fue/sequences.php @@ -0,0 +1,11 @@ + + 'CREATE SEQUENCE fue.seq_projekt_dokument_projekt_dokument_id + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1;' +); diff --git a/application/dbskel/fue/views.php b/application/dbskel/fue/views.php new file mode 100644 index 000000000..69b911714 --- /dev/null +++ b/application/dbskel/fue/views.php @@ -0,0 +1,50 @@ + + 'CREATE OR REPLACE VIEW fue.vw_projektressourcen ( + projekt_ressource_id, + projekt_kurzbz, + projektphase_id, + projektphase, + typ, + ressource_id, + ressource, + funktion_kurzbz, + start, + ende, + oe_kurzbz, + projektbudget, + aufwandstyp_kurzbz, + projektphase_fk, + phasenbudget, + personentage, + nummer, + titel, + aufwand + ) + AS + SELECT tpr.projekt_ressource_id, + COALESCE(tpr.projekt_kurzbz, tpp.projekt_kurzbz) AS projekt_kurzbz, + tpr.projektphase_id, + tpp.bezeichnung AS projektphase, + COALESCE(tpp.typ, \'Projekt\'::character varying) AS typ, + tpr.ressource_id, + tr.bezeichnung AS ressource, + tpr.funktion_kurzbz, + COALESCE(tpp.start, tp.beginn) AS start, + COALESCE(tpp.ende, tp.ende) AS ende, + tp.oe_kurzbz, + tp.budget AS projektbudget, + tp.aufwandstyp_kurzbz, + tpp.projektphase_fk, + tpp.budget AS phasenbudget, + tpp.personentage, + tp.nummer, + tp.titel, + tpr.aufwand + FROM fue.tbl_projekt_ressource tpr + JOIN fue.tbl_ressource tr USING (ressource_id) + LEFT JOIN fue.tbl_projekt tp USING (projekt_kurzbz) + LEFT JOIN fue.tbl_projektphase tpp USING (projektphase_id);' +); diff --git a/application/dbskel/index.html b/application/dbskel/index.html new file mode 100644 index 000000000..b702fbc39 --- /dev/null +++ b/application/dbskel/index.html @@ -0,0 +1,11 @@ + + + + 403 Forbidden + + + +

Directory access is forbidden.

+ + + From ddaa129bab09adec06e9e9715406811d34fb7343 Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 4 Jul 2019 17:25:13 +0200 Subject: [PATCH 005/106] - Removed controllers api/v1/system/CallerLibrary.php and api/v1/system/CallerModel.php - Removed library libraries/CallerLib.php --- .../api/v1/system/CallerLibrary.php | 77 ---- .../controllers/api/v1/system/CallerModel.php | 77 ---- application/libraries/CallerLib.php | 361 ------------------ 3 files changed, 515 deletions(-) delete mode 100644 application/controllers/api/v1/system/CallerLibrary.php delete mode 100644 application/controllers/api/v1/system/CallerModel.php delete mode 100644 application/libraries/CallerLib.php diff --git a/application/controllers/api/v1/system/CallerLibrary.php b/application/controllers/api/v1/system/CallerLibrary.php deleted file mode 100644 index 594786384..000000000 --- a/application/controllers/api/v1/system/CallerLibrary.php +++ /dev/null @@ -1,77 +0,0 @@ - 'admin:rw')); - - // Loads the CallerLib - $this->load->library('CallerLib'); - } - - /** - * Manages a HTTP get call - */ - public function getCall() - { - // Start me up! - $result = $this->callerlib->callLibrary($this->get()); - - // Print the result - $this->response($result, REST_Controller::HTTP_OK); - } - - /** - * @return void - */ - public function postCall() - { - // Start me up! - $result = $this->callerlib->callLibrary($this->post()); - - // Print the result - $this->response($result, REST_Controller::HTTP_OK); - } - - /** - * @return void - */ - public function putCall() - { - // Start me up! - $result = $this->callerlib->callLibrary($this->put()); - - // Print the result - $this->response($result, REST_Controller::HTTP_OK); - } - - /** - * @return void - */ - public function deleteCall() - { - // Start me up! - $result = $this->callerlib->callLibrary($this->delete()); - - // Print the result - $this->response($result, REST_Controller::HTTP_OK); - } -} diff --git a/application/controllers/api/v1/system/CallerModel.php b/application/controllers/api/v1/system/CallerModel.php deleted file mode 100644 index 68296aff8..000000000 --- a/application/controllers/api/v1/system/CallerModel.php +++ /dev/null @@ -1,77 +0,0 @@ - 'admin:rw')); - - // Loads the CallerLib - $this->load->library('CallerLib'); - } - - /** - * Manages a HTTP get call - */ - public function getCall() - { - // Start me up! - $result = $this->callerlib->callModel($this->get()); - - // Print the result - $this->response($result, REST_Controller::HTTP_OK); - } - - /** - * @return void - */ - public function postCall() - { - // Start me up! - $result = $this->callerlib->callModel($this->post()); - - // Print the result - $this->response($result, REST_Controller::HTTP_OK); - } - - /** - * @return void - */ - public function putCall() - { - // Start me up! - $result = $this->callerlib->callModel($this->put()); - - // Print the result - $this->response($result, REST_Controller::HTTP_OK); - } - - /** - * @return void - */ - public function deleteCall() - { - // Start me up! - $result = $this->callerlib->callModel($this->delete()); - - // Print the result - $this->response($result, REST_Controller::HTTP_OK); - } -} diff --git a/application/libraries/CallerLib.php b/application/libraries/CallerLib.php deleted file mode 100644 index 0b46cf0c6..000000000 --- a/application/libraries/CallerLib.php +++ /dev/null @@ -1,361 +0,0 @@ -_ci =& get_instance(); // Gets CI instance - } - - /** - * Wrapper method for _call - */ - public function callLibrary($callParameters) - { - return $this->_call($callParameters); - } - - /** - * Wrapper method for _call - */ - public function callModel($callParameters) - { - return $this->_call($callParameters); - } - - /** - * Everything starts here... - */ - private function _call($callParameters) - { - $result = null; - $parameters = $this->_getParameters($callParameters); - $validation = $this->_validateCall($parameters); - - // If the validation was passed - if (isSuccess($validation)) - { - $loaded = null; - // If the given resource is a model - if (strpos($parameters->resourceName, CallerLib::MODEL_PREFIX) !== false) - { - // Try to load the model - $result = $this->_loadModel($parameters->resourcePath, $parameters->resourceName); - if (isSuccess($result)) - { - $loaded = $result->retval; - } - } - // If the given resource is a library - elseif (strpos($parameters->resourceName, CallerLib::LIB_PREFIX) !== false) - { - // Check if the resource is already loaded, it works only with libraries and drivers - $isLoaded = $this->_ci->load->is_loaded($parameters->resourceName); - // If not loaded then load it - if ($isLoaded === false) - { - // Try to load the library - $result = $this->_loadLibrary($parameters->resourcePath, $parameters->resourceName); - if (isSuccess($result)) - { - $loaded = $result->retval; - } - } - // If it is already loaded $isLoaded contains the instance of the library - else - { - $loaded = $isLoaded; - } - } - // Wrong selection! - else - { - $result = error('Neither a lib nor model: '.$parameters->resourcePath.$parameters->resourceName); - } - - // If the resource was found and loaded - if (!is_null($loaded)) - { - $result = $this->_callThis($parameters->resourceName, $parameters->function, $parameters->parameters); - } - else - { - // Resource not loaded - } - } - else - { - $result = $validation; - } - - return $result; - } - - /** - * Gets the parameters from the http call - * Search for parameters and - * is the name of the model or of the library - * is the name of the method present in the model/library - * All the others parameters will be given to the method in the same order that - * they are present in the HTTP call - * EX: - * URL: ../system/CallerLibrary/Call?resource=&function=&=&=&= - * will call .(par1, par2, par3) - */ - private function _getParameters($parametersArray) - { - $parameters = new stdClass(); - $parameters->parameters = array(); - $count = 0; - - foreach ($parametersArray as $parameterName => $parameterValue) - { - // The name of the resource, path included - if ($parameterName == CallerLib::RESOURCE_PARAMETER) - { - // Separates the resource path from the resource name - $splittedResource = preg_split(CallerLib::REG_SPLIT_EXPR, $parameterValue); - $parameters->resourceName = $splittedResource[count($splittedResource) - 1]; - $parameters->resourcePath = str_replace($parameters->resourceName, '', $parameterValue); - } - // The name of the function - elseif ($parameterName == CallerLib::FUNCTION_PARAMETER) - { - $parameters->function = $parameterValue; - } - // It is assumed that all other parameters are the parameters to be passed to the function - // They will be passed to the function in the same order in which they are passed to - // this controller - else - { - $parameters->parameters[$count++] = $parameterValue; - } - } - - return $parameters; - } - - /** - * Validate the given parameters - */ - private function _validateCall($parameters) - { - if (!is_object($parameters)) - { - return error('Parameter is not an object'); - } - if (!isset($parameters->resourcePath)) - { - return error('Resource path is not specified'); - } - if (!isset($parameters->resourceName)) - { - return error('Resource name is not specified'); - } - if (!isset($parameters->function)) - { - return error('Function is not specified'); - } - if (!is_array($parameters->parameters)) - { - return error('Parameters are not specified'); - } - if (in_array($parameters->resourceName, CallerLib::$RESOURCES_BLACK_LIST)) - { - return error('You are trying to access to unauthorized resources'); - } - - return success('Input data are valid'); - } - - /** - * Loads a model using the given path and name - * - * NOTE: the models automatically handle the permissions - */ - private function _loadModel($resourcePath, $resourceName) - { - $loaded = null; - $result = null; - - try - { - $loaded = $this->_ci->load->model($resourcePath.$resourceName); - } - catch (Exception $e) - { - // Errors while loading the model - $result = error('Errors while loading the model: '.$e->getMessage()); - } - - if (!is_null($loaded)) - { - $result = success($loaded); - } - - return $result; - } - - /** - * Loads a library using the given path and name - * - * The method 'library' of the class CI_Loader provided by CI has some limitations, - * so to be able to check errors was used a workaround. - * It consists in: - * - Checking if the file (identified by parameters $resourcePath and $resourceName) exists - * - If exists it will be loaded using the method 'file' from CI_Loader - * - Checks if the loaded file contains a class identified by parameter $resourceName - * - * If one of the previous tests fails, it will be returned a null value - */ - private function _loadLibrary($resourcePath, $resourceName) - { - $loaded = null; - - try - { - // Gets all the configured resources paths - $packagePaths = $this->_ci->load->get_package_paths(); - // Looking for a file in every paths with the same name of the resource - $found = null; - for ($i = 0; $i < count($packagePaths) && is_null($found); $i++) - { - $file = $packagePaths[$i].CallerLib::LIBS_PATH.DIRECTORY_SEPARATOR. - $resourcePath.$resourceName.CallerLib::LIB_FILE_EXTENSION; - if (file_exists($file)) - { - $found = $file; - } - } - - // If the file was found - if (!is_null($found)) - { - // Load the file - $loaded = $this->_ci->load->file($found); - // If the resource is not present inside the file - if (!class_exists($resourceName)) - { - $loaded = null; - // Same phrase error as load->model() provided by CI - $result = error($found.' exists, but doesn\'t declare class '.$resourceName); - } - } - else - { - $loaded = null; - // Same phrase error as load->model() provided by CI - $result = error('Unable to load the requested class: '.$resourceName); - } - } - catch (Exception $e) - { - // Errors while loading the library - $result = error('Errors while loading the library: '.$e->getMessage()); - } - - if (!is_null($loaded)) - { - $result = success($loaded); - } - - return $result; - } - - /** - * Calls a method of a class with the given parameters and returns its result - * - * @param string $resourceName identifies the class name - * @param string $function identifies the method name - * @param array $parameters contains the parameters to be passed to the method - */ - private function _callThis($resourceName, $function, $parameters) - { - $result = null; - - try - { - // Get informations about the function - $reflectionMethod = new ReflectionMethod($resourceName, $function); - // If the number of given parameters is greater or equal to the number of - // parameters required by the function - if (count($parameters) >= $reflectionMethod->getNumberOfRequiredParameters()) - { - // If the function is static - if ($reflectionMethod->isStatic() === true) - { - $classMethod = $resourceName.'::'.$function; - } - // If the function is not static - else - { - $classMethod = array(new $resourceName(), $function); - } - - // If the resource's function is callable - if (is_callable($classMethod)) - { - // Call resource->function() - // @ was applied to prevent really ugly and unmanageable errors - $resultCall = @call_user_func_array($classMethod, $parameters); - // If errors occurred while running it - // NOTE: if the called function via call_user_func_array returns a boolean set as false, - // it will be recognized like a running error. A little bit tricky ;) - if ($resultCall === false) - { - $result = error('Error running '.$resourceName.'->'.$function.'()'); - } - // Returns the result of resource->function() - else - { - $result = success($resultCall); - } - } - else - { - $result = error($resourceName.'->'.$function.'() is not callable!'); - } - } - else - { - $result = error( - 'Number of required parameters: '.$reflectionMethod->getNumberOfRequiredParameters().'. Given: '.count($parameters) - ); - } - } - catch (Exception $e) - { - $result = error($e->getMessage()); - } - - return $result; - } -} From e9e58decee0752f0e6ed1a453363901317f8fa67 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 5 Jul 2019 11:56:43 +0200 Subject: [PATCH 006/106] Changed file naming convention for table files --- .../dbskel/fue/{TBL-aktivitaet.php => TBL_aktivitaet.php} | 0 application/libraries/DBSkelLib.php | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename application/dbskel/fue/{TBL-aktivitaet.php => TBL_aktivitaet.php} (100%) diff --git a/application/dbskel/fue/TBL-aktivitaet.php b/application/dbskel/fue/TBL_aktivitaet.php similarity index 100% rename from application/dbskel/fue/TBL-aktivitaet.php rename to application/dbskel/fue/TBL_aktivitaet.php diff --git a/application/libraries/DBSkelLib.php b/application/libraries/DBSkelLib.php index df8a71a6c..4a52f44d6 100644 --- a/application/libraries/DBSkelLib.php +++ b/application/libraries/DBSkelLib.php @@ -32,7 +32,7 @@ class DBSkelLib // Configuration file names const SCHEMA_FILENAME = 'schema.sql'; // File name that contains schema creation SQL and SQL to comment a schema const SEQUENCES_FILENAME = 'sequences.php'; // PHP file that contains all the sequences - const TABLE_PREFIX = 'TBL-'; // Table file prefix + const TABLE_PREFIX = 'TBL_'; // Table file prefix const CONSTRAINTS_FILENAME = 'constraints.php'; // PHP file that contains all the constraints const VIEWS_FILENAME = 'views.php'; // PHP file that contains all the views const FUNCTIONS_FILENAME = 'functions.php'; // PHP file that contains all the functions From 2f846796b6eb6e0c48017667d5317db1d7f7b2e3 Mon Sep 17 00:00:00 2001 From: Paolo Date: Fri, 5 Jul 2019 17:29:48 +0200 Subject: [PATCH 007/106] Work in prorgess on _manageTableColumns --- application/dbskel/fue/TBL_aktivitaet.php | 2 +- application/libraries/DBSkelLib.php | 79 +++++++++++++++-------- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/application/dbskel/fue/TBL_aktivitaet.php b/application/dbskel/fue/TBL_aktivitaet.php index 937ca3057..05027ba92 100644 --- a/application/dbskel/fue/TBL_aktivitaet.php +++ b/application/dbskel/fue/TBL_aktivitaet.php @@ -1,7 +1,7 @@ array( + 'tbl_aktivitaet' => array( 'aktivitaet_kurzbz' => array( 'comment' => 'I guess this is the PK', 'type' => 'character varying(16)', diff --git a/application/libraries/DBSkelLib.php b/application/libraries/DBSkelLib.php index 4a52f44d6..06130cf50 100644 --- a/application/libraries/DBSkelLib.php +++ b/application/libraries/DBSkelLib.php @@ -442,23 +442,10 @@ class DBSkelLib } else // if table is already present in database { - if ($this->_isNewMode()) // only if in new mode - { - $this->_printMessage('Table already present in database: '.$tableName); - } - elseif ($this->_isDiffMode()) // only if in diff mode - { - // Then diff the already present table with the one from php file! If it fails then ends execution - if ($this->_diffTable($schema, $tableArray)) - { - $this->_printMessage('Table diff success: '.$tableName); - } - else - { - $this->_printError('Error occurred while diff table: '.$tableName); - return false; - } - } + $this->_printMessage('Table already present in database: '.$tableName); + + // Manage the differences between the table present in database and the one present in php file + return $this->_manageTableColumns($schema, $tableArray); } } else // otherwise the array present in the php table file is not well formatted @@ -1036,10 +1023,15 @@ class DBSkelLib private function _listColumns($schema, $table) { $columnsArray = array(); - $query = sprintf('SELECT * + $query = sprintf('SELECT column_name AS name, + data_type AS type, + column_default AS default, + is_nullable AS nullable, + character_maximum_length AS string_length, + numeric_precision AS number_length FROM information_schema.columns WHERE table_schema = \'%s\' - AND table_name = \'%s\'', $schema, $table); + AND table_name = \'%s\'', $schema, $table); if ($columns = @$this->_ci->db->query($query)) { @@ -1118,7 +1110,7 @@ class DBSkelLib * TODO * Changes the structure of a table using the given schema and an array that defines the table structure */ - private function _diffTable($schema, $tableArray) + private function _manageTableColumns($schema, $tableArray) { $tableName = ''; $tableComment = ''; @@ -1138,10 +1130,27 @@ class DBSkelLib } } - // Query to alter the table - $query = ''; - // Query to comment the table and its columns + // Comments the table $queryComment = sprintf('COMMENT ON TABLE %s.%s IS \'%s\';', $schema, $tableName, $tableComment); + if ($this->_isDryrunMode()) + { + $this->_printInfo('Dry run >> table .'$tableName'. would be commented with: '.$queryComment); + } + else // new and diff mode + { + if (!$this->_execQuery($queryComment)) + { + $this->_printError('Error occurred while commenting table: '.$tableName); + return false; + } + else + { + $this->_printMessage('Table successfully commented: '.$tableName); + } + } + + // Retrieves the list of columns and their attributes from database + $dbTableColumns = $this->_listColumns($schema, $tableName); // For each element of the table structure foreach ($tableStructure as $colName => $colStructure) @@ -1159,14 +1168,32 @@ class DBSkelLib } // Part of the query related to this column - $query .= sprintf('%s %s %s %s,', $colName, $colStructure[self::T_TYPE], $notNull, $default); + $query = sprintf('%s %s %s %s,', $colName, $colStructure[self::T_TYPE], $notNull, $default); - // If a comment is present for this column then the query is built + // Comments a column if (isset($colStructure[self::T_COMMENT])) { - $queryComment .= sprintf('COMMENT ON COLUMN %s.%s.%s IS \'%s\';', $schema, $tableName, $colName, $colStructure[self::T_COMMENT]); + if ($this->_isDryrunMode()) + { + $this->_printInfo('Dry run >> column '$tableName.'.'.$colName.' would be commented with: '.$colStructure[self::T_COMMENT]); + } + else // new and diff mode + { + $queryComment = sprintf('COMMENT ON COLUMN %s.%s.%s IS \'%s\';', $schema, $tableName, $colName, $colStructure[self::T_COMMENT]); + if (!$this->_execQuery($queryComment)) + { + $this->_printError('Error occurred while commenting column: '.$tableName.'.'.$colName); + return false; + } + else + { + $this->_printMessage('Column successfully commented: '.$tableName.'.'.$colName); + } + } } } + + return true; } //------------------------------------------------------------------------------------------------------------------ From 61e21d605ef62cb43bf982441f7115e72aa0184d Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 8 Jul 2019 10:47:54 +0200 Subject: [PATCH 008/106] Fixes --- application/libraries/DBSkelLib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/libraries/DBSkelLib.php b/application/libraries/DBSkelLib.php index 06130cf50..1d556d9a4 100644 --- a/application/libraries/DBSkelLib.php +++ b/application/libraries/DBSkelLib.php @@ -1134,7 +1134,7 @@ class DBSkelLib $queryComment = sprintf('COMMENT ON TABLE %s.%s IS \'%s\';', $schema, $tableName, $tableComment); if ($this->_isDryrunMode()) { - $this->_printInfo('Dry run >> table .'$tableName'. would be commented with: '.$queryComment); + $this->_printInfo('Dry run >> table '.$tableName.' would be commented with: '.$queryComment); } else // new and diff mode { @@ -1175,7 +1175,7 @@ class DBSkelLib { if ($this->_isDryrunMode()) { - $this->_printInfo('Dry run >> column '$tableName.'.'.$colName.' would be commented with: '.$colStructure[self::T_COMMENT]); + $this->_printInfo('Dry run >> column '.$tableName.'.'.$colName.' would be commented with: '.$colStructure[self::T_COMMENT]); } else // new and diff mode { From 2c773b703523958a1dd06f2b01e3b02815193b99 Mon Sep 17 00:00:00 2001 From: Manfred Kindl Date: Wed, 10 Jul 2019 18:13:18 +0200 Subject: [PATCH 009/106] SQL der Auswertung angepasst. Buttons eingebaut --- content/student/aufnahmetermine.js.php | 6 ++++++ content/student/aufnahmetermine.xul.php | 2 ++ include/pruefling.class.php | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/content/student/aufnahmetermine.js.php b/content/student/aufnahmetermine.js.php index e77980654..17674eadf 100644 --- a/content/student/aufnahmetermine.js.php +++ b/content/student/aufnahmetermine.js.php @@ -350,6 +350,12 @@ function AufnahemTermineReihungstestPunkteTransmit() } } +function setEndpunkteAsPunkte(id) +{ + var punkte = document.getElementById(id).value; + document.getElementById('aufnahmetermine-textbox-punkte').value = punkte; +} + /** * Speichert einen AufnahmeTermin */ diff --git a/content/student/aufnahmetermine.xul.php b/content/student/aufnahmetermine.xul.php index fe0cff1c4..d8e88294f 100644 --- a/content/student/aufnahmetermine.xul.php +++ b/content/student/aufnahmetermine.xul.php @@ -303,12 +303,14 @@ echo ']>