diff --git a/application/libraries/MigrationLib.php b/application/libraries/MigrationLib.php index a27670a3b..0a9d1dfef 100644 --- a/application/libraries/MigrationLib.php +++ b/application/libraries/MigrationLib.php @@ -257,6 +257,44 @@ class MigrationLib extends CI_Migration } } + /** + * 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->printMessage(sprintf("Added unique key %s on table %s.%s", $name, $schema, $table)); + } + else + { + $this->printError(sprintf("Adding unique key %s on table %s.%s", $name, $schema, $table)); + } + } + /** * Grants permissions to a user on a table and schema */ @@ -356,6 +394,90 @@ class MigrationLib extends CI_Migration } } + /** + * 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->printMessage(sprintf("Comment added to %s.%s.%s", $schema, $table, $field)); + } + else + { + $this->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->printMessage(sprintf("Comment added to %s.%s", $schema, $table)); + } + else + { + $this->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->printMessage( + sprintf("Granted permissions %s on sequence %s.%s to user %s", + is_null($stringPermission) ? $permissions : $stringPermission, + $schema, + $sequence, + $user + )); + } + else + { + $this->printError( + sprintf("Granting permissions %s on sequence %s.%s to user %s", + is_null($stringPermission) ? $permissions : $stringPermission, + $schema, + $sequence, + $user + )); + } + } + + /** * Executes the given query */ diff --git a/application/migrations/008_message.php b/application/migrations/008_message.php index 091d51add..5f3e65f33 100644 --- a/application/migrations/008_message.php +++ b/application/migrations/008_message.php @@ -1,99 +1,294 @@ startUP(); + + // Create table public.tbl_msg_message + $fields = array( + "message_id" => array( + "type" => "serial" + ), + "person_id" => array( + "type" => "bigint" + ), + "subject" => array( + "type" => "varchar(256)", + "null" => false + ), + "body" => array( + "type" => "text", + "null" => false + ), + "priority" => array( + "type" => "smallint DEFAULT 0", + "null" => false + ), + "relationmessage_id" => array( + "type" => "bigint", + "null" => true + ), + "oe_kurzbz" => array( + "type" => "varchar(32)", + "null" => true + ), + "insertamum" => array( + "type" => "timestamp DEFAULT NOW()", + "null" => false + ), + "insertvon" => array( + "type" => "varchar(32)", + "null" => true + ) + ); + $this->createTable("public", "tbl_msg_message", $fields); + $this->addPrimaryKey( + "public", + "tbl_msg_message", + "pk_tbl_msg_message", + array("message_id") + ); + $this->addForeingKey( + "public", + "tbl_msg_message", + "fk_tbl_msg_message_person_id", + "person_id", + "public", + "tbl_person", + "person_id", + "ON UPDATE CASCADE ON DELETE RESTRICT" + ); + $this->addForeingKey( + "public", + "tbl_msg_message", + "fk_tbl_msg_message_relationmessage_id", + "relationmessage_id", + "public", + "tbl_msg_message", + "message_id", + "ON UPDATE CASCADE ON DELETE RESTRICT" + ); + $this->addForeingKey( + "public", + "tbl_msg_message", + "fk_tbl_msg_message_oe_kurzbz", + "oe_kurzbz", + "public", + "tbl_organisationseinheit", + "oe_kurzbz", + "ON UPDATE CASCADE ON DELETE RESTRICT" + ); + $this->addCommentToColumn("public", "tbl_msg_message", "person_id", "Sender"); + $this->addCommentToColumn("public", "tbl_msg_message", "priority", "Codex in config/message.php"); + $this->grantTable("SELECT", "public", "tbl_msg_message", "web"); + $this->grantTable(array("SELECT", "INSERT", "DELETE", "UPDATE"), "public", "tbl_msg_message", "admin"); + $this->grantTable(array("SELECT", "INSERT", "DELETE", "UPDATE"), "public", "tbl_msg_message", "vilesci"); + $this->grantSequence(array("SELECT", "UPDATE"), "public", "tbl_msg_message_message_id_seq", "web"); + $this->grantSequence(array("SELECT", "UPDATE"), "public", "tbl_msg_message_message_id_seq", "admin"); + $this->grantSequence(array("SELECT", "UPDATE"), "public", "tbl_msg_message_message_id_seq", "vilesci"); + + // Create table public.tbl_msg_recipient + $fields = array( + "person_id" => array( + "type" => "bigint", + "null" => false + ), + "message_id" => array( + "type" => "bigint", + "null" => false + ), + "token" => array( + "type" => "varchar(128)", + "null" => true + ), + "sent" => array( + "type" => "timestamp DEFAULT NULL", + "null" => true + ), + "sentinfo" => array( + "type" => "text DEFAULT NULL", + "null" => true + ), + "insertamum" => array( + "type" => "timestamp DEFAULT NOW()", + "null" => false + ), + "insertvon" => array( + "type" => "varchar(32)", + "null" => true + ) + ); + $this->createTable("public", "tbl_msg_recipient", $fields); + $this->addPrimaryKey( + "public", + "tbl_msg_recipient", + "pk_tbl_msg_recipient", + array("person_id", "message_id") + ); + $this->addForeingKey( + "public", + "tbl_msg_recipient", + "fk_tbl_msg_recipient_person_id", + "person_id", + "public", + "tbl_person", + "person_id", + "ON UPDATE CASCADE ON DELETE RESTRICT" + ); + $this->addForeingKey( + "public", + "tbl_msg_recipient", + "fk_tbl_msg_recipient_message_id", + "message_id", + "public", + "tbl_msg_message", + "message_id", + "ON UPDATE CASCADE ON DELETE RESTRICT" + ); + $this->addUniqueKey( + "public", + "tbl_msg_recipient", + "uk_tbl_msg_recipient_token", + array("token") + ); + $this->addCommentToColumn("public", "tbl_msg_recipient", "person_id", "Receiver"); + $this->addCommentToColumn("public", "tbl_msg_recipient", "sent", "If NULL not sent, otherwise the shipping date"); + $this->grantTable("SELECT", "public", "tbl_msg_recipient", "web"); + $this->grantTable(array("SELECT", "INSERT", "DELETE", "UPDATE"), "public", "tbl_msg_recipient", "admin"); + $this->grantTable(array("SELECT", "INSERT", "DELETE", "UPDATE"), "public", "tbl_msg_recipient", "vilesci"); + + // Create table public.tbl_msg_status + $fields = array( + "message_id" => array( + "type" => "bigint", + "null" => false + ), + "person_id" => array( + "type" => "bigint", + "null" => false + ), + "status" => array( + "type" => "smallint", + "null" => false + ), + "statusinfo" => array( + "type" => "text", + "null" => true + ), + "insertamum" => array( + "type" => "timestamp DEFAULT NOW()", + "null" => false + ), + "insertvon" => array( + "type" => "varchar(32)", + "null" => true + ), + "updateamum" => array( + "type" => "timestamp DEFAULT NOW()", + "null" => false + ), + "updatevon" => array( + "type" => "varchar(32)", + "null" => true + ) + ); + $this->createTable("public", "tbl_msg_status", $fields); + $this->addPrimaryKey( + "public", + "tbl_msg_status", + "pk_tbl_msg_status", + array("message_id", "person_id", "status") + ); + $this->addForeingKey( + "public", + "tbl_msg_status", + "fk_tbl_msg_status_person_id", + "person_id", + "public", + "tbl_person", + "person_id", + "ON UPDATE CASCADE ON DELETE RESTRICT" + ); + $this->addForeingKey( + "public", + "tbl_msg_status", + "fk_tbl_msg_status_message_id", + "message_id", + "public", + "tbl_msg_message", + "message_id", + "ON UPDATE CASCADE ON DELETE RESTRICT" + ); + $this->addCommentToColumn("public", "tbl_msg_status", "person_id", "Receiver"); + $this->grantTable("SELECT", "public", "tbl_msg_status", "web"); + $this->grantTable(array("SELECT", "INSERT", "DELETE", "UPDATE"), "public", "tbl_msg_status", "admin"); + $this->grantTable(array("SELECT", "INSERT", "DELETE", "UPDATE"), "public", "tbl_msg_status", "vilesci"); + + // Create table public.tbl_msg_attachment + $fields = array( + "attachment_id" => array( + "type" => "serial" + ), + "message_id" => array( + "type" => "bigint", + "null" => false + ), + "name" => array( + "type" => "text", + "null" => true + ), + "filename" => array( + "type" => "text", + "null" => true + ) + ); + $this->createTable("public", "tbl_msg_attachment", $fields); + $this->addPrimaryKey( + "public", + "tbl_msg_attachment", + "pk_tbl_msg_attachment", + array("attachment_id") + ); + $this->addForeingKey( + "public", + "tbl_msg_attachment", + "fk_tbl_msg_attachment_message_id", + "message_id", + "public", + "tbl_msg_message", + "message_id", + "ON UPDATE CASCADE ON DELETE RESTRICT" + ); + $this->grantTable("SELECT", "public", "tbl_msg_attachment", "web"); + $this->grantTable(array("SELECT", "INSERT", "DELETE", "UPDATE"), "public", "tbl_msg_attachment", "admin"); + $this->grantTable(array("SELECT", "INSERT", "DELETE", "UPDATE"), "public", "tbl_msg_attachment", "vilesci"); + $this->grantSequence(array("SELECT", "UPDATE"), "public", "tbl_msg_attachment_attachment_id_seq", "web"); + $this->grantSequence(array("SELECT", "UPDATE"), "public", "tbl_msg_attachment_attachment_id_seq", "admin"); + $this->grantSequence(array("SELECT", "UPDATE"), "public", "tbl_msg_attachment_attachment_id_seq", "vilesci"); + + $this->endUP(); + } - public function up() - { - if (! $this->db->table_exists('public.tbl_msg_message')) - { - $query= " - CREATE TABLE public.tbl_msg_message ( - message_id serial, - person_id bigint NOT NULL references public.tbl_person(person_id), - subject varchar(256) NOT NULL, - body text NOT NULL, - priority smallint NOT NULL DEFAULT 0, - relationmessage_id bigint references public.tbl_msg_message(message_id), - oe_kurzbz varchar(32) references public.tbl_organisationseinheit(oe_kurzbz), - insertamum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - insertvon varchar(32), - PRIMARY KEY (message_id) - ); - COMMENT ON COLUMN public.tbl_msg_message.person_id IS 'Sender'; - COMMENT ON COLUMN public.tbl_msg_message.priority IS 'Codex in config/message.php'; - GRANT SELECT ON TABLE public.tbl_msg_message TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_message TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_message TO vilesci; - GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_message_message_id_seq TO web; - GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_message_message_id_seq TO admin; - GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_message_message_id_seq TO vilesci; + public function down() + { + $this->startDown(); - CREATE TABLE public.tbl_msg_recipient ( - person_id bigint NOT NULL references public.tbl_person(person_id), - message_id bigint NOT NULL references public.tbl_msg_message(message_id), - token varchar(128) UNIQUE, - insertamum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - insertvon varchar(32), - PRIMARY KEY (person_id,message_id) - ); - GRANT SELECT ON TABLE public.tbl_msg_recipient TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_recipient TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_recipient TO vilesci; - - CREATE TABLE public.tbl_msg_status ( - message_id bigint NOT NULL references public.tbl_msg_message(message_id), - person_id bigint NOT NULL references public.tbl_person(person_id), - status smallint NOT NULL, - statusinfo text, - insertamum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - insertvon varchar(32), - updateamum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - updatevon varchar(32), - PRIMARY KEY (message_id,person_id, status) - ); - GRANT SELECT ON TABLE public.tbl_msg_status TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_status TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_status TO vilesci; - - CREATE TABLE public.tbl_msg_attachment ( - attachment_id serial, - message_id bigint NOT NULL references public.tbl_msg_message(message_id), - name text, - filename text, - PRIMARY KEY (attachment_id) - ); - GRANT SELECT ON TABLE public.tbl_msg_attachment TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_attachment TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_msg_attachment TO vilesci; - GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_attachment_attachment_id_seq TO web; - GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_attachment_attachment_id_seq TO admin; - GRANT SELECT, UPDATE ON SEQUENCE public.tbl_msg_attachment_attachment_id_seq TO vilesci; -"; - if (!$this->db->simple_query($query)) - { - echo "Error creating Basis DB-Schema!"; - } - } - } - - public function down() - { - try - { - $this->dbforge->drop_table('public.tbl_msg_recipient'); - $this->dbforge->drop_table('public.tbl_msg_status'); - $this->dbforge->drop_table('public.tbl_msg_attachment'); - $this->dbforge->drop_table('public.tbl_msg_message'); - echo "Table public.tbl_msg_message, public.tbl_msg_status, public.tbl_msg_attachment and public.tbl_msg_recipient dropped!"; - } - catch(Exception $e) - { - echo 'Exception abgefangen: ', $e->getMessage(), "\n"; - echo $this->db->error(); - } - } -} + $this->dropTable("public", "tbl_msg_recipient"); + $this->dropTable("public", "tbl_msg_status"); + $this->dropTable("public", "tbl_msg_attachment"); + $this->dropTable("public", "tbl_msg_message"); + $this->endDown(); + } +} \ No newline at end of file diff --git a/application/migrations/012_bewerbungsfrist.php b/application/migrations/012_bewerbungsfrist.php index 6be2bf1ff..63952bd26 100644 --- a/application/migrations/012_bewerbungsfrist.php +++ b/application/migrations/012_bewerbungsfrist.php @@ -35,30 +35,30 @@ class Migration_Bewerbungsfrist extends MigrationLib ), "nachfrist" => array( "type" => "boolean DEFAULT FALSE", - "null" => TRUE + "null" => true ), "nachfristende" => array( "type" => "date", - "null" => TRUE + "null" => true ), "anmerkung" => array( "type" => "text" ), "insertamum" => array( "type" => "timestamp DEFAULT NOW()", - "null" => TRUE + "null" => true ), "insertvon" => array( "type" => "varchar(32)", - "null" => TRUE + "null" => true ), "updateamum" => array( "type" => "timestamp DEFAULT NOW()", - "null" => TRUE + "null" => true ), "updatevon" => array( "type" => "varchar(32)", - "null" => TRUE + "null" => true ) ); $this->createTable("lehre", "tbl_bewerbungsfrist", $fields);