-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathSQLiteGrammar.php
More file actions
executable file
·185 lines (164 loc) · 7.29 KB
/
SQLiteGrammar.php
File metadata and controls
executable file
·185 lines (164 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Winter\Storm\Database\Schema\Grammars;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\ColumnDefinition;
use Illuminate\Database\Schema\ForeignKeyDefinition;
use Illuminate\Database\Schema\IndexDefinition;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar as BaseSQLiteGrammar;
use Illuminate\Support\Fluent;
class SQLiteGrammar extends BaseSQLiteGrammar
{
/**
* Compile a change column command into a series of SQL statements.
*
* Starting with Laravel 11, previous column attributes do not persist when changing a column.
* This restores Laravel previous behavior where existing column attributes are kept
* unless they get changed by the new Blueprint.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return array|string
*
* @throws \RuntimeException
*/
public function compileChange(Blueprint $blueprint, Fluent $command)
{
$autoIncrementColumn = null;
$columnNames = [];
$schema = $this->connection->getSchemaBuilder();
$table = $blueprint->getTable();
$changedColumns = collect($blueprint->getChangedColumns());
$oldColumns = collect($schema->getColumns($table));
$columns = $oldColumns
->map(function ($column) use ($blueprint, $changedColumns, &$columnNames, &$autoIncrementColumn, $oldColumns) {
$column = $changedColumns->first(fn ($col) => $col->name === $column['name'], $column);
if (! $column instanceof Fluent) {
$isGenerated = ! is_null($column['generation']);
$column = new ColumnDefinition([
'change' => true,
'name' => $column['name'],
'type' => $column['type_name'],
'nullable' => $column['nullable'],
'default' => $column['default'] ? new Expression($column['default']) : null,
'autoIncrement' => $column['auto_increment'],
'collation' => $column['collation'],
'comment' => $column['comment'],
'virtualAs' => $isGenerated && $column['generation']['type'] === 'virtual'
? $column['generation']['expression'] : null,
'storedAs' => $isGenerated && $column['generation']['type'] === 'stored'
? $column['generation']['expression'] : null,
]);
}
$name = $this->wrap($column);
$autoIncrementColumn = $column->autoIncrement ? $column->name : $autoIncrementColumn;
if (is_null($column->virtualAs) && is_null($column->virtualAsJson) &&
is_null($column->storedAs) && is_null($column->storedAsJson)
) {
$columnNames[] = $name;
}
$oldColumn = $oldColumns->where('name', $column->name)->first();
if (!$oldColumn instanceof ColumnDefinition) {
$oldColumn = new ColumnDefinition($oldColumn);
}
$sql = $name.' '.$this->getType($column);
foreach ($this->modifiers as $modifier) {
if (method_exists($this, $method = "modify{$modifier}")) {
$mod = strtolower($modifier);
$col = isset($oldColumn->{$mod}) && !isset($column->{$mod}) ? $oldColumn : $column;
$sql .= $this->{$method}($blueprint, $col);
}
}
return $sql;
})->all();
$foreignKeys = collect($schema->getForeignKeys($table))->map(fn ($foreignKey) => new ForeignKeyDefinition([
'columns' => $foreignKey['columns'],
'on' => $foreignKey['foreign_table'],
'references' => $foreignKey['foreign_columns'],
'onUpdate' => $foreignKey['on_update'],
'onDelete' => $foreignKey['on_delete'],
]))->all();
[$primary, $indexes] = collect($schema->getIndexes($table))->map(fn ($index) => new IndexDefinition([
'name' => match (true) {
$index['primary'] => 'primary',
$index['unique'] => 'unique',
default => 'index',
},
'index' => $index['name'],
'columns' => $index['columns'],
]))->partition(fn ($index) => $index->name === 'primary');
$indexes = collect($indexes)->reject(fn ($index) => str_starts_with('sqlite_', $index->index))->map(
fn ($index) => $this->{'compile'.ucfirst($index->name)}($blueprint, $index)
)->all();
$tempTable = $this->wrapTable($blueprint, '__temp__'.$this->connection->getTablePrefix());
$table = $this->wrapTable($blueprint);
$columnNames = implode(', ', $columnNames);
$foreignKeyConstraintsEnabled = $this->connection->scalar($this->pragma('foreign_keys'));
$sqlQuery = array_filter(
array_merge(
[
$foreignKeyConstraintsEnabled ? $this->compileDisableForeignKeyConstraints() : null,
sprintf(
'create table %s (%s%s%s)',
$tempTable,
implode(', ', $columns),
$this->addForeignKeys($foreignKeys),
$autoIncrementColumn ? '' : $this->addPrimaryKeys($primary->first())
),
sprintf(
'insert into %s (%s) select %s from %s',
$tempTable,
$columnNames,
$columnNames,
$table
),
sprintf(
'drop table %s',
$table
),
sprintf(
'alter table %s rename to %s',
$tempTable,
$table
),
],
$indexes,
[
$foreignKeyConstraintsEnabled ? $this->compileEnableForeignKeyConstraints() : null
]
)
);
return $sqlQuery;
}
public function getDefaultValue($value)
{
if (is_string($value)) {
$value = preg_replace('#\'#', '', $value);
}
return parent::getDefaultValue($value);
}
/**
* Create the column definition for a tinyint type.
*
* SQLite's column introspection returns 'tinyint' as the type_name for boolean and tinyInteger
* columns. The base grammar only has typeTinyInteger, so we need this alias to avoid
* BadMethodCallException when compileChange rebuilds a table that contains boolean columns.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTinyint(Fluent $column)
{
return 'integer';
}
/**
* Create the column definition for a varchar type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeVarChar(Fluent $column)
{
return 'varchar';
}
}