{"id":18929466,"url":"https://github.com/thecodingmachine/schema-analyzer","last_synced_at":"2025-04-15T15:30:59.480Z","repository":{"id":57020142,"uuid":"42710630","full_name":"thecodingmachine/schema-analyzer","owner":"thecodingmachine","description":"A package that offers utility tools to analyze database schemas (on top of Doctrine DBAL)","archived":false,"fork":false,"pushed_at":"2024-02-01T11:56:54.000Z","size":49,"stargazers_count":7,"open_issues_count":0,"forks_count":5,"subscribers_count":13,"default_branch":"1.1","last_synced_at":"2025-04-11T18:59:50.570Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-18T08:58:43.000Z","updated_at":"2025-01-27T22:14:34.000Z","dependencies_parsed_at":"2024-06-19T00:29:59.793Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/schema-analyzer","commit_stats":{"total_commits":36,"total_committers":3,"mean_commits":12.0,"dds":0.08333333333333337,"last_synced_commit":"bb3e99eef214fd45be56da561d6623baaaf03f4b"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fschema-analyzer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fschema-analyzer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fschema-analyzer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fschema-analyzer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/schema-analyzer/tar.gz/refs/heads/1.1","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248661496,"owners_count":21141449,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T11:32:53.345Z","updated_at":"2025-04-15T15:30:59.230Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://poser.pugx.org/mouf/schema-analyzer/v/stable)](https://packagist.org/packages/mouf/schema-analyzer)\n[![Latest Unstable Version](https://poser.pugx.org/mouf/schema-analyzer/v/unstable)](https://packagist.org/packages/mouf/schema-analyzer)\n[![License](https://poser.pugx.org/mouf/schema-analyzer/license)](https://packagist.org/packages/mouf/schema-analyzer)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thecodingmachine/schema-analyzer/badges/quality-score.png?b=1.0)](https://scrutinizer-ci.com/g/thecodingmachine/schema-analyzer/?branch=1.0)\n[![Build Status](https://travis-ci.org/thecodingmachine/schema-analyzer.svg?branch=1.0)](https://travis-ci.org/thecodingmachine/schema-analyzer)\n[![Coverage Status](https://coveralls.io/repos/thecodingmachine/schema-analyzer/badge.svg?branch=1.0\u0026service=github)](https://coveralls.io/github/thecodingmachine/schema-analyzer?branch=1.0)\n\n# Schema analyzer for DBAL\n\nThis package offer utility functions to analyze database schemas. It is built on top of Doctrine DBAL.\n\nIn this package, you will find:\n\n- Functions to automatically detect **junction tables**\n- Functions to compute the shortest path between 2 tables based on the relationships stored in the schema.\n\n## Installation\n\nYou can install this package through Composer:\n\n```json\n{\n    \"require\": {\n        \"mouf/schema-analyzer\": \"~1.0\"\n    }\n}\n```\n\nThe packages adheres to the [SemVer](http://semver.org/) specification, and there will be full backward compatibility\nbetween minor versions.\n\n## Detecting junction tables\n\nThe starting point is always a DBAL Schema. Pass the schema manager to SchemaAnalyzer, and then, simply call the functions.\n\n```php\n// $conn is the DBAL connection.\n$schemaAnalyzer = new SchemaAnalyzer($conn-\u003egetSchemaManager());\n\n// Let's detect all junctions tables\n$tables = $schemaAnalyzer-\u003edetectJunctionTables();\n// This will return an array of Doctrine\\DBAL\\Schema\\Table objects\n```\n\nA **junction table** is a table:\n\n- that has **exactly 2 foreign keys**\n- that has **only 2 columns** (or **3 columns** if the one of those is an *autoincremented primary key*).\n\nThere is an optional parameter you can use with `detectJunctionTables` that will automatically ignore any junction \ntable that is referenced by a foreign key of another table.\n\n```php\n// Get all junction tables except the ones that are references by a foreign key.\n$tables = $schemaAnalyzer-\u003edetectJunctionTables(true);\n```\n\n\n## Detecting inheritance relationship between tables\n\n### About inheritance relationships\n\nIf a table \"user\" has a primary key that is also a foreign key pointing on table \"contact\", then table \"user\" is \nconsidered to be a child of table \"contact\". This is because you cannot create a row in \"user\" without having a row \nwith the same ID in \"contact\".\n\nTherefore, a \"user\" ID has to match a \"contact\", but a \"contact\" has not necessarily a \"user\" associated. \n\n### Detecting inheritance relationships\n\nYou can use `SchemaAnalyzer` to detect parent / child relationships.\n\n- `getParentRelationship` takes a table name in parameter and returns the DBAL `ForeignKeyConstraint` representing\n  the relationship between this table and its parent.\n- `getChildrenRelationships` takes a table name in parameter and returns an array of DBAL `ForeignKeyConstraint` \n  representing the relationship between this table and its children.\n  \n\n```php\n$parentKeyConstraint = $schemaAnalyzer-\u003egetParentRelationship(\"user\");\n/* @var $parentKeyConstraint ForeignKeyConstraint */\n$parent = $parentKeyConstraint-\u003egetForeignTableName();\n// This will return the \"contact\" table (as a string)\n\n$childrenKeyConstraints = $schemaAnalyzer-\u003egetChildrenRelationships(\"contact\");\n/* @var $childrenKeyConstraints ForeignKeyConstraint[] */\n$children = array_map(function($item) { return $item-\u003egetLocalTableName(); }, $childrenKeyConstraints);\n// This will return an array of tables whose parent is contact: [\"user\"]\n```\n\n## Computing the shortest path between 2 tables\n\nFollowing foreign keys, the `getShortestPath` function will try to find the shortest path between 2 tables.\nIt will return the list of foreign keys it used to link the 2 tables.\n\nInternals:\n\n- Each foreign key has a *cost* of 1\n- Junction tables have a *cost* of 1.5, instead of 2 (one for each foreign key)\n- Foreign keys representing an inheritance relationship (i.e. foreign keys binding the primary keys of 2 tables)\n  have a *cost* of 0.1\n\n```php\n// $conn is the DBAL connection.\n$schemaAnalyzer = new SchemaAnalyzer($conn-\u003egetSchemaManager());\n\n// Let's detect the shortest path between 2 tables:\n$fks = $schemaAnalyzer-\u003egetShortestPath(\"users\", \"rights\");\n// This will return an array of Doctrine\\DBAL\\Schema\\ForeignKeyConstraint objects\n```\n\n\u003cdiv class=\"alert alert-info\"\u003e\u003cstrong\u003eHeads up!\u003c/strong\u003e The shortest path is based on the \u003cem\u003ecost\u003c/em\u003e of the \nforeign keys. It is perfectly possible to have several shortest paths (if several paths have the same total cost). \nIf there are several shortest paths, rather than choosing one path amongst the others, SchemaAnalyzer will throw\na \u003ccode\u003eShortestPathAmbiguityException\u003c/code\u003e. The exception message details all the possible shortest\npaths.\u003c/div\u003e\n\n## Caching results\n\nAnalyzing the full data model and looking for shortest paths can take a long time. For anything that should run \nin a production environment, it is recommended to cache the result. `SchemaAnalyzer` can be passed a Doctrine cache,\nalong a cache prefix. The cache prefix is a string that will be used to prefix all cache keys. It is useful to \navoid cache collisions between several databases.\n\nUsage:\n\n```php\n// $conn is the DBAL connection.\n// Let's use the ApcCache (or any other Doctrine cache...)\n$cache = new ApcCache();\n$schemaAnalyzer = new SchemaAnalyzer($conn-\u003egetSchemaManager(), $cache, \"my_prefix\");\n```\n\n## Changing the cost of the foreign keys to alter the shortest path\n\nIf you are facing an ambiguity exception or if the shortest path simply does not suit you, you can alter the \ncost of the foreign keys.\n\n```php\n$schemaAnalyzer-\u003esetForeignKeyCost($tableName, $columnName, $cost);\n```\n\nThe `$cost` can be any number. Remember that the default cost for a foreign key is **1**.\n\nSchemaAnalyzer comes with a set of default constants to help you work with costs:\n\n- `SchemaAnalyzer::WEIGHT_IMPORTANT` (0.75) for foreign keys that should be followed in priority \n- `SchemaAnalyzer::WEIGHT_IRRELEVANT` (2) for foreign keys that should be generally avoided \n- `SchemaAnalyzer::WEIGHT_IGNORE` (Infinity) for foreign keys that should never be used as part of the shortest path\n\nAnother option is to add a cost modifier to a table. This will alter the cost of all foreign keys pointing to or\noriginating from this table.\n\n```php\n$schemaAnalyzer-\u003esetTableCostModifier($tableName, $cost);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fschema-analyzer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Fschema-analyzer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fschema-analyzer/lists"}