-
Notifications
You must be signed in to change notification settings - Fork 1
/
lytics.install
174 lines (161 loc) · 4.75 KB
/
lytics.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
use Drupal\Core\Database\Schema\Blueprint;
use Drupal\Core\Database\Schema\Schema;
use Drupal\Core\Database\Database;
/**
* Implements hook_schema().
*/
function lytics_schema()
{
$schema['lytics_widget'] = [
'description' => 'The base table for Lytics Widgets.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique Widget ID.',
],
'title' => [
'type' => 'varchar',
'length' => 255,
'description' => 'Widget title.',
],
'description' => [
'type' => 'text',
'description' => 'Widget description.',
],
'config' => [
'type' => 'text',
'description' => 'Widget configuration.',
],
'status' => [
'type' => 'varchar',
'length' => 255,
'description' => 'Widget status.',
],
],
'primary key' => ['id'],
];
return $schema;
}
/**
* Implements hook_update_N().
*
* Create the table for the new entity lytics_experience.
* If the table already exists, ensure it has proper columns.
*/
function lytics_update_9001()
{
$schema = Database::getConnection()->schema();
$table_name = 'lytics_experience';
$table = [
'description' => 'Table for storing Lytics Experience entities.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique ID for the Lytics Experience entity.',
],
'title' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Title of the Lytics Experience entity.',
],
'description' => [
'type' => 'text',
'size' => 'big',
'description' => 'Description of the Lytics Experience entity.',
],
'config' => [
'type' => 'text',
'size' => 'big',
'description' => 'Configuration of the Lytics Experience entity.',
],
'status' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => 'draft',
'description' => 'Status of the Lytics Experience entity.',
],
],
'primary key' => ['id'],
];
// Check if the table already exists.
if ($schema->tableExists($table_name)) {
foreach ($table['fields'] as $field_name => $field_definition) {
// If the field does not exist in the existing table, add it.
if (!$schema->fieldExists($table_name, $field_name)) {
$schema->addField($table_name, $field_name, $field_definition);
}
}
} else {
// Create the table if it doesn't exist.
$schema->createTable($table_name, $table);
}
}
/**
* Implements hook_update_N().
*
* Rename the existing lytics_experience table to lytics_widget if it exists. In addition, verify that the column definitions are correct. If no table exists, create lytics_widget table.
*/
function lytics_update_9002()
{
$schema = Database::getConnection()->schema();
$table_name = 'lytics_widget';
// Check if the lytics_experience table exists.
if ($schema->tableExists('lytics_experience')) {
// If it exists, rename it to lytics_widget.
$schema->renameTable('lytics_experience', $table_name);
}
// Define the schema for the lytics_widget table.
$table = [
'description' => 'Table for storing Lytics Widget entities.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique ID for the Lytics Widget entity.',
],
'title' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Title of the Lytics Widget entity.',
],
'description' => [
'type' => 'text',
'size' => 'big',
'description' => 'Description of the Lytics Widget entity.',
],
'config' => [
'type' => 'text',
'size' => 'big',
'description' => 'Configuration of the Lytics Widget entity.',
],
'status' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => 'draft',
'description' => 'Status of the Lytics Widget entity.',
],
],
'primary key' => ['id'],
];
// Check if the lytics_widget table exists.
if ($schema->tableExists($table_name)) {
foreach ($table['fields'] as $field_name => $field_definition) {
// If the field does not exist in the existing table, add it.
if (!$schema->fieldExists($table_name, $field_name)) {
$schema->addField($table_name, $field_name, $field_definition);
}
}
} else {
// Create the table if it doesn't exist.
$schema->createTable($table_name, $table);
}
}