Skip to content

Commit ad81fcb

Browse files
committed
Updated Code: Updated Stubs
1 parent ec54813 commit ad81fcb

10 files changed

Lines changed: 221 additions & 4 deletions

src/ParkmanSchema.php

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,112 @@ protected function generateMigrationContentForOperations()
108108

109109
protected function handleAlterTable($operation)
110110
{
111-
// Implementation remains the same
111+
$tableName = $operation['params']['table'];
112+
$alterations = $operation['params']['alterations'];
113+
114+
$upOperations = [];
115+
$downOperations = [];
116+
117+
foreach ($alterations as $alteration) {
118+
switch ($alteration['type']) {
119+
case 'AddColumn':
120+
$upOperations[] = $this->stubParser->parse('add_column', [
121+
'type' => $this->mapPrismaTypeToLaravel($alteration['column']['type']),
122+
'name' => $alteration['column']['name'],
123+
'nullable' => $alteration['column']['nullable'] ? '->nullable()' : '',
124+
'default' => isset($alteration['column']['default']) ? "->default('{$alteration['column']['default']}')" : '',
125+
]);
126+
$downOperations[] = $this->stubParser->parse('drop_column', [
127+
'name' => $alteration['column']['name'],
128+
]);
129+
break;
130+
case 'DropColumn':
131+
$upOperations[] = $this->stubParser->parse('drop_column', [
132+
'name' => $alteration['column'],
133+
]);
134+
// Note: We can't reliably reverse a column drop without knowing its original definition
135+
$downOperations[] = "// TODO: Manually add column '{$alteration['column']}' definition here";
136+
break;
137+
case 'RenameColumn':
138+
$upOperations[] = $this->stubParser->parse('rename_column', [
139+
'old_name' => $alteration['from'],
140+
'new_name' => $alteration['to'],
141+
]);
142+
$downOperations[] = $this->stubParser->parse('rename_column', [
143+
'old_name' => $alteration['to'],
144+
'new_name' => $alteration['from'],
145+
]);
146+
break;
147+
}
148+
}
149+
150+
$up = $this->stubParser->parse('alter_table', [
151+
'table' => $tableName,
152+
'operations' => implode("\n ", $upOperations),
153+
]);
154+
155+
$down = $this->stubParser->parse('alter_table', [
156+
'table' => $tableName,
157+
'operations' => implode("\n ", array_reverse($downOperations)),
158+
]);
159+
160+
return [$up, $down];
112161
}
113162

114163
protected function handleRenameTable($operation)
115164
{
116-
// Implementation remains the same
165+
$oldName = $operation['params']['from'];
166+
$newName = $operation['params']['to'];
167+
168+
$up = $this->stubParser->parse('rename_table', [
169+
'old_name' => $oldName,
170+
'new_name' => $newName,
171+
]);
172+
173+
$down = $this->stubParser->parse('rename_table', [
174+
'old_name' => $newName,
175+
'new_name' => $oldName,
176+
]);
177+
178+
return [$up, $down];
117179
}
118180

119181
protected function handleAddForeignKey($operation)
120182
{
121-
// Implementation remains the same
183+
$table = $operation['params']['table'];
184+
$foreignKey = $operation['params']['foreignKey'];
185+
186+
$up = $this->stubParser->parse('add_foreign_key', [
187+
'table' => $table,
188+
'column' => $foreignKey['column'],
189+
'referenced_table' => $foreignKey['referencedTable'],
190+
'referenced_column' => $foreignKey['referencedColumn'],
191+
'on_delete' => $foreignKey['onDelete'] ?? 'cascade',
192+
'on_update' => $foreignKey['onUpdate'] ?? 'cascade',
193+
]);
194+
195+
$down = $this->stubParser->parse('drop_foreign_key', [
196+
'table' => $table,
197+
'foreign_key_name' => "{$table}_{$foreignKey['column']}_foreign",
198+
]);
199+
200+
return [$up, $down];
122201
}
123202

