From ba0e2bb7ff49962650219905b304ce2dbd38ffd7 Mon Sep 17 00:00:00 2001 From: paolo Date: Mon, 4 Jul 2016 15:36:53 +0200 Subject: [PATCH] MigrationLib to help with the migration procedure --- application/libraries/MigrationLib.php | 146 ++++++++++++++++++++ application/migrations/011_reihungstest.php | 127 ++++++----------- 2 files changed, 190 insertions(+), 83 deletions(-) create mode 100644 application/libraries/MigrationLib.php diff --git a/application/libraries/MigrationLib.php b/application/libraries/MigrationLib.php new file mode 100644 index 000000000..747f094e0 --- /dev/null +++ b/application/libraries/MigrationLib.php @@ -0,0 +1,146 @@ +MSG_PREFIX, $message); + } + + private function printInfo($info) + { + printf("%s %s" . PHP_EOL, $this->INFO_PREFIX, $info); + } + + private function printError($error) + { + printf("%s %s" . PHP_EOL, $this->ERROR_PREFIX, $error); + } + + protected function startUP() + { + $this->printInfo(sprintf("%s Start method up of class %s %s", $this->SEPARATOR, get_called_class(), $this->SEPARATOR)); + } + + protected function endUP() + { + $this->printInfo(sprintf("%s End method up of class %s %s", $this->SEPARATOR, get_called_class(), $this->SEPARATOR)); + } + + protected function columnExists($column, $schema, $table) + { + $query = sprintf("SELECT COUNT(%s) FROM %s.%s", $column, $schema, $table); + + if (! @$this->db->simple_query($query)) + { + return false; + } + + return true; + } + + protected function addColumn($schema, $table, $column, $type) + { + if (!$this->columnExists($column, $schema, $table)) + { + $query = sprintf("ALTER TABLE %s.%s ADD COLUMN %s %s", $schema, $table, $column, $type); + if (@$this->db->simple_query($query)) + { + $this->printMessage(sprintf("Column %s.%s.%s of type %s added", $schema, $table, $column, $type)); + } + else + { + $this->printError(sprintf("Error while adding column %s.%s.%s of type %s", $schema, $table, $column, $type)); + } + } + else + { + $this->printInfo(sprintf("Column %s.%s.%s already exists", $schema, $table, $column)); + } + } + + protected function grantTable($permission, $schema, $table, $user) + { + $stringPermission = null; + + if (is_array($permission)) + { + if (count($permission) > 0) + { + $stringPermission = ""; + for ($i = 0; $i < count($permission); $i++) + { + $stringPermission .= $permission[$i]; + if ($i != count($permission) - 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", $permission, $schema, $table, $user); + } + + if (@$this->db->simple_query($query)) + { + $this->printMessage( + sprintf("Granted permissions %s on table %s.%s to user %s", + is_null($stringPermission) ? $permission : $stringPermission, + $schema, + $table, + $user + )); + } + else + { + $this->printError( + sprintf("Granting permissions %s on table %s.%s to user %s", + is_null($stringPermission) ? $permission : $stringPermission, + $schema, + $table, + $user + )); + } + } + + protected function createTable($schema, $table, $fields) + { + if (! $this->db->table_exists($schema . "." . $table)) + { + $query = sprintf("CREATE TABLE %s.%s (%s)", $schema, $table, $fields); + + if (@$this->db->simple_query($query)) + { + $this->printMessage(sprintf("Table %s.%s created", $schema, $table)); + } + else + { + $this->printError(sprintf("Creating table %s.%s", $schema, $table)); + } + } + else + { + $this->printInfo(sprintf("Table %s.%s already exists", $schema, $table)); + } + } + + protected function execQuery($query) + { + if (! @$this->db->simple_query($query)) + { + $this->printError($this->db->error()); + } + + $this->printInfo("Query correctly executed"); + } +} \ No newline at end of file diff --git a/application/migrations/011_reihungstest.php b/application/migrations/011_reihungstest.php index 237d1bc28..54f2cde84 100644 --- a/application/migrations/011_reihungstest.php +++ b/application/migrations/011_reihungstest.php @@ -2,103 +2,64 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); -class Migration_Reihungstest extends CI_Migration { +require_once APPPATH . '/libraries/MigrationLib.php'; +class Migration_Reihungstest extends MigrationLib +{ public function up() { + $this->startUP(); + // Add stufe to public.tbl_reihungstest - if (! @$this->db->simple_query('SELECT stufe FROM public.tbl_reihungstest')) - { - $query = "ALTER TABLE public.tbl_reihungstest ADD COLUMN stufe smallint;"; - if ($this->db->simple_query($query)) - echo 'Column public.tbl_reihungstest.stufe added!'; - else - echo "Error adding public.tbl_reihungstest.stufe!"; - } + $this->addColumn('public', 'tbl_reihungstest', 'stufe', 'smallint'); // Add anmeldefrist to public.tbl_reihungstest - if (! @$this->db->simple_query('SELECT anmeldefrist FROM public.tbl_reihungstest')) - { - $query = "ALTER TABLE public.tbl_reihungstest ADD COLUMN anmeldefrist date;"; - if ($this->db->simple_query($query)) - echo 'Column public.tbl_reihungstest.anmeldefrist added!'; - else - echo "Error adding public.tbl_reihungstest.anmeldefrist!"; - } + $this->addColumn('public', 'tbl_reihungstest', 'anmeldefrist', 'date'); // Add rt_stufe to public.tbl_prestudentstatus - if (! @$this->db->simple_query('SELECT rt_stufe FROM public.tbl_prestudentstatus')) - { - $query = "ALTER TABLE public.tbl_prestudentstatus ADD COLUMN rt_stufe smallint DEFAULT 1;"; - if ($this->db->simple_query($query)) - echo 'Column public.tbl_prestudentstatus.rt_stufe added!'; - else - echo "Error adding public.tbl_prestudentstatus.rt_stufe!"; - } + $this->addColumn('public', 'tbl_prestudentstatus', 'rt_stufe', 'smallint DEFAULT 1'); // Create table public.tbl_rt_studienplan - if (! $this->db->table_exists('public.tbl_rt_studienplan')) - { - $query= "CREATE TABLE public.tbl_rt_studienplan ( - reihungstest_id integer, - stundenplan_id integer, - CONSTRAINT pk_tbl_rt_studienplan PRIMARY KEY (reihungstest_id, stundenplan_id), - CONSTRAINT fk_rt_studienplan_reihungstest_id FOREIGN KEY (reihungstest_id) REFERENCES public.tbl_reihungstest(reihungstest_id) ON UPDATE CASCADE ON DELETE RESTRICT, - CONSTRAINT fk_rt_studienplan_stundenplan_id FOREIGN KEY (stundenplan_id) REFERENCES lehre.tbl_stundenplan(stundenplan_id) ON UPDATE CASCADE ON DELETE RESTRICT - ); - GRANT SELECT ON TABLE public.tbl_rt_studienplan TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_rt_studienplan TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_rt_studienplan TO vilesci;"; - - if (!$this->db->simple_query($query)) - { - echo "Error creating table public.tbl_rt_studienplan!"; - } - } + $this->createTable('public', 'tbl_rt_studienplan', + 'reihungstest_id integer, + stundenplan_id integer, + CONSTRAINT pk_tbl_rt_studienplan PRIMARY KEY (reihungstest_id, stundenplan_id), + CONSTRAINT fk_rt_studienplan_reihungstest_id FOREIGN KEY (reihungstest_id) REFERENCES public.tbl_reihungstest(reihungstest_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT fk_rt_studienplan_stundenplan_id FOREIGN KEY (stundenplan_id) REFERENCES lehre.tbl_stundenplan(stundenplan_id) ON UPDATE CASCADE ON DELETE RESTRICT' + ); + $this->grantTable('SELECT', 'public', 'tbl_rt_studienplan', 'web'); + $this->grantTable(array('SELECT', 'INSERT', 'DELETE', 'UPDATE'), 'public', 'tbl_rt_studienplan', 'admin'); + $this->grantTable(array('SELECT', 'INSERT', 'DELETE', 'UPDATE'), 'public', 'tbl_rt_studienplan', 'vilesci'); // Create table public.tbl_rt_person - if (! $this->db->table_exists('public.tbl_rt_person')) - { - $query= "CREATE TABLE public.tbl_rt_person ( - person_id integer, - rt_id integer, - anmeldedatum date, - teilgenommen boolean DEFAULT FALSE, - ort_kurzbz varchar(16) NOT NULL, - CONSTRAINT pk_tbl_rt_person PRIMARY KEY (person_id, rt_id), - CONSTRAINT fk_rt_person_ort_kurzbz FOREIGN KEY (ort_kurzbz) REFERENCES public.tbl_ort(ort_kurzbz) ON UPDATE CASCADE ON DELETE RESTRICT, - CONSTRAINT fk_rt_person_reihungstest_id FOREIGN KEY (rt_id) REFERENCES public.tbl_reihungstest(reihungstest_id) ON UPDATE CASCADE ON DELETE RESTRICT - ); - GRANT SELECT ON TABLE public.tbl_rt_person TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_rt_person TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_rt_person TO vilesci;"; - - if (!$this->db->simple_query($query)) - { - echo "Error creating table public.tbl_rt_person!"; - } - } + $this->createTable('public', 'tbl_rt_person', + 'person_id integer, + rt_id integer, + anmeldedatum date, + teilgenommen boolean DEFAULT FALSE, + ort_kurzbz varchar(16) NOT NULL, + CONSTRAINT pk_tbl_rt_person PRIMARY KEY (person_id, rt_id), + CONSTRAINT fk_rt_person_ort_kurzbz FOREIGN KEY (ort_kurzbz) REFERENCES public.tbl_ort(ort_kurzbz) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT fk_rt_person_reihungstest_id FOREIGN KEY (rt_id) REFERENCES public.tbl_reihungstest(reihungstest_id) ON UPDATE CASCADE ON DELETE RESTRICT' + ); + $this->grantTable('SELECT', 'public', 'tbl_rt_person', 'web'); + $this->grantTable(array('SELECT', 'INSERT', 'DELETE', 'UPDATE'), 'public', 'tbl_rt_person', 'admin'); + $this->grantTable(array('SELECT', 'INSERT', 'DELETE', 'UPDATE'), 'public', 'tbl_rt_person', 'vilesci'); // Create table public.tbl_rt_ort - if (! $this->db->table_exists('public.tbl_rt_ort')) - { - $query= "CREATE TABLE public.tbl_rt_ort ( - rt_id integer, - ort_kurzbz varchar(16), - uid varchar(32), - CONSTRAINT pk_tbl_rt_ort PRIMARY KEY (rt_id, ort_kurzbz), - CONSTRAINT fk_rt_ort_reihungstest_id FOREIGN KEY (rt_id) REFERENCES public.tbl_reihungstest(reihungstest_id) ON UPDATE CASCADE ON DELETE RESTRICT, - CONSTRAINT fk_rt_ort_ort_kurzbz FOREIGN KEY (ort_kurzbz) REFERENCES public.tbl_ort(ort_kurzbz) ON UPDATE CASCADE ON DELETE RESTRICT - ); - GRANT SELECT ON TABLE public.tbl_rt_ort TO web; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_rt_ort TO admin; - GRANT SELECT,INSERT,DELETE,UPDATE ON TABLE public.tbl_rt_ort TO vilesci;"; - - if (!$this->db->simple_query($query)) - { - echo "Error creating table public.tbl_rt_ort!"; - } - } + $this->createTable('public', 'tbl_rt_ort', + 'rt_id integer, + ort_kurzbz varchar(16), + uid varchar(32), + CONSTRAINT pk_tbl_rt_ort PRIMARY KEY (rt_id, ort_kurzbz), + CONSTRAINT fk_rt_ort_reihungstest_id FOREIGN KEY (rt_id) REFERENCES public.tbl_reihungstest(reihungstest_id) ON UPDATE CASCADE ON DELETE RESTRICT, + CONSTRAINT fk_rt_ort_ort_kurzbz FOREIGN KEY (ort_kurzbz) REFERENCES public.tbl_ort(ort_kurzbz) ON UPDATE CASCADE ON DELETE RESTRICT' + ); + $this->grantTable('SELECT', 'public', 'tbl_rt_ort', 'web'); + $this->grantTable(array('SELECT', 'INSERT', 'DELETE', 'UPDATE'), 'public', 'tbl_rt_ort', 'admin'); + $this->grantTable(array('SELECT', 'INSERT', 'DELETE', 'UPDATE'), 'public', 'tbl_rt_ort', 'vilesci'); + + $this->endUP(); } public function down()