Skip to content

Commit a504c2d

Browse files
authored
Merge pull request #149 from mrysav/mrysav/3.91-b3
v3.91 beta 3 - debug tools and lab config fixes
2 parents 0b9e049 + d6011ce commit a504c2d

File tree

7 files changed

+179
-57
lines changed

7 files changed

+179
-57
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
INSERT INTO `user_type` (`level`, `name`, `defaultdisplay`, `created_by`, `created_on`) VALUES ('20', 'SATELLITE_LAB', '1', 'Princesca', CURRENT_TIMESTAMP);
1+
-- Using INSERT IGNORE - it's important to have this user type,
2+
-- but it might conflict with what's already in the table and we
3+
-- don't have a sophisticated way to deal with that yet.
4+
INSERT IGNORE INTO `user_type` (`level`, `name`, `defaultdisplay`, `created_by`, `created_on`) VALUES ('20', 'SATELLITE_LAB', '1', 'Princesca', CURRENT_TIMESTAMP);

htdocs/debug/debug.php

+83-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_admin_or_401();
66

77
require_once(__DIR__."/../includes/header.php");
8+
require_once(__DIR__."/../includes/migrations.php");
89

910
LangUtil::setPageId("debug");
1011

@@ -16,6 +17,26 @@
1617
font-weight: bold;
1718
color: red;
1819
}
20+
21+
.migration_table th:first-of-type {
22+
text-align: center;
23+
}
24+
25+
.migration_table th:first-of-type {
26+
text-align: right;
27+
}
28+
29+
.migration_table tr td:first-of-type {
30+
text-align: right;
31+
}
32+
33+
.migration_table {
34+
margin: 1rem 2rem;
35+
}
36+
37+
.migration_table td {
38+
padding: 0.2rem 0.5rem;
39+
}
1940
</style>
2041

2142
<h2><?php echo LangUtil::$pageTerms['DEBUG_UTILITIES']; ?></h2>
@@ -54,29 +75,78 @@
5475

5576
<h3><?php echo LangUtil::$pageTerms['DATABASE_UTILITIES']; ?></h3>
5677

57-
<h4><?php echo LangUtil::$pageTerms['LEGACY_LAB_DATABASE_MIGRATIONS']; ?></h4>
78+
<h4>Database Migrations</h4>
5879

5980
<p>
6081
<span class="red-danger"><?php echo LangUtil::$pageTerms['WARNING']; ?></span><br/>
6182
<?php echo LangUtil::$pageTerms['MIGRATION_WARNING']; ?><br/>
6283
<?php echo LangUtil::$pageTerms['MIGRATION_DESCRIPTION']; ?>
6384
</p>
6485

