|
1 | 1 | # codeigniter4-schemas
|
2 | 2 | Database schema management, for CodeIgniter 4
|
| 3 | + |
| 4 | +## Quick Start |
| 5 | + |
| 6 | +1. Install with Composer: `> composer require codenom/schemas` |
| 7 | +2. Generate a new schema: `> php spark schemas` |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +* View your entire database mapped out in a cascading structure |
| 12 | +* Read or detect table relationships for easy object-relation mapping (see e.g. [Tatter\Relations](https://github.com/tattersoftware/codeigniter4-relations)) |
| 13 | +* Get helpful advice on optimizations to your database structure with schema analysis<sup>1</sup> |
| 14 | +* Backup, restore, or deploy an entire database structure between servers or environments<sup>1</sup> |
| 15 | +* Generate CodeIgniter 4 migration files from an existing database<sup>1</sup> |
| 16 | +* Transfer projects to CodeIgniter 4 by reading schema files from other supported formats<sup>1</sup> |
| 17 | + |
| 18 | +<sup>1</sup> Some features are still in development. See **Handlers > Development** for |
| 19 | +planned future expansion. |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities |
| 24 | +and always be up-to-date: |
| 25 | +* `> composer require codenom/schemas` |
| 26 | + |
| 27 | +Or, install manually by downloading the source files and adding the directory to |
| 28 | +`app/Config/Autoload.php`. |
| 29 | + |
| 30 | +## Configuration (optional) |
| 31 | + |
| 32 | +The library's default behavior can be altered by extending its config file. Copy |
| 33 | +**bin/Schemas.php** to **app/Config/** and follow the instructions |
| 34 | +in the comments. If no config file is found in **app/Config** the library will use its own. |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +**Schemas** has four main functions, each with a variety of handlers available: |
| 39 | +* *Draft*: Generates a new schema from a variety of sources |
| 40 | +* *Archive*: Stores a copy of a schema for later use |
| 41 | +* *Read*: Loads a schema for live access |
| 42 | +* *Publish*: (not yet available) Modifies environments to match schema specs |
| 43 | + |
| 44 | +The **Schemas** service is also available to simplify a workflow with convenient wrapper functions. |
| 45 | +At its most basic (with automation enabled), the service will draft, archive, and return |
| 46 | +a schema with one simple command: |
| 47 | + |
| 48 | + $schema = service('schemas')->get(); |
| 49 | + |
| 50 | +You may want to control when portions of the workflow take place to optimize performance. |
| 51 | +Here is an example of one common process, mapping the default database group and storing |
| 52 | +the resulting schema to the cache: |
| 53 | + |
| 54 | +``` |
| 55 | +// Map the database and store the schema in cache |
| 56 | +$schemas = service('schemas'); |
| 57 | +$schemas->draft('database')->archive('cache'); |
| 58 | +
|
| 59 | +// Load the schema from cache, add Model data, and get the updated schema |
| 60 | +$schema = $schemas->read('cache')->draft('model')->get(); |
| 61 | +``` |
| 62 | + |
| 63 | +If you need to deviate from default handler configurations you can inject the handlers yourself: |
| 64 | +``` |
| 65 | +$db = db_connect('alternate_database'); |
| 66 | +$databaseHandler = new \Codenom\Schemas\Drafter\Handlers\DatabaseHandler(null, $db); |
| 67 | +$schema = $schemas->draft($databaseHandler)->get(); |
| 68 | +``` |
| 69 | + |
| 70 | +## Command |
| 71 | + |
| 72 | +**Schemas** comes with a `spark` command for convenient schema generation and display: |
| 73 | + |
| 74 | + `schemas [-draft handler1,handler2,...] [-archive handler1,... | -print]` |
| 75 | + |
| 76 | +Use the command to test and troubleshoot, or add it to your cron for periodic schema caching: |
| 77 | + |
| 78 | + php spark schemas -draft database,model -archive cache |
| 79 | + |
| 80 | +## Automation |
| 81 | + |
| 82 | +By default automation is turned on, but this can be configured via the `$automate` toggles |
| 83 | +in your config file. Automation will allow the service to fall back on a Reader, or even on |
| 84 | +a Drafter should it fail to have a schema already loaded. While automation makes using the |
| 85 | +library very easy, it can come at a performance cost if your application is not configured |
| 86 | +correctly, since it may draft a schema on every page load. Use automation to help but don't |
| 87 | +let it become a crutch. |
| 88 | + |
| 89 | +## Structure |
| 90 | + |
| 91 | +**Schemas** uses foreign keys, indexes, and naming convention to detect relationships |
| 92 | +automatically. Make sure your database is setup using the appropriate keys and |
| 93 | +foreign keys to assist with the detection. Naming conventions follow the format of |
| 94 | +`{table}_id` for foreign keys and `{table1}_{table2}` for pivot tables. For more examples |
| 95 | +on relationship naming conventions consult the Rails Guide |
| 96 | +[Active Record Associations](https://guides.rubyonrails.org/association_basics.html#the-types-of-associations) |
| 97 | +(and please excuse the Ruby reference). |
| 98 | + |
| 99 | +### Intervention |
| 100 | + |
| 101 | +Should autodetection fail or should you need to deviate from conventions there are a few |
| 102 | +tools you can use to overwrite or augment the generated schema. |
| 103 | + |
| 104 | +* **Config/Schemas**: the Config file includes a variable for `$ignoredTables` that will let you skip tables entirely. By default this includes the framework's `migrations` table. |
| 105 | +* **app/Schemas/{file}.php**: The `DirectoryHandler` will load any schemas detected in your **Schemas** directory - this gives you a chance to specify anything you want. See [tests/_support/Schemas/Good/Products.php](tests/_support/Schemas/Good/Products.php) for an example. |
| 106 | + |
| 107 | +## Drafting |
| 108 | + |
| 109 | +Currently supported handlers: |
| 110 | + |
| 111 | +* Database |
| 112 | +* Model |
| 113 | +* PHP |
| 114 | +* Directory (PHP import only) |
| 115 | + |
| 116 | +## Archiving/reading |
| 117 | + |
| 118 | +* Cache |
| 119 | + |
| 120 | +## Database Support |
| 121 | + |
| 122 | +All CodeIgniter 4 database drivers work but due to some differences in index handling they |
| 123 | +may not all report the same results. Example: see skipped tests for SQLite3. |
| 124 | + |
| 125 | +## Development |
| 126 | + |
| 127 | +The eventual goal is to support mapping from and deploying to any source. Planned handler |
| 128 | +implementations include: |
| 129 | + |
| 130 | +* `Publisher\DatabaseHandler`: Recreate a live database from its schema |
| 131 | +* `MigrationsHandler`: Create a schema from migration files, or vice versa |
| 132 | +* `FileHandler`: A wrapper for importing and exporting from popular schema file formats |
| 133 | +* Lots more... |
| 134 | + |
| 135 | +And the file-specific handlers: |
| 136 | +* `PhpHandler->archive()`: Create a PHP file with a Schema object in `$schema` |
| 137 | +* `XmlHandler`: Support for Doctrine-style XML files |
| 138 | +* More to come... |
| 139 | + |
| 140 | +Want to help out? All code and issues are managed on GitHub at [Codenom\Schemas](https://github.com/codenomdev/codeigniter4-schemas) |
0 commit comments