Skip to content

Commit

Permalink
[11.x] Add vector column support to migrations (#52884)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Jim-Webfox and taylorotwell authored Sep 25, 2024
1 parent c972797 commit 296f3f8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,18 @@ public function computed($column, $expression)
return $this->addColumn('computed', $column, compact('expression'));
}

/**
* Create a new vector column on the table.
*
* @param string $column
* @param int $dimensions
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function vector($column, $dimensions)
{
return $this->addColumn('vector', $column, compact('dimensions'));
}

/**
* Add the proper columns for a polymorphic table.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ protected function typeComputed(Fluent $column)
throw new RuntimeException('This database driver does not support the computed type.');
}

/**
* Create the column definition for a vector type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeVector(Fluent $column)
{
throw new RuntimeException('This database driver does not support the vector type.');
}

/**
* Add the column modifiers to the definition.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,17 @@ protected function typeComputed(Fluent $column)
throw new RuntimeException('This database driver requires a type, see the virtualAs / storedAs modifiers.');
}

/**
* Create the column definition for a vector type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeVector(Fluent $column)
{
return "vector($column->dimensions)";
}

/**
* Get the SQL for a generated virtual column modifier.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,17 @@ protected function typeGeography(Fluent $column)
return 'geography';
}

/**
* Create the column definition for a vector type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeVector(Fluent $column)
{
return "vector($column->dimensions)";
}

/**
* Get the SQL for a collation column modifier.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,16 @@ public function testAddingComment()
$this->assertSame("alter table `users` add `foo` varchar(255) not null comment 'Escape \\' when using words like it\\'s'", $statements[0]);
}

public function testAddingVector()
{
$blueprint = new Blueprint('embeddings');
$blueprint->vector('embedding', 384);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `embeddings` add `embedding` vector(384) not null', $statements[0]);
}

public function testCreateDatabase()
{
$connection = $this->getConnection();
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public function testBasicCreateTable()
], $statements);
}

public function testAddingVector()
{
$blueprint = new Blueprint('embeddings');
$blueprint->vector('embedding', 384);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table "embeddings" add column "embedding" vector(384) not null', $statements[0]);
}

public function testCreateTableWithAutoIncrementStartingValue()
{
$blueprint = new Blueprint('users');
Expand Down

0 comments on commit 296f3f8

Please sign in to comment.