1+ /*
2+ * OpenESPI Usage Schema - Base Tables Migration
3+ *
4+ * Copyright (c) 2018-2025 Green Button Alliance, Inc.
5+ * Licensed under the Apache License, Version 2.0
6+ *
7+ * This migration creates the foundational tables for the OpenESPI usage schema.
8+ * These tables implement the NAESB Energy Services Provider Interface (ESPI) 1.0 specification
9+ * for energy usage data exchange and Green Button data standards.
10+ */
11+
12+ -- Set schema for usage data
13+ USE openespi_usage;
14+
15+ -- Application Information Table
16+ -- Stores information about third-party applications that access usage data
17+ CREATE TABLE application_information (
18+ id BIGINT AUTO_INCREMENT PRIMARY KEY ,
19+ uuid VARCHAR (36 ) NOT NULL UNIQUE,
20+ uuid_msb BIGINT ,
21+ uuid_lsb BIGINT ,
22+ description VARCHAR (255 ),
23+ created DATETIME(6 ) NOT NULL DEFAULT CURRENT_TIMESTAMP (6 ),
24+ updated DATETIME(6 ) NOT NULL DEFAULT CURRENT_TIMESTAMP (6 ) ON UPDATE CURRENT_TIMESTAMP (6 ),
25+ published DATETIME(6 ),
26+ up_link_rel VARCHAR (255 ),
27+ up_link_href VARCHAR (1024 ),
28+ self_link_rel VARCHAR (255 ),
29+ self_link_href VARCHAR (1024 ),
30+
31+ -- Application specific fields
32+ client_name VARCHAR (255 ),
33+ client_id VARCHAR (255 ) NOT NULL UNIQUE,
34+ client_secret VARCHAR (255 ),
35+ client_id_issued_at BIGINT ,
36+ client_secret_expires_at BIGINT ,
37+ registration_client_uri VARCHAR (1024 ),
38+ registration_access_token VARCHAR (1024 ),
39+ redirect_uris TEXT ,
40+ software_id VARCHAR (255 ),
41+ software_version VARCHAR (255 ),
42+ token_endpoint_auth_method VARCHAR (50 ),
43+ response_types VARCHAR (255 ),
44+ grant_types VARCHAR (255 ),
45+ application_type VARCHAR (50 ),
46+ contacts TEXT ,
47+ logo_uri VARCHAR (1024 ),
48+ policy_uri VARCHAR (1024 ),
49+ tos_uri VARCHAR (1024 ),
50+ jwks_uri VARCHAR (1024 ),
51+ sector_identifier_uri VARCHAR (1024 ),
52+ subject_type VARCHAR (50 ),
53+ id_token_signed_response_alg VARCHAR (50 ),
54+ id_token_encrypted_response_alg VARCHAR (50 ),
55+ id_token_encrypted_response_enc VARCHAR (50 ),
56+ userinfo_signed_response_alg VARCHAR (50 ),
57+ userinfo_encrypted_response_alg VARCHAR (50 ),
58+ userinfo_encrypted_response_enc VARCHAR (50 ),
59+ request_object_signing_alg VARCHAR (50 ),
60+ request_object_encryption_alg VARCHAR (50 ),
61+ request_object_encryption_enc VARCHAR (50 ),
62+ default_max_age BIGINT ,
63+ require_auth_time BOOLEAN DEFAULT FALSE,
64+ default_acr_values VARCHAR (255 ),
65+ initiate_login_uri VARCHAR (1024 ),
66+ request_uris TEXT ,
67+
68+ INDEX idx_application_client_id (client_id),
69+ INDEX idx_application_uuid (uuid),
70+ INDEX idx_application_created (created),
71+ INDEX idx_application_updated (updated)
72+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE= utf8mb4_unicode_ci;
73+
74+ -- Related Links Table for Application Information
75+ CREATE TABLE application_information_related_links (
76+ application_information_id BIGINT NOT NULL ,
77+ related_links VARCHAR (1024 ),
78+ FOREIGN KEY (application_information_id) REFERENCES application_information(id) ON DELETE CASCADE ,
79+ INDEX idx_app_info_related_links (application_information_id)
80+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE= utf8mb4_unicode_ci;
81+
82+ -- Retail Customer Table
83+ -- Represents energy consumers with access to their usage data
84+ CREATE TABLE retail_customers (
85+ id BIGINT AUTO_INCREMENT PRIMARY KEY ,
86+ uuid VARCHAR (36 ) NOT NULL UNIQUE,
87+ uuid_msb BIGINT ,
88+ uuid_lsb BIGINT ,
89+ description VARCHAR (255 ),
90+ created DATETIME(6 ) NOT NULL DEFAULT CURRENT_TIMESTAMP (6 ),
91+ updated DATETIME(6 ) NOT NULL DEFAULT CURRENT_TIMESTAMP (6 ) ON UPDATE CURRENT_TIMESTAMP (6 ),
92+ published DATETIME(6 ),
93+ up_link_rel VARCHAR (255 ),
94+ up_link_href VARCHAR (1024 ),
95+ self_link_rel VARCHAR (255 ),
96+ self_link_href VARCHAR (1024 ),
97+
98+ -- Retail customer specific fields
99+ username VARCHAR (255 ) UNIQUE,
100+ first_name VARCHAR (255 ),
101+ last_name VARCHAR (255 ),
102+ password VARCHAR (255 ),
103+ enabled BOOLEAN DEFAULT TRUE,
104+ role VARCHAR (50 ) DEFAULT ' ROLE_USER' ,
105+
106+ INDEX idx_retail_customer_uuid (uuid),
107+ INDEX idx_retail_customer_username (username),
108+ INDEX idx_retail_customer_created (created),
109+ INDEX idx_retail_customer_updated (updated)
110+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE= utf8mb4_unicode_ci;
111+
112+ -- Related Links Table for Retail Customers
113+ CREATE TABLE retail_customer_related_links (
114+ retail_customer_id BIGINT NOT NULL ,
115+ related_links VARCHAR (1024 ),
116+ FOREIGN KEY (retail_customer_id) REFERENCES retail_customers(id) ON DELETE CASCADE ,
117+ INDEX idx_retail_customer_related_links (retail_customer_id)
118+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE= utf8mb4_unicode_ci;
119+
120+ -- Authorization Table
121+ -- OAuth 2.0 authorization grants for third-party access to usage data
122+ CREATE TABLE authorizations (
123+ id BIGINT AUTO_INCREMENT PRIMARY KEY ,
124+ uuid VARCHAR (36 ) NOT NULL UNIQUE,
125+ uuid_msb BIGINT ,
126+ uuid_lsb BIGINT ,
127+ description VARCHAR (255 ),
128+ created DATETIME(6 ) NOT NULL DEFAULT CURRENT_TIMESTAMP (6 ),
129+ updated DATETIME(6 ) NOT NULL DEFAULT CURRENT_TIMESTAMP (6 ) ON UPDATE CURRENT_TIMESTAMP (6 ),
130+ published DATETIME(6 ),
131+ up_link_rel VARCHAR (255 ),
132+ up_link_href VARCHAR (1024 ),
133+ self_link_rel VARCHAR (255 ),
134+ self_link_href VARCHAR (1024 ),
135+
136+ -- Authorization specific fields
137+ application_information_id BIGINT ,
138+ retail_customer_id BIGINT ,
139+ access_token VARCHAR (1024 ),
140+ refresh_token VARCHAR (1024 ),
141+ authorization_uri VARCHAR (1024 ),
142+ ap_title VARCHAR (255 ),
143+ ap_description TEXT ,
144+ ap_duration BIGINT ,
145+ scope VARCHAR (1024 ),
146+ token_type VARCHAR (50 ) DEFAULT ' Bearer' ,
147+ expires_in BIGINT ,
148+ state VARCHAR (255 ),
149+ error VARCHAR (255 ),
150+ error_description TEXT ,
151+ error_uri VARCHAR (1024 ),
152+ resource_uri VARCHAR (1024 ),
153+ customer_resource_uri VARCHAR (1024 ),
154+ status VARCHAR (50 ) DEFAULT ' ACTIVE' ,
155+ expires_at DATETIME(6 ),
156+ grant_type VARCHAR (50 ),
157+ response_type VARCHAR (50 ),
158+ third_party VARCHAR (255 ),
159+
160+ FOREIGN KEY (application_information_id) REFERENCES application_information(id) ON DELETE CASCADE ,
161+ FOREIGN KEY (retail_customer_id) REFERENCES retail_customers(id) ON DELETE CASCADE ,
162+
163+ INDEX idx_authorization_uuid (uuid),
164+ INDEX idx_authorization_access_token (access_token(255 )),
165+ INDEX idx_authorization_refresh_token (refresh_token(255 )),
166+ INDEX idx_authorization_app_id (application_information_id),
167+ INDEX idx_authorization_customer_id (retail_customer_id),
168+ INDEX idx_authorization_status (status),
169+ INDEX idx_authorization_expires_at (expires_at),
170+ INDEX idx_authorization_created (created),
171+ INDEX idx_authorization_updated (updated)
172+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE= utf8mb4_unicode_ci;
173+
174+ -- Related Links Table for Authorizations
175+ CREATE TABLE authorization_related_links (
176+ authorization_id BIGINT NOT NULL ,
177+ related_links VARCHAR (1024 ),
178+ FOREIGN KEY (authorization_id) REFERENCES authorizations(id) ON DELETE CASCADE ,
179+ INDEX idx_authorization_related_links (authorization_id)
180+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE= utf8mb4_unicode_ci;
181+
182+ -- Service Delivery Point Table
183+ -- Physical locations where energy services are delivered
184+ CREATE TABLE service_delivery_points (
185+ id BIGINT AUTO_INCREMENT PRIMARY KEY ,
186+ uuid VARCHAR (36 ) NOT NULL UNIQUE,
187+ uuid_msb BIGINT ,
188+ uuid_lsb BIGINT ,
189+ description VARCHAR (255 ),
190+ created DATETIME(6 ) NOT NULL DEFAULT CURRENT_TIMESTAMP (6 ),
191+ updated DATETIME(6 ) NOT NULL DEFAULT CURRENT_TIMESTAMP (6 ) ON UPDATE CURRENT_TIMESTAMP (6 ),
192+ published DATETIME(6 ),
193+ up_link_rel VARCHAR (255 ),
194+ up_link_href VARCHAR (1024 ),
195+ self_link_rel VARCHAR (255 ),
196+ self_link_href VARCHAR (1024 ),
197+
198+ -- Service delivery point specific fields
199+ name VARCHAR (256 ),
200+ tariff_profile VARCHAR (256 ),
201+ customer_agreement VARCHAR (256 ),
202+
203+ INDEX idx_sdp_uuid (uuid),
204+ INDEX idx_sdp_name (name),
205+ INDEX idx_sdp_tariff_profile (tariff_profile),
206+ INDEX idx_sdp_customer_agreement (customer_agreement),
207+ INDEX idx_sdp_created (created),
208+ INDEX idx_sdp_updated (updated)
209+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE= utf8mb4_unicode_ci;
210+
211+ -- Related Links Table for Service Delivery Points
212+ CREATE TABLE service_delivery_point_related_links (
213+ service_delivery_point_id BIGINT NOT NULL ,
214+ related_links VARCHAR (1024 ),
215+ FOREIGN KEY (service_delivery_point_id) REFERENCES service_delivery_points(id) ON DELETE CASCADE ,
216+ INDEX idx_sdp_related_links (service_delivery_point_id)
217+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4 COLLATE= utf8mb4_unicode_ci;
0 commit comments