laravel-tipi / schema
Declarative database schemas and safe migration generation for Laravel.
Requires
- php: ^8.5
- illuminate/console: ^13.0
- illuminate/contracts: ^13.0
- illuminate/database: ^13.0
- illuminate/filesystem: ^13.0
- illuminate/support: ^13.0
- laravel/prompts: ^0.3
Requires (Dev)
- laravel/pint: ^1.32
- orchestra/testbench: ^11.3
- pestphp/pest: ^5.3
- pestphp/pest-plugin-laravel: ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Declarative database schemas and safe migration generation for Laravel and MySQL 8.4.
You describe tables with PHP attributes. The package compares them with version-controlled snapshots, asks you about anything ambiguous or destructive, and writes ordinary, self-contained Laravel migrations that you review and run with php artisan migrate.
Philosophy
- Explicit over guessed. A missing declaration is never permission to delete data. Renames, drops, replacements and risky conversions need an explicit intention, and risky plans need approval of exactly the plan you reviewed.
- Migrations are yours. Generated migrations are application artifacts. They contain their own SQL and checks, depend on no package class, and never change when declarations, configuration or the package change later.
- Refuse rather than mislead. Unsupported structures, unverifiable states and databases that differ from what a migration was generated for are refused with an explanation, before anything is written or changed.
- Structure only. Schemas describe columns, keys, indexes and foreign keys. Models, relationships, validation and data migrations stay in your application.
- No surprises at runtime. Ordinary requests never compile schemas, inspect the database or load snapshots. Installing or upgrading a package that ships schemas never changes the database by itself.
Supported environment
PHP 8.5, Laravel 13 and MySQL 8.4 (InnoDB). Other databases are not supported. Generated migrations require strict SQL mode, foreign_key_checks = 1 and (for TIMESTAMP literal defaults) a UTC session, and check them before their first statement. Details: Supported environment.
Installation
composer require laravel-tipi/schema
php artisan vendor:publish --tag=schema-config # optional
The service provider is auto-discovered. Commit database/schemas/ (snapshots and history) together with your migrations.
Quick start
php artisan make:schema Product
namespace App\Schemas; use Tipi\Schema\Attributes\Column; use Tipi\Schema\Attributes\ForeignKey; use Tipi\Schema\Attributes\Id; use Tipi\Schema\Attributes\Index; use Tipi\Schema\Attributes\Table; use Tipi\Schema\Attributes\Timestamps; use Tipi\Schema\Schema; #[Table('products')] #[Timestamps] class ProductSchema extends Schema { #[Id] public int $id; #[Column(length: 120)] #[Index] public string $name; #[Column(type: 'decimal', precision: 10, scale: 2)] public string $price; #[Column(type: 'bigInteger', unsigned: true)] #[ForeignKey(on: CategorySchema::class, onDelete: 'cascade')] public int $category_id; }
php artisan schema:diff # what differs from the snapshots php artisan schema:generate --dry-run # the plan, its risks and fingerprint; writes nothing php artisan schema:generate # write the migration, snapshots and history php artisan migrate php artisan schema:status # the live database against the expected structure
Existing tables are adopted without generating anything: php artisan make:schema Team --from-table=teams, review the class, then php artisan schema:snapshot teams --from=database.
Documentation
| Guide | Contents |
|---|---|
| Schema declarations | Columns, types, defaults, keys and indexes, foreign keys, PHP enums, tables without models, pivot tables, inheritance and traits, configurable schemas |
| Registration and packages | Registration and ownership, the integration contract for reusable packages, application customization, installation and upgrades |
| Workflows | Adopting existing tables, snapshots and history, reconciliation, legacy baselines, hand-written migrations, multiple connections, status |
| Generating migrations | Intentions, risks, approvals, CI usage, fingerprints, what a generated migration contains |
| Execution and rollback | Execution-time preconditions, SQL modes, atomicity boundaries, operational considerations, rollback guarantees |
| Failure recovery | What to do when a migration fails part-way |
| Reference | Commands, public API, supported environment and features, scope boundaries, limitations, compatibility and upgrades |
What it does not guarantee
Migration-wide or cross-database atomicity, zero-downtime migrations, visibility of metadata the database user cannot see, protection against concurrent changes made while a migration runs, lossless reversal of destructive operations, automatic recovery after a failure, unlimited SQL expressions, every MySQL feature, or other database engines. See Limitations.
Testing
composer install docker compose up -d --wait # isolated MySQL 8.4 on port 33061 (tmpfs) composer test # Unit + Feature + Integration composer test:unit composer test:mysql docker compose down
Integration tests run generated migrations on the isolated server, inspect the result, and assert there are zero differences from the declared definitions, including data preservation and rollbacks. Without the server they are reported as skipped (set SCHEMA_TEST_REQUIRE_DB=1 to make them fail instead, as CI does). They refuse to run against a database whose name does not start with tipi_schema. Override the connection with SCHEMA_TEST_DB_HOST, _PORT, _DATABASE, _USERNAME and _PASSWORD.
License
MIT