Skip to content

Commit 9c78fd6

Browse files
authored
feat(database): add string method on CreateTableStatement (#1454)
1 parent 3d459fd commit 9c78fd6

File tree

2 files changed

+132
-69
lines changed

2 files changed

+132
-69
lines changed

packages/database/src/QueryStatements/CreateTableStatement.php

Lines changed: 93 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ public static function forModel(string $modelClass): self
3232
return new self(model($modelClass)->getTableDefinition()->name);
3333
}
3434

35+
/**
36+
* Adds a primary key column to the table. MySQL and SQLite use an auto-incrementing `INTEGER` column, and PostgreSQL uses `SERIAL`.
37+
*/
3538
public function primary(string $name = 'id'): self
3639
{
3740
$this->statements[] = new PrimaryKeyStatement($name);
3841

3942
return $this;
4043
}
4144

45+
/**
46+
* Adds a foreign key relationship to another table.
47+
*/
4248
public function belongsTo(
4349
string $local,
4450
string $foreign,
@@ -60,11 +66,11 @@ public function belongsTo(
6066
return $this;
6167
}
6268

63-
public function text(
64-
string $name,
65-
bool $nullable = false,
66-
?string $default = null,
67-
): self {
69+
/**
70+
* Adds a `TEXT` column to the table.
71+
*/
72+
public function text(string $name, bool $nullable = false, ?string $default = null): self
73+
{
6874
$this->statements[] = new TextStatement(
6975
name: $name,
7076
nullable: $nullable,
@@ -74,12 +80,11 @@ public function text(
7480
return $this;
7581
}
7682

77-
public function varchar(
78-
string $name,
79-
int $length = 255,
80-
bool $nullable = false,
81-
?string $default = null,
82-
): self {
83+
/**
84+
* Adds a `VARCHAR` column to the table.
85+
*/
86+
public function varchar(string $name, int $length = 255, bool $nullable = false, ?string $default = null): self
87+
{
8388
$this->statements[] = new VarcharStatement(
8489
name: $name,
8590
size: $length,
@@ -90,11 +95,24 @@ public function varchar(
9095
return $this;
9196
}
9297

93-
public function char(
94-
string $name,
95-
bool $nullable = false,
96-
?string $default = null,
97-
): self {
98+
/**
99+
* Adds a `VARCHAR` column to the table.
100+
*/
101+
public function string(string $name, int $length = 255, bool $nullable = false, ?string $default = null): self
102+
{
103+
return $this->varchar(
104+
name: $name,
105+
length: $length,
106+
nullable: $nullable,
107+
default: $default,
108+
);
109+
}
110+
111+
/**
112+
* Adds a `CHAR` column to the table.
113+
*/
114+
public function char(string $name, bool $nullable = false, ?string $default = null): self
115+
{
98116
$this->statements[] = new CharStatement(
99117
name: $name,
100118
nullable: $nullable,
@@ -104,12 +122,11 @@ public function char(
104122
return $this;
105123
}
106124

107-
public function integer(
108-
string $name,
109-
bool $unsigned = false,
110-
bool $nullable = false,
111-
?int $default = null,
112-
): self {
125+
/**
126+
* Adds an `INTEGER` column to the table.
127+
*/
128+
public function integer(string $name, bool $unsigned = false, bool $nullable = false, ?int $default = null): self
129+
{
113130
$this->statements[] = new IntegerStatement(
114131
name: $name,
115132
unsigned: $unsigned,
@@ -120,11 +137,11 @@ public function integer(
120137
return $this;
121138
}
122139

123-
public function float(
124-
string $name,
125-
bool $nullable = false,
126-
?float $default = null,
127-
): self {
140+
/**
141+
* Adds a `FLOAT` column to the table.
142+
*/
143+
public function float(string $name, bool $nullable = false, ?float $default = null): self
144+
{
128145
$this->statements[] = new FloatStatement(
129146
name: $name,
130147
nullable: $nullable,
@@ -134,11 +151,11 @@ public function float(
134151
return $this;
135152
}
136153

137-
public function datetime(
138-
string $name,
139-
bool $nullable = false,
140-
?string $default = null,
141-
): self {
154+
/**
155+
* Adds a datetime column to the table. Uses `DATETIME` for MySQL/SQLite and `TIMESTAMP` for PostgreSQL.
156+
*/
157+
public function datetime(string $name, bool $nullable = false, ?string $default = null): self
158+
{
142159
$this->statements[] = new DatetimeStatement(
143160
name: $name,
144161
nullable: $nullable,
@@ -148,11 +165,11 @@ public function datetime(
148165
return $this;
149166
}
150167

151-
public function date(
152-
string $name,
153-
bool $nullable = false,
154-
?string $default = null,
155-
): self {
168+
/**
169+
* Adds a `DATE` column to the table.
170+
*/
171+
public function date(string $name, bool $nullable = false, ?string $default = null): self
172+
{
156173
$this->statements[] = new DateStatement(
157174
name: $name,
158175
nullable: $nullable,
@@ -162,11 +179,11 @@ public function date(
162179
return $this;
163180
}
164181

165-
public function boolean(
166-
string $name,
167-
bool $nullable = false,
168-
?bool $default = null,
169-
): self {
182+
/**
183+
* Adds a `BOOLEAN` column to the table.
184+
*/
185+
public function boolean(string $name, bool $nullable = false, ?bool $default = null): self
186+
{
170187
$this->statements[] = new BooleanStatement(
171188
name: $name,
172189
nullable: $nullable,
@@ -176,11 +193,11 @@ public function boolean(
176193
return $this;
177194
}
178195

179-
public function json(
180-
string $name,
181-
bool $nullable = false,
182-
?string $default = null,
183-
): self {
196+
/**
197+
* Adds a JSON column to the table. Uses `JSON` for MySQL, `TEXT` for SQLite, and `JSONB` for PostgreSQL.
198+
*/
199+
public function json(string $name, bool $nullable = false, ?string $default = null): self
200+
{
184201
$this->statements[] = new JsonStatement(
185202
name: $name,
186203
nullable: $nullable,
@@ -190,11 +207,11 @@ public function json(
190207
return $this;
191208
}
192209

193-
public function dto(
194-
string $name,
195-
bool $nullable = false,
196-
?string $default = null,
197-
): self {
210+
/**
211+
* Alias for `json()` method. Adds a JSON column for storing serializable objects.
212+
*/
213+
public function dto(string $name, bool $nullable = false, ?string $default = null): self
214+
{
198215
$this->statements[] = new JsonStatement(
199216
name: $name,
200217
nullable: $nullable,
@@ -204,11 +221,11 @@ public function dto(
204221
return $this;
205222
}
206223

207-
public function array(
208-
string $name,
209-
bool $nullable = false,
210-
array $default = [],
211-
): self {
224+
/**
225+
* Adds a JSON column for storing arrays. The default value is automatically JSON-encoded, as opposed to `json` and `object`.
226+
*/
227+
public function array(string $name, bool $nullable = false, array $default = []): self
228+
{
212229
$this->statements[] = new JsonStatement(
213230
name: $name,
214231
nullable: $nullable,
@@ -218,12 +235,11 @@ public function array(
218235
return $this;
219236
}
220237

221-
public function enum(
222-
string $name,
223-
string $enumClass,
224-
bool $nullable = false,
225-
null|UnitEnum|BackedEnum $default = null,
226-
): self {
238+
/**
239+
* Adds an enum column to the table. Uses the `ENUM` type for MySQL, falls back to `TEXT` for SQLite, and uses a custom enum type for PostgreSQL.
240+
*/
241+
public function enum(string $name, string $enumClass, bool $nullable = false, null|UnitEnum|BackedEnum $default = null): self
242+
{
227243
$this->statements[] = new EnumStatement(
228244
name: $name,
229245
enumClass: $enumClass,
@@ -234,12 +250,11 @@ enumClass: $enumClass,
234250
return $this;
235251
}
236252

237-
public function set(
238-
string $name,
239-
array $values,
240-
bool $nullable = false,
241-
?string $default = null,
242-
): self {
253+
/**
254+
* Adds a `SET` column to the table. Only supported in MySQL.
255+
*/
256+
public function set(string $name, array $values, bool $nullable = false, ?string $default = null): self
257+
{
243258
$this->statements[] = new SetStatement(
244259
name: $name,
245260
values: $values,
@@ -250,6 +265,9 @@ public function set(
250265
return $this;
251266
}
252267

268+
/**
269+
* Adds a unique constraint on the specified columns.
270+
*/
253271
public function unique(string ...$columns): self
254272
{
255273
$this->trailingStatements[] = new UniqueStatement(
@@ -260,6 +278,9 @@ public function unique(string ...$columns): self
260278
return $this;
261279
}
262280

281+
/**
282+
* Adds an index on the specified columns.
283+
*/
263284
public function index(string ...$columns): self
264285
{
265286
$this->trailingStatements[] = new IndexStatement(
@@ -270,6 +291,9 @@ public function index(string ...$columns): self
270291
return $this;
271292
}
272293

294+
/**
295+
* Adds a raw SQL statement to the table definition.
296+
*/
273297
public function raw(string $statement): self
274298
{
275299
$this->statements[] = new RawStatement($statement);

tests/Integration/Database/QueryStatements/CreateTableStatementTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,43 @@ public function down(): ?QueryStatement
242242
$migration,
243243
);
244244
}
245+
246+
public function test_string_method_integration(): void
247+
{
248+
$migration = new class() implements DatabaseMigration {
249+
private(set) string $name = '0';
250+
251+
public function up(): QueryStatement
252+
{
253+
return new CreateTableStatement('frieren_table')
254+
->primary()
255+
->string('name', length: 100, nullable: false, default: 'Frieren')
256+
->string('type', nullable: true);
257+
}
258+
259+
public function down(): ?QueryStatement
260+
{
261+
return null;
262+
}
263+
};
264+
265+
$this->migrate(CreateMigrationsTable::class, $migration);
266+
267+
$this->expectNotToPerformAssertions();
268+
}
269+
270+
public function test_string_method_with_custom_parameters(): void
271+
{
272+
$varcharStatement = new CreateTableStatement('frieren_mages')
273+
->primary()
274+
->varchar('name', length: 120, nullable: true, default: 'Himmel')
275+
->compile(DatabaseDialect::MYSQL);
276+
277+
$stringStatement = new CreateTableStatement('frieren_mages')
278+
->primary()
279+
->varchar('name', length: 120, nullable: true, default: 'Himmel')
280+
->compile(DatabaseDialect::MYSQL);
281+
282+
$this->assertSame($varcharStatement, $stringStatement);
283+
}
245284
}

0 commit comments

Comments
 (0)