mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-23 18:02:18 +00:00
- Improved MigrationLib
- Moved 010_vorlage migration script to new lib
This commit is contained in:
@@ -2,19 +2,35 @@
|
||||
|
||||
if (! defined("BASEPATH")) exit("No direct script access allowed");
|
||||
|
||||
/**
|
||||
* Utility class to be used in the database migration process
|
||||
*/
|
||||
class MigrationLib extends CI_Migration
|
||||
{
|
||||
// Prefixes and separator for messages
|
||||
private $MSG_PREFIX = "[-]";
|
||||
private $INFO_PREFIX = "[I]";
|
||||
private $ERROR_PREFIX = "[E]";
|
||||
private $SEPARATOR = "------------------------------";
|
||||
|
||||
// Used to set if the migration process is called via command line or via browser
|
||||
private $cli;
|
||||
|
||||
/**
|
||||
* Object initialization
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->setCli();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set property cli to false if the migration process is called via command line
|
||||
* otherwise to false if it's called via browser
|
||||
*/
|
||||
private function setCli()
|
||||
{
|
||||
if ($this->input->is_cli_request())
|
||||
{
|
||||
$this->cli = true;
|
||||
@@ -25,6 +41,11 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the character of end of line
|
||||
* PHP_EOL platform dependent if cli is true
|
||||
* Tag <br> if cli is false
|
||||
*/
|
||||
private function getEOL()
|
||||
{
|
||||
if ($this->cli === true)
|
||||
@@ -37,24 +58,36 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints a formatted message
|
||||
*/
|
||||
private function printMessage($message)
|
||||
{
|
||||
printf("%s %s" . $this->getEOL(), $this->MSG_PREFIX, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints a formatted info
|
||||
*/
|
||||
private function printInfo($info)
|
||||
{
|
||||
printf("%s %s" . $this->getEOL(), $this->INFO_PREFIX, $info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints a formatted error
|
||||
*/
|
||||
private function printError($error)
|
||||
{
|
||||
printf("%s %s" . $this->getEOL(), $this->ERROR_PREFIX, $error);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
$query = sprintf("SELECT %s FROM %s.%s LIMIT 1", $name, $schema, $table);
|
||||
|
||||
if (@$this->db->simple_query($query))
|
||||
{
|
||||
@@ -64,26 +97,41 @@ class MigrationLib extends CI_Migration
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an info about the starting of method up
|
||||
*/
|
||||
protected function startUP()
|
||||
{
|
||||
$this->printInfo(sprintf("%s Start method up of class %s %s", $this->SEPARATOR, get_called_class(), $this->SEPARATOR));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an info about the ending of method up
|
||||
*/
|
||||
protected function endUP()
|
||||
{
|
||||
$this->printInfo(sprintf("%s End method up of class %s %s", $this->SEPARATOR, get_called_class(), $this->SEPARATOR));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an info about the starting of method down
|
||||
*/
|
||||
protected function startDown()
|
||||
{
|
||||
$this->printInfo(sprintf("%s Start method down of class %s %s", $this->SEPARATOR, get_called_class(), $this->SEPARATOR));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an info about the ending of method down
|
||||
*/
|
||||
protected function endDown()
|
||||
{
|
||||
$this->printInfo(sprintf("%s End method down of class %s %s", $this->SEPARATOR, get_called_class(), $this->SEPARATOR));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a column, with attributes, to a table and schema
|
||||
*/
|
||||
protected function addColumn($schema, $table, $fields)
|
||||
{
|
||||
foreach($fields as $name => $definition)
|
||||
@@ -106,6 +154,34 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->printMessage(sprintf("Column %s.%s.%s has been modified", $schema, $table, $name));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->printError(sprintf("Error while modifying column %s.%s.%s", $schema, $table, $name));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->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))
|
||||
@@ -125,6 +201,9 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a column as primary key of a table and schema
|
||||
*/
|
||||
protected function addPrimaryKey($schema, $table, $name, $fields)
|
||||
{
|
||||
$stringFields = null;
|
||||
@@ -160,6 +239,9 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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",
|
||||
@@ -175,6 +257,9 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Grants permissions to a user on a table and schema
|
||||
*/
|
||||
protected function grantTable($permissions, $schema, $table, $user)
|
||||
{
|
||||
$stringPermission = null;
|
||||
@@ -222,6 +307,9 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a table in a schema with columns
|
||||
*/
|
||||
protected function createTable($schema, $table, $fields)
|
||||
{
|
||||
$this->dbforge->add_field($fields);
|
||||
@@ -236,6 +324,9 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a table from a schema
|
||||
*/
|
||||
protected function dropTable($schema, $table)
|
||||
{
|
||||
if ($this->dbforge->drop_table($schema . "." . $table))
|
||||
@@ -248,6 +339,26 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->printMessage(sprintf("Sequence %s.%s has been initialized", $schemaSrc, $sequence));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->printError(sprintf("Initializing sequence %s.%s", $schemaSrc, $sequence));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the given query
|
||||
*/
|
||||
protected function execQuery($query)
|
||||
{
|
||||
if (! @$this->db->simple_query($query))
|
||||
|
||||
@@ -1,95 +1,84 @@
|
||||
<?php
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
if (! defined("BASEPATH")) exit("No direct script access allowed");
|
||||
|
||||
class Migration_Vorlage extends CI_Migration
|
||||
require_once APPPATH . "/libraries/MigrationLib.php";
|
||||
|
||||
class Migration_Vorlage extends MigrationLib
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function up()
|
||||
{
|
||||
// Change PK to varchar 32
|
||||
$query = "ALTER TABLE public.tbl_vorlage
|
||||
ALTER COLUMN vorlage_kurzbz TYPE varchar(32);
|
||||
ALTER TABLE public.tbl_vorlagestudiengang
|
||||
ALTER COLUMN vorlage_kurzbz TYPE varchar(32);
|
||||
";
|
||||
if ($this->db->simple_query($query))
|
||||
echo 'Column public.tbl_vorlage.vorlage_kurzbz altered!';
|
||||
else
|
||||
echo "Error altering vorlage_kurzbz!";
|
||||
$this->startUP();
|
||||
|
||||
// Attribut public.tbl_vorlagestudiengang.sprache
|
||||
if (! @$this->db->simple_query('SELECT sprache FROM public.tbl_vorlagestudiengang'))
|
||||
{
|
||||
$query = "ALTER TABLE public.tbl_vorlagestudiengang
|
||||
ADD COLUMN sprache varchar(16) references public.tbl_sprache(sprache);
|
||||
";
|
||||
if ($this->db->simple_query($query))
|
||||
echo 'Column public.tbl_vorlagestudiengang.sprache added!';
|
||||
else
|
||||
echo "Error adding public.tbl_vorlagestudiengang.sprache!";
|
||||
}
|
||||
|
||||
// Attribut public.tbl_vorlage.attribute
|
||||
if (! @$this->db->simple_query('SELECT attribute FROM public.tbl_vorlage'))
|
||||
{
|
||||
$query = "ALTER TABLE public.tbl_vorlage
|
||||
ADD COLUMN attribute json;
|
||||
";
|
||||
if ($this->db->simple_query($query))
|
||||
echo 'Column public.tbl_vorlage.attribute added!';
|
||||
else
|
||||
echo "Error adding public.tbl_vorlage.attribute!";
|
||||
}
|
||||
|
||||
// OEen ohne Eltern holen
|
||||
$query = 'SELECT oe_kurzbz FROM public.tbl_organisationseinheit WHERE oe_parent_kurzbz IS NULL;';
|
||||
$oe = $this->db->query($query)->result();
|
||||
|
||||
|
||||
// tbl_vorlagestudiengang->Subject
|
||||
if (! @$this->db->simple_query('SELECT subject FROM public.tbl_vorlagestudiengang'))
|
||||
{
|
||||
$query= "ALTER TABLE public.tbl_vorlagestudiengang
|
||||
ADD COLUMN subject text;
|
||||
";
|
||||
if ($this->db->simple_query($query))
|
||||
echo 'Column public.tbl_vorlagestudiengang.subject added!';
|
||||
else
|
||||
echo "Error adding public.tbl_vorlagestudiengang.subject!";
|
||||
}
|
||||
|
||||
// tbl_vorlagestudiengang->OrgForm
|
||||
if (! @$this->db->simple_query('SELECT orgform_kurzbz FROM public.tbl_vorlagestudiengang'))
|
||||
{
|
||||
$query= "ALTER TABLE public.tbl_vorlagestudiengang
|
||||
ADD COLUMN orgform_kurzbz varchar(3) references bis.tbl_orgform(orgform_kurzbz);
|
||||
";
|
||||
if ($this->db->simple_query($query))
|
||||
{
|
||||
echo 'Column public.tbl_vorlagestudiengang.orgform_kurzbz added!';
|
||||
// Insert Demo Data
|
||||
$query = "SELECT setval('seq_vorlagestudiengang_vorlagestudiengang_id', (SELECT MAX(vorlagestudiengang_id) FROM public.tbl_vorlagestudiengang));";
|
||||
$this->db->simple_query($query);
|
||||
}
|
||||
else
|
||||
echo "Error adding public.tbl_vorlagestudiengang.orgform_kurzbz!";
|
||||
}
|
||||
// Change vorlage_kurzbz to varchar 32
|
||||
$columns = array(
|
||||
"vorlage_kurzbz" => array("type" => "varchar(32)")
|
||||
);
|
||||
$this->modifyColumn("public", "tbl_vorlage", $columns);
|
||||
|
||||
// Change vorlage_kurzbz to varchar 32
|
||||
$columns = array(
|
||||
"vorlage_kurzbz" => array("type" => "varchar(32)")
|
||||
);
|
||||
$this->modifyColumn("public", "tbl_vorlagestudiengang", $columns);
|
||||
|
||||
// Add attribute to public.tbl_vorlage
|
||||
$columns = array(
|
||||
"attribute" => array("type" => "json")
|
||||
);
|
||||
$this->addColumn("public", "tbl_vorlage", $columns);
|
||||
|
||||
// Add sprache, subject and orgform_kurzbz to public.tbl_vorlagestudiengang
|
||||
$columns = array(
|
||||
"sprache" => array("type" => "varchar(16)"),
|
||||
"subject" => array("type" => "text"),
|
||||
"orgform_kurzbz" => array("type" => "varchar(3)")
|
||||
);
|
||||
$this->addColumn("public", "tbl_vorlagestudiengang", $columns);
|
||||
|
||||
$this->initializeSequence(
|
||||
"public", "seq_vorlagestudiengang_vorlagestudiengang_id", "public",
|
||||
"tbl_vorlagestudiengang", "vorlagestudiengang_id"
|
||||
);
|
||||
|
||||
// Add foreign keys to tbl_vorlagestudiengang
|
||||
$this->addForeingKey(
|
||||
"public",
|
||||
"tbl_vorlagestudiengang",
|
||||
"fk_vorlagestudiengang_sprache",
|
||||
"sprache",
|
||||
"public",
|
||||
"tbl_sprache",
|
||||
"sprache",
|
||||
"ON UPDATE CASCADE ON DELETE RESTRICT"
|
||||
);
|
||||
$this->addForeingKey(
|
||||
"public",
|
||||
"tbl_vorlagestudiengang",
|
||||
"fk_vorlagestudiengang_orgform_kurzbz",
|
||||
"orgform_kurzbz",
|
||||
"bis",
|
||||
"tbl_orgform",
|
||||
"orgform_kurzbz",
|
||||
"ON UPDATE CASCADE ON DELETE RESTRICT"
|
||||
);
|
||||
|
||||
$this->endUP();
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->dbforge->drop_column('public.tbl_vorlage', 'attribute');
|
||||
$this->dbforge->drop_column('public.tbl_vorlagestudiengang', 'subject');
|
||||
$this->dbforge->drop_column('public.tbl_vorlagestudiengang', 'orgform_kurzbz');
|
||||
echo "Column public.tbl_vorlage.attribute, public.tbl_vorlagestudiengang.subject, public.tbl_vorlagestudiengang.orgform_kurzbz dropped!";
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
echo 'Exception abgefangen: ', $e->getMessage(), "\n";
|
||||
echo $this->db->error();
|
||||
}
|
||||
$this->startDown();
|
||||
|
||||
$this->dropColumn("public", "tbl_vorlage", "attribute");
|
||||
$this->dropColumn("public", "tbl_vorlagestudiengang", "subject");
|
||||
$this->dropColumn("public", "tbl_vorlagestudiengang", "orgform_kurzbz");
|
||||
|
||||
$this->endDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user