mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-20 00:12:15 +00:00
- MigrationLib is now using, where it is possible, dbforge from CI
- 011_reihungstest.php migration script adapted to the new MigrationLib
This commit is contained in:
@@ -13,6 +13,8 @@ class MigrationLib extends CI_Migration
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
if ($this->input->is_cli_request())
|
||||
{
|
||||
$this->cli = true;
|
||||
@@ -60,51 +62,91 @@ class MigrationLib extends CI_Migration
|
||||
$this->printInfo(sprintf("%s End method up of class %s %s", $this->SEPARATOR, get_called_class(), $this->SEPARATOR));
|
||||
}
|
||||
|
||||
protected function columnExists($column, $schema, $table)
|
||||
protected function addColumn($schema, $table, $fields)
|
||||
{
|
||||
$query = sprintf("SELECT COUNT(%s) FROM %s.%s", $column, $schema, $table);
|
||||
|
||||
if (! @$this->db->simple_query($query))
|
||||
foreach($fields as $name => $definition)
|
||||
{
|
||||
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))
|
||||
if (!$this->db->field_exists($name, $schema . '.' . $table))
|
||||
{
|
||||
$this->printMessage(sprintf("Column %s.%s.%s of type %s added", $schema, $table, $column, $type));
|
||||
if ($this->dbforge->add_column($schema . '.' . $table, array($name => $definition)))
|
||||
{
|
||||
$this->printMessage(sprintf("Column %s.%s.%s of type %s added", $schema, $table, $name, $definition["type"]));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->printError(sprintf("Error while adding column %s.%s.%s of type %s", $schema, $table, $name, $definition["type"]));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->printError(sprintf("Error while adding column %s.%s.%s of type %s", $schema, $table, $column, $type));
|
||||
$this->printInfo(sprintf("Column %s.%s.%s already exists", $schema, $table, $name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
$this->printInfo(sprintf("Column %s.%s.%s already exists", $schema, $table, $column));
|
||||
$query = sprintf("ALTER TABLE %s.%s ADD CONSTRAINT %s PRIMARY KEY (%s)", $schema, $table, $name, $fields);
|
||||
}
|
||||
|
||||
if (@$this->db->simple_query($query))
|
||||
{
|
||||
$this->printMessage(sprintf("Added primary key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->printError(sprintf("Adding primary key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
}
|
||||
|
||||
protected function grantTable($permission, $schema, $table, $user)
|
||||
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->printMessage(sprintf("Added foreign key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->printError(sprintf("Adding foreign key %s on table %s.%s", $name, $schema, $table));
|
||||
}
|
||||
}
|
||||
|
||||
protected function grantTable($permissions, $schema, $table, $user)
|
||||
{
|
||||
$stringPermission = null;
|
||||
|
||||
if (is_array($permission))
|
||||
if (is_array($permissions))
|
||||
{
|
||||
if (count($permission) > 0)
|
||||
if (count($permissions) > 0)
|
||||
{
|
||||
$stringPermission = "";
|
||||
for ($i = 0; $i < count($permission); $i++)
|
||||
for ($i = 0; $i < count($permissions); $i++)
|
||||
{
|
||||
$stringPermission .= $permission[$i];
|
||||
if ($i != count($permission) - 1)
|
||||
$stringPermission .= $permissions[$i];
|
||||
if ($i != count($permissions) - 1)
|
||||
{
|
||||
$stringPermission .= ", ";
|
||||
}
|
||||
@@ -114,14 +156,14 @@ class MigrationLib extends CI_Migration
|
||||
}
|
||||
else
|
||||
{
|
||||
$query = sprintf("GRANT %s ON TABLE %s.%s TO %s", $permission, $schema, $table, $user);
|
||||
$query = sprintf("GRANT %s ON TABLE %s.%s TO %s", $permissions, $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,
|
||||
is_null($stringPermission) ? $permissions : $stringPermission,
|
||||
$schema,
|
||||
$table,
|
||||
$user
|
||||
@@ -131,7 +173,7 @@ class MigrationLib extends CI_Migration
|
||||
{
|
||||
$this->printError(
|
||||
sprintf("Granting permissions %s on table %s.%s to user %s",
|
||||
is_null($stringPermission) ? $permission : $stringPermission,
|
||||
is_null($stringPermission) ? $permissions : $stringPermission,
|
||||
$schema,
|
||||
$table,
|
||||
$user
|
||||
@@ -141,22 +183,15 @@ class MigrationLib extends CI_Migration
|
||||
|
||||
protected function createTable($schema, $table, $fields)
|
||||
{
|
||||
if (! $this->db->table_exists($schema . "." . $table))
|
||||
$this->dbforge->add_field($fields);
|
||||
|
||||
if ($this->dbforge->create_table($schema . '.' . $table, true))
|
||||
{
|
||||
$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));
|
||||
}
|
||||
$this->printMessage(sprintf("Table %s.%s created or existing", $schema, $table));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->printInfo(sprintf("Table %s.%s already exists", $schema, $table));
|
||||
$this->printError(sprintf("Creating table %s.%s", $schema, $table));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
if (! defined("BASEPATH")) exit("No direct script access allowed");
|
||||
|
||||
require_once APPPATH . '/libraries/MigrationLib.php';
|
||||
require_once APPPATH . "/libraries/MigrationLib.php";
|
||||
|
||||
class Migration_Reihungstest extends MigrationLib
|
||||
{
|
||||
@@ -15,57 +15,164 @@ class Migration_Reihungstest extends MigrationLib
|
||||
{
|
||||
$this->startUP();
|
||||
|
||||
// Add stufe to public.tbl_reihungstest
|
||||
$this->addColumn('public', 'tbl_reihungstest', 'stufe', 'smallint');
|
||||
|
||||
// Add anmeldefrist to public.tbl_reihungstest
|
||||
$this->addColumn('public', 'tbl_reihungstest', 'anmeldefrist', 'date');
|
||||
// Add stufe and anmeldefrist to public.tbl_reihungstest
|
||||
$columns = array(
|
||||
"stufe" => array("type" => "smallint"),
|
||||
"anmeldefrist" => array("type" => "date")
|
||||
);
|
||||
$this->addColumn("public", "tbl_reihungstest", $columns);
|
||||
|
||||
// Add rt_stufe and punkte to public.tbl_prestudentstatus
|
||||
$this->addColumn('public', 'tbl_prestudentstatus', 'rt_stufe', 'smallint DEFAULT NULL');
|
||||
$columns = array(
|
||||
"rt_stufe" => array("type" => "smallint DEFAULT NULL")
|
||||
);
|
||||
$this->addColumn("public", "tbl_prestudentstatus", $columns);
|
||||
|
||||
// Create 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'
|
||||
$fields = array(
|
||||
"reihungstest_id" => array(
|
||||
"type" => "integer"
|
||||
),
|
||||
"stundenplan_id" => array(
|
||||
"type" => "integer"
|
||||
)
|
||||
);
|
||||
$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');
|
||||
$this->createTable("public", "tbl_rt_studienplan", $fields);
|
||||
$this->addPrimaryKey(
|
||||
"public",
|
||||
"tbl_rt_studienplan",
|
||||
"pk_tbl_rt_studienplan",
|
||||
array("reihungstest_id", "stundenplan_id")
|
||||
);
|
||||
$this->addForeingKey(
|
||||
"public",
|
||||
"tbl_rt_studienplan",
|
||||
"fk_rt_studienplan_reihungstest_id",
|
||||
"reihungstest_id",
|
||||
"public",
|
||||
"tbl_reihungstest",
|
||||
"reihungstest_id",
|
||||
"ON UPDATE CASCADE ON DELETE RESTRICT"
|
||||
);
|
||||
$this->addForeingKey(
|
||||
"public",
|
||||
"tbl_rt_studienplan",
|
||||
"fk_rt_studienplan_stundenplan_id",
|
||||
"stundenplan_id",
|
||||
"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
|
||||
$this->createTable('public', 'tbl_rt_person',
|
||||
'person_id integer,
|
||||
rt_id integer,
|
||||
anmeldedatum date,
|
||||
teilgenommen boolean DEFAULT FALSE,
|
||||
ort_kurzbz varchar(16),
|
||||
punkte numeric(8,4) DEFAULT 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'
|
||||
$fields = array(
|
||||
"person_id" => array(
|
||||
"type" => "integer"
|
||||
),
|
||||
"rt_id" => array(
|
||||
"type" => "integer"
|
||||
),
|
||||
"anmeldedatum" => array(
|
||||
"type" => "date"
|
||||
),
|
||||
"teilgenommen" => array(
|
||||
"type" => "boolean DEFAULT FALSE"
|
||||
),
|
||||
"ort_kurzbz" => array(
|
||||
"type" => "varchar(16)"
|
||||
),
|
||||
"punkte" => array(
|
||||
"type" => "numeric(8,4) DEFAULT NULL"
|
||||
)
|
||||
);
|
||||
$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');
|
||||
|
||||
$this->createTable("public", "tbl_rt_person", $fields);
|
||||
$this->addPrimaryKey(
|
||||
"public",
|
||||
"tbl_rt_person",
|
||||
"pk_tbl_rt_person",
|
||||
array("person_id", "rt_id")
|
||||
);
|
||||
$this->addForeingKey(
|
||||
"public",
|
||||
"tbl_rt_person",
|
||||
"fk_rt_person_ort_kurzbz",
|
||||
"ort_kurzbz",
|
||||
"public",
|
||||
"tbl_ort",
|
||||
"ort_kurzbz",
|
||||
"ON UPDATE CASCADE ON DELETE RESTRICT"
|
||||
);
|
||||
$this->addForeingKey(
|
||||
"public",
|
||||
"tbl_rt_person",
|
||||
"fk_rt_person_reihungstest_id",
|
||||
"rt_id",
|
||||
"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
|
||||
$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,
|
||||
CONSTRAINT fk_rt_ort_uid FOREIGN KEY (uid) REFERENCES public.tbl_benutzer(uid) ON UPDATE CASCADE ON DELETE RESTRICT'
|
||||
$fields = array(
|
||||
"rt_id" => array(
|
||||
"type" => "integer"
|
||||
),
|
||||
"ort_kurzbz" => array(
|
||||
"type" => "varchar(16)"
|
||||
),
|
||||
"uid" => array(
|
||||
"type" => "varchar(32)"
|
||||
)
|
||||
);
|
||||
$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->createTable("public", "tbl_rt_ort", $fields);
|
||||
$this->addPrimaryKey(
|
||||
"public",
|
||||
"tbl_rt_ort",
|
||||
"pk_tbl_rt_ort",
|
||||
array("rt_id", "ort_kurzbz")
|
||||
);
|
||||
$this->addForeingKey(
|
||||
"public",
|
||||
"tbl_rt_ort",
|
||||
"fk_rt_ort_reihungstest_id",
|
||||
"rt_id",
|
||||
"public",
|
||||
"tbl_reihungstest",
|
||||
"reihungstest_id",
|
||||
"ON UPDATE CASCADE ON DELETE RESTRICT"
|
||||
);
|
||||
$this->addForeingKey(
|
||||
"public",
|
||||
"tbl_rt_ort",
|
||||
"fk_rt_ort_ort_kurzbz",
|
||||
"ort_kurzbz",
|
||||
"public",
|
||||
"tbl_ort",
|
||||
"ort_kurzbz",
|
||||
"ON UPDATE CASCADE ON DELETE RESTRICT"
|
||||
);
|
||||
$this->addForeingKey(
|
||||
"public",
|
||||
"tbl_rt_ort",
|
||||
"fk_rt_ort_uid",
|
||||
"uid",
|
||||
"public",
|
||||
"tbl_benutzer",
|
||||
"uid",
|
||||
"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();
|
||||
}
|
||||
|
||||
@@ -73,21 +180,21 @@ class Migration_Reihungstest extends MigrationLib
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->dbforge->drop_column('public.tbl_reihungstest', 'stufe');
|
||||
$this->dbforge->drop_column('public.tbl_reihungstest', 'anmeldefrist');
|
||||
$this->dbforge->drop_column('public.tbl_prestudentstatus', 'rt_stufe');
|
||||
$this->dbforge->drop_column("public.tbl_reihungstest", "stufe");
|
||||
$this->dbforge->drop_column("public.tbl_reihungstest", "anmeldefrist");
|
||||
$this->dbforge->drop_column("public.tbl_prestudentstatus", "rt_stufe");
|
||||
|
||||
echo "Columns public.tbl_reihungstest.stufe, public.tbl_reihungstest.anmeldefrist, public.tbl_prestudentstatus.rt_stufe dropped!";
|
||||
|
||||
$this->dbforge->drop_table('public.tbl_rt_studienplan');
|
||||
$this->dbforge->drop_table('public.tbl_rt_person');
|
||||
$this->dbforge->drop_table('public.tbl_rt_ort');
|
||||
$this->dbforge->drop_table("public.tbl_rt_studienplan");
|
||||
$this->dbforge->drop_table("public.tbl_rt_person");
|
||||
$this->dbforge->drop_table("public.tbl_rt_ort");
|
||||
|
||||
echo "Tables public.tbl_rt_studienplan, public.tbl_rt_person, public.tbl_rt_ort dropped!";
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
echo 'Exception: ', $e->getMessage(), "\n";
|
||||
echo "Exception: ", $e->getMessage(), "\n";
|
||||
echo $this->db->error();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user