Skip to content

Commit

Permalink
Merge pull request #12 from kitloong/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kitloong committed Jun 22, 2020
2 parents 7a7a18e + a2bc1f2 commit d1d2ef1
Show file tree
Hide file tree
Showing 68 changed files with 430 additions and 344 deletions.
14 changes: 13 additions & 1 deletion README.md
Expand Up @@ -10,6 +10,7 @@ Generate Laravel Migrations from an existing database, including indexes and for
## V4 Changes

1. Major rewrite on `FieldGenerator` and `IndexGenerator`.
1. Fixed miscellaneous bugs.
1. Added `spatial` data type support such as `geometry`, `point`, etc.
1. Support more Laravel migration types such as `json`, `uuid`, `longText`, `year`, etc
1. Added `spatialIndex` support.
Expand All @@ -19,7 +20,6 @@ Generate Laravel Migrations from an existing database, including indexes and for
1. Support `set` for MySQL.
1. It is now possible to generate nullable `timestamp`
1. Removed unused classes.
1. Fixed miscellaneous bugs.
1. Added UT!
1. More UT will be added to increase coverage.

Expand All @@ -44,6 +44,8 @@ The recommended way to install this is through composer:
composer require --dev "kitloong/laravel-migrations-generator"
```

### Laravel Setup

Laravel will automatically register service provider for you.

### Lumen Setup
Expand Down Expand Up @@ -78,6 +80,16 @@ You can also specify the connection name if you are not using your default conne

Run `php artisan help migrate:generate` for a list of options.

|Options|Description|
|---|---|
|-c, --connection[=CONNECTION]|The database connection to use|
|-t, --tables[=TABLES]|A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments|
|-i, --ignore[=IGNORE]|A list of Tables you wish to ignore, separated by a comma: users,posts,comments|
|-p, --path[=PATH]|Where should the file be created?|
| --defaultIndexNames|Don't use db index names for migrations|
| --defaultFKNames|Don't use db foreign key names for migrations|
|-tp, --templatePath[=TEMPLATEPATH]|The location of the template for this generator|

## Thank You

Thanks to Bernhard Breytenbach for his great work. This package is cloned from https://github.com/Xethron/migrations-generator
Expand Down
Expand Up @@ -3,7 +3,6 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/29
* Time: 14:52
*/

namespace KitLoong\MigrationsGenerator\Generators;
Expand Down
7 changes: 3 additions & 4 deletions src/KitLoong/MigrationsGenerator/Generators/DecimalField.php
Expand Up @@ -3,13 +3,12 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/29
* Time: 14:54
*/

namespace KitLoong\MigrationsGenerator\Generators;

use Doctrine\DBAL\Schema\Column;
use KitLoong\MigrationsGenerator\MigrationGeneratorSetting;
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
use KitLoong\MigrationsGenerator\Types\DBALTypes;

Expand All @@ -32,8 +31,8 @@ public function __construct(Decorator $decorator)

public function makeField(array $field, Column $column): array
{
/** @var MigrationGeneratorSetting $setting */
$setting = app(MigrationGeneratorSetting::class);
/** @var MigrationsGeneratorSetting $setting */
$setting = app(MigrationsGeneratorSetting::class);

switch ($setting->getPlatform()) {
case Platform::POSTGRESQL:
Expand Down
17 changes: 16 additions & 1 deletion src/KitLoong/MigrationsGenerator/Generators/Decorator.php
Expand Up @@ -3,11 +3,12 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/29
* Time: 11:59
*/

namespace KitLoong\MigrationsGenerator\Generators;

use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;

class Decorator
{
/**
Expand Down Expand Up @@ -45,4 +46,18 @@ public function addSlash(string $string): string
{
return addcslashes($string, "\\'");
}

public function tableWithoutPrefix(string $table): string
{
/** @var MigrationsGeneratorSetting $setting */
$setting = app(MigrationsGeneratorSetting::class);

return substr($table, strlen($setting->getConnection()->getTablePrefix()));
}

public function tableUsedInFilename(string $table): string
{
$tableNameEscaped = preg_replace('/[^a-zA-Z0-9_]/', '_', $table);
return $this->tableWithoutPrefix($tableNameEscaped);
}
}
1 change: 0 additions & 1 deletion src/KitLoong/MigrationsGenerator/Generators/EnumField.php
Expand Up @@ -3,7 +3,6 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/29
* Time: 15:13
*/

namespace KitLoong\MigrationsGenerator\Generators;
Expand Down
Expand Up @@ -3,7 +3,6 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/28
* Time: 20:29
*/

namespace KitLoong\MigrationsGenerator\Generators;
Expand Down
@@ -1,4 +1,4 @@
<?php namespace Xethron\MigrationsGenerator\Generators;
<?php namespace KitLoong\MigrationsGenerator\Generators;

class ForeignKeyGenerator
{
Expand All @@ -7,6 +7,13 @@ class ForeignKeyGenerator
*/
protected $table;

private $decorator;

public function __construct(Decorator $decorator)
{
$this->decorator = $decorator;
}

/**
* Get array of foreign keys
*
Expand All @@ -32,7 +39,7 @@ public function generate(string $table, $schema, bool $ignoreForeignKeyNames): a
'name' => $this->getName($foreignKey, $ignoreForeignKeyNames),
'field' => $foreignKey->getLocalColumns()[0],
'references' => $foreignKey->getForeignColumns()[0],
'on' => $foreignKey->getForeignTableName(),
'on' => $this->decorator->tableWithoutPrefix($foreignKey->getForeignTableName()),
'onUpdate' => $foreignKey->hasOption('onUpdate') ? $foreignKey->getOption('onUpdate') : 'RESTRICT',
'onDelete' => $foreignKey->hasOption('onDelete') ? $foreignKey->getOption('onDelete') : 'RESTRICT',
];
Expand All @@ -46,7 +53,7 @@ public function generate(string $table, $schema, bool $ignoreForeignKeyNames): a
*
* @return null|string
*/
private function getName($foreignKey, bool $ignoreForeignKeyNames): ?string
protected function getName($foreignKey, bool $ignoreForeignKeyNames): ?string
{
if ($ignoreForeignKeyNames or $this->isDefaultName($foreignKey)) {
return null;
Expand All @@ -59,7 +66,7 @@ private function getName($foreignKey, bool $ignoreForeignKeyNames): ?string
*
* @return bool
*/
private function isDefaultName($foreignKey): bool
protected function isDefaultName($foreignKey): bool
{
return $foreignKey->getName() === $this->createIndexName($foreignKey->getLocalColumns()[0]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/KitLoong/MigrationsGenerator/Generators/GeometryField.php
Expand Up @@ -7,7 +7,7 @@

namespace KitLoong\MigrationsGenerator\Generators;

use KitLoong\MigrationsGenerator\MigrationGeneratorSetting;
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
use KitLoong\MigrationsGenerator\MigrationMethod\PgSQLGeography;
use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository;

Expand All @@ -22,8 +22,8 @@ public function __construct(PgSQLRepository $pgSQLRepository)

public function makeField(string $tableName, array $field)
{
/** @var MigrationGeneratorSetting $setting */
$setting = app(MigrationGeneratorSetting::class);
/** @var MigrationsGeneratorSetting $setting */
$setting = app(MigrationsGeneratorSetting::class);

switch ($setting->getPlatform()) {
case Platform::POSTGRESQL:
Expand Down
Expand Up @@ -3,7 +3,6 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/29
* Time: 17:23
*/

namespace KitLoong\MigrationsGenerator\Generators;
Expand Down
10 changes: 4 additions & 6 deletions src/KitLoong/MigrationsGenerator/Generators/IntegerField.php
Expand Up @@ -3,15 +3,13 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/29
* Time: 12:50
*/

namespace KitLoong\MigrationsGenerator\Generators;

use Doctrine\DBAL\Schema\Column;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use KitLoong\MigrationsGenerator\MigrationGeneratorSetting;
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnModifier;
use KitLoong\MigrationsGenerator\MigrationMethod\ColumnType;

Expand Down Expand Up @@ -65,13 +63,13 @@ private function handleInteger(array $field, Column $column, Collection $indexes

private function checkIsMySQLBoolean(string $tableName, array $field, Column $column): bool
{
/** @var MigrationGeneratorSetting $setting */
$setting = app(MigrationGeneratorSetting::class);
/** @var MigrationsGeneratorSetting $setting */
$setting = app(MigrationsGeneratorSetting::class);

if ($setting->getPlatform() === Platform::MYSQL &&
$field['type'] === ColumnType::TINY_INTEGER &&
!$column->getAutoincrement()) {
$column = DB::connection($setting->getConnection())->select("SHOW COLUMNS FROM `${tableName}` where Field = '${field['field']}' AND Type LIKE 'tinyint(1)%'");
$column = $setting->getConnection()->select("SHOW COLUMNS FROM `${tableName}` where Field = '${field['field']}' AND Type LIKE 'tinyint(1)%'");
return !empty($column);
}
return false;
Expand Down
Expand Up @@ -3,7 +3,6 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/31
* Time: 18:46
*/

namespace KitLoong\MigrationsGenerator\Generators\Modifier;
Expand Down
Expand Up @@ -3,7 +3,6 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/31
* Time: 18:41
*/

namespace KitLoong\MigrationsGenerator\Generators\Modifier;
Expand Down
Expand Up @@ -3,7 +3,6 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/31
* Time: 18:44
*/

namespace KitLoong\MigrationsGenerator\Generators\Modifier;
Expand Down
Expand Up @@ -3,7 +3,6 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/31
* Time: 18:36
*/

namespace KitLoong\MigrationsGenerator\Generators\Modifier;
Expand Down
1 change: 0 additions & 1 deletion src/KitLoong/MigrationsGenerator/Generators/OtherField.php
Expand Up @@ -3,7 +3,6 @@
* Created by PhpStorm.
* User: liow.kitloong
* Date: 2020/03/29
* Time: 14:58
*/

namespace KitLoong\MigrationsGenerator\Generators;
Expand Down

0 comments on commit d1d2ef1

Please sign in to comment.