Skip to content

Commit adbb552

Browse files
authored
Database VIEW management via separated files (apache#7417)
Co-authored-by: Daniel Augusto Veronezi Salvador <[email protected]>
1 parent db27c0a commit adbb552

13 files changed

+1174
-424
lines changed

engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java

+28
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.InputStreamReader;
25+
import java.nio.file.Paths;
2526
import java.sql.Connection;
2627
import java.sql.PreparedStatement;
2728
import java.sql.ResultSet;
2829
import java.sql.SQLException;
2930
import java.util.Arrays;
3031
import java.util.Date;
32+
import java.util.List;
3133

3234
import javax.inject.Inject;
3335

36+
import com.cloud.utils.FileUtil;
3437
import org.apache.cloudstack.utils.CloudStackVersion;
3538
import org.apache.commons.lang3.StringUtils;
3639
import org.apache.log4j.Logger;
@@ -123,6 +126,7 @@
123126
public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
124127
private static final Logger s_logger = Logger.getLogger(DatabaseUpgradeChecker.class);
125128
private final DatabaseVersionHierarchy hierarchy;
129+
private static final String VIEWS_DIRECTORY = Paths.get("META-INF", "db", "views").toString();
126130

127131
@Inject
128132
VersionDao _dao;
@@ -363,9 +367,33 @@ protected void upgrade(CloudStackVersion dbVersion, CloudStackVersion currentVer
363367
txn.close();
364368
}
365369
}
370+
371+
executeViewScripts();
366372
updateSystemVmTemplates(upgrades);
367373
}
368374

