schema_mysql_copy.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef SCHEMA_COPY_H
  15. #define SCHEMA_COPY_H
  16. namespace {
  17. // What follows is a set of statements that creates a copy of the schema
  18. // in the test database. It is used by the MySQL unit test prior to each
  19. // test.
  20. //
  21. // Each SQL statement is a single string. The statements are not terminated
  22. // by semicolons, and the strings must end with a comma. The final line
  23. // statement must be NULL (not in quotes)
  24. // NOTE: This file mirrors the schema in src/lib/dhcpsrv/dhcpdb_create.mysql.
  25. // If this file is altered, please ensure that any change is compatible
  26. // with the schema in dhcpdb_create.mysql.
  27. // Deletion of existing tables.
  28. const char* destroy_statement[] = {
  29. "DROP TABLE lease4",
  30. "DROP TABLE lease6",
  31. "DROP TABLE lease6_types",
  32. "DROP TABLE lease_hwaddr_source",
  33. "DROP TABLE schema_version",
  34. // We need to drop ipv6_reservations before hosts, as it has constrains
  35. // that depend on hosts. Therefore hosts table cannot be deleted while
  36. // ipv6_reservations exists.
  37. "DROP TABLE ipv6_reservations",
  38. "DROP TABLE hosts",
  39. "DROP TABLE dhcp4_options",
  40. "DROP TABLE dhcp6_options",
  41. NULL
  42. };
  43. // Creation of the new tables.
  44. const char* create_statement[] = {
  45. // Schema initialization to 1.0 starts here.
  46. "START TRANSACTION",
  47. "CREATE TABLE lease4 ("
  48. "address INT UNSIGNED PRIMARY KEY NOT NULL,"
  49. "hwaddr VARBINARY(20),"
  50. "client_id VARBINARY(128),"
  51. "valid_lifetime INT UNSIGNED,"
  52. "expire TIMESTAMP,"
  53. "subnet_id INT UNSIGNED,"
  54. "fqdn_fwd BOOL,"
  55. "fqdn_rev BOOL,"
  56. "hostname VARCHAR(255)"
  57. ") ENGINE = INNODB",
  58. "CREATE INDEX lease4_by_hwaddr_subnet_id ON lease4 (hwaddr, subnet_id)",
  59. "CREATE INDEX lease4_by_client_id_subnet_id ON lease4 (client_id, subnet_id)",
  60. "CREATE TABLE lease6 ("
  61. "address VARCHAR(39) PRIMARY KEY NOT NULL,"
  62. "duid VARBINARY(128),"
  63. "valid_lifetime INT UNSIGNED,"
  64. "expire TIMESTAMP,"
  65. "subnet_id INT UNSIGNED,"
  66. "pref_lifetime INT UNSIGNED,"
  67. "lease_type TINYINT,"
  68. "iaid INT UNSIGNED,"
  69. "prefix_len TINYINT UNSIGNED,"
  70. "fqdn_fwd BOOL,"
  71. "fqdn_rev BOOL,"
  72. "hostname VARCHAR(255)"
  73. ") ENGINE = INNODB",
  74. "CREATE INDEX lease6_by_iaid_subnet_id_duid ON lease6 (iaid, subnet_id, duid)",
  75. "CREATE TABLE lease6_types ("
  76. "lease_type TINYINT PRIMARY KEY NOT NULL,"
  77. "name VARCHAR(5)"
  78. ")",
  79. "INSERT INTO lease6_types VALUES (0, \"IA_NA\")",
  80. "INSERT INTO lease6_types VALUES (1, \"IA_TA\")",
  81. "INSERT INTO lease6_types VALUES (2, \"IA_PD\")",
  82. "CREATE TABLE schema_version ("
  83. "version INT PRIMARY KEY NOT NULL,"
  84. "minor INT"
  85. ")",
  86. "INSERT INTO schema_version VALUES (1, 0)",
  87. "COMMIT",
  88. // Schema initialization to 1.0 ends here.
  89. // Schema upgrade to 2.0 starts here.
  90. "ALTER TABLE lease6 "
  91. "ADD COLUMN hwaddr varbinary(20),"
  92. "ADD COLUMN hwtype smallint unsigned,"
  93. "ADD COLUMN hwaddr_source int unsigned;",
  94. // Production schema has lease_hwaddr_source table. It is not used by
  95. // kea code and is simply useful for formulating more human readable
  96. // queries. Hence no need to create it in tests. The actual SQL
  97. // code remains here commented out to keep a trace that the omission
  98. // is intentional.
  99. /* "CREATE TABLE lease_hwaddr_source ("
  100. "hwaddr_source INT PRIMARY KEY NOT NULL,"
  101. "name VARCHAR(40) )",
  102. "INSERT INTO lease_hwaddr_source VALUES (1, \"HWADDR_SOURCE_RAW\");",
  103. "INSERT INTO lease_hwaddr_source VALUES (2, \"HWADDR_SOURCE_IPV6_LINK_LOCAL\");",
  104. "INSERT INTO lease_hwaddr_source VALUES (4, \"HWADDR_SOURCE_DUID\");",
  105. "INSERT INTO lease_hwaddr_source VALUES (8, \"HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION\");",
  106. "INSERT INTO lease_hwaddr_source VALUES (16, \"HWADDR_SOURCE_REMOTE_ID\");",
  107. "INSERT INTO lease_hwaddr_source VALUES (32, \"HWADDR_SOURCE_SUBSCRIBER_ID\");",
  108. "INSERT INTO lease_hwaddr_source VALUES (64, \"HWADDR_SOURCE_DOCSIS\");", */
  109. "UPDATE schema_version SET version=\"2\", minor=\"0\";",
  110. // Schema upgrade to 2.0 ends here.
  111. // Schema upgrade to 3.0 starts here.
  112. "CREATE TABLE IF NOT EXISTS hosts ("
  113. "host_id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
  114. "dhcp_identifier VARBINARY(128) NOT NULL,"
  115. "dhcp_identifier_type TINYINT NOT NULL,"
  116. "dhcp4_subnet_id INT UNSIGNED NULL,"
  117. "dhcp6_subnet_id INT UNSIGNED NULL,"
  118. "ipv4_address INT UNSIGNED NULL,"
  119. "hostname VARCHAR(255) NULL,"
  120. "dhcp4_client_classes VARCHAR(255) NULL,"
  121. "dhcp6_client_classes VARCHAR(255) NULL,"
  122. "PRIMARY KEY (host_id),"
  123. "INDEX key_dhcp4_identifier_subnet_id (dhcp_identifier ASC , dhcp_identifier_type ASC),"
  124. "INDEX key_dhcp6_identifier_subnet_id (dhcp_identifier ASC , dhcp_identifier_type ASC , dhcp6_subnet_id ASC)"
  125. ") ENGINE=INNODB",
  126. "CREATE TABLE IF NOT EXISTS ipv6_reservations ("
  127. "reservation_id INT NOT NULL AUTO_INCREMENT,"
  128. "address VARCHAR(39) NOT NULL,"
  129. "prefix_len TINYINT(3) UNSIGNED NOT NULL DEFAULT 128,"
  130. "type TINYINT(4) UNSIGNED NOT NULL DEFAULT 0,"
  131. "dhcp6_iaid INT UNSIGNED NULL,"
  132. "host_id INT UNSIGNED NOT NULL,"
  133. "PRIMARY KEY (reservation_id),"
  134. "INDEX fk_ipv6_reservations_host_idx (host_id ASC),"
  135. "CONSTRAINT fk_ipv6_reservations_Host FOREIGN KEY (host_id)"
  136. "REFERENCES hosts (host_id)"
  137. "ON DELETE NO ACTION ON UPDATE NO ACTION"
  138. ") ENGINE=INNODB",
  139. "CREATE TABLE IF NOT EXISTS dhcp4_options ("
  140. "option_id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
  141. "code TINYINT UNSIGNED NOT NULL,"
  142. "value BLOB NULL,"
  143. "formatted_value TEXT NULL,"
  144. "space VARCHAR(128) NULL,"
  145. "persistent TINYINT(1) NOT NULL DEFAULT 0,"
  146. "dhcp_client_class VARCHAR(128) NULL,"
  147. "dhcp4_subnet_id INT NULL,"
  148. "host_id INT UNSIGNED NULL,"
  149. "PRIMARY KEY (option_id),"
  150. "UNIQUE INDEX option_id_UNIQUE (option_id ASC),"
  151. "INDEX fk_options_host1_idx (host_id ASC),"
  152. "CONSTRAINT fk_options_host1 FOREIGN KEY (host_id)"
  153. "REFERENCES hosts (host_id)"
  154. "ON DELETE NO ACTION ON UPDATE NO ACTION"
  155. ") ENGINE=INNODB",
  156. "CREATE TABLE IF NOT EXISTS dhcp6_options ("
  157. "option_id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
  158. "code INT UNSIGNED NOT NULL,"
  159. "value BLOB NULL,"
  160. "formatted_value TEXT NULL,"
  161. "space VARCHAR(128) NULL,"
  162. "persistent TINYINT(1) NOT NULL DEFAULT 0,"
  163. "dhcp_client_class VARCHAR(128) NULL,"
  164. "dhcp6_subnet_id INT NULL,"
  165. "host_id INT UNSIGNED NULL,"
  166. "PRIMARY KEY (option_id),"
  167. "UNIQUE INDEX option_id_UNIQUE (option_id ASC),"
  168. "INDEX fk_options_host1_idx (host_id ASC),"
  169. "CONSTRAINT fk_options_host10 FOREIGN KEY (host_id)"
  170. "REFERENCES hosts (host_id)"
  171. "ON DELETE NO ACTION ON UPDATE NO ACTION"
  172. ") ENGINE=INNODB",
  173. //"DELIMITER $$ ",
  174. "CREATE TRIGGER host_BDEL BEFORE DELETE ON hosts FOR EACH ROW "
  175. "BEGIN "
  176. "DELETE FROM ipv6_reservations WHERE ipv6_reservations.host_id = OLD.host_id; "
  177. "END ",
  178. //"$$ ",
  179. //"DELIMITER ;",
  180. "UPDATE schema_version SET version = '3', minor = '0';",
  181. // This line concludes database upgrade to version 3.0.
  182. // Schema upgrade to 4.0 starts here.
  183. "ALTER TABLE lease4 "
  184. "ADD COLUMN state INT UNSIGNED DEFAULT 0",
  185. "ALTER TABLE lease6 "
  186. "ADD COLUMN state INT UNSIGNED DEFAULT 0",
  187. "CREATE INDEX lease4_by_state_expire ON lease4 (state, expire)",
  188. "CREATE INDEX lease6_by_state_expire ON lease6 (state, expire)",
  189. // Production schema includes the lease_state table which maps
  190. // the lease states to their names. This is not used in the unit tests
  191. // so it is commented out.
  192. /*"CREATE TABLE IF NOT EXISTS lease_state (",
  193. "state INT UNSIGNED PRIMARY KEY NOT NULL,"
  194. "name VARCHAR(64) NOT NULL);",
  195. "INSERT INTO lease_state VALUES (0, \"default\");",
  196. "INSERT INTO lease_state VALUES (1, \"declined\");",
  197. "INSERT INTO lease_state VALUES (2, \"expired-reclaimed\");",*/
  198. // Schema upgrade to 4.0 ends here.
  199. NULL
  200. };
  201. }; // Anonymous namespace
  202. #endif // SCHEMA_COPY_H