-
Notifications
You must be signed in to change notification settings - Fork 37
SchemaMaintainer
hooman helmi edited this page Jun 2, 2025
·
3 revisions
The SchemaMaintainer class is responsible for maintaining and repairing the integrity of the WP Statistics plugin's database schema.
It compares the actual database structure with the expected schema definitions and provides tools to detect and fix inconsistencies such as missing columns or indexes.
\WP_Statistics\Service\Database\Managers\SchemaMaintainer
The SchemaMaintainer performs schema inspection and automated repair by:
- Scanning all WP Statistics database tables
- Detecting missing columns and indexes
- Reporting or fixing discrepancies
It works with the Schema\Manager to retrieve expected table definitions and uses DatabaseFactory to inspect and alter tables as needed.
use WP_Statistics\Service\Database\Managers\SchemaMaintainer;
$results = SchemaMaintainer::check();[
'status' => 'success',
'issues' => [],
'errors' => []
][
'status' => 'warning',
'issues' => [
[
'type' => 'missing_column',
'table' => 'wp_statistics_visitor',
'column' => 'browser',
'columnDefinition' => 'VARCHAR(255) NOT NULL',
'indexDefinition' => ''
]
],
'errors' => []
][
'status' => 'error',
'issues' => [],
'errors' => [
[
'type' => 'table_missing',
'table' => 'wp_statistics_visitor',
'message' => 'Table wp_statistics_visitor does not exist.'
]
]
]use WP_Statistics\Service\Database\Managers\SchemaMaintainer;
$results = SchemaMaintainer::repair();[
'status' => 'success',
'fixed' => [
[
'type' => 'missing_column',
'table' => 'wp_statistics_visitor',
'column' => 'browser',
'columnDefinition' => [
'type' => 'varchar(255)',
'null' => false,
'default' => '',
'extra' => ''
],
'indexDefinition' => 'INDEX `browser` (`browser`)' // optional, might be empty
]
],
'failed' => []
][
'status' => 'partial',
'fixed' => [
[
'type' => 'missing_column',
'table' => 'wp_statistics_visitor',
'column' => 'browser',
'columnDefinition' => [...],
'indexDefinition' => '...' // optional, might be empty
]
],
'failed' => [
[
'issue' => [...], // Same structure as above
'type' => 'repair_failed',
'message' => 'Error message from exception'
]
]
][
'status' => 'error',
'fixed' => [],
'failed' => [],
'errors' => [
[
'type' => 'system_error',
'message' => 'Unexpected exception occurred.'
]
]
]- You do not need to call
SchemaMaintainer::check()before callingrepair(). - The
repair()method automatically runscheck()internally to find any schema issues. - Call
check()only when you want to:- Inspect the current database schema,
- View missing columns or other structural issues,
- Log or display issues without making changes.