georgeff/schema 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

georgeff/schema

Composer 安装命令:

composer require georgeff/schema

包简介

Database schema builder — driver-agnostic blueprints compiled to SQL for MySQL, PostgreSQL, and SQLite

README 文档

README

Database schema builder for PHP. Define table structures using a fluent blueprint API and compile them to SQL for MySQL, PostgreSQL, or SQLite. Produces raw SQL strings — no connection required.

Installation

composer require georgeff/schema

Overview

georgeff/schema separates schema definition from execution. You build a Blueprint, pass it to a compiler, and receive an array of SQL strings to run however you like.

use Georgeff\Schema\Blueprint;
use Georgeff\Schema\Compiler\PostgreSQLCompiler;

$blueprint = new Blueprint('users');
$blueprint->id();
$blueprint->string('email')->unique();
$blueprint->string('name');
$blueprint->timestamps();

$compiler = new PostgreSQLCompiler();

foreach ($compiler->create($blueprint) as $sql) {
    // execute $sql against your connection
}

Compilers

Three compilers are available, each implementing CompilerInterface:

use Georgeff\Schema\Compiler\MySQLCompiler;
use Georgeff\Schema\Compiler\PostgreSQLCompiler;
use Georgeff\Schema\Compiler\SQLiteCompiler;

Both MySQLCompiler and PostgreSQLCompiler accept optional constructor arguments:

$compiler = new MySQLCompiler(
    engine:  'InnoDB',            // default
    charset: 'utf8mb4',           // default
    collate: 'utf8mb4_unicode_ci' // default
);

$compiler = new PostgreSQLCompiler(
    schema: 'public' // default; set to your target schema if not using the default
);

Compiler Methods

create(Blueprint $blueprint): string[]

Returns one or more SQL statements to create the table. MySQL always returns a single statement with indexes inline. PostgreSQL and SQLite return additional CREATE INDEX statements when non-primary indexes are present.

$statements = $compiler->create($blueprint);
// ['CREATE TABLE ...', 'CREATE UNIQUE INDEX ...']

drop(string $table, bool $ifExists = false): string

$compiler->drop('users');
// DROP TABLE "users";

$compiler->drop('users', ifExists: true);
// DROP TABLE IF EXISTS "users";

tableExists(): string

Returns a parameterized SQL query that checks whether a table exists. Pass the table name as a bound parameter when executing — do not interpolate it directly.

$sql = $compiler->tableExists();
// execute with [$tableName] as bindings; returns a count

The query shape varies by driver:

  • MySQL: queries information_schema.tables scoped to DATABASE()
  • PostgreSQL: queries information_schema.tables scoped to the configured schema (default public)
  • SQLite: queries sqlite_master

alter(Blueprint $blueprint): string[]

Returns one statement per change. Used for adding/dropping columns, indexes, and foreign keys on an existing table.

$blueprint = new Blueprint('users');
$blueprint->string('phone')->nullable();
$blueprint->dropColumn('bio');

foreach ($compiler->alter($blueprint) as $sql) {
    // execute $sql
}

SQLite limitation: SQLite only supports ADD COLUMN and DROP COLUMN. Calls to dropIndex(), dropForeign(), and foreign() on a Blueprint passed to SQLiteCompiler::alter() are silently ignored.

Blueprint

Column Types

Method Description
id(string $name = 'id') BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
bigInteger(string $name) 8-byte integer
integer(string $name) 4-byte integer
smallInteger(string $name) 2-byte integer
tinyInteger(string $name) 1-byte integer (MySQL) / SMALLINT (PostgreSQL)
float(string $name) Floating point
decimal(string $name, int $precision = 8, int $scale = 2) Fixed precision
string(string $name, int $length = 255) VARCHAR
char(string $name, int $length = 1) Fixed-length string
text(string $name) Unbounded text
boolean(string $name) Boolean (TINYINT(1) on MySQL, BOOLEAN on PostgreSQL, INTEGER on SQLite)
json(string $name) JSON (TEXT on SQLite)
uuid(string $name) UUID (CHAR(36) on MySQL, UUID on PostgreSQL, TEXT on SQLite)
date(string $name) Date
time(string $name) Time
timestamp(string $name) Timestamp (TEXT on SQLite)
timestamps() Adds nullable created_at and updated_at timestamp columns

Column Modifiers

Modifiers chain off any column factory method:

$blueprint->string('email')
    ->nullable()
    ->default('guest@example.com')
    ->unique();

defaultRaw() emits the provided string directly into the DDL without quoting — use it for SQL functions and expressions:

$blueprint->timestamp('created_at')->defaultRaw('CURRENT_TIMESTAMP');
$blueprint->string('id')->defaultRaw('gen_random_uuid()');

Warning: defaultRaw() must never be called with user-supplied input. It is intended for developer-authored migration files only.

Modifier Description
nullable(bool $value = true) Allows NULL; pass false to revert
unsigned() UNSIGNED (MySQL only; ignored on PostgreSQL and SQLite)
default(mixed $value) Sets a default value
defaultRaw(string $sql) Sets a raw SQL expression as the default — emitted unquoted
incrementing() AUTO_INCREMENT (MySQL) / SERIAL/BIGSERIAL (PostgreSQL) / AUTOINCREMENT (SQLite)
primary(?string $name = null) Marks column as primary key
unique(?string $name = null) Adds a unique index
index(?string $name = null) Adds a regular index

Foreign Keys

$blueprint->bigInteger('user_id');
$blueprint->foreign('user_id')
    ->references('id')
    ->on('users')
    ->onDelete('CASCADE')
    ->onUpdate('RESTRICT');

An optional constraint name can be passed as the second argument to foreign():

$blueprint->foreign('user_id', 'fk_posts_user_id')
    ->references('id')
    ->on('users');

Valid actions for onDelete and onUpdate: NO ACTION, RESTRICT, SET NULL, CASCADE. Both default to RESTRICT.

ALTER Operations

$blueprint = new Blueprint('users');

// Add columns
$blueprint->string('phone')->nullable();

// Drop columns
$blueprint->dropColumn('legacy_field');

// Drop indexes
$blueprint->dropIndex('users_email_unique');

// Drop foreign keys
$blueprint->dropForeign('posts_user_id_foreign');

Index Naming

When no name is provided, indexes are auto-named using the pattern:

{table}_{column}_{suffix}

Where suffix is primary, unique, index, or foreign. For example:

$blueprint->string('email')->unique();
// Index name: users_email_unique

$blueprint->foreign('user_id')->references('id')->on('users');
// Constraint name: posts_user_id_foreign

Pass an explicit name to override:

$blueprint->string('email')->unique('my_email_constraint');

Driver Differences

Feature MySQL PostgreSQL SQLite
Quoting Backticks Double quotes Double quotes
Auto-increment AUTO_INCREMENT SERIAL / BIGSERIAL INTEGER PRIMARY KEY AUTOINCREMENT
Unsigned columns Supported Silently ignored Silently ignored
Boolean TINYINT(1) BOOLEAN INTEGER
UUID CHAR(36) UUID TEXT
Indexes in CREATE TABLE Inline Separate statements Separate statements
ALTER foreign key support Yes Yes No
tableExists source information_schema + DATABASE() information_schema + configured schema sqlite_master

georgeff/schema 适用场景与选型建议

georgeff/schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 georgeff/schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 georgeff/schema 我们能提供哪些服务?
定制开发 / 二次开发

基于 georgeff/schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 47
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-12