Skip to content

Commit 6e6a276

Browse files
authored
add procedures procedure (#9385)
1 parent c98f1b8 commit 6e6a276

20 files changed

+486
-349
lines changed

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

Lines changed: 99 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
128128
private static final Logger s_logger = Logger.getLogger(DatabaseUpgradeChecker.class);
129129
private final DatabaseVersionHierarchy hierarchy;
130130
private static final String VIEWS_DIRECTORY = Paths.get("META-INF", "db", "views").toString();
131+
private static final String PROCEDURES_DIRECTORY = Paths.get("META-INF", "db", "procedures").toString();
131132

132133
@Inject
133134
VersionDao _dao;
@@ -295,83 +296,120 @@ private void updateSystemVmTemplates(DbUpgrade[] upgrades) {
295296
}
296297

297298
protected void upgrade(CloudStackVersion dbVersion, CloudStackVersion currentVersion) {
299+
executeProcedureScripts();
300+
final DbUpgrade[] upgrades = executeUpgrades(dbVersion, currentVersion);
301+
302+
executeViewScripts();
303+
updateSystemVmTemplates(upgrades);
304+
}
305+
306+
protected void executeProcedureScripts() {
307+
s_logger.info(String.format("Executing Stored Procedure scripts that are under resource directory [%s].", PROCEDURES_DIRECTORY));
308+
List<String> filesPathUnderViewsDirectory = FileUtil.getFilesPathsUnderResourceDirectory(PROCEDURES_DIRECTORY);
309+
310+
try (TransactionLegacy txn = TransactionLegacy.open("execute-procedure-scripts")) {
311+
Connection conn = txn.getConnection();
312+
313+
for (String filePath : filesPathUnderViewsDirectory) {
314+
s_logger.debug(String.format("Executing PROCEDURE script [%s].", filePath));
315+
316+
InputStream viewScript = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
317+
runScript(conn, viewScript);
318+
}
319+
320+
s_logger.info(String.format("Finished execution of PROCEDURE scripts that are under resource directory [%s].", PROCEDURES_DIRECTORY));
321+
} catch (SQLException e) {
322+
String message = String.format("Unable to execute PROCEDURE scripts due to [%s].", e.getMessage());
323+
s_logger.error(message, e);
324+
throw new CloudRuntimeException(message, e);
325+
}
326+
}
327+
328+
private DbUpgrade[] executeUpgrades(CloudStackVersion dbVersion, CloudStackVersion currentVersion) {
298329
s_logger.info("Database upgrade must be performed from " + dbVersion + " to " + currentVersion);
299330

300331
final DbUpgrade[] upgrades = calculateUpgradePath(dbVersion, currentVersion);
301332

302333
for (DbUpgrade upgrade : upgrades) {
303-
VersionVO version;
304-
s_logger.debug("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade
305-
.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
306-
TransactionLegacy txn = TransactionLegacy.open("Upgrade");
307-
txn.start();
308-
try {
309-
Connection conn;
310-
try {
311-
conn = txn.getConnection();
312-
} catch (SQLException e) {
313-
String errorMessage = "Unable to upgrade the database";
314-
s_logger.error(errorMessage, e);
315-
throw new CloudRuntimeException(errorMessage, e);
316-
}
317-
InputStream[] scripts = upgrade.getPrepareScripts();
318-
if (scripts != null) {
319-
for (InputStream script : scripts) {
320-
runScript(conn, script);
321-
}
322-
}
323-
324-
upgrade.performDataMigration(conn);
325-
326-
version = new VersionVO(upgrade.getUpgradedVersion());
327-
version = _dao.persist(version);
334+
VersionVO version = executeUpgrade(upgrade);
335+
executeUpgradeCleanup(upgrade, version);
336+
}
337+
return upgrades;
338+
}
328339

329-
txn.commit();
330-
} catch (CloudRuntimeException e) {
340+
private VersionVO executeUpgrade(DbUpgrade upgrade) {
341+
VersionVO version;
342+
s_logger.debug("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade
343+
.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
344+
TransactionLegacy txn = TransactionLegacy.open("Upgrade");
345+
txn.start();
346+
try {
347+
Connection conn;
348+
try {
349+
conn = txn.getConnection();
350+
} catch (SQLException e) {
331351
String errorMessage = "Unable to upgrade the database";
332352
s_logger.error(errorMessage, e);
333353
throw new CloudRuntimeException(errorMessage, e);
334-
} finally {
335-
txn.close();
354+
}
355+
InputStream[] scripts = upgrade.getPrepareScripts();
356+
if (scripts != null) {
357+
for (InputStream script : scripts) {
358+
runScript(conn, script);
359+
}
336360
}
337361

338-
// Run the corresponding '-cleanup.sql' script
339-
txn = TransactionLegacy.open("Cleanup");
340-
try {
341-
s_logger.info("Cleanup upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade
342-
.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
362+
upgrade.performDataMigration(conn);
343363

344-
txn.start();
345-
Connection conn;
346-
try {
347-
conn = txn.getConnection();
348-
} catch (SQLException e) {
349-
s_logger.error("Unable to cleanup the database", e);
350-
throw new CloudRuntimeException("Unable to cleanup the database", e);
351-
}
364+
version = new VersionVO(upgrade.getUpgradedVersion());
365+
version = _dao.persist(version);
352366

353-
InputStream[] scripts = upgrade.getCleanupScripts();
354-
if (scripts != null) {
355-
for (InputStream script : scripts) {
356-
runScript(conn, script);
357-
s_logger.debug("Cleanup script " + upgrade.getClass().getSimpleName() + " is executed successfully");
358-
}
359-
}
360-
txn.commit();
367+
txn.commit();
368+
} catch (CloudRuntimeException e) {
369+
String errorMessage = "Unable to upgrade the database";
370+
s_logger.error(errorMessage, e);
371+
throw new CloudRuntimeException(errorMessage, e);
372+
} finally {
373+
txn.close();
374+
}
375+
return version;
376+
}
361377

362-
txn.start();
363-
version.setStep(Step.Complete);
364-
version.setUpdated(new Date());
365-
_dao.update(version.getId(), version);
366-
txn.commit();
367-
s_logger.debug("Upgrade completed for version " + version.getVersion());
368-
} finally {
369-
txn.close();
378+
private void executeUpgradeCleanup(DbUpgrade upgrade, VersionVO version) {
379+
TransactionLegacy txn;
380+
// Run the corresponding '-cleanup.sql' script
381+
txn = TransactionLegacy.open("Cleanup");
382+
try {
383+
s_logger.info("Cleanup upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade
384+
.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
385+
386+
txn.start();
387+
Connection conn;
388+
try {
389+
conn = txn.getConnection();
390+
} catch (SQLException e) {
391+
s_logger.error("Unable to cleanup the database", e);
392+
throw new CloudRuntimeException("Unable to cleanup the database", e);
370393
}
371-
}
372394

373-
executeViewScripts();
374-
updateSystemVmTemplates(upgrades);
395+
InputStream[] scripts = upgrade.getCleanupScripts();
396+
if (scripts != null) {
397+
for (InputStream script : scripts) {
398+
runScript(conn, script);
399+
s_logger.debug("Cleanup script " + upgrade.getClass().getSimpleName() + " is executed successfully");
400+
}
401+
}
402+
txn.commit();
403+
404+
txn.start();
405+
version.setStep(Step.Complete);
406+
version.setUpdated(new Date());
407+
_dao.update(version.getId(), version);
408+
txn.commit();
409+
s_logger.debug("Upgrade completed for version " + version.getVersion());
410+
} finally {
411+
txn.close();
412+
}
375413
}
376414

377415
protected void executeViewScripts() {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
-- PR#4699 Drop the procedure `ADD_GUEST_OS_AND_HYPERVISOR_MAPPING` if it already exist.
19+
DROP PROCEDURE IF EXISTS `cloud`.`ADD_GUEST_OS_AND_HYPERVISOR_MAPPING`;
20+
21+
-- PR#4699 Create the procedure `ADD_GUEST_OS_AND_HYPERVISOR_MAPPING` to add guest_os and guest_os_hypervisor mapping.
22+
CREATE PROCEDURE `cloud`.`ADD_GUEST_OS_AND_HYPERVISOR_MAPPING` (
23+
IN guest_os_category_id bigint(20) unsigned,
24+
IN guest_os_display_name VARCHAR(255),
25+
IN guest_os_hypervisor_hypervisor_type VARCHAR(32),
26+
IN guest_os_hypervisor_hypervisor_version VARCHAR(32),
27+
IN guest_os_hypervisor_guest_os_name VARCHAR(255)
28+
)
29+
BEGIN
30+
INSERT INTO cloud.guest_os (uuid, category_id, display_name, created)
31+
SELECT UUID(), guest_os_category_id, guest_os_display_name, now()
32+
FROM DUAL
33+
WHERE not exists( SELECT 1
34+
FROM cloud.guest_os
35+
WHERE cloud.guest_os.category_id = guest_os_category_id
36+
AND cloud.guest_os.display_name = guest_os_display_name)
37+
38+
; INSERT INTO cloud.guest_os_hypervisor (uuid, hypervisor_type, hypervisor_version, guest_os_name, guest_os_id, created)
39+
SELECT UUID(), guest_os_hypervisor_hypervisor_type, guest_os_hypervisor_hypervisor_version, guest_os_hypervisor_guest_os_name, guest_os.id, now()
40+
FROM cloud.guest_os
41+
WHERE guest_os.category_id = guest_os_category_id
42+
AND guest_os.display_name = guest_os_display_name
43+
AND NOT EXISTS (SELECT 1
44+
FROM cloud.guest_os_hypervisor as hypervisor
45+
WHERE hypervisor_type = guest_os_hypervisor_hypervisor_type
46+
AND hypervisor_version = guest_os_hypervisor_hypervisor_version
47+
AND hypervisor.guest_os_id = guest_os.id
48+
AND hypervisor.guest_os_name = guest_os_hypervisor_guest_os_name)
49+
;END;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
-- in cloud
19+
DROP PROCEDURE IF EXISTS `cloud`.`IDEMPOTENT_ADD_COLUMN`;
20+
CREATE PROCEDURE `cloud`.`IDEMPOTENT_ADD_COLUMN` (
21+
IN in_table_name VARCHAR(200),
22+
IN in_column_name VARCHAR(200),
23+
IN in_column_definition VARCHAR(1000)
24+
)
25+
BEGIN
26+
27+
DECLARE CONTINUE HANDLER FOR 1060 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name); SET @ddl = CONCAT(@ddl, ' ', 'ADD COLUMN') ; SET @ddl = CONCAT(@ddl, ' ', in_column_name); SET @ddl = CONCAT(@ddl, ' ', in_column_definition); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
DROP PROCEDURE IF EXISTS `cloud`.`IDEMPOTENT_ADD_KEY`;
19+
20+
CREATE PROCEDURE `cloud`.`IDEMPOTENT_ADD_KEY` (
21+
IN in_index_name VARCHAR(200)
22+
, IN in_table_name VARCHAR(200)
23+
, IN in_key_definition VARCHAR(1000)
24+
)
25+
BEGIN
26+
27+
DECLARE CONTINUE HANDLER FOR 1061 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name); SET @ddl = CONCAT(@ddl, ' ', ' ADD KEY ') ; SET @ddl = CONCAT(@ddl, ' ', in_index_name); SET @ddl = CONCAT(@ddl, ' ', in_key_definition); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
-- Idempotent ADD UNIQUE INDEX
19+
DROP PROCEDURE IF EXISTS `cloud_usage`.`IDEMPOTENT_ADD_UNIQUE_INDEX`;
20+
CREATE PROCEDURE `cloud_usage`.`IDEMPOTENT_ADD_UNIQUE_INDEX` (
21+
IN in_table_name VARCHAR(200)
22+
, IN in_index_name VARCHAR(200)
23+
, IN in_index_definition VARCHAR(1000)
24+
)
25+
BEGIN
26+
DECLARE CONTINUE HANDLER FOR 1061 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name); SET @ddl = CONCAT(@ddl, ' ', 'ADD UNIQUE INDEX ', in_index_name); SET @ddl = CONCAT(@ddl, ' ', in_index_definition); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
-- Idempotent ADD UNIQUE KEY
19+
DROP PROCEDURE IF EXISTS `cloud`.`IDEMPOTENT_ADD_UNIQUE_KEY`;
20+
CREATE PROCEDURE `cloud`.`IDEMPOTENT_ADD_UNIQUE_KEY` (
21+
IN in_table_name VARCHAR(200)
22+
, IN in_key_name VARCHAR(200)
23+
, IN in_key_definition VARCHAR(1000)
24+
)
25+
BEGIN
26+
DECLARE CONTINUE HANDLER FOR 1061 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name); SET @ddl = CONCAT(@ddl, ' ', 'ADD UNIQUE KEY ', in_key_name); SET @ddl = CONCAT(@ddl, ' ', in_key_definition); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
-- in usage Idempotent CHANGE COLUMN
19+
DROP PROCEDURE IF EXISTS `cloud`.`IDEMPOTENT_CHANGE_COLUMN`;
20+
CREATE PROCEDURE `cloud`.`IDEMPOTENT_CHANGE_COLUMN` (
21+
IN in_table_name VARCHAR(200)
22+
, IN in_column_name VARCHAR(200)
23+
, IN in_column_new_name VARCHAR(200)
24+
, IN in_column_new_definition VARCHAR(1000)
25+
)
26+
BEGIN
27+
DECLARE CONTINUE HANDLER FOR 1054 BEGIN END; SET @ddl = CONCAT('ALTER TABLE ', in_table_name); SET @ddl = CONCAT(@ddl, ' ', 'CHANGE COLUMN') ; SET @ddl = CONCAT(@ddl, ' ', in_column_name); SET @ddl = CONCAT(@ddl, ' ', in_column_new_name); SET @ddl = CONCAT(@ddl, ' ', in_column_new_definition); PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt; END;

0 commit comments

Comments
 (0)