| Line 1... | 
            Line 1... | 
          
          
            | 1 | 
            /*
  | 
            1 | 
            /*
  | 
          
          
            | - | 
               | 
            2 | 
            Remove everything wit
  | 
          
          
            | - | 
               | 
            3 | 
            drop table if exists _config;
  | 
          
          
            | - | 
               | 
            4 | 
            drop table if exists _menu;
  | 
          
          
            | - | 
               | 
            5 | 
            drop table if exists _permissions;
  | 
          
          
            | - | 
               | 
            6 | 
            drop table if exists _permissions_categories;
  | 
          
          
            | - | 
               | 
            7 | 
            drop table if exists _system;
  | 
          
          
            | - | 
               | 
            8 | 
            drop table if exists _users_permissions;
  | 
          
          
            | - | 
               | 
            9 | 
            drop view if exists _view_users_permissions;
  | 
          
          
            | - | 
               | 
            10 | 
            drop table if exists client;
  | 
          
          
            | - | 
               | 
            11 | 
            drop table if exists client_device;
  | 
          
          
            | - | 
               | 
            12 | 
            drop table if exists client_site;
  | 
          
          
            | - | 
               | 
            13 | 
            drop table if exists device;
  | 
          
          
            | - | 
               | 
            14 | 
            drop table if exists device_device;
  | 
          
          
            | - | 
               | 
            15 | 
            drop table if exists device_device_type;
  | 
          
          
            | - | 
               | 
            16 | 
            drop table if exists device_type;
  | 
          
          
            | - | 
               | 
            17 | 
            drop table if exists site;
  | 
          
          
            | - | 
               | 
            18 | 
            drop table if exists site_device;
  | 
          
          
            | - | 
               | 
            19 | 
            drop table if exists temp;
  | 
          
          
            | - | 
               | 
            20 | 
            drop view if exists view_device_types;
  | 
          
          
            | - | 
               | 
            21 | 
            drop view if exists view_device_site_client_type;
  | 
          
          
            | - | 
               | 
            22 | 
             
  | 
          
          
            | - | 
               | 
            23 | 
            */
  | 
          
          
            | - | 
               | 
            24 | 
             
  | 
          
          
            | - | 
               | 
            25 | 
            /*
  | 
          
          
            | 2 | 
               The base install of CAMP2.
  | 
            26 | 
               The base install of CAMP2.
  | 
          
          
            | 3 | 
             
  | 
            27 | 
             
  | 
          
          
            | 4 | 
               Just track devices. A device can be any arbitrary item.
  | 
            28 | 
               Just track devices. A device can be any arbitrary item.
  | 
          
          
            | 5 | 
               A device has one owner, and is located at one site (location)
  | 
            29 | 
               A device has one owner, and is located at one location (location)
  | 
          
          
            | 6 | 
               A site also has an owner. Devices can be located on locations
  | 
            30 | 
               A location also has an owner. Devices can be located on locations
  | 
          
          
            | 7 | 
               owned by a different client, ie when a device is lent to a client
  | 
            31 | 
               owned by a different owner, ie when a device is lent to a owner
  | 
          
          
            | 8 | 
               or colocated at a third party NOC.
  | 
            32 | 
               or colocated at a third party NOC.
  | 
          
          
            | 9 | 
               
  | 
            33 | 
               
  | 
          
          
            | 10 | 
               This is very basic. It only tracks device ownership and locations,
  | 
            34 | 
               This is very basic. It only tracks device ownership and locations,
  | 
          
          
            | 11 | 
               and movement between them (via the created and removed fields).
  | 
            35 | 
               and movement between them (via the created and removed fields).
  | 
          
          
            | 12 | 
               When a device is moved to a different location, or sold to a different
  | 
            36 | 
               When a device is moved to a different location, or sold to a different
  | 
          
          
            | 13 | 
               client, the old record in the linking field has its removed field 
  | 
            37 | 
               owner, the old record in the linking field has its removed field 
  | 
          
          
            | 14 | 
               updated and a new record created with a created date. With this, we
  | 
            38 | 
               updated and a new record created with a created date. With this, we
  | 
          
          
            | 15 | 
               can track the lifespan of a device.
  | 
            39 | 
               can track the lifespan of a device.
  | 
          
          
            | 16 | 
               
  | 
            40 | 
               
  | 
          
          
            | 17 | 
               It is assumed various modules will be created to extend the capabilities
  | 
            41 | 
               It is assumed various modules will be created to extend the capabilities
  | 
          
          
            | 18 | 
               of this basic structure. However, modules should not modify the basic
  | 
            42 | 
               of this basic structure. However, modules should not modify the basic
  | 
          
          
            | Line 43... | 
            Line 67... | 
          
          
            | 43 | 
            create table _menu (
  | 
            67 | 
            create table _menu (
  | 
          
          
            | 44 | 
               _menu_id       int unsigned not null auto_increment,
  | 
            68 | 
               _menu_id       int unsigned not null auto_increment,
  | 
          
          
            | 45 | 
               parent_id      int unsigned default null references _menu (_menu_id),
  | 
            69 | 
               parent_id      int unsigned default null references _menu (_menu_id),
  | 
          
          
            | 46 | 
               caption        varchar(20) not null comment 'Caption displayed for menu',
  | 
            70 | 
               caption        varchar(20) not null comment 'Caption displayed for menu',
  | 
          
          
            | 47 | 
               url            varchar(64) default null comment 'optional URL when they click here',
  | 
            71 | 
               url            varchar(64) default null comment 'optional URL when they click here',
  | 
          
          
            | - | 
               | 
            72 | 
               shortname      varchar(16) default null comment 'used for permissions to determine if we display',
  | 
          
          
            | 48 | 
               primary key    (_menu_id)
  | 
            73 | 
               primary key    (_menu_id)
  | 
          
          
            | 49 | 
            ) comment = 'Menus for the application';
  | 
            74 | 
            ) comment = 'Menus for the application';
  | 
          
          
            | 50 | 
             
  | 
            75 | 
             
  | 
          
          
            | - | 
               | 
            76 | 
            /*
  | 
          
          
            | - | 
               | 
            77 | 
               insert the menu options for the main program
  | 
          
          
            | - | 
               | 
            78 | 
            */
  | 
          
          
            | 51 | 
            insert into camp2._menu values (null,null,'Home', '/index.php');
  | 
            79 | 
            insert into camp2._menu values (null,null,'Home', '/index.php', 'menu_home' );
  | 
          
          
            | - | 
               | 
            80 | 
            insert into camp2._menu select null,_menu_id,'Owners', '/index.php?action=owner', 'menu_owner' from camp2._menu where caption = 'Home';
  | 
          
          
            | - | 
               | 
            81 | 
            insert into camp2._menu select null,_menu_id,'Locations', '/index.php?action=location', 'menu_location' from camp2._menu where caption = 'Home';
  | 
          
          
            | - | 
               | 
            82 | 
            insert into camp2._menu select null,_menu_id,'Devices', '/index.php?action=device', 'menu_device' from camp2._menu where caption = 'Home';
  | 
          
          
            | - | 
               | 
            83 | 
            insert into camp2._menu values (null,null,'Reports', '/index.php?action=report', 'menu_report' );
  | 
          
          
            | 52 | 
             
  | 
            84 | 
             
  | 
          
          
            | 53 | 
            /*
  | 
            85 | 
            /*
  | 
          
          
            | 54 | 
               simple table to hold ownership information
  | 
            86 | 
               simple table to hold ownership information
  | 
          
          
            | 55 | 
            */
  | 
            87 | 
            */
  | 
          
          
            | 56 | 
            drop table if exists client;
  | 
            88 | 
            drop table if exists owner;
  | 
          
          
            | 57 | 
            create table client (
  | 
            89 | 
            create table owner (
  | 
          
          
            | 58 | 
               client_id      int unsigned not null auto_increment,
  | 
            90 | 
               owner_id       int unsigned not null auto_increment,
  | 
          
          
            | 59 | 
               name           varchar(64) comment 'name of client',
  | 
            91 | 
               name           varchar(64) comment 'name of owner',
  | 
          
          
            | 60 | 
               created        date comment 'date record was created',
  | 
            92 | 
               created        date comment 'date record was created',
  | 
          
          
            | 61 | 
               removed        date comment 'date record was removed',
  | 
            93 | 
               removed        date comment 'date record was removed',
  | 
          
          
            | 62 | 
               primary key    (client_id)
  | 
            94 | 
               primary key    (owner_id)
  | 
          
          
            | 63 | 
            ) comment 'hold client information';
  | 
            95 | 
            ) comment 'hold owner information';
  | 
          
          
            | 64 | 
             
  | 
            96 | 
             
  | 
          
          
            | 65 | 
            /*
  | 
            97 | 
            /*
  | 
          
          
            | 66 | 
               simple table to hold site where a device is located
  | 
            98 | 
               simple table to hold location where a device is located
  | 
          
          
            | 67 | 
            */
  | 
            99 | 
            */
  | 
          
          
            | 68 | 
            drop table if exists site;
  | 
            100 | 
            drop table if exists location;
  | 
          
          
            | 69 | 
            create table site (
  | 
            101 | 
            create table location (
  | 
          
          
            | 70 | 
               site_id        int unsigned not null auto_increment,
  | 
            102 | 
               location_id    int unsigned not null auto_increment,
  | 
          
          
            | 71 | 
               name           varchar(64) comment 'name of site',
  | 
            103 | 
               name           varchar(64) comment 'name of location',
  | 
          
          
            | 72 | 
               created        date comment 'date record was created',
  | 
            104 | 
               created        date comment 'date record was created',
  | 
          
          
            | 73 | 
               removed        date comment 'date record was removed',
  | 
            105 | 
               removed        date comment 'date record was removed',
  | 
          
          
            | 74 | 
               primary key    (site_id)
  | 
            106 | 
               primary key    (location_id)
  | 
          
          
            | 75 | 
            ) comment 'hold site information';
  | 
            107 | 
            ) comment 'hold location information';
  | 
          
          
            | 76 | 
             
  | 
            108 | 
             
  | 
          
          
            | 77 | 
            /*
  | 
            109 | 
            /*
  | 
          
          
            | 78 | 
               table which holds a device type, such as server, workstation
  | 
            110 | 
               table which holds a device type, such as server, workstation
  | 
          
          
            | 79 | 
               printer, virtual, etc...
  | 
            111 | 
               printer, virtual, etc...
  | 
          
          
            | 80 | 
            */
  | 
            112 | 
            */
  | 
          
          
            | 81 | 
            drop table if exists device_type;
  | 
            113 | 
            drop table if exists device_type;
  | 
          
          
            | 82 | 
            create table device_type (
  | 
            114 | 
            create table device_type (
  | 
          
          
            | 83 | 
               device_type_id int unsigned not null auto_increment,
  | 
            115 | 
               device_type_id int unsigned not null auto_increment,
  | 
          
          
            | 84 | 
               name           varchar(64) comment 'name of device type',
  | 
            116 | 
               name           varchar(64) comment 'name of device type',
  | 
          
          
            | 85 | 
               show_as_system boolean comment 'if true, this is a system, ie a computer or virtual',
  | 
            117 | 
               is_system      boolean comment 'if true, this is a system, ie a computer or virtual',
  | 
          
          
            | 86 | 
               created        date comment 'date record was created',
  | 
            118 | 
               created        date comment 'date record was created',
  | 
          
          
            | 87 | 
               removed        date comment 'date record was removed',
  | 
            119 | 
               removed        date comment 'date record was removed',
  | 
          
          
            | 88 | 
               primary key    (device_type_id)
  | 
            120 | 
               primary key    (device_type_id)
  | 
          
          
            | 89 | 
            ) comment 'simple child table to determine the type of device we have';
  | 
            121 | 
            ) comment 'simple child table to determine the type of device we have';
  | 
          
          
            | 90 | 
               
  | 
            122 | 
               
  | 
          
          
            | Line 124... | 
            Line 156... | 
          
          
            | 124 | 
               device_type_id int unsigned not null references device_type( device_type_id ),
  | 
            156 | 
               device_type_id int unsigned not null references device_type( device_type_id ),
  | 
          
          
            | 125 | 
               primary key (device_id,device_type_id)
  | 
            157 | 
               primary key (device_id,device_type_id)
  | 
          
          
            | 126 | 
            ) comment 'many to many join for device and device_type tables';
  | 
            158 | 
            ) comment 'many to many join for device and device_type tables';
  | 
          
          
            | 127 | 
             
  | 
            159 | 
             
  | 
          
          
            | 128 | 
            /*
  | 
            160 | 
            /*
  | 
          
          
            | 129 | 
               Set ownership of a site. These records are not deleted, but by
  | 
            161 | 
               Set ownership of a location. These records are not deleted, but by
  | 
          
          
            | 130 | 
               setting field removed to non-null value, then creating a new record,
  | 
            162 | 
               setting field removed to non-null value, then creating a new record,
  | 
          
          
            | 131 | 
               we can track ownership of sites.
  | 
            163 | 
               we can track ownership of locations.
  | 
          
          
            | 132 | 
            */
  | 
            164 | 
            */
  | 
          
          
            | 133 | 
            drop table if exists client_site;
  | 
            165 | 
            drop table if exists owner_location;
  | 
          
          
            | 134 | 
            create table client_site (
  | 
            166 | 
            create table owner_location (
  | 
          
          
            | 135 | 
               client_site_id int unsigned not null auto_increment,
  | 
            167 | 
               owner_location_id int unsigned not null auto_increment,
  | 
          
          
            | 136 | 
               client_id      int unsigned not null references client( client_id ),
  | 
            168 | 
               owner_id      int unsigned not null references owner( owner_id ),
  | 
          
          
            | 137 | 
               site_id        int unsigned not null references site( site_id ),
  | 
            169 | 
               location_id        int unsigned not null references location( location_id ),
  | 
          
          
            | 138 | 
               created        date comment 'date record was created',
  | 
            170 | 
               created        date comment 'date record was created',
  | 
          
          
            | 139 | 
               removed        date comment 'date record was removed',
  | 
            171 | 
               removed        date comment 'date record was removed',
  | 
          
          
            | 140 | 
               index          site_device ( client_id,site_id ),
  | 
            172 | 
               index          location_device ( owner_id,location_id ),
  | 
          
          
            | 141 | 
               primary key    (client_site_id)
  | 
            173 | 
               primary key    (owner_location_id)
  | 
          
          
            | 142 | 
            ) comment 'links ownership of a site to a client';   
  | 
            174 | 
            ) comment 'links ownership of a location to a owner';   
  | 
          
          
            | 143 | 
             
  | 
            175 | 
             
  | 
          
          
            | 144 | 
            /*
  | 
            176 | 
            /*
  | 
          
          
            | 145 | 
               Set location of a device. These records are not deleted, but by
  | 
            177 | 
               Set location of a device. These records are not deleted, but by
  | 
          
          
            | 146 | 
               setting field removed to non-null value, then creating a new record,
  | 
            178 | 
               setting field removed to non-null value, then creating a new record,
  | 
          
          
            | 147 | 
               we can track movement of devices.
  | 
            179 | 
               we can track movement of devices.
  | 
          
          
            | 148 | 
            */
  | 
            180 | 
            */
  | 
          
          
            | 149 | 
            drop table if exists site_device;
  | 
            181 | 
            drop table if exists location_device;
  | 
          
          
            | 150 | 
            create table site_device (
  | 
            182 | 
            create table location_device (
  | 
          
          
            | 151 | 
               site_device_id int unsigned not null auto_increment,
  | 
            183 | 
               location_device_id int unsigned not null auto_increment,
  | 
          
          
            | 152 | 
               site_id        int unsigned not null references site( site_id ),
  | 
            184 | 
               location_id        int unsigned not null references location( location_id ),
  | 
          
          
            | 153 | 
               device_id      int unsigned not null references device( device_id ),
  | 
            185 | 
               device_id      int unsigned not null references device( device_id ),
  | 
          
          
            | 154 | 
               created        date comment 'date record was created',
  | 
            186 | 
               created        date comment 'date record was created',
  | 
          
          
            | 155 | 
               removed        date comment 'date record was removed',
  | 
            187 | 
               removed        date comment 'date record was removed',
  | 
          
          
            | 156 | 
               index          site_device ( site_id,device_id ),
  | 
            188 | 
               index          location_device ( location_id,device_id ),
  | 
          
          
            | 157 | 
               primary key    (site_device_id)
  | 
            189 | 
               primary key    (location_device_id)
  | 
          
          
            | 158 | 
            ) comment 'links a device to its location';
  | 
            190 | 
            ) comment 'links a device to its location';
  | 
          
          
            | 159 | 
             
  | 
            191 | 
             
  | 
          
          
            | 160 | 
            /*
  | 
            192 | 
            /*
  | 
          
          
            | 161 | 
               Set ownership of a device. These records are not deleted, but by
  | 
            193 | 
               Set ownership of a device. These records are not deleted, but by
  | 
          
          
            | 162 | 
               setting field removed to non-null value, then creating a new record,
  | 
            194 | 
               setting field removed to non-null value, then creating a new record,
  | 
          
          
            | 163 | 
               we can track ownership of devices.
  | 
            195 | 
               we can track ownership of devices.
  | 
          
          
            | 164 | 
            */
  | 
            196 | 
            */
  | 
          
          
            | 165 | 
            drop table if exists client_device;
  | 
            197 | 
            drop table if exists owner_device;
  | 
          
          
            | 166 | 
            create table client_device (
  | 
            198 | 
            create table owner_device (
  | 
          
          
            | 167 | 
               client_device_id  int unsigned not null auto_increment,
  | 
            199 | 
               owner_device_id  int unsigned not null auto_increment,
  | 
          
          
            | 168 | 
               client_id      int unsigned not null references client( client_id ),
  | 
            200 | 
               owner_id      int unsigned not null references owner( owner_id ),
  | 
          
          
            | 169 | 
               device_id      int unsigned not null references device( device_id ),
  | 
            201 | 
               device_id      int unsigned not null references device( device_id ),
  | 
          
          
            | 170 | 
               created        date comment 'date record was created',
  | 
            202 | 
               created        date comment 'date record was created',
  | 
          
          
            | 171 | 
               removed        date comment 'date record was removed',
  | 
            203 | 
               removed        date comment 'date record was removed',
  | 
          
          
            | 172 | 
               index          client_device( client_id, device_id ),
  | 
            204 | 
               index          owner_device( owner_id, device_id ),
  | 
          
          
            | 173 | 
               primary key    ( client_device_id )
  | 
            205 | 
               primary key    ( owner_device_id )
  | 
          
          
            | 174 | 
            ) comment 'links a device to its owner';
  | 
            206 | 
            ) comment 'links a device to its owner';
  | 
          
          
            | 175 | 
             
  | 
            207 | 
             
  | 
          
          
            | 176 | 
            /*
  | 
            208 | 
            /*
  | 
          
          
            | 177 | 
               There can be a parent/child relationship with devices. For example, a virtual
  | 
            209 | 
               There can be a parent/child relationship with devices. For example, a virtual
  | 
          
          
            | 178 | 
               resides on a DOM0, so the parent_id of a DOMU is the device_id of the DOM0.
  | 
            210 | 
               resides on a DOM0, so the parent_id of a DOMU is the device_id of the DOM0.
  | 
          
          
            | Line 197... | 
            Line 229... | 
          
          
            | 197 | 
            */
  | 
            229 | 
            */
  | 
          
          
            | 198 | 
             
  | 
            230 | 
             
  | 
          
          
            | 199 | 
            /*
  | 
            231 | 
            /*
  | 
          
          
            | 200 | 
              a simple view that concats the values in device_device_type for 
  | 
            232 | 
              a simple view that concats the values in device_device_type for 
  | 
          
          
            | 201 | 
              display. Since mySQL will not allow subqueries in views, required
  | 
            233 | 
              display. Since mySQL will not allow subqueries in views, required
  | 
          
          
            | 202 | 
              to have this information in view_device_site_client_type
  | 
            234 | 
              to have this information in view_device_location_owner_type
  | 
          
          
            | 203 | 
            */
  | 
            235 | 
            */
  | 
          
          
            | 204 | 
            create view view_device_types as
  | 
            236 | 
            create or replace view view_device_types as
  | 
          
          
            | 205 | 
               select 
  | 
            237 | 
               select 
  | 
          
          
            | 206 | 
                  device_id,
  | 
            238 | 
                  device_id,
  | 
          
          
            | 207 | 
                  group_concat(distinct device_type.name) as device_types 
  | 
            239 | 
                  group_concat(distinct device_type.name) as device_types 
  | 
          
          
            | 208 | 
               from 
  | 
            240 | 
               from 
  | 
          
          
            | 209 | 
                  device_device_type 
  | 
            241 | 
                  device_device_type 
  | 
          
          
            | Line 214... | 
            Line 246... | 
          
          
            | 214 | 
            /*
  | 
            246 | 
            /*
  | 
          
          
            | 215 | 
               Mongo view that gets all the information together to display
  | 
            247 | 
               Mongo view that gets all the information together to display
  | 
          
          
            | 216 | 
               device name, location, owner and type(s)
  | 
            248 | 
               device name, location, owner and type(s)
  | 
          
          
            | 217 | 
            */
  | 
            249 | 
            */
  | 
          
          
            | 218 | 
             
  | 
            250 | 
             
  | 
          
          
            | 219 | 
            drop view if exists view_device_site_client_type;
  | 
            - | 
               | 
          
          
            | 220 | 
            create view view_device_site_client_type as
  | 
            251 | 
            create or replace view view_device_location_owner_type as
  | 
          
          
            | 221 | 
               select
  | 
            252 | 
               select
  | 
          
          
            | 222 | 
                  device.device_id device_id,
  | 
            253 | 
                  device.device_id device_id,
  | 
          
          
            | 223 | 
                  device.uuid uuid,
  | 
            254 | 
                  device.uuid uuid,
  | 
          
          
            | 224 | 
                  device.serial serial,
  | 
            255 | 
                  device.serial serial,
  | 
          
          
            | 225 | 
                  device.name device,
  | 
            256 | 
                  device.name device,
  | 
          
          
            | 226 | 
                  device.created device_created,
  | 
            257 | 
                  device.created device_created,
  | 
          
          
            | 227 | 
                  device.removed device_removed,
  | 
            258 | 
                  device.removed device_removed,
  | 
          
          
            | 228 | 
                  view_device_types.device_types,
  | 
            259 | 
                  view_device_types.device_types,
  | 
          
          
            | 229 | 
                  site.site_id site_id,
  | 
            260 | 
                  location.location_id location_id,
  | 
          
          
            | 230 | 
                  site.name site,
  | 
            261 | 
                  location.name location,
  | 
          
          
            | 231 | 
                  site.created site_created,
  | 
            262 | 
                  location.created location_created,
  | 
          
          
            | 232 | 
                  site.removed site_removed,
  | 
            263 | 
                  location.removed location_removed,
  | 
          
          
            | 233 | 
                  client.client_id client_id,
  | 
            264 | 
                  owner.owner_id owner_id,
  | 
          
          
            | 234 | 
                  client.name client,
  | 
            265 | 
                  owner.name owner,
  | 
          
          
            | 235 | 
                  client.created client_created,
  | 
            266 | 
                  owner.created owner_created,
  | 
          
          
            | 236 | 
                  client.removed client_removed
  | 
            267 | 
                  owner.removed owner_removed
  | 
          
          
            | 237 | 
               from
  | 
            268 | 
               from
  | 
          
          
            | 238 | 
                  device
  | 
            269 | 
                  device
  | 
          
          
            | 239 | 
                  join view_device_types using (device_id )
  | 
            270 | 
                  join view_device_types using (device_id )
  | 
          
          
            | 240 | 
                  join site_device using (device_id)
  | 
            271 | 
                  join location_device using (device_id)
  | 
          
          
            | 241 | 
                  join site using (site_id)
  | 
            272 | 
                  join location using (location_id)
  | 
          
          
            | 242 | 
                  join client_device using (device_id)
  | 
            273 | 
                  join owner_device using (device_id)
  | 
          
          
            | 243 | 
                  join client using (client_id);
  | 
            274 | 
                  join owner using (owner_id);
  | 
          
          
            | 244 | 
             
  | 
            275 | 
             
  | 
          
          
            | 245 | 
             
  | 
            276 | 
             
  | 
          
          
            | 246 | 
            insert into _system ( key_value, group_name, key_name ) values ( '0.1','database','version' ) 
  | 
            277 | 
            insert into _system ( key_value, group_name, key_name ) values ( '0.1','database','version' ) 
  | 
          
          
            | 247 | 
               on duplicate key update key_value = '0.1';
  | 
            278 | 
               on duplicate key update key_value = '0.1';
  |