ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
Test11DBUpdateSteps.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Test\Setup;
22
24
26{
28
29 protected \ilDBInterface $db;
30
31 public function prepare(\ilDBInterface $db): void
32 {
33 $this->db = $db;
34 }
35
36 public function step_1(): void
37 {
38 if ($this->db->tableColumnExists('tst_tests', 'mailnotification')) {
39 $this->db->dropTableColumn('tst_tests', 'mailnotification');
40 }
41 if ($this->db->tableColumnExists('tst_tests', 'mailnottype')) {
42 $this->db->dropTableColumn('tst_tests', 'mailnottype');
43 }
44 }
45
46 public function step_2(): void
47 {
48 // 1. Create table schema
49 if (!$this->db->tableExists('tst_test_settings')) {
50 // Create table and sequence table
51 $this->db->createTable('tst_test_settings', ['id' => ['type' => \ilDBConstants::T_INTEGER]]);
52 $this->db->createSequence('tst_test_settings');
53 $this->db->addPrimaryKey('tst_test_settings', ['id']);
54
55 // Create table columns
56 foreach (self::SETTINGS_COLUMNS as $key => $value) {
57 [$column_def] = $value;
58
59 // No columns should be nullable, except those with NULL by default
60 if (!isset($column_def['notnull'])) {
61 $column_def['notnull'] = !$this->columnIsNullable($column_def);
62 }
63
64 $this->db->addTableColumn('tst_test_settings', $key, $column_def);
65 }
66 }
67
68 // 2. Create a foreign key column in tst_tests
69 if (!$this->db->tableColumnExists('tst_tests', 'settings_id')) {
70 $this->db->addTableColumn(
71 'tst_tests',
72 'settings_id',
73 ['type' => \ilDBConstants::T_INTEGER, 'default' => null, 'notnull' => false],
74 );
75 $this->db->addForeignKey(
76 'test_settings_fkey',
77 ['settings_id'],
78 'tst_tests',
79 ['id'],
80 'tst_test_settings',
81 ForeignKeyConstraints::NO_ACTION,
82 ForeignKeyConstraints::RESTRICT,
83 );
84 }
85
86 // 3. Create a foreign key column and new columns in tst_test_defaults
87 if (!$this->db->tableColumnExists('tst_test_defaults', 'settings_id')) {
88 $this->db->addTableColumn(
89 'tst_test_defaults',
90 'settings_id',
91 ['type' => \ilDBConstants::T_INTEGER, 'default' => null, 'notnull' => false],
92 );
93 $this->db->addForeignKey(
94 'test_default_fkey',
95 ['settings_id'],
96 'tst_test_defaults',
97 ['id'],
98 'tst_test_settings',
99 ForeignKeyConstraints::NO_ACTION,
100 ForeignKeyConstraints::RESTRICT
101 );
102 }
103 if (!$this->db->tableColumnExists('tst_test_defaults', 'description')) {
104 $this->db->addTableColumn(
105 'tst_test_defaults',
106 'description',
107 ['type' => \ilDBConstants::T_TEXT, 'length' => 4000, 'default' => null],
108 );
109 }
110 if (!$this->db->tableColumnExists('tst_test_defaults', 'author')) {
111 $this->db->addTableColumn(
112 'tst_test_defaults',
113 'author',
114 ['type' => \ilDBConstants::T_TEXT, 'length' => 255, 'default' => null],
115 );
116 }
117
118 // 4. Create tst_defaults_marks table to store marks reference
119 if (!$this->db->tableExists('tst_defaults_marks')) {
120 $this->db->createTable(
121 'tst_defaults_marks',
122 [
123 'defaults_id' => ['type' => \ilDBConstants::T_INTEGER],
124 'mark_id' => ['type' => \ilDBConstants::T_INTEGER],
125 ],
126 );
127 $this->db->addPrimaryKey('tst_defaults_marks', ['defaults_id', 'mark_id']);
128
129 $this->db->addForeignKey(
130 'test_default_fkey2',
131 ['defaults_id '],
132 'tst_defaults_marks',
133 ['test_defaults_id'],
134 'tst_test_defaults',
135 ForeignKeyConstraints::NO_ACTION,
136 ForeignKeyConstraints::RESTRICT
137 );
138
139 $this->db->addForeignKey(
140 'test_mark_fkey',
141 ['mark_id '],
142 'tst_defaults_marks',
143 ['mark_id'],
144 'tst_mark',
145 ForeignKeyConstraints::NO_ACTION,
147 );
148 }
149 }
150}
const CASCADE
Definition: Title.php:33
prepare(\ilDBInterface $db)
Prepare the execution of the steps.
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
columnIsNullable(array $column_def)