| Line 163... |
Line 163... |
| 163 |
setting field removed to non-null value, then creating a new record,
|
163 |
setting field removed to non-null value, then creating a new record,
|
| 164 |
we can track ownership of locations.
|
164 |
we can track ownership of locations.
|
| 165 |
*/
|
165 |
*/
|
| 166 |
drop table if exists owner_location;
|
166 |
drop table if exists owner_location;
|
| 167 |
create table owner_location (
|
167 |
create table owner_location (
|
| 168 |
owner_location_id int unsigned not null auto_increment,
|
- |
|
| 169 |
owner_id int unsigned not null references owner( owner_id ),
|
168 |
owner_id int unsigned not null references owner( owner_id ),
|
| 170 |
location_id int unsigned not null references location( location_id ),
|
169 |
location_id int unsigned not null references location( location_id ),
|
| 171 |
created date default current_timestamp comment 'date record was created',
|
170 |
created date default current_timestamp comment 'date record was created',
|
| 172 |
removed date comment 'date record was removed',
|
171 |
removed date comment 'date record was removed',
|
| 173 |
index location_device ( owner_id,location_id ),
|
- |
|
| 174 |
primary key (owner_location_id)
|
172 |
unique key (owner_id,location_id, removed)
|
| 175 |
) comment 'links ownership of a location to a owner';
|
173 |
) comment 'links ownership of a location to a owner';
|
| 176 |
|
174 |
|
| 177 |
/*
|
175 |
/*
|
| 178 |
Set location of a device. These records are not deleted, but by
|
176 |
Set location of a device. These records are not deleted, but by
|
| 179 |
setting field removed to non-null value, then creating a new record,
|
177 |
setting field removed to non-null value, then creating a new record,
|
| 180 |
we can track movement of devices.
|
178 |
we can track movement of devices.
|
| 181 |
*/
|
179 |
*/
|
| 182 |
drop table if exists location_device;
|
180 |
drop table if exists location_device;
|
| 183 |
create table location_device (
|
181 |
create table location_device (
|
| 184 |
location_device_id int unsigned not null auto_increment,
|
- |
|
| 185 |
location_id int unsigned not null references location( location_id ),
|
182 |
location_id int unsigned not null references location( location_id ),
|
| 186 |
device_id int unsigned not null references device( device_id ),
|
183 |
device_id int unsigned not null references device( device_id ),
|
| 187 |
created date default current_timestamp comment 'date record was created',
|
184 |
created date default current_timestamp comment 'date record was created',
|
| 188 |
removed date comment 'date record was removed',
|
185 |
removed date comment 'date record was removed',
|
| 189 |
index location_device ( location_id,device_id ),
|
- |
|
| 190 |
primary key (location_device_id)
|
186 |
unique key (location_id,device_id, removed)
|
| 191 |
) comment 'links a device to its location';
|
187 |
) comment 'links a device to its location';
|
| 192 |
|
188 |
|
| 193 |
/*
|
189 |
/*
|
| 194 |
Set ownership of a device. These records are not deleted, but by
|
190 |
Set ownership of a device. These records are not deleted, but by
|
| 195 |
setting field removed to non-null value, then creating a new record,
|
191 |
setting field removed to non-null value, then creating a new record,
|
| 196 |
we can track ownership of devices.
|
192 |
we can track ownership of devices.
|
| 197 |
*/
|
193 |
*/
|
| 198 |
drop table if exists owner_device;
|
194 |
drop table if exists owner_device;
|
| 199 |
create table owner_device (
|
195 |
create table owner_device (
|
| 200 |
owner_device_id int unsigned not null auto_increment,
|
- |
|
| 201 |
owner_id int unsigned not null references owner( owner_id ),
|
196 |
owner_id int unsigned not null references owner( owner_id ),
|
| 202 |
device_id int unsigned not null references device( device_id ),
|
197 |
device_id int unsigned not null references device( device_id ),
|
| 203 |
created date default current_timestamp comment 'date record was created',
|
198 |
created date default current_timestamp comment 'date record was created',
|
| 204 |
removed date comment 'date record was removed',
|
199 |
removed date default null comment 'date record was removed',
|
| 205 |
index owner_device( owner_id, device_id ),
|
- |
|
| 206 |
primary key ( owner_device_id )
|
200 |
unique key ( owner_id, device_id, removed )
|
| 207 |
) comment 'links a device to its owner';
|
201 |
) comment 'links a device to its owner';
|
| 208 |
|
202 |
|
| 209 |
/*
|
203 |
/*
|
| 210 |
There can be a parent/child relationship with devices. For example, a virtual
|
204 |
There can be a parent/child relationship with devices. For example, a virtual
|
| 211 |
resides on a DOM0, so the parent_id of a DOMU is the device_id of the DOM0.
|
205 |
resides on a DOM0, so the parent_id of a DOMU is the device_id of the DOM0.
|
| 212 |
or, a printer can be attached to a computer, so the printers parent is the
|
206 |
or, a printer can be attached to a computer, so the printers parent is the
|
| 213 |
device_id of the computer it is attached to.
|
207 |
device_id of the computer it is attached to.
|
| 214 |
*/
|
208 |
*/
|
| 215 |
drop table if exists device_device;
|
209 |
drop table if exists device_device;
|
| 216 |
create table device_device (
|
210 |
create table device_device (
|
| 217 |
device_device_id int unsigned not null auto_increment,
|
- |
|
| 218 |
device_id int unsigned not null references device( device_id ),
|
211 |
device_id int unsigned not null references device( device_id ),
|
| 219 |
parent_id int unsigned not null references device( device_id ),
|
212 |
parent_id int unsigned not null references device( device_id ),
|
| 220 |
created date default current_timestamp comment 'date record was created',
|
213 |
created date default current_timestamp comment 'date record was created',
|
| 221 |
removed date comment 'date record was removed',
|
214 |
removed date default null comment 'date record was removed',
|
| 222 |
index device_device( device_id, parent_id ),
|
- |
|
| 223 |
primary key ( device_device_id )
|
215 |
unique key ( device_id, parent_id, removed )
|
| 224 |
) comment 'links a device to another device';
|
216 |
) comment 'links a device to another device';
|
| 225 |
|
217 |
|
| 226 |
|
218 |
|
| 227 |
/*
|
219 |
/*
|
| 228 |
Some views so we don't have to reinvent the wheel when we're trying
|
220 |
Some views so we don't have to reinvent the wheel when we're trying
|
| Line 251... |
Line 243... |
| 251 |
virtualization to show a virtual machine is on a particular
|
243 |
virtualization to show a virtual machine is on a particular
|
| 252 |
hypervisor
|
244 |
hypervisor
|
| 253 |
*/
|
245 |
*/
|
| 254 |
create or replace view view_device_device as
|
246 |
create or replace view view_device_device as
|
| 255 |
select
|
247 |
select
|
| 256 |
device_device_id,
|
- |
|
| 257 |
device_device.device_id,
|
248 |
device_device.device_id,
|
| 258 |
device.name device_name,
|
249 |
device.name device_name,
|
| 259 |
device_device.parent_id,
|
250 |
device_device.parent_id,
|
| 260 |
parent.name parent_name,
|
251 |
parent.name parent_name,
|
| 261 |
device_device.created,
|
252 |
device_device.created,
|
| 262 |
device_device.removed
|
253 |
device_device.removed
|
| 263 |
from
|
254 |
from
|
| 264 |
device
|
255 |
device
|
| 265 |
join device_device using (device_id)
|
256 |
join device_device using (device_id)
|
| 266 |
join device parent on (device_device.parent_id = parent.device_id);
|
257 |
join device parent on (device_device.parent_id = parent.device_id)
|
| - |
|
258 |
where
|
| - |
|
259 |
device_device.removed is null;
|
| - |
|
260 |
|
| - |
|
261 |
/*
|
| - |
|
262 |
View combines view_device_types, device, and the parent relationship
|
| - |
|
263 |
(device_device) to give us all the information about one simple device
|
| - |
|
264 |
*/
|
| - |
|
265 |
create or replace view view_device as
|
| - |
|
266 |
select
|
| - |
|
267 |
device.device_id,
|
| - |
|
268 |
device.uuid,
|
| - |
|
269 |
device.serial,
|
| - |
|
270 |
device.name device,
|
| - |
|
271 |
device.created device_created,
|
| - |
|
272 |
device.removed device_removed,
|
| - |
|
273 |
view_device_types.device_types,
|
| - |
|
274 |
view_device_types.device_type_ids,
|
| - |
|
275 |
view_device_device.parent_id,
|
| - |
|
276 |
view_device_device.parent_name parent,
|
| - |
|
277 |
view_device_device.created parent_added,
|
| - |
|
278 |
view_device_device.removed parent_removed
|
| - |
|
279 |
from
|
| - |
|
280 |
device
|
| - |
|
281 |
join view_device_types using (device_id)
|
| - |
|
282 |
left outer join view_device_device using (device_id);
|
| - |
|
283 |
|
| 267 |
|
284 |
|
| - |
|
285 |
/*
|
| 268 |
/* link owners and locations together */
|
286 |
link owners and locations together for current connection only
|
| - |
|
287 |
Note that even if an owner doesn't have a location, it will still
|
| - |
|
288 |
show up here with a null location
|
| - |
|
289 |
*/
|
| 269 |
create or replace view view_owner_location as
|
290 |
create or replace view view_owner_location as
|
| 270 |
select distinct
|
291 |
select distinct
|
| 271 |
owner.name owner,
|
292 |
owner.name owner,
|
| 272 |
owner.owner_id,
|
293 |
owner.owner_id,
|
| 273 |
location.name location,
|
294 |
location.name location,
|
| 274 |
location.location_id,
|
295 |
location.location_id,
|
| 275 |
owner_location.created,
|
296 |
owner_location.created,
|
| 276 |
owner_location.removed
|
297 |
owner_location.removed
|
| 277 |
from
|
298 |
from
|
| 278 |
owner
|
299 |
owner
|
| 279 |
join owner_location using (owner_id)
|
300 |
left outer join owner_location using (owner_id)
|
| 280 |
join location using (location_id);
|
301 |
left outer join location using (location_id)
|
| - |
|
302 |
where
|
| - |
|
303 |
owner_location.removed is null;
|
| - |
|
304 |
|
| - |
|
305 |
/*
|
| - |
|
306 |
link location to device in such a way as location will still show up
|
| - |
|
307 |
if it has no devices, ie left outer join
|
| - |
|
308 |
will not display historical where location_device is not null (ie, moved)
|
| - |
|
309 |
*/
|
| - |
|
310 |
create or replace view view_location_device as
|
| - |
|
311 |
select
|
| - |
|
312 |
location.location_id,
|
| - |
|
313 |
location.name location,
|
| - |
|
314 |
location.created location_created,
|
| - |
|
315 |
location.removed location_removed,
|
| - |
|
316 |
view_device.device_id,
|
| - |
|
317 |
view_device.device device,
|
| - |
|
318 |
view_device.device_created,
|
| - |
|
319 |
view_device.device_removed
|
| - |
|
320 |
from
|
| - |
|
321 |
location
|
| - |
|
322 |
left outer join location_device using (location_id)
|
| - |
|
323 |
left outer join view_device using (device_id)
|
| - |
|
324 |
where
|
| - |
|
325 |
location_device.removed is null;
|
| 281 |
|
326 |
|
| - |
|
327 |
/*
|
| - |
|
328 |
link owner to device in such a way as owner will still show up
|
| - |
|
329 |
if it has no devices, ie left outer join
|
| - |
|
330 |
*/
|
| - |
|
331 |
create or replace view view_owner_device as
|
| - |
|
332 |
select
|
| - |
|
333 |
owner.owner_id,
|
| - |
|
334 |
owner.name owner,
|
| - |
|
335 |
owner.created owner_created,
|
| - |
|
336 |
owner.removed owner_removed,
|
| - |
|
337 |
view_device.device_id,
|
| - |
|
338 |
view_device.device,
|
| - |
|
339 |
view_device.device_created,
|
| - |
|
340 |
view_device.device_removed
|
| - |
|
341 |
from
|
| - |
|
342 |
owner
|
| - |
|
343 |
left outer join owner_device using (owner_id)
|
| - |
|
344 |
left outer join view_device using (device_id)
|
| - |
|
345 |
where
|
| - |
|
346 |
owner_device.removed is null;
|
| 282 |
/*
|
347 |
/*
|
| 283 |
Mongo view that gets all the information together to display
|
348 |
Mongo view that gets all the information together to display
|
| 284 |
device name, location, owner and type(s)
|
349 |
device name, location, owner and type(s)
|
| 285 |
NOTE: we are limiting the "part of" to only currently active
|
350 |
NOTE: we are limiting the "part of" to only currently active
|
| 286 |
relationships
|
351 |
relationships
|
| 287 |
*/
|
352 |
*/
|
| 288 |
|
353 |
|
| 289 |
create or replace view view_device_location_owner_type as
|
354 |
create or replace view view_device_location_owner_type as
|
| 290 |
select
|
355 |
select
|
| 291 |
device.device_id device_id,
|
356 |
view_owner_device.device_id device_id,
|
| 292 |
device.uuid uuid,
|
- |
|
| 293 |
device.serial serial,
|
- |
|
| 294 |
device.name device,
|
357 |
view_owner_device.device device,
|
| 295 |
device.created device_created,
|
358 |
view_owner_device.device_created,
|
| 296 |
device.removed device_removed,
|
359 |
view_owner_device.device_removed,
|
| 297 |
view_device_types.device_types,
|
360 |
view_owner_device.owner_id owner_id,
|
| 298 |
location.location_id location_id,
|
- |
|
| 299 |
location.name location,
|
361 |
view_owner_device.owner,
|
| 300 |
location.created location_created,
|
362 |
view_owner_device.owner_created,
|
| 301 |
location.removed location_removed,
|
363 |
view_owner_device.owner_removed,
|
| 302 |
dev_owner.owner_id owner_id,
|
364 |
view_location_device.location_id location_id,
|
| 303 |
dev_owner.name owner,
|
365 |
view_location_device.location,
|
| 304 |
dev_owner.created owner_created,
|
366 |
view_location_device.location_created,
|
| 305 |
dev_owner.removed owner_removed,
|
367 |
view_location_device.location_removed,
|
| 306 |
loc_owner.name location_owner,
|
368 |
view_owner_location.owner location_owner,
|
| 307 |
loc_owner.owner_id location_owner_id,
|
369 |
view_owner_location.owner_id location_owner_id,
|
| 308 |
loc_owner.created location_owner_created,
|
- |
|
| 309 |
loc_owner.removed location_owner_removed,
|
- |
|
| 310 |
parent.parent_id,
|
370 |
view_device.parent_id,
|
| 311 |
parent.parent_name parent,
|
371 |
view_device.parent,
|
| 312 |
parent.created parent_created,
|
- |
|
| 313 |
parent.removed parent_removed
|
372 |
view_device.device_types
|
| 314 |
from
|
373 |
from
|
| 315 |
device
|
374 |
view_owner_device
|
| 316 |
join view_device_types using (device_id )
|
375 |
join view_location_device using (device_id)
|
| 317 |
join owner_device using (device_id)
|
376 |
join view_device using (device_id)
|
| 318 |
join owner dev_owner on (owner_device.owner_id = dev_owner.owner_id)
|
- |
|
| 319 |
join location_device using (device_id)
|
- |
|
| 320 |
join location using (location_id)
|
377 |
join view_owner_location using (location_id);
|
| 321 |
join owner_location on (location.location_id = owner_location.location_id)
|
- |
|
| 322 |
join owner loc_owner on (loc_owner.owner_id = owner_location.owner_id)
|
- |
|
| 323 |
left outer join view_device_device parent using (device_id)
|
- |
|
| 324 |
where
|
- |
|
| 325 |
parent.removed is null
|
- |
|
| 326 |
and owner_device.removed is null
|
- |
|
| 327 |
and location_device.removed is null
|
- |
|
| 328 |
and owner_location.removed is null;
|
- |
|
| 329 |
|
378 |
|
| 330 |
|
379 |
|
| 331 |
insert into _system ( key_value, group_name, key_name ) values ( '0.1','database','version' )
|
380 |
insert into _system ( key_value, group_name, key_name ) values ( '0.1','database','version' )
|
| 332 |
on duplicate key update key_value = '0.1';
|
381 |
on duplicate key update key_value = '0.1';
|
| 333 |
insert into _system ( key_value, group_name, key_name ) values ( '', 'program', 'motd' );
|
382 |
insert into _system ( key_value, group_name, key_name ) values ( '', 'program', 'motd' );
|