124203
protected function handleDropForeignKey($operation)
125204
{
126-
// Implementation remains the same
205+
$table = $operation['params']['table'];
206+
$foreignKeyName = $operation['params']['foreignKey'];
207+
208+
$up = $this->stubParser->parse('drop_foreign_key', [
209+
'table' => $table,
210+
'foreign_key_name' => $foreignKeyName,
211+
]);
212+
213+
// Note: We can't reliably reverse a foreign key drop without knowing its original definition
214+
$down = "// TODO: Manually add foreign key '{$foreignKeyName}' definition here";
215+
216+
return [$up, $down];
127217
}
128218

129219
protected function generateCreateTableMethod($tableName, $fields)

src/StubParser.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Paulobunga\ParkmanSchema;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Illuminate\Support\Str;
7+
8+
class StubParser
9+
{
10+
/**
11+
* The path to the stubs directory.
12+
*
13+
* @var string
14+
*/
15+
protected $stubPath;
16+
17+
/**
18+
* Create a new StubParser instance.
19+
*
20+
* @param string $stubPath
21+
*/
22+
public function __construct($stubPath)
23+
{
24+
$this->stubPath = $stubPath;
25+
}
26+
27+
/**
28+
* Parse a stub file and replace placeholders.
29+
*
30+
* @param string $stubName
31+
* @param array $replacements
32+
* @return string
33+
*/
34+
public function parse($stubName, $replacements = [])
35+
{
36+
$stubContent = $this->getStubContent($stubName);
37+
38+
return $this->replacePlaceholders($stubContent, $replacements);
39+
}
40+
41+
/**
42+
* Get the content of a stub file.
43+
*
44+
* @param string $stubName
45+
* @return string
46+
* @throws \Exception
47+
*/
48+
protected function getStubContent($stubName)
49+
{
50+
$stubFile = $this->stubPath . "/{$stubName}.stub";
51+
52+
if (!File::exists($stubFile)) {
53+
throw new \Exception("Stub file not found: {$stubFile}");
54+
}
55+
56+
return File::get($stubFile);
57+
}
58+
59+
/**
60+
* Replace placeholders in the stub content.
61+
*
62+
* @param string $stubContent
63+
* @param array $replacements
64+
* @return string
65+
*/
66+
protected function replacePlaceholders($stubContent, $replacements)
67+
{
68+
foreach ($replacements as $key => $value) {
69+
$placeholder = "{{ {$key} }}";
70+
71+
// If the value is an array, implode it
72+
if (is_array($value)) {
73+
$value = implode(', ', $value);
74+
}
75+
76+
// If the placeholder is wrapped in quotes, ensure the replacement is properly quoted
77+
if (Str::contains($stubContent, "'{$placeholder}'")) {
78+
$value = addslashes($value);
79+
}
80+
81+
$stubContent = str_replace($placeholder, $value, $stubContent);
82+
}
83+
84+
return $stubContent;
85+
}
86+
87+
/**
88+
* Set a new stub path.
89+
*
90+
* @param string $stubPath
91+
* @return $this
92+
*/
93+
public function setStubPath($stubPath)
94+
{
95+
$this->stubPath = $stubPath;
96+
97+
return $this;
98+
}
99+
100+
/**
101+
* Get the current stub path.
102+
*
103+
* @return string
104+
*/
105+
public function getStubPath()
106+
{
107+
return $this->stubPath;
108+
}
109+
}

stubs/add_foreign_key.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$table->foreign('{{ column }}')
2+
->references('{{ referenced_column }}')
3+
->on('{{ referenced_table }}')
4+
->onDelete('{{ on_delete }}')
5+
->onUpdate('{{ on_update }}');

stubs/alter_table.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Schema::table('{{ table }}', function (Blueprint $table) {
2+
{{ operations }}
3+
});

stubs/create_enum.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DB::statement("CREATE TYPE {{ enum_name }} AS ENUM ({{ enum_values }})");

stubs/create_table.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Schema::create('{{ table }}', function (Blueprint $table) {
2+
$table->id();
3+
{{ fields }}
4+
$table->timestamps();
5+
});

stubs/drop_column.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$table->dropColumn('{{ name }}');

stubs/drop_enum.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DB::statement("DROP TYPE {{ enum_name }}");

stubs/drop_foreign_key.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$table->dropForeign('{{ foreign_key_name }}');

stubs/rename_column.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$table->renameColumn('{{ old_name }}', '{{ new_name }}');

0 commit comments

Comments
 (0)