wip
This commit is contained in:
Regular → Executable
+60
-2
@@ -13,7 +13,7 @@ class MLS_DB {
|
||||
* Schema version for index migrations
|
||||
* Increment this when adding new indexes or columns
|
||||
*/
|
||||
const SCHEMA_VERSION = 3;
|
||||
const SCHEMA_VERSION = 5;
|
||||
|
||||
/**
|
||||
* Get table name with prefix
|
||||
@@ -415,8 +415,66 @@ class MLS_DB {
|
||||
update_option('mls_schema_version', 3);
|
||||
}
|
||||
|
||||
// Migration to schema version 4: Add spatial POINT column and index
|
||||
if ($current_schema < 4) {
|
||||
$table_properties = $wpdb->prefix . MLS_TABLE_PROPERTIES;
|
||||
|
||||
// Check if location column exists
|
||||
$column_exists = $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = 'location'",
|
||||
DB_NAME,
|
||||
$table_properties
|
||||
));
|
||||
|
||||
if (!$column_exists) {
|
||||
// Add POINT column (nullable initially for population)
|
||||
$wpdb->query("ALTER TABLE {$table_properties} ADD COLUMN location POINT SRID 4326 DEFAULT NULL AFTER longitude");
|
||||
|
||||
// Populate location from existing lat/lng
|
||||
// Note: SRID 4326 uses axis order (latitude, longitude) in MySQL 8.0+
|
||||
$wpdb->query("UPDATE {$table_properties} SET location = ST_PointFromText(CONCAT('POINT(', latitude, ' ', longitude, ')'), 4326) WHERE latitude IS NOT NULL AND longitude IS NOT NULL");
|
||||
|
||||
// Make column NOT NULL (required for spatial index)
|
||||
$wpdb->query("ALTER TABLE {$table_properties} MODIFY location POINT NOT NULL SRID 4326");
|
||||
|
||||
// Add spatial index
|
||||
$existing_indexes = self::get_existing_indexes($table_properties);
|
||||
if (!isset($existing_indexes['idx_location_spatial'])) {
|
||||
$wpdb->query("ALTER TABLE {$table_properties} ADD SPATIAL INDEX idx_location_spatial (location)");
|
||||
}
|
||||
}
|
||||
|
||||
update_option('mls_schema_version', 4);
|
||||
}
|
||||
|
||||
// Migration to schema version 5: Add coordinates_invalid column for geo validation
|
||||
if ($current_schema < 5) {
|
||||
$table_properties = $wpdb->prefix . MLS_TABLE_PROPERTIES;
|
||||
|
||||
// Check if column exists
|
||||
$column_exists = $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = 'coordinates_invalid'",
|
||||
DB_NAME,
|
||||
$table_properties
|
||||
));
|
||||
|
||||
if (!$column_exists) {
|
||||
$wpdb->query("ALTER TABLE {$table_properties} ADD COLUMN coordinates_invalid TINYINT(1) NOT NULL DEFAULT 0 AFTER longitude");
|
||||
}
|
||||
|
||||
// Add index if not exists
|
||||
$existing_indexes = self::get_existing_indexes($table_properties);
|
||||
if (!isset($existing_indexes['idx_coordinates_invalid'])) {
|
||||
$wpdb->query("ALTER TABLE {$table_properties} ADD INDEX idx_coordinates_invalid (coordinates_invalid)");
|
||||
}
|
||||
|
||||
update_option('mls_schema_version', 5);
|
||||
}
|
||||
|
||||
// Future migrations go here:
|
||||
// if ($current_schema < 4) { ... }
|
||||
// if ($current_schema < 6) { ... }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user