@@ -370,6 +370,7 @@ The ``db`` commands manage database schema, indices, and data conversions::
370370 db:convert-filecache-bigint convert the ID columns of the filecache to BigInt
371371 db:convert-mysql-charset convert charset of MySQL/MariaDB to utf8mb4
372372 db:convert-type convert the Nextcloud database to a different type
373+ db:schema:check compare the live database schema with the expected schema
373374 db:schema:expected export the expected database schema for a fresh installation
374375 db:schema:export export the current database schema
375376
@@ -417,6 +418,124 @@ Use ``--dry-run`` to preview the SQL statements without executing them.
417418Schema inspection
418419^^^^^^^^^^^^^^^^^^
419420
421+ db:schema: check
422+ """""""""""""""
423+
424+ .. versionadded :: 35.0
425+
426+ Compare the live database schema with the schema expected for the
427+ currently installed Nextcloud version::
428+
429+ sudo -E -u www-data php occ db:schema:check
430+ The live database schema matches the expected schema.
431+
432+ The expected schema is generated from the migrations provided by
433+ Nextcloud core, all enabled apps, and any disabled apps whose code is
434+ still present. The command reports missing or unexpected tables,
435+ columns, and indexes. It also reports column properties that differ,
436+ such as the type, length, default value, or whether the column is
437+ nullable.
438+
439+ The command only inspects the schema and does not modify the database.
440+ It exits with status ``0 `` if there are no findings for core or an
441+ enabled app, and status ``1 `` otherwise. This makes it suitable for use
442+ in monitoring scripts. Findings that only concern a disabled app never
443+ affect the exit status. They are excluded from the plain output unless
444+ you add ``-v ``::
445+
446+ sudo -E -u www-data php occ db:schema:check -v
447+ oc_example: column 'size' differs in: type, default
448+ Disabled apps (not affecting exit code):
449+ If the schema for a disabled app differs from what is expected, this might indicate the app was
450+ updated since it was disabled. Missing migrations will be applied once the app is enabled again.
451+ files_external:
452+ - oc_storages: missing column 'backend'
453+
454+ Specify a table name to limit the comparison to that table::
455+
456+ sudo -E -u www-data php occ db:schema:check oc_filecache
457+
458+ Use the complete table name, including the configured database table
459+ prefix. Use ``--output=json `` or ``--output=json_pretty `` to produce
460+ machine-readable output. Unlike plain output, JSON output always
461+ includes every finding, including those for disabled apps. For example:
462+
463+ .. code-block :: console
464+
465+ $ sudo -E -u www-data php occ db:schema:check --output=json_pretty
466+ [
467+ {
468+ "table": "oc_filecache",
469+ "type": "modified_column",
470+ "name": "size",
471+ "changes": [
472+ "type",
473+ "default"
474+ ],
475+ "app": null,
476+ "enabled": true
477+ },
478+ {
479+ "table": "oc_storages",
480+ "type": "missing_column",
481+ "name": "backend",
482+ "app": "files_external",
483+ "enabled": false
484+ },
485+ {
486+ "table": "oc_example",
487+ "type": "unexpected_table",
488+ "app": null,
489+ "enabled": true
490+ }
491+ ]
492+
493+ Each finding contains:
494+
495+ * ``table `` — the complete database table name.
496+ * ``type `` — one of ``missing_table ``, ``unexpected_table ``,
497+ ``missing_column ``, ``unexpected_column ``, ``modified_column ``,
498+ ``missing_index ``, or ``unexpected_index ``.
499+ * ``name `` — the affected column or index. This field is omitted for
500+ table findings.
501+ * ``changes `` — the changed column properties. This field is only
502+ present when ``type `` is ``modified_column ``. Possible values include
503+ ``type ``, ``length ``, ``precision ``, ``scale ``, ``nullable ``,
504+ ``default ``, ``autoincrement ``, ``unsigned ``, ``fixed ``, and
505+ ``comment ``.
506+ * ``app `` — the ID of the disabled app that owns the affected table, or
507+ ``null `` if the table belongs to core or an enabled app.
508+ * ``enabled `` — ``false `` if the finding only concerns a disabled app
509+ and does not affect the exit status, ``true `` otherwise.
510+
511+ When the schemas match, JSON output contains an empty array.
512+
513+ .. note ::
514+
515+ The expected schema does not account for all database-wide settings
516+ or defaults, so some reported differences may be valid. A table or
517+ other object still counts as unexpected if it belongs to an app that
518+ was disabled and then fully removed. Review each finding before
519+ changing the database.
520+
521+ The schema check also runs automatically after ``occ upgrade ``.
522+ Findings for core or an enabled app are displayed as warnings but do
523+ not cause the upgrade to fail; findings that only concern a disabled
524+ app are not shown here. For example, the following output may appear
525+ near the end of an upgrade:
526+
527+ .. code-block :: console
528+
529+ The database schema does not match what is expected for the installed version:
530+ - oc_filecache: column 'size' differs in: type, default
531+ - oc_filecache: missing index 'fs_storage_path_hash'
532+ - unexpected table 'oc_example'
533+ Run "occ db:schema:check -v" for details.
534+
535+ Run ``occ db:schema:check `` separately to inspect the differences in
536+ plain (add ``-v `` to include disabled-app findings) or machine-readable
537+ format.
538+
420539db:schema: export
421540""""""""""""""""
422541
0 commit comments