Skip to content

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.

Location

\WP_Statistics\Service\Database\Managers\SchemaMaintainer

Description

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.

Example: SchemaMaintainer::check()

How to Call

use WP_Statistics\Service\Database\Managers\SchemaMaintainer;

$results = SchemaMaintainer::check();

Possible Output

Success (no issues or errors):

[
    'status' => 'success',
    'issues' => [],
    'errors' => []
]

Warning (issues found, but no system errors):

[
    'status' => 'warning',
    'issues' => [
        [
            'type' => 'missing_column',
            'table' => 'wp_statistics_visitor',
            'column' => 'browser',
            'columnDefinition' => 'VARCHAR(255) NOT NULL',
            'indexDefinition' => ''
        ]
    ],
    'errors' => []
]

Error (e.g. missing table, runtime error):

[
    'status' => 'error',
    'issues' => [],
    'errors' => [
        [
            'type' => 'table_missing',
            'table' => 'wp_statistics_visitor',
            'message' => 'Table wp_statistics_visitor does not exist.'
        ]
    ]
]

Example: SchemaMaintainer::repair()

How to Call

use WP_Statistics\Service\Database\Managers\SchemaMaintainer;

$results = SchemaMaintainer::repair();

Possible Output

Success (all issues fixed or no issues found):

[
    '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' => []
]

Partial (some issues fixed, some 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'
        ]
    ]
]

Error (system-level failure):

[
    'status' => 'error',
    'fixed' => [],
    'failed' => [],
    'errors' => [
        [
            'type' => 'system_error',
            'message' => 'Unexpected exception occurred.'
        ]
    ]
]

Notes

  • You do not need to call SchemaMaintainer::check() before calling repair().
  • The repair() method automatically runs check() 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.

See Also

Clone this wiki locally