65-
<form action="/debug/debug_apply_migration.php" method="GET">
66-
<label for="migration-lab-select"><?php echo LangUtil::$pageTerms['LAB_DATABASE']; ?>:</label>
67-
<select id="migration-lab-select" name="lab">
68-
<option value=""><?php echo LangUtil::$pageTerms['SELECT_LAB']; ?></option>
69-
<?php
70-
$lab_configs = get_lab_configs();
71-
foreach($lab_configs as $lab) {
72-
$display_name = $lab->name . " (" . $lab->dbName . ")";
73-
echo("<option value=\"".$lab->dbName."\">$display_name</option>\n");
86+
<?php
87+
$selected_database = $_GET["selected_database"];
88+
?>
89+
90+
<form action="debug.php" method="GET">
91+
<label for="migration-lab-select"><?php echo LangUtil::$pageTerms['LAB_DATABASE']; ?>:</label>
92+
<select id="migration-lab-select" name="selected_database">
93+
<option value="">Select a database</option>
94+
<option value="blis_revamp" <?php echo($selected_database == "blis_revamp" ? "selected" : ""); ?> >blis_revamp</option>
95+
<?php
96+
$lab_configs = get_lab_configs();
97+
foreach($lab_configs as $lab) {
98+
$display_name = $lab->name . " (" . $lab->dbName . ")";
99+
$selected = ($lab->dbName == $selected_database) ? "selected" : "";
100+
echo("<option value=\"".$lab->dbName."\" $selected>$display_name</option>\n");
101+
}
102+
?>
103+
</select>
104+
<input type="submit">
105+
</form>
106+
107+
<?php
108+
$dbtype = $selected_database == "blis_revamp" ? "revamp" : "lab";
109+
if ("$selected_database" != "") {
110+
$migrator = new LabDatabaseMigrator($selected_database, $dbtype);
111+
$available = array_reverse($migrator->get_available_migrations());
112+
$applied = array_reverse($migrator->get_applied_migrations());
113+
?>
114+
<table class="migration_table">
115+
<tr>
116+
<th>Migration Name</th>
117+
</tr>
118+
<?php
119+
foreach($available as $name) {
120+
$is_applied = in_array($name, $applied);
121+
122+
if (!$is_applied) {
123+
?>
124+
<tr>
125+
<td><?php echo($name); ?></td>
126+
<td><a href="debug/debug_apply_v2_migration.php?database=<?php echo($selected_database); ?>&migration=<?php echo($name); ?>">Apply</a></td>
127+
<td><a href="debug/debug_apply_v2_migration.php?database=<?php echo($selected_database); ?>&migration=<?php echo($name); ?>&skip=true">Skip</a></td>
128+
</tr>
129+
<?php
130+
} else {
131+
?>
132+
<tr>
133+
<td><?php echo($name); ?></td>
134+
<td><a href="debug/debug_apply_v2_migration.php?database=<?php echo($selected_database); ?>&migration=<?php echo($name); ?>&delete=true">Delete</a></td>
135+
<td></td>
136+
</tr>
137+
<?php
138+
}
74139
}
75-
?>
76-
</select>
140+
?>
141+
</table>
142+
<?php
143+
}
144+
?>
77145

78-
<br/>
146+
<h4><?php echo LangUtil::$pageTerms['LEGACY_LAB_DATABASE_MIGRATIONS']; ?></h4>
79147

148+
<form action="/debug/debug_apply_migration.php" method="GET">
149+
<input type="hidden" name="lab" value="<?php echo($selected_database); ?>"/>
80150
<label for="migration-select"><?php echo LangUtil::$pageTerms['SQL_MIGRATION']; ?>:</label>
81151
<select id="migration-select" name="migration">
82152
<option value=""><?php echo LangUtil::$pageTerms['SELECT_MIGRATION']; ?></option>
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
require_once(__DIR__."/util.php");
4+
require_once(__DIR__."/../includes/migrations.php");
5+
6+
require_admin_or_401();
7+
8+
$database_name = $_GET["database"];
9+
$migration_file = $_GET["migration"];
10+
$skip = $_GET["skip"] == "true" ? true : false;
11+
$delete = $_GET["delete"] == "true" ? true : false;
12+
13+
$dbtype = $database_name == "blis_revamp" ? "revamp" : "lab";
14+
$migrator = new LabDatabaseMigrator($database_name, $dbtype);
15+
16+
db_change($database_name);
17+
18+
if($skip) {
19+
$migrator->insert_migration($migration_file);
20+
$_SESSION['FLASH'] = "Skipped migration: $migration_file for database $database_name";
21+
header("Location: /debug.php?selected_database=$database_name");
22+
exit();
23+
}
24+
25+
if($delete) {
26+
$migrator->delete_migration($migration_file);
27+
$_SESSION['FLASH'] = "Deleted migration: $migration_file for database $database_name";
28+
header("Location: /debug.php?selected_database=$database_name");
29+
exit();
30+
}
31+
32+
$result = $migrator->apply_migration($migration_file);
33+
if ($result) {
34+
$_SESSION['FLASH'] = "Applied $migration_file to $database_name";
35+
} else {
36+
$_SESSION['FLASH'] = "Database migration $migration_file could not be applied to $database_name. Check application.log for details.";
37+
}
38+
39+
header("Location: /debug.php?selected_database=$database_name");
40+
exit();

htdocs/export/backupDataUI.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<?php
22
require_once("../includes/features.php");
3+
require_once("../config/lab_config_resolver.php");
34

4-
$lab_config_id = null;
5-
if (isset($_REQUEST['id'])) {
6-
$lab_config_id = $_REQUEST["id"];
7-
} else if (isset($_SESSION['lab_config_id'])) {
8-
$lab_config_id = $_SESSION["lab_config_id"];
9-
}
5+
$lab_config_id = LabConfigResolver::resolveId();
6+
$labConfigId = $lab_config_id;
107

118
if (Features::lab_config_v2_enabled()) {
129
header("Location: /config/v2/lab_config_backups.php?id=$lab_config_id");
@@ -16,13 +13,6 @@
1613
require_once("../includes/header.php");
1714
require_once("../includes/keymgmt.php");
1815

19-
$labConfigId = null;
20-
if (isset($_REQUEST['id'])) {
21-
$labConfigId = $_REQUEST["id"];
22-
} else if (isset($_SESSION['lab_config_id'])) {
23-
$labConfigId = $_SESSION["lab_config_id"];
24-
}
25-
2616
putUILog('backup_data_ui', 'X', basename($_SERVER['REQUEST_URI'], ".php"), 'X', 'X', 'X');
2717
$target_set=KeyMgmt::getAllKeys();
2818
$encryption_enabled = (KeyMgmt::read_enc_setting() == 1);

htdocs/includes/composer.php

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
mkdir(__DIR__."/../../files", 0755);
2727
}
2828

29+
if (!file_exists(__DIR__."/../../files/backups")) {
30+
mkdir(__DIR__."/../../files/backups", 0755);
31+
}
32+
33+
if (!file_exists(__DIR__."/../../files/storage")) {
34+
mkdir(__DIR__."/../../files/storage", 0755);
35+
}
36+
2937
require_once(__DIR__."/platform_lib.php");
3038

3139
# Ensure that we create the language files in local/

htdocs/includes/migrations.php

+39-20
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,41 @@ public function has_pending_migrations() {
3434
return count($this->pending_migrations()) > 0;
3535
}
3636

37+
public function insert_migration($migration_name) {
38+
query_insert_one("INSERT INTO blis_migrations (`name`) VALUES('$migration_name');");
39+
}
40+
41+
public function delete_migration($migration_name) {
42+
query_delete("DELETE FROM blis_migrations WHERE `name` = '$migration_name';");
43+
}
44+
45+
public function apply_migration($migration_name) {
46+
global $log;
47+
48+
$filepath = $this->migration_directory . "/$migration_name";
49+
50+
$result = null;
51+
52+
if ($migration_name == "20240815195015_add_all_columns_and_keys.sql") {
53+
$log->info("First migration detected. Welcome to the new BLIS!");
54+
$migration_file = file_get_contents($filepath);
55+
$migration_lines = explode("\n", $migration_file);
56+
$result = $this->execute_sql_lines_and_ignore_errors($migration_lines);
57+
} else {
58+
$result = $this->execute_sql($filepath);
59+
}
60+
61+
if ($result == true) {
62+
$this->insert_migration($migration_name);
63+
$log->info("Applied migration to " . $this->lab_db_name . ": $migration_name");
64+
} else {
65+
$log->error("Failed to apply migration: $migration_name");
66+
return false;
67+
}
68+
69+
return true;
70+
}
71+
3772
public function apply_migrations() {
3873
global $log;
3974

@@ -42,24 +77,8 @@ public function apply_migrations() {
4277
$pending_migrations = $this->pending_migrations();
4378

4479
foreach($pending_migrations as $migration) {
45-
$filepath = $this->migration_directory . "/$migration";
46-
47-
$result = null;
48-
49-
if ($migration == "20240815195015_add_all_columns_and_keys.sql") {
50-
$log->info("First migration detected. Welcome to the new BLIS!");
51-
$migration_file = file_get_contents($filepath);
52-
$migration_lines = explode("\n", $migration_file);
53-
$result = $this->execute_sql_lines_and_ignore_errors($migration_lines);
54-
} else {
55-
$result = $this->execute_sql($filepath);
56-
}
57-
58-
if ($result == true) {
59-
query_insert_one("INSERT INTO blis_migrations (name) VALUES('$migration');");
60-
$log->info("Applied migration to " . $this->lab_db_name . ": $migration");
61-
} else {
62-
$log->error("Failed to apply migration: $migration");
80+
$result = $this->apply_migration($migration);
81+
if (!$result) {
6382
return false;
6483
}
6584
}
@@ -128,7 +147,7 @@ private function execute_sql_lines_and_ignore_errors($sql_lines) {
128147
return true;
129148
}
130149

131-
private function get_available_migrations() {
150+
public function get_available_migrations() {
132151
$migrations = array();
133152
foreach(scandir($this->migration_directory) as $dir) {
134153
if ($dir == "." || $dir == "..") {
@@ -140,7 +159,7 @@ private function get_available_migrations() {
140159
return $migrations;
141160
}
142161

143-
private function get_applied_migrations() {
162+
public function get_applied_migrations() {
144163
global $log;
145164

146165
db_change($this->lab_db_name);

htdocs/update/blis_update.php

+2-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require_once("../includes/user_lib.php");
1616
require_once("../includes/composer.php");
1717
require_once("../includes/migrations.php");
18+
require_once("../config/lab_config_resolver.php");
1819

1920
include_once("../lang/lang_util.php");
2021

@@ -102,16 +103,7 @@
102103
$query_string = "INSERT INTO version_data (version, status, user_id, i_ts) VALUES ('$vers', $status, '$uid', NOW())";
103104
query_insert_one($query_string);
104105

105-
$lab_config_id = null;
106-
if (isset($_REQUEST["lab_config_id"])) {
107-
$lab_config_id = $_REQUEST["lab_config_id"];
108-
} else if (isset($_SESSION["lab_config_id"])) {
109-
$lab_config_id = $_SESSION["lab_config_id"];
110-
} else {
111-
$log->error("Could not determine lab config ID in blis_update.php.");
112-
echo("false");
113-
return;
114-
}
106+
$lab_config_id = LabConfigResolver::resolveId();
115107

116108
$lab_db_name_query = "SELECT db_name FROM lab_config WHERE lab_config_id = '$lab_config_id';";
117109
$result = query_associative_one($lab_db_name_query);

0 commit comments

Comments
 (0)