Create revisions for each stored time entry
[project/time_entry.git] / time_entry.install
1 <?php
2
3 /**
4 * Implements hook_install().
5 */
6 function time_entry_install() {
7 if (!drupal_installation_attempted()) {
8 $type = entity_create('time_entry_type', array(
9 'type' => 'default',
10 'label' => t('Default'),
11 'weight' => 0,
12 'data' => array(),
13 ));
14 $type->save();
15 }
16 }
17
18 /**
19 * Implements hook_enable().
20 */
21 function time_entry_enable() {
22 if(db_table_exists('time_entry_type') == FALSE) {
23 drupal_install_schema('time_entry');
24 }
25 }
26
27 /**
28 * Implements hook_schema().
29 */
30 function time_entry_schema() {
31 $schema['time_entry'] = array(
32 'fields' => array(
33 'id' => array(
34 'type' => 'serial',
35 'not null' => TRUE,
36 'description' => 'Internal identifier for time entry.',
37 ),
38 'vid' => array(
39 'type' => 'int',
40 'not null' => TRUE,
41 'default' => 0,
42 'description' => 'The version id.',
43 ),
44 'time' => array(
45 'description' => 'Base time represented as Unix timestamp (on UTC).',
46 'type' => 'int',
47 'not null' => TRUE,
48 'default' => 0,
49 ),
50 'duration' => array(
51 'description' => 'Time entry duration, in seconds.',
52 'type' => 'int',
53 'not null' => TRUE,
54 'default' => 0,
55 ),
56 'type' => array(
57 'description' => 'Type of this time entry.',
58 'type' => 'varchar',
59 'length' => 32,
60 'not null' => TRUE,
61 'default' => '',
62 ),
63 ),
64 'primary key' => array('id'),
65 );
66
67 $schema['time_entry_revision'] = array(
68 'fields' => array(
69 'id' => array(
70 'type' => 'int',
71 'not null' => TRUE,
72 'default' => 0,
73 'description' => 'The {time_entry} this version belongs to.',
74 ),
75 'vid' => array(
76 'description' => 'The primary identifier for this version.',
77 'type' => 'serial',
78 'unsigned' => TRUE,
79 'not null' => TRUE,
80 ),
81 'time' => array(
82 'description' => 'Base time represented as Unix timestamp (on UTC).',
83 'type' => 'int',
84 'not null' => TRUE,
85 'default' => 0,
86 ),
87 'duration' => array(
88 'description' => 'Time entry duration, in seconds.',
89 'type' => 'int',
90 'not null' => TRUE,
91 'default' => 0,
92 ),
93 'type' => array(
94 'description' => 'Type of this time entry.',
95 'type' => 'varchar',
96 'length' => 32,
97 'not null' => TRUE,
98 'default' => '',
99 ),
100 ),
101 'primary key' => array('vid'),
102 );
103
104 $schema['time_entry_type'] = array(
105 'description' => 'Stores information about all defined time entry types.',
106 'fields' => array(
107 'id' => array(
108 'type' => 'serial',
109 'not null' => TRUE,
110 'description' => 'Primary Key: Unique profile type ID.',
111 ),
112 'type' => array(
113 'description' => 'The machine-readable name of this profile type.',
114 'type' => 'varchar',
115 'length' => 32,
116 'not null' => TRUE,
117 ),
118 'label' => array(
119 'description' => 'The human-readable name of this profile type.',
120 'type' => 'varchar',
121 'length' => 255,
122 'not null' => TRUE,
123 'default' => '',
124 ),
125 'weight' => array(
126 'type' => 'int',
127 'not null' => TRUE,
128 'default' => 0,
129 'size' => 'tiny',
130 'description' => 'The weight of this profile type in relation to others.',
131 ),
132 'data' => array(
133 'type' => 'text',
134 'not null' => FALSE,
135 'size' => 'big',
136 'serialize' => TRUE,
137 'description' => 'A serialized array of additional data related to this profile type.',
138 ),
139 'status' => array(
140 'type' => 'int',
141 'not null' => TRUE,
142 // Set the default to ENTITY_CUSTOM without using the constant as it is
143 // not safe to use it at this point.
144 'default' => 0x01,
145 'size' => 'tiny',
146 'description' => 'The exportable status of the entity.',
147 ),
148 'module' => array(
149 'description' => 'The name of the providing module if the entity has been defined in code.',
150 'type' => 'varchar',
151 'length' => 255,
152 'not null' => FALSE,
153 ),
154 ),
155 'primary key' => array('id'),
156 'unique keys' => array(
157 'type' => array('type'),
158 ),
159 );
160
161 return $schema;
162 }
163
164 /**
165 * Create revision database structure.
166 */
167 function time_entry_update_7100() {
168 if (!db_field_exists('time_entry', 'vid')) {
169 $vid = array(
170 'type' => 'int',
171 'not null' => TRUE,
172 'default' => 0,
173 'description' => 'The version id.',
174 'initial' => 0,
175 );
176 db_add_field('time_entry', 'vid', $vid);
177 }
178 if (!db_table_exists('time_entry_revision')) {
179 $time_entry_revision = array(
180 'fields' => array(
181 'id' => array(
182 'type' => 'int',
183 'not null' => TRUE,
184 'default' => 0,
185 'description' => 'The {time_entry} this version belongs to.',
186 ),
187 'vid' => array(
188 'description' => 'The primary identifier for this version.',
189 'type' => 'serial',
190 'unsigned' => TRUE,
191 'not null' => TRUE,
192 ),
193 'time' => array(
194 'description' => 'Base time represented as Unix timestamp (on UTC).',
195 'type' => 'int',
196 'not null' => TRUE,
197 'default' => 0,
198 ),
199 'duration' => array(
200 'description' => 'Time entry duration, in seconds.',
201 'type' => 'int',
202 'not null' => TRUE,
203 'default' => 0,
204 ),
205 'type' => array(
206 'description' => 'Type of this time entry.',
207 'type' => 'varchar',
208 'length' => 32,
209 'not null' => TRUE,
210 'default' => '',
211 ),
212 ),
213 'primary key' => array('vid'),
214 );
215 db_create_table('time_entry_revision', $time_entry_revision);
216 }
217 }
218
219 /**
220 * Create revisions for each stored time entry.
221 */
222 function time_entry_update_7105(&$sandbox) {
223 if (!isset($sandbox['progress'])) {
224 $sandbox['progress'] = 0;
225 $sandbox['current_id'] = 0;
226 $sandbox['max'] = db_query('SELECT COUNT(DISTINCT id) FROM {time_entry} WHERE vid=0')->fetchField();
227 }
228
229 $time_entries = db_select('time_entry', 't')
230 ->fields('t', array('id', 'time', 'duration', 'type', 'vid'))
231 ->condition('id', $sandbox['current_id'], '>')
232 ->condition('t.vid', 0, '=')
233 ->range(0, 3)
234 ->orderBy('id', 'ASC')
235 ->execute();
236
237 foreach ($time_entries as $time_entry) {
238 // Lazy mode: Since this is a new table, I'm sure that id is available as vid.
239 $time_entry->vid = $time_entry->id;
240
241 drupal_write_record('time_entry', $time_entry, 'id');
242 drupal_write_record('time_entry_revision', $time_entry);
243
244 $sandbox['progress']++;
245 $sandbox['current_id'] = $time_entry->id;
246 }
247
248 $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
249
250 return t('Created revisions for each stored time entry.');
251 }