375+
protected void executeViewScripts() {
376+
s_logger.info(String.format("Executing VIEW scripts that are under resource directory [%s].", VIEWS_DIRECTORY));
377+
List<String> filesPathUnderViewsDirectory = FileUtil.getFilesPathsUnderResourceDirectory(VIEWS_DIRECTORY);
378+
379+
try (TransactionLegacy txn = TransactionLegacy.open("execute-view-scripts")) {
380+
Connection conn = txn.getConnection();
381+
382+
for (String filePath : filesPathUnderViewsDirectory) {
383+
s_logger.debug(String.format("Executing VIEW script [%s].", filePath));
384+
385+
InputStream viewScript = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
386+
runScript(conn, viewScript);
387+
}
388+
389+
s_logger.info(String.format("Finished execution of VIEW scripts that are under resource directory [%s].", VIEWS_DIRECTORY));
390+
} catch (SQLException e) {
391+
String message = String.format("Unable to execute VIEW scripts due to [%s].", e.getMessage());
392+
s_logger.error(message, e);
393+
throw new CloudRuntimeException(message, e);
394+
}
395+
}
396+
369397
@Override
370398
public void check() {
371399
GlobalLock lock = GlobalLock.getInternLock("DatabaseUpgrade");

engine/schema/src/main/resources/META-INF/db/schema-41810to41900.sql

-424
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
-- VIEW `cloud`.`async_job_view`;
19+
20+
DROP VIEW IF EXISTS `cloud`.`async_job_view`;
21+
22+
CREATE VIEW `cloud`.`async_job_view` AS
23+
select
24+
account.id account_id,
25+
account.uuid account_uuid,
26+
account.account_name account_name,
27+
account.type account_type,
28+
domain.id domain_id,
29+
domain.uuid domain_uuid,
30+
domain.name domain_name,
31+
domain.path domain_path,
32+
user.id user_id,
33+
user.uuid user_uuid,
34+
async_job.id,
35+
async_job.uuid,
36+
async_job.job_cmd,
37+
async_job.job_status,
38+
async_job.job_process_status,
39+
async_job.job_result_code,
40+
async_job.job_result,
41+
async_job.created,
42+
async_job.removed,
43+
async_job.instance_type,
44+
async_job.instance_id,
45+
async_job.job_executing_msid,
46+
CASE
47+
WHEN async_job.instance_type = 'Volume' THEN volumes.uuid
48+
WHEN
49+
async_job.instance_type = 'Template'
50+
or async_job.instance_type = 'Iso'
51+
THEN
52+
vm_template.uuid
53+
WHEN
54+
async_job.instance_type = 'VirtualMachine'
55+
or async_job.instance_type = 'ConsoleProxy'
56+
or async_job.instance_type = 'SystemVm'
57+
or async_job.instance_type = 'DomainRouter'
58+
THEN
59+
vm_instance.uuid
60+
WHEN async_job.instance_type = 'Snapshot' THEN snapshots.uuid
61+
WHEN async_job.instance_type = 'Host' THEN host.uuid
62+
WHEN async_job.instance_type = 'StoragePool' THEN storage_pool.uuid
63+
WHEN async_job.instance_type = 'IpAddress' THEN user_ip_address.uuid
64+
WHEN async_job.instance_type = 'SecurityGroup' THEN security_group.uuid
65+
WHEN async_job.instance_type = 'PhysicalNetwork' THEN physical_network.uuid
66+
WHEN async_job.instance_type = 'TrafficType' THEN physical_network_traffic_types.uuid
67+
WHEN async_job.instance_type = 'PhysicalNetworkServiceProvider' THEN physical_network_service_providers.uuid
68+
WHEN async_job.instance_type = 'FirewallRule' THEN firewall_rules.uuid
69+
WHEN async_job.instance_type = 'Account' THEN acct.uuid
70+
WHEN async_job.instance_type = 'User' THEN us.uuid
71+
WHEN async_job.instance_type = 'StaticRoute' THEN static_routes.uuid
72+
WHEN async_job.instance_type = 'PrivateGateway' THEN vpc_gateways.uuid
73+
WHEN async_job.instance_type = 'Counter' THEN counter.uuid
74+
WHEN async_job.instance_type = 'Condition' THEN conditions.uuid
75+
WHEN async_job.instance_type = 'AutoScalePolicy' THEN autoscale_policies.uuid
76+
WHEN async_job.instance_type = 'AutoScaleVmProfile' THEN autoscale_vmprofiles.uuid
77+
WHEN async_job.instance_type = 'AutoScaleVmGroup' THEN autoscale_vmgroups.uuid
78+
ELSE null
79+
END instance_uuid
80+
from
81+
`cloud`.`async_job`
82+
left join
83+
`cloud`.`account` ON async_job.account_id = account.id
84+
left join
85+
`cloud`.`domain` ON domain.id = account.domain_id
86+
left join
87+
`cloud`.`user` ON async_job.user_id = user.id
88+
left join
89+
`cloud`.`volumes` ON async_job.instance_id = volumes.id
90+
left join
91+
`cloud`.`vm_template` ON async_job.instance_id = vm_template.id
92+
left join
93+
`cloud`.`vm_instance` ON async_job.instance_id = vm_instance.id
94+
left join
95+
`cloud`.`snapshots` ON async_job.instance_id = snapshots.id
96+
left join
97+
`cloud`.`host` ON async_job.instance_id = host.id
98+
left join
99+
`cloud`.`storage_pool` ON async_job.instance_id = storage_pool.id
100+
left join
101+
`cloud`.`user_ip_address` ON async_job.instance_id = user_ip_address.id
102+
left join
103+
`cloud`.`security_group` ON async_job.instance_id = security_group.id
104+
left join
105+
`cloud`.`physical_network` ON async_job.instance_id = physical_network.id
106+
left join
107+
`cloud`.`physical_network_traffic_types` ON async_job.instance_id = physical_network_traffic_types.id
108+
left join
109+
`cloud`.`physical_network_service_providers` ON async_job.instance_id = physical_network_service_providers.id
110+
left join
111+
`cloud`.`firewall_rules` ON async_job.instance_id = firewall_rules.id
112+
left join
113+
`cloud`.`account` acct ON async_job.instance_id = acct.id
114+
left join
115+
`cloud`.`user` us ON async_job.instance_id = us.id
116+
left join
117+
`cloud`.`static_routes` ON async_job.instance_id = static_routes.id
118+
left join
119+
`cloud`.`vpc_gateways` ON async_job.instance_id = vpc_gateways.id
120+
left join
121+
`cloud`.`counter` ON async_job.instance_id = counter.id
122+
left join
123+
`cloud`.`conditions` ON async_job.instance_id = conditions.id
124+
left join
125+
`cloud`.`autoscale_policies` ON async_job.instance_id = autoscale_policies.id
126+
left join
127+
`cloud`.`autoscale_vmprofiles` ON async_job.instance_id = autoscale_vmprofiles.id
128+
left join
129+
`cloud`.`autoscale_vmgroups` ON async_job.instance_id = autoscale_vmgroups.id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
-- VIEW `cloud`.`data_center_view`;
19+
20+
DROP VIEW IF EXISTS `cloud`.`data_center_view`;
21+
22+
CREATE VIEW `cloud`.`data_center_view` AS
23+
select
24+
data_center.id,
25+
data_center.uuid,
26+
data_center.name,
27+
data_center.is_security_group_enabled,
28+
data_center.is_local_storage_enabled,
29+
data_center.description,
30+
data_center.dns1,
31+
data_center.dns2,
32+
data_center.ip6_dns1,
33+
data_center.ip6_dns2,
34+
data_center.internal_dns1,
35+
data_center.internal_dns2,
36+
data_center.guest_network_cidr,
37+
data_center.domain,
38+
data_center.networktype,
39+
data_center.allocation_state,
40+
data_center.zone_token,
41+
data_center.dhcp_provider,
42+
data_center.type,
43+
data_center.removed,
44+
data_center.sort_key,
45+
domain.id domain_id,
46+
domain.uuid domain_uuid,
47+
domain.name domain_name,
48+
domain.path domain_path,
49+
dedicated_resources.affinity_group_id,
50+
dedicated_resources.account_id,
51+
affinity_group.uuid affinity_group_uuid
52+
from
53+
`cloud`.`data_center`
54+
left join
55+
`cloud`.`domain` ON data_center.domain_id = domain.id
56+
left join
57+
`cloud`.`dedicated_resources` ON data_center.id = dedicated_resources.data_center_id
58+
left join
59+
`cloud`.`affinity_group` ON dedicated_resources.affinity_group_id = affinity_group.id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
-- VIEW `cloud`.`disk_offering_view`;
19+
20+
DROP VIEW IF EXISTS `cloud`.`disk_offering_view`;
21+
22+
CREATE VIEW `cloud`.`disk_offering_view` AS
23+
SELECT
24+
`disk_offering`.`id` AS `id`,
25+
`disk_offering`.`uuid` AS `uuid`,
26+
`disk_offering`.`name` AS `name`,
27+
`disk_offering`.`display_text` AS `display_text`,
28+
`disk_offering`.`provisioning_type` AS `provisioning_type`,
29+
`disk_offering`.`disk_size` AS `disk_size`,
30+
`disk_offering`.`min_iops` AS `min_iops`,
31+
`disk_offering`.`max_iops` AS `max_iops`,
32+
`disk_offering`.`created` AS `created`,
33+
`disk_offering`.`tags` AS `tags`,
34+
`disk_offering`.`customized` AS `customized`,
35+
`disk_offering`.`customized_iops` AS `customized_iops`,
36+
`disk_offering`.`removed` AS `removed`,
37+
`disk_offering`.`use_local_storage` AS `use_local_storage`,
38+
`disk_offering`.`hv_ss_reserve` AS `hv_ss_reserve`,
39+
`disk_offering`.`bytes_read_rate` AS `bytes_read_rate`,
40+
`disk_offering`.`bytes_read_rate_max` AS `bytes_read_rate_max`,
41+
`disk_offering`.`bytes_read_rate_max_length` AS `bytes_read_rate_max_length`,
42+
`disk_offering`.`bytes_write_rate` AS `bytes_write_rate`,
43+
`disk_offering`.`bytes_write_rate_max` AS `bytes_write_rate_max`,
44+
`disk_offering`.`bytes_write_rate_max_length` AS `bytes_write_rate_max_length`,
45+
`disk_offering`.`iops_read_rate` AS `iops_read_rate`,
46+
`disk_offering`.`iops_read_rate_max` AS `iops_read_rate_max`,
47+
`disk_offering`.`iops_read_rate_max_length` AS `iops_read_rate_max_length`,
48+
`disk_offering`.`iops_write_rate` AS `iops_write_rate`,
49+
`disk_offering`.`iops_write_rate_max` AS `iops_write_rate_max`,
50+
`disk_offering`.`iops_write_rate_max_length` AS `iops_write_rate_max_length`,
51+
`disk_offering`.`cache_mode` AS `cache_mode`,
52+
`disk_offering`.`sort_key` AS `sort_key`,
53+
`disk_offering`.`compute_only` AS `compute_only`,
54+
`disk_offering`.`display_offering` AS `display_offering`,
55+
`disk_offering`.`state` AS `state`,
56+
`disk_offering`.`disk_size_strictness` AS `disk_size_strictness`,
57+
`vsphere_storage_policy`.`value` AS `vsphere_storage_policy`,
58+
`disk_offering`.`encrypt` AS `encrypt`,
59+
GROUP_CONCAT(DISTINCT(domain.id)) AS domain_id,
60+
GROUP_CONCAT(DISTINCT(domain.uuid)) AS domain_uuid,
61+
GROUP_CONCAT(DISTINCT(domain.name)) AS domain_name,
62+
GROUP_CONCAT(DISTINCT(domain.path)) AS domain_path,
63+
GROUP_CONCAT(DISTINCT(zone.id)) AS zone_id,
64+
GROUP_CONCAT(DISTINCT(zone.uuid)) AS zone_uuid,
65+
GROUP_CONCAT(DISTINCT(zone.name)) AS zone_name
66+
FROM
67+
`cloud`.`disk_offering`
68+
LEFT JOIN
69+
`cloud`.`disk_offering_details` AS `domain_details` ON `domain_details`.`offering_id` = `disk_offering`.`id` AND `domain_details`.`name`='domainid'
70+
LEFT JOIN
71+
`cloud`.`domain` AS `domain` ON FIND_IN_SET(`domain`.`id`, `domain_details`.`value`)
72+
LEFT JOIN
73+
`cloud`.`disk_offering_details` AS `zone_details` ON `zone_details`.`offering_id` = `disk_offering`.`id` AND `zone_details`.`name`='zoneid'
74+
LEFT JOIN
75+
`cloud`.`data_center` AS `zone` ON FIND_IN_SET(`zone`.`id`, `zone_details`.`value`)
76+
LEFT JOIN
77+
`cloud`.`disk_offering_details` AS `vsphere_storage_policy` ON `vsphere_storage_policy`.`offering_id` = `disk_offering`.`id`
78+
AND `vsphere_storage_policy`.`name` = 'storagepolicy'
79+
WHERE
80+
`disk_offering`.`state`='Active'
81+
GROUP BY
82+
`disk_offering`.`id`;

0 commit comments

Comments
 (0)