Search by

laravel-tipi / db-schema

ika1224

Declarative database schemas and safe migration generation for Laravel.

Package info

github.com/laravel-tipi/db-schema

pkg:composer/laravel-tipi/db-schema

Statistics

Installs: 18

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.0 2026-11-30 22:45 UTC

This package is auto-updated.

Last update: 2026-11-30 22:55:07 UTC


README

Declarative database schemas and safe migration generation for Laravel, MySQL 8.4, PostgreSQL 15+ and SQLite 3.37+.

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 (including generated ones), keys, indexes (including expression, partial and conditional unique ones), foreign keys and check constraints. 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 and Laravel 13, with any of:

Engine Versions Each migration runs
MySQL (InnoDB) 8.4 one atomic ALTER TABLE per table, checks before the first statement; requires strict SQL mode and foreign_key_checks = 1
PostgreSQL 15 or later (VIRTUAL generated columns: 18) in one transaction, its tables locked before the checks
SQLite 3.37 or later in its own transaction holding the write lock; table changes rebuild the table

One schema class works on every engine; options only one engine has are refused elsewhere with the portable alternative, never ignored. Several connections, also on different engines, work in one application. Other databases (MariaDB, MySQL 9, SQL Server) are not supported. Details: Supported environment, the feature matrix, PostgreSQL and SQLite.

Installation

composer require laravel-tipi/db-schema
php artisan vendor:publish --tag=db-schema-config   # optional: config/tipi/db-schema.php
php artisan vendor:publish --tag=db-schema-stubs    # optional: stubs/schema.stub for make:schema

The service provider (Tipi\DbSchema\DbSchemaServiceProvider) is auto-discovered. The configuration lives under tipi.db-schema; a published file only needs the options it changes (see Configuration). Commit database/schemas/ (snapshots and history) together with your migrations.

Quick start

php artisan make:schema Product
namespace App\Schemas;

use Tipi\DbSchema\Attributes\Column;
use Tipi\DbSchema\Attributes\ForeignId;
use Tipi\DbSchema\Attributes\ForeignKey;
use Tipi\DbSchema\Attributes\Id;
use Tipi\DbSchema\Attributes\Index;
use Tipi\DbSchema\Attributes\Table;
use Tipi\DbSchema\Attributes\Timestamps;
use Tipi\DbSchema\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;

    #[ForeignId]
    #[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
Portability One declaration on every engine: per-engine variants (dialect:), the cross-database feature matrix, connection isolation
SQLite Supported versions, types and affinity, refused options, canonical expressions, table rebuilds, the migration's own transaction and its checks
PostgreSQL Supported versions, schemas and names, types, native enums, refused options, expressions, transactional execution, safety guarantees compared with MySQL
Reference Commands, public API, supported environment and features, scope boundaries, limitations, compatibility and upgrades

What it does not guarantee

Migration-wide atomicity on MySQL (PostgreSQL and SQLite run each migration in one transaction), atomicity across connections, zero-downtime migrations, visibility of metadata the database user cannot see, protection on MySQL against concurrent changes made while a migration runs (PostgreSQL and SQLite lock first), lossless reversal of destructive operations, automatic recovery after a failure, unlimited SQL expressions, every feature of an engine, or engines other than MySQL, PostgreSQL and SQLite. See Limitations.

Testing

composer install
docker compose up -d --wait   # isolated MySQL 8.4 (port 33061), PostgreSQL 15 (54315) and 18 (54318), tmpfs
composer test                 # every suite (PostgreSQL on 18)
composer test:unit
composer test:mysql           # MySQL integration tests
composer test:pgsql           # the PostgreSQL suite on 15 and 18
composer test:sqlite          # the SQLite suite on the host's SQLite and on 3.37.2 (docker)
composer test:portability     # one application with MySQL, PostgreSQL and SQLite connections
docker compose down

Database tests run generated migrations on the isolated servers (and on SQLite files), inspect the result, and assert there are zero differences from the declared definitions, including data preservation and rollbacks. Without a server they are reported as skipped; set SCHEMA_TEST_REQUIRE_DB=1 to make them fail instead. CI does, runs every suite with --fail-on-skipped --fail-on-empty-test-suite and verifies the reference fixtures, on MySQL 8.4, PostgreSQL 15 and 18, PHP's SQLite and SQLite 3.37.2. The MySQL tests refuse to run against a database whose name does not start with tipi_db_schema; override that connection with SCHEMA_TEST_DB_HOST, _PORT, _DATABASE, _USERNAME and _PASSWORD, and PostgreSQL's with SCHEMA_TEST_PG_HOST and SCHEMA_TEST_PG_PORT (54315 selects the PostgreSQL 15 fixtures).

Reference fixtures (tests/Reference/, per engine and PostgreSQL version) record everything each Feature and database test makes the package write (migrations, snapshots, history) and the live structure it inspects. SCHEMA_REFERENCE=verify composer test fails any test whose output differs byte for byte; SCHEMA_REFERENCE=record re-records them after an intended change.

License

MIT