Skip to content

Commit 296f3f8

Browse files
[11.x] Add vector column support to migrations (#52884)
* Add vector column support to Blueprint and Schema grammars * FIX sql not defined * MOD remove redundant param on doc * FIX code styling * FIX code styling * formatting * remove method from sqlite grammar --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent c972797 commit 296f3f8

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,18 @@ public function computed($column, $expression)
14521452
return $this->addColumn('computed', $column, compact('expression'));
14531453
}
14541454

1455+
/**
1456+
* Create a new vector column on the table.
1457+
*
1458+
* @param string $column
1459+
* @param int $dimensions
1460+
* @return \Illuminate\Database\Schema\ColumnDefinition
1461+
*/
1462+
public function vector($column, $dimensions)
1463+
{
1464+
return $this->addColumn('vector', $column, compact('dimensions'));
1465+
}
1466+
14551467
/**
14561468
* Add the proper columns for a polymorphic table.
14571469
*

src/Illuminate/Database/Schema/Grammars/Grammar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,17 @@ protected function typeComputed(Fluent $column)
234234
throw new RuntimeException('This database driver does not support the computed type.');
235235
}
236236

237+
/**
238+
* Create the column definition for a vector type.
239+
*
240+
* @param \Illuminate\Support\Fluent $column
241+
* @return string
242+
*/
243+
protected function typeVector(Fluent $column)
244+
{
245+
throw new RuntimeException('This database driver does not support the vector type.');
246+
}
247+
237248
/**
238249
* Add the column modifiers to the definition.
239250
*

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,17 @@ protected function typeComputed(Fluent $column)
11031103
throw new RuntimeException('This database driver requires a type, see the virtualAs / storedAs modifiers.');
11041104
}
11051105

1106+
/**
1107+
* Create the column definition for a vector type.
1108+
*
1109+
* @param \Illuminate\Support\Fluent $column
1110+
* @return string
1111+
*/
1112+
protected function typeVector(Fluent $column)
1113+
{
1114+
return "vector($column->dimensions)";
1115+
}
1116+
11061117
/**
11071118
* Get the SQL for a generated virtual column modifier.
11081119
*

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,17 @@ protected function typeGeography(Fluent $column)
10531053
return 'geography';
10541054
}
10551055

1056+
/**
1057+
* Create the column definition for a vector type.
1058+
*
1059+
* @param \Illuminate\Support\Fluent $column
1060+
* @return string
1061+
*/
1062+
protected function typeVector(Fluent $column)
1063+
{
1064+
return "vector($column->dimensions)";
1065+
}
1066+
10561067
/**
10571068
* Get the SQL for a collation column modifier.
10581069
*

tests/Database/DatabaseMySqlSchemaGrammarTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,16 @@ public function testAddingComment()
13221322
$this->assertSame("alter table `users` add `foo` varchar(255) not null comment 'Escape \\' when using words like it\\'s'", $statements[0]);
13231323
}
13241324

1325+
public function testAddingVector()
1326+
{
1327+
$blueprint = new Blueprint('embeddings');
1328+
$blueprint->vector('embedding', 384);
1329+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
1330+
1331+
$this->assertCount(1, $statements);
1332+
$this->assertSame('alter table `embeddings` add `embedding` vector(384) not null', $statements[0]);
1333+
}
1334+
13251335
public function testCreateDatabase()
13261336
{
13271337
$connection = $this->getConnection();

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public function testBasicCreateTable()
4242
], $statements);
4343
}
4444

45+
public function testAddingVector()
46+
{
47+
$blueprint = new Blueprint('embeddings');
48+
$blueprint->vector('embedding', 384);
49+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
50+
51+
$this->assertCount(1, $statements);
52+
$this->assertSame('alter table "embeddings" add column "embedding" vector(384) not null', $statements[0]);
53+
}
54+
4555
public function testCreateTableWithAutoIncrementStartingValue()
4656
{
4757
$blueprint = new Blueprint('users');

0 commit comments

Comments
 (0)