PHPackages                             maba/database-inconsistency-finder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. maba/database-inconsistency-finder

ActiveLibrary[Database &amp; ORM](/categories/database)

maba/database-inconsistency-finder
==================================

Library that helps finding inconsistencies between database tables or different databases

0.1.0(6y ago)04MITPHPPHP &gt;=7.2

Since Aug 7Pushed 6y ago1 watchersCompare

[ Source](https://github.com/mariusbalcytis/database-inconsistency-finder)[ Packagist](https://packagist.org/packages/maba/database-inconsistency-finder)[ RSS](/packages/maba-database-inconsistency-finder/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (5)Versions (2)Used By (0)

Database inconsistency finder
=============================

[](#database-inconsistency-finder)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6d45e444a92c036570351309f8d5c7d43c0ffdeeaea084c4048c4836633233a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6162612f64617461626173652d696e636f6e73697374656e63792d66696e6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maba/database-inconsistency-finder)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/d3f27e73ddcb97fd316b3cd77415a04f5513c05bb40082099a4ef7f7a56d05af/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d617269757362616c63797469732f64617461626173652d696e636f6e73697374656e63792d66696e6465722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/mariusbalcytis/database-inconsistency-finder)[![Coverage Status](https://camo.githubusercontent.com/07da7bb3709676e010f14d71ebc4c60a75b8a65aa013a238776fb58b7761932e/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6d617269757362616c63797469732f64617461626173652d696e636f6e73697374656e63792d66696e6465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mariusbalcytis/database-inconsistency-finder/code-structure)[![Quality Score](https://camo.githubusercontent.com/48082ac6800bc3992b6e565421cebe0726756e842d6af91c1399caf009dfb896/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d617269757362616c63797469732f64617461626173652d696e636f6e73697374656e63792d66696e6465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mariusbalcytis/database-inconsistency-finder)[![Total Downloads](https://camo.githubusercontent.com/8d31f81279f2de88ec6df36e2fa28c23fdb9850d85ffd3d43965dbe8ac6bf21e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6162612f64617461626173652d696e636f6e73697374656e63792d66696e6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maba/database-inconsistency-finder)

Library that helps to find orphaned records (if they should be removed when nothing points to them), references to non-existing records and invalid number of references to those records.

When could this be helpful?
---------------------------

[](#when-could-this-be-helpful)

- you don't use foreign keys and could have invalid references. This could happen due to different reasons:
    - you have several different databases, for example when sharding or using microservices;
    - you don't use foreign keys for easier database structure migrations;
    - you have application that just does not use foreign keys;
- you want to find orphaned records. For example, you can have Files in a database table and if nothing points to this record, we want to delete the file itself.

Normally, these restrictions would be guaranteed by your application. Unfortunately, stuff happens and there might be some inconsistencies that just occur time to time.

Installation
------------

[](#installation)

```
composer require maba/database-inconsistency-finder
```

Configuration and usage
-----------------------

[](#configuration-and-usage)

```
$connection = DriverManager::getConnection(['url' => 'mysql://user:secret@localhost/mydb']);
$connection1 = DriverManager::getConnection(['url' => 'mysql://user:secret@db.example.org/otherdb']);

$referencesConfiguration = (new ReferencesConfiguration())
   ->setReferencedColumn(
       (new ReferencedColumn())
           ->setConnection($connection)
           ->setTableName('files')
           ->setIdColumnName('id')
           ->setReferenceNumberColumnName('reference_count')
   )
   ->addTableReferences(
       (new TableReferences())
           ->setConnection($connection)
           ->setTableName('profiles')
           ->setColumnNames(['avatar_file_id', 'cv_file_id'])
   )
   ->addTableReferences(
       (new TableReferences())
           ->setConnection($connection1)
           ->setTableName('documents')
           ->setColumnNames(['file_id'])
   )
;

$inconsistencyFinder = (new Factory())
    ->createInconsistencyFinder($referencesConfiguration)
;

$result = $inconsistencyFinder->find();

if ($result->areInconsistenciesFound()) {
    var_dump(
        $result->getOrphanedRecordIds(),
        $result->getMissingReferenceCounts(),
        $result->getInvalidReferenceCounts()
    );
}
```

Currently all work is done synchronously. You can configure this by implementing `JobDistributorFactoryInterface` and related `JobDistributorInterface`. In this case create service tree yourself, do not use the `Factory` class.

Internals
---------

[](#internals)

Consistency validation is performed in the following manner:

- ID range is queried from the database (from-to IDs in the main table)
- range is divided into separate intervals for job distribution
- each job is given to concrete worker
- worker validates consistency by using `SUM` query to the database, which is relatively fast
- if inconsistencies are found in the interval, it's split into even smaller intervals
- with each smaller interval `SUM` query is repeated
- for those intervals where inconsistencies are found, inconsistency seeking algorithm is ran

### Inconsistency seeking algorithm

[](#inconsistency-seeking-algorithm)

- all IDs and their corresponding reference counts are fetched from the database
- all related tables are iterated over and all IDs are fetched from there
- fetched data is looped to find any inconsistencies

These actions are performed in-memory, so it's essential that interval in this stage would be already quite small.

### Consistency validation

[](#consistency-validation)

Consistency is validated by issuing `SUM` queries to database. To avoid false positives, we select not the sum of reference counts, but sum of CRC32 of referenced IDs (and sum them that many times how many times they were referenced).

Semantic versioning
-------------------

[](#semantic-versioning)

This library follows [semantic versioning](http://semver.org/spec/v2.0.0.html).

See [Symfony BC rules](http://symfony.com/doc/current/contributing/code/bc.html) for basic information about what can be changed and what not in the API.

Running tests
-------------

[](#running-tests)

```
composer update
cd docker
docker-compose up -d
docker exec -it database_inconsistency_finder_test_php bin/phpunit
docker-compose down

```

Contributing
------------

[](#contributing)

Feel free to create issues and give pull requests.

You can fix any code style issues using this command:

```
composer fix-cs

```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2467d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6e131c9aa5f5a4fc3cc2f5e01b377ef33bbc3468acc95ff890fc6e8353d9cf67?d=identicon)[mariusbalcytis](/maintainers/mariusbalcytis)

---

Top Contributors

[![mariusbalcytis](https://avatars.githubusercontent.com/u/1590072?v=4)](https://github.com/mariusbalcytis "mariusbalcytis (6 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/maba-database-inconsistency-finder/health.svg)

```
[![Health](https://phpackages.com/badges/maba-database-inconsistency-finder/health.svg)](https://phpackages.com/packages/maba-database-inconsistency-finder)
```

###  Alternatives

[martin-georgiev/postgresql-for-doctrine

Extends Doctrine with native PostgreSQL support for arrays, JSONB, ranges, PostGIS geometries, text search, ltree, uuid, and 100+ PostgreSQL-specific functions.

4485.3M4](/packages/martin-georgiev-postgresql-for-doctrine)[damienharper/auditor-bundle

Integrate auditor library in your Symfony projects.

4542.8M](/packages/damienharper-auditor-bundle)[sonata-project/entity-audit-bundle

Audit for Doctrine Entities

644989.8k1](/packages/sonata-project-entity-audit-bundle)[overtrue/laravel-versionable

Make Laravel model versionable.

585308.0k5](/packages/overtrue-laravel-versionable)[worksome/foggy

Foggy is a tool for making database dumps with some data removed/changed.

26571.7k1](/packages/worksome-foggy)[flow-php/doctrine-dbal-bulk

Bulk inserts and updates for Doctrine DBAL

14295.2k1](/packages/flow-php-doctrine-dbal-bulk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
