schema_mysql_copy.h 9.39 KB
Newer Older
1
// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
2
//
3
4
5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#ifndef SCHEMA_COPY_H
#define SCHEMA_COPY_H

namespace {

// What follows is a set of statements that creates a copy of the schema
// in the test database.  It is used by the MySQL unit test prior to each
// test.
//
// Each SQL statement is a single string.  The statements are not terminated
// by semicolons, and the strings must end with a comma.  The final line
// statement must be NULL (not in quotes)

20
21
22
// NOTE: This file mirrors the schema in src/lib/dhcpsrv/dhcpdb_create.mysql.
//       If this file is altered, please ensure that any change is compatible
//       with the schema in dhcpdb_create.mysql.
23
24
25
26

// Deletion of existing tables.

const char* destroy_statement[] = {
27
28
    // Turning off referential integrity checks ensures tables get dropped
    "SET SESSION FOREIGN_KEY_CHECKS = 0",
29
30
31
    "DROP TABLE lease4",
    "DROP TABLE lease6",
    "DROP TABLE lease6_types",
32
    "DROP TABLE lease_hwaddr_source",
33
    "DROP TABLE schema_version",
34
    "DROP TABLE ipv6_reservations",
35
36
37
    "DROP TABLE hosts",
    "DROP TABLE dhcp4_options",
    "DROP TABLE dhcp6_options",
38
39

    "DROP TRIGGER host_BDEL",
40
41
42
43
44
45
    NULL
};

// Creation of the new tables.

const char* create_statement[] = {
46
47

    // Schema initialization to 1.0 starts here.
48
    "START TRANSACTION",
49
50
51
52
    "CREATE TABLE lease4 ("
        "address INT UNSIGNED PRIMARY KEY NOT NULL,"
        "hwaddr VARBINARY(20),"
        "client_id VARBINARY(128),"
53
        "valid_lifetime INT UNSIGNED,"
54
        "expire TIMESTAMP,"
55
56
57
58
        "subnet_id INT UNSIGNED,"
        "fqdn_fwd BOOL,"
        "fqdn_rev BOOL,"
        "hostname VARCHAR(255)"
59
60
        ") ENGINE = INNODB",

61
62
63
64
    "CREATE INDEX lease4_by_hwaddr_subnet_id ON lease4 (hwaddr, subnet_id)",

    "CREATE INDEX lease4_by_client_id_subnet_id ON lease4 (client_id, subnet_id)",

65
    "CREATE TABLE lease6 ("
66
        "address VARCHAR(39) PRIMARY KEY NOT NULL,"
67
68
69
70
71
72
73
        "duid VARBINARY(128),"
        "valid_lifetime INT UNSIGNED,"
        "expire TIMESTAMP,"
        "subnet_id INT UNSIGNED,"
        "pref_lifetime INT UNSIGNED,"
        "lease_type TINYINT,"
        "iaid INT UNSIGNED,"
74
75
76
77
        "prefix_len TINYINT UNSIGNED,"
        "fqdn_fwd BOOL,"
        "fqdn_rev BOOL,"
        "hostname VARCHAR(255)"
78
79
        ") ENGINE = INNODB",

80
81
    "CREATE INDEX lease6_by_iaid_subnet_id_duid ON lease6 (iaid, subnet_id, duid)",

82
83
84
    "CREATE TABLE lease6_types ("
        "lease_type TINYINT PRIMARY KEY NOT NULL,"
        "name VARCHAR(5)"
85
        ") ENGINE = INNODB",
86
87
88
89
90
91
92
93

    "INSERT INTO lease6_types VALUES (0, \"IA_NA\")",
    "INSERT INTO lease6_types VALUES (1, \"IA_TA\")",
    "INSERT INTO lease6_types VALUES (2, \"IA_PD\")",

    "CREATE TABLE schema_version ("
        "version INT PRIMARY KEY NOT NULL,"
        "minor INT"
94
        ") ENGINE = INNODB",
95

96
    "INSERT INTO schema_version VALUES (1, 0)",
97
    "COMMIT",
98

99
100
101
    // Schema initialization to 1.0 ends here.

    // Schema upgrade to 2.0 starts here.
102
    "ALTER TABLE lease6 "
103
104
105
        "ADD COLUMN hwaddr varbinary(20),"
        "ADD COLUMN hwtype smallint unsigned,"
        "ADD COLUMN hwaddr_source int unsigned;",
106

107
    // Production schema has lease_hwaddr_source table. It is not used by
108
109
110
111
112
    // kea code and is simply useful for formulating more human readable
    // queries. Hence no need to create it in tests. The actual SQL
    // code remains here commented out to keep a trace that the omission
    // is intentional.

113
    /* "CREATE TABLE lease_hwaddr_source ("
114
115
116
    "hwaddr_source INT PRIMARY KEY NOT NULL,"
    "name VARCHAR(40) )",

117
118
119
120
121
122
123
    "INSERT INTO lease_hwaddr_source VALUES (1, \"HWADDR_SOURCE_RAW\");",
    "INSERT INTO lease_hwaddr_source VALUES (2, \"HWADDR_SOURCE_IPV6_LINK_LOCAL\");",
    "INSERT INTO lease_hwaddr_source VALUES (4, \"HWADDR_SOURCE_DUID\");",
    "INSERT INTO lease_hwaddr_source VALUES (8, \"HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION\");",
    "INSERT INTO lease_hwaddr_source VALUES (16, \"HWADDR_SOURCE_REMOTE_ID\");",
    "INSERT INTO lease_hwaddr_source VALUES (32, \"HWADDR_SOURCE_SUBSCRIBER_ID\");",
    "INSERT INTO lease_hwaddr_source VALUES (64, \"HWADDR_SOURCE_DOCSIS\");", */
124
125

    "UPDATE schema_version SET version=\"2\", minor=\"0\";",
126
    // Schema upgrade to 2.0 ends here.
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    // Schema upgrade to 3.0 starts here.

    "CREATE TABLE IF NOT EXISTS hosts ("
        "host_id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
        "dhcp_identifier VARBINARY(128) NOT NULL,"
        "dhcp_identifier_type TINYINT NOT NULL,"
        "dhcp4_subnet_id INT UNSIGNED NULL,"
        "dhcp6_subnet_id INT UNSIGNED NULL,"
        "ipv4_address INT UNSIGNED NULL,"
        "hostname VARCHAR(255) NULL,"
        "dhcp4_client_classes VARCHAR(255) NULL,"
        "dhcp6_client_classes VARCHAR(255) NULL,"
        "PRIMARY KEY (host_id),"
        "INDEX key_dhcp4_identifier_subnet_id (dhcp_identifier ASC , dhcp_identifier_type ASC),"
        "INDEX key_dhcp6_identifier_subnet_id (dhcp_identifier ASC , dhcp_identifier_type ASC , dhcp6_subnet_id ASC)"
    ")  ENGINE=INNODB",

    "CREATE TABLE IF NOT EXISTS ipv6_reservations ("
        "reservation_id INT NOT NULL AUTO_INCREMENT,"
        "address VARCHAR(39) NOT NULL,"
        "prefix_len TINYINT(3) UNSIGNED NOT NULL DEFAULT 128,"
        "type TINYINT(4) UNSIGNED NOT NULL DEFAULT 0,"
        "dhcp6_iaid INT UNSIGNED NULL,"
        "host_id INT UNSIGNED NOT NULL,"
        "PRIMARY KEY (reservation_id),"
        "INDEX fk_ipv6_reservations_host_idx (host_id ASC),"
        "CONSTRAINT fk_ipv6_reservations_Host FOREIGN KEY (host_id)"
            "REFERENCES hosts (host_id)"
            "ON DELETE NO ACTION ON UPDATE NO ACTION"
    ")  ENGINE=INNODB",

    "CREATE TABLE IF NOT EXISTS dhcp4_options ("
        "option_id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
        "code TINYINT UNSIGNED NOT NULL,"
        "value BLOB NULL,"
        "formatted_value TEXT NULL,"
        "space VARCHAR(128) NULL,"
        "persistent TINYINT(1) NOT NULL DEFAULT 0,"
        "dhcp_client_class VARCHAR(128) NULL,"
        "dhcp4_subnet_id INT NULL,"
        "host_id INT UNSIGNED NULL,"
        "PRIMARY KEY (option_id),"
        "UNIQUE INDEX option_id_UNIQUE (option_id ASC),"
        "INDEX fk_options_host1_idx (host_id ASC),"
        "CONSTRAINT fk_options_host1 FOREIGN KEY (host_id)"
            "REFERENCES hosts (host_id)"
            "ON DELETE NO ACTION ON UPDATE NO ACTION"
    ")  ENGINE=INNODB",

    "CREATE TABLE IF NOT EXISTS dhcp6_options ("
        "option_id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
        "code INT UNSIGNED NOT NULL,"
        "value BLOB NULL,"
        "formatted_value TEXT NULL,"
        "space VARCHAR(128) NULL,"
        "persistent TINYINT(1) NOT NULL DEFAULT 0,"
        "dhcp_client_class VARCHAR(128) NULL,"
        "dhcp6_subnet_id INT NULL,"
        "host_id INT UNSIGNED NULL,"
        "PRIMARY KEY (option_id),"
        "UNIQUE INDEX option_id_UNIQUE (option_id ASC),"
        "INDEX fk_options_host1_idx (host_id ASC),"
        "CONSTRAINT fk_options_host10 FOREIGN KEY (host_id)"
            "REFERENCES hosts (host_id)"
            "ON DELETE NO ACTION ON UPDATE NO ACTION"
    ")  ENGINE=INNODB",


    //"DELIMITER $$ ",
    "CREATE TRIGGER host_BDEL BEFORE DELETE ON hosts FOR EACH ROW "
    "BEGIN "
    "DELETE FROM ipv6_reservations WHERE ipv6_reservations.host_id = OLD.host_id; "
    "END ",
    //"$$ ",
    //"DELIMITER ;",

    "UPDATE schema_version SET version = '3', minor = '0';",

    // This line concludes database upgrade to version 3.0.

208
209
    // Schema upgrade to 4.0 starts here.
    "ALTER TABLE lease4 "
210
        "ADD COLUMN state INT UNSIGNED DEFAULT 0",
211
212

    "ALTER TABLE lease6 "
213
        "ADD COLUMN state INT UNSIGNED DEFAULT 0",
214

215
216
    "CREATE INDEX lease4_by_state_expire ON lease4 (state ASC, expire ASC)",
    "CREATE INDEX lease6_by_state_expire ON lease6 (state ASC, expire ASC)",
217

218
219
220
    // Production schema includes the lease_state table and
    // lease_hwaddr_source tables which map to the values in lease4
    // and lease6 tables. This is not used in the unit tests
221
222
223
224
225
226
227
228
    // so it is commented out.

    /*"CREATE TABLE IF NOT EXISTS lease_state (",
        "state INT UNSIGNED PRIMARY KEY NOT NULL,"
        "name VARCHAR(64) NOT NULL);",

    "INSERT INTO lease_state VALUES (0, \"default\");",
    "INSERT INTO lease_state VALUES (1, \"declined\");",
229
    "INSERT INTO lease_state VALUES (2, \"expired-reclaimed\");",
230

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    "ALTER TABLE lease4 "
        "ADD CONSTRAINT fk_lease4_state FOREIGN KEY (state) "
        "REFERENCES lease_state (state)",

    "ALTER TABLE lease6 "
        "ADD CONSTRAINT fk_lease6_state FOREIGN KEY (state) "
        "REFERENCES lease_state (state)",

    "ALTER TABLE lease6 "
        "ADD CONSTRAINT fk_lease6_type FOREIGN KEY (lease_type) "
        "REFERENCES lease6_types (lease_type)",

    "UPDATE lease_hwaddr_source "
        "SET name = 'HWADDR_SOURCE_DOCSIS_CMTS' "
        "WHERE hwaddr_source = 64",

    "INSERT INTO lease_hwaddr_source VALUES (128, 'HWADDR_SOURCE_DOCSIS_MODEM')",

    "ALTER TABLE lease_hwaddr_source "
        "MODIFY COLUMN hwaddr_source INT UNSIGNED NOT NULL DEFAULT 0",

    "ALTER TABLE lease6 "
        "ADD CONSTRAINT fk_lease6_hwaddr_source FOREIGN KEY (hwaddr_source) "
        "REFERENCES lease_hwaddr_source (hwaddr_source)",*/
255

256
257
    // Schema upgrade to 4.0 ends here.

258
259
260
261
262
263
    NULL
};

};  // Anonymous namespace

#endif // SCHEMA_COPY_H