Skip to content

Commit f30badc

Browse files
authored
Merge pull request #264 from kitloong/feature/improve
Code cleaning
2 parents b0d526a + 0a55612 commit f30badc

22 files changed

+158
-119
lines changed

src/Repositories/MySQLRepository.php

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,6 @@ public function isOnUpdateCurrentTimestamp(string $table, string $column): bool
4242
return !($result === null);
4343
}
4444

45-
/**
46-
* Get the virtual column definition by table and column name.
47-
*
48-
* @param string $table Table name.
49-
* @param string $column Column name.
50-
* @return string|null The virtual column definition. NULL if not found.
51-
*/
52-
public function getVirtualDefinition(string $table, string $column): ?string
53-
{
54-
return $this->getGenerationExpression($table, $column, 'VIRTUAL GENERATED');
55-
}
56-
57-
/**
58-
* Get the stored column definition by table and column name.
59-
*
60-
* @param string $table Table name.
61-
* @param string $column Column name.
62-
* @return string|null The stored column definition. NULL if not found.
63-
*/
64-
public function getStoredDefinition(string $table, string $column): ?string
65-
{
66-
return $this->getGenerationExpression($table, $column, 'STORED GENERATED');
67-
}
68-
6945
/**
7046
* Get a list of stored procedures.
7147
*
@@ -141,45 +117,4 @@ private function getProcedure(string $procedure): mixed
141117
{
142118
return DB::selectOne("SHOW CREATE PROCEDURE $procedure");
143119
}
144-
145-
/**
146-
* Get the column GENERATION_EXPRESSION when EXTRA is 'VIRTUAL GENERATED' or 'STORED GENERATED'.
147-
*
148-
* @param 'VIRTUAL GENERATED'|'STORED GENERATED' $extra
149-
*/
150-
private function getGenerationExpression(string $table, string $column, string $extra): ?string
151-
{
152-
try {
153-
$definition = DB::selectOne(
154-
"SELECT GENERATION_EXPRESSION
155-
FROM information_schema.COLUMNS
156-
WHERE TABLE_NAME = '$table'
157-
AND COLUMN_NAME = '$column'
158-
AND EXTRA = '$extra'",
159-
);
160-
} catch (QueryException $exception) {
161-
// Check if error caused by missing column 'GENERATION_EXPRESSION'.
162-
// The column is introduced since MySQL 5.7 and MariaDB 10.2.5.
163-
// @see https://mariadb.com/kb/en/information-schema-columns-table/
164-
// @see https://dev.mysql.com/doc/refman/5.7/en/information-schema-columns-table.html
165-
if (
166-
Str::contains(
167-
$exception->getMessage(),
168-
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'GENERATION_EXPRESSION'",
169-
true,
170-
)
171-
) {
172-
return null;
173-
}
174-
175-
throw $exception;
176-
}
177-
178-
if ($definition === null) {
179-
return null;
180-
}
181-
182-
$definitionArr = array_change_key_case((array) $definition);
183-
return $definitionArr['generation_expression'] !== '' ? $definitionArr['generation_expression'] : null;
184-
}
185120
}

src/Repositories/PgSQLRepository.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,4 @@ public function getProcedures(): Collection
104104

105105
return $list;
106106
}
107-
108-
/**
109-
* Get the stored column definition by table and column name.
110-
*
111-
* @param string $table Table name.
112-
* @param string $column Column name.
113-
* @return string|null The stored column definition. NULL if not found.
114-
*/
115-
public function getStoredDefinition(string $table, string $column): ?string
116-
{
117-
$definition = DB::selectOne(
118-
"SELECT generation_expression
119-
FROM information_schema.columns
120-
WHERE table_name = '$table'
121-
AND column_name = '$column'
122-
AND is_generated = 'ALWAYS'",
123-
);
124-
125-
if ($definition === null) {
126-
return null;
127-
}
128-
129-
$definitionArr = array_change_key_case((array) $definition);
130-
return $definitionArr['generation_expression'] !== '' ? $definitionArr['generation_expression'] : null;
131-
}
132107
}

tests/Feature/FeatureTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected function rollbackMigrationsFrom(string $connection, string $path): voi
107107
* Generate migration files to $this->storageMigrations()
108108
*
109109
* @param array<string, string|bool|int> $options
110-
* @see \KitLoong\MigrationsGenerator\Tests\Feature\FeatureTestCase::getEnvironmentSetUp()
110+
* @see \KitLoong\MigrationsGenerator\Tests\Feature\FeatureTestCase::defineEnvironment()
111111
*/
112112
protected function generateMigrations(array $options = []): void
113113
{

tests/Feature/MariaDB/MariaDBTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ abstract class MariaDBTestCase extends FeatureTestCase
1212
/**
1313
* @inheritDoc
1414
*/
15-
protected function getEnvironmentSetUp($app): void
15+
protected function defineEnvironment($app): void
1616
{
17-
parent::getEnvironmentSetUp($app);
17+
parent::defineEnvironment($app);
1818

1919
$app['config']->set('database.default', 'mariadb');
2020
$app['config']->set('database.connections.mariadb', [

tests/Feature/MariaDB/TablePrefixTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public function testTablePrefix(): void
2020
/**
2121
* @inheritDoc
2222
*/
23-
protected function getEnvironmentSetUp($app): void
23+
protected function defineEnvironment($app): void
2424
{
25-
parent::getEnvironmentSetUp($app);
25+
parent::defineEnvironment($app);
2626

2727
$app['config']->set('database.connections.mariadb.prefix', 'prefix_');
2828
}

tests/Feature/MySQL57/DBConnectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public function testLogMigrationToAnotherSource(): void
8181
/**
8282
* @inheritDoc
8383
*/
84-
protected function getEnvironmentSetUp($app): void
84+
protected function defineEnvironment($app): void
8585
{
86-
parent::getEnvironmentSetUp($app);
86+
parent::defineEnvironment($app);
8787

8888
$app['config']->set('database.default', 'mysql8');
8989
$app['config']->set('database.connections.mysql8', [

tests/Feature/MySQL57/MySQL57TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ abstract class MySQL57TestCase extends FeatureTestCase
1212
/**
1313
* @inheritDoc
1414
*/
15-
protected function getEnvironmentSetUp($app): void
15+
protected function defineEnvironment($app): void
1616
{
17-
parent::getEnvironmentSetUp($app);
17+
parent::defineEnvironment($app);
1818

1919
$app['config']->set('database.default', 'mysql57');
2020
$app['config']->set('database.connections.mysql57', [

tests/Feature/MySQL57/StackedCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public function testRunAsCall(): void
6060
/**
6161
* @inheritDoc
6262
*/
63-
protected function getEnvironmentSetUp($app): void
63+
protected function defineEnvironment($app): void
6464
{
65-
parent::getEnvironmentSetUp($app);
65+
parent::defineEnvironment($app);
6666

6767
$app['config']->set('database.connections.migration2', [
6868
'driver' => 'mysql',

tests/Feature/MySQL57/TablePrefixTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public function testTablePrefix(): void
2020
/**
2121
* @inheritDoc
2222
*/
23-
protected function getEnvironmentSetUp($app): void
23+
protected function defineEnvironment($app): void
2424
{
25-
parent::getEnvironmentSetUp($app);
25+
parent::defineEnvironment($app);
2626

2727
$app['config']->set('database.connections.mysql57.prefix', 'prefix_');
2828
}

tests/Feature/MySQL8/MySQL8TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ abstract class MySQL8TestCase extends FeatureTestCase
1212
/**
1313
* @inheritDoc
1414
*/
15-
protected function getEnvironmentSetUp($app): void
15+
protected function defineEnvironment($app): void
1616
{
17-
parent::getEnvironmentSetUp($app);
17+
parent::defineEnvironment($app);
1818

1919
$app['config']->set('database.default', 'mysql8');
2020
$app['config']->set('database.connections.mysql8', [

0 commit comments

Comments
 (0)