Skip to content

Commit

Permalink
added - exclude table columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Tint Naing Win committed Sep 30, 2019
1 parent 4ce3f8d commit 6308566
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 40 deletions.
34 changes: 32 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,35 @@

All notable changes to `kuu-pyaung` will be documented in this file.

## 1.0.0
- Initial release
## 3.0.3 (2019-09-30)
- fix database table primary key.
- add excluded table columns.

## 3.0.2 (2019-09-28)
- updated README.md

## 3.0.1 (2019-09-27)
- fix extra double quote at column

## 3.0.0 (2019-09-27)
- initial release

## 2.0.2 (2019-09-30)
- fix database table primary key.
- add excluded table columns.

## 2.0.1 (2019-09-28)
- change [myanfont](https://github.com/tintnaingwinn/MyanFont) package v0.2

## 2.0.0 (2019-09-27)
- initial release

## 1.0.3 (2019-09-30)
- fix database table primary key.
- add excluded table columns.

## 1.0.1 (2019-09-28)
- fix wrong table when the model saves to the database.

## 1.0.0 (2019-09-28)
- initial release
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ If the context is Unicode, don't worry about the conflict context, Kuu Pyaung ha
## Installation and usage

``` bash
composer require tintnaingwin/kuu-pyaung
composer require tintnaingwin/kuu-pyaung:"~2.0"
```

For laravel >=5.5 that's all. This package supports Laravel new [Package Discovery](https://laravel.com/docs/5.5/packages#package-discovery).
Expand Down Expand Up @@ -99,6 +99,18 @@ return [
'telescope_entries_tags',
'telescope_monitoring',
],


/*
* These database table columns will be excluded from the convert.
*
* The value of the some columns may be filenames or you don't want to convert.
* Eg - 'table_name' => [ 'exclude_column', 'exclude_column' ]
*/
'exclude_table_columns' => [
'users' => [ 'profile_pic', 'file_path' ],
'orders' => [ 'invoice_path' ]
]
];
```

Expand All @@ -118,7 +130,9 @@ This package convert only folder under the `resource directories`. You can deter

**Database Convert**

This package convert only `string` data types from database. You can determine which tables will be excluded from the convert.
*Exclude Tables* -Kuu Pyaung converts only `string` data types from database. You can determine which tables will be excluded from the convert.
In addition, if your table does not have `primary key (id or UUID)`, this table will not be converted.


``` php
/*
Expand All @@ -134,6 +148,21 @@ This package convert only `string` data types from database. You can determine w
],
```

*Exclude Columns* - If the value of some columns is maybe zawgyi filenames or file paths. you can add these columns in the exclude_table_columns at the config file.

``` php
/*
* These database table columns will be excluded from the convert.
*
* The value of the some columns may be filenames that you don't want to convert.
* Eg - 'table_name' => [ 'exclude_column', 'exclude_column' ]
*/
'exclude_table_columns' => [
'users' => [ 'profile_pic', 'file_path' ],
'orders' => [ 'invoice_path' ]
]
```

**We highly recommend that you should use maintenance mode when you convert the database tables in production server.**

### Supported databases
Expand All @@ -142,12 +171,6 @@ This package convert only `string` data types from database. You can determine w
- PostgreSQL
- SQLite

## Todo

- Backup database
- Restore database
- Convert database with UI

## Testing

Run the tests with:
Expand Down
14 changes: 14 additions & 0 deletions config/kuu-pyaung.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,18 @@
'telescope_entries_tags',
'telescope_monitoring',
],

/*
* These database table columns will be excluded from the convert.
*
* The value of the some columns may be filenames or you don't want to convert.
* Eg - 'table_name' => [ 'exclude_column', 'exclude_column' ]
*/
/*
'exclude_table_columns' => [
'users' => [ 'profile_pic', 'file_path' ],
'orders' => [ 'invoice_path' ]
]
*/

];
10 changes: 7 additions & 3 deletions src/Convert/ConvertJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ class ConvertJob implements ConvertJobInterface
*/
protected $dbJob;

/**
* @throws \Exception
*/
public function run()
{
if ($this->isConvertFiles) {
Expand All @@ -37,6 +34,8 @@ public function run()
}

/**
* Convert the resource files from zawgyi to unicode.
*
* @return void
*/
public function convertFolders()
Expand All @@ -45,6 +44,8 @@ public function convertFolders()
}

/**
* Convert the database from zawgyi to unicode.
*
* @return void
*/
public function convertDatabase()
Expand All @@ -53,6 +54,8 @@ public function convertDatabase()
}

/**
* Set the DatabaseJob Object.
*
* @return self
*/
public function setDbJob(DatabaseJob $dbJob)
Expand All @@ -63,6 +66,7 @@ public function setDbJob(DatabaseJob $dbJob)
}

/**
* Set the FileJob Object.
* @return self
*/
public function setFileJob(FileJob $fileJob)
Expand Down
110 changes: 83 additions & 27 deletions src/Convert/DatabaseJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\DBAL\DBALException;
use Exception;
use Doctrine\DBAL\Types\Type;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Tintnaingwin\KuuPyaung\Exceptions\InvalidConvertJob;
use Tintnaingwin\KuuPyaung\Models\Dynamic;
Expand Down Expand Up @@ -34,6 +35,13 @@ class DatabaseJob
*/
protected $tables;

/**
* The primary keys for the table.
*
* @var string[]
*/
protected $primaryKeys = [ "id", "uuid" ];

/**
* DatabaseJob constructor.
*/
Expand Down Expand Up @@ -85,8 +93,6 @@ public function convert()
*/
public function convertTable($table, $columns)
{
$select_columns = $this->addIdColumn($columns);

$model = new Dynamic();
$model->setTable($table);

Expand All @@ -100,7 +106,7 @@ public function convertTable($table, $columns)

commandText()->getOutput()->progressStart($count);

$model->select($select_columns)->orderBy('id')->chunk(100, function ($data) use($columns){
$model->select($columns)->orderBy('id')->chunk(100, function ($data) use($columns){
foreach ($data as $value) {

foreach ($columns as $column) {
Expand All @@ -117,18 +123,6 @@ public function convertTable($table, $columns)
commandText()->getOutput()->progressFinish();
}

/**
* Add id column.
*
* @param array $columns
* @return array
*/
protected function addIdColumn($columns)
{
array_push($columns, 'id');
return $columns;
}

/**
* Declare the unsupported data types to other type.
*
Expand Down Expand Up @@ -185,11 +179,23 @@ protected function filterTableNames($tables)
*/
protected function getTableStringColumns($table)
{
$columns = $this->db_connection->getDoctrineSchemaManager()->listTableColumns($table);
try {
$columns = $this->db_connection->getDoctrineSchemaManager()->listTableColumns($table);

if (! $this->hasPrimaryKey($columns)) {
throw InvalidConvertJob::noPrimaryKey($table);
}

$columns = $this->filterTableColumns($columns);
$columns = $this->filterTableColumns($columns, $table);

return array_map('self::removeExtraQuotes', array_keys($columns));
return array_map('self::removeExtraQuotes', $columns);

} catch (Exception $exception) {
commandText()->error("Convert failed because {$exception->getMessage()}");
commandText()->getOutput()->newLine();

return [];
}
}

/**
Expand All @@ -198,28 +204,78 @@ protected function getTableStringColumns($table)
* @param \Doctrine\DBAL\Schema\Column[] $columns
* @return \Doctrine\DBAL\Schema\Column[]
*/
protected function filterTableColumns($columns)
protected function filterTableColumns($columns, $table)
{
$columns = array_filter($columns, function ($column)
{
/**
* @var \Doctrine\DBAL\Schema\Column $column
*/
$column_data_type = $column->getType()->getName();
{
/**
* @var \Doctrine\DBAL\Schema\Column $column
*/
if ($this->isPrimaryKey($column->getName())) {
return true;
}

$column_data_type = $column->getType()->getName();

return in_array($column_data_type, $this->columnDataTypes);
});

return in_array($column_data_type, $this->columnDataTypes);
});
$columns = array_keys($columns);

if ($exclude_table_columns = $this->getExcludeTableColumns($table)) {
$columns = array_diff($columns, $exclude_table_columns);
}

return $columns;
}

/**
* Get the exclude_table_columns from the config.
*
* @param string $table
* @return array|null
*/
protected function getExcludeTableColumns($table)
{
return config('kuu-pyaung.exclude_table_columns.'.$table);
}

/**
* Determine the column is primary or not.
*
* @param $name
* @return bool
*/
protected function isPrimaryKey($name)
{
return in_array($name, $this->primaryKeys);
}

/**
* Determine if the primary key exist or not in the table.
*
* @param $columns
* @return bool
*/
protected function hasPrimaryKey($columns)
{
foreach ($this->primaryKeys as $primaryKey) {
if (Arr::exists($columns, $primaryKey)) {
return true;
}
}

return false;
}

/**
* Remove the extra quotes.
*
* @param string $value
* @return string
*/
protected function removeExtraQuotes($value) {
protected function removeExtraQuotes($value)
{

return str_replace( array( "`", '"', "'" ),'',$value);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/InvalidConvertJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public static function noTableToBeConvert()
{
return new static('there are no table to be converted.');
}

public static function noPrimaryKey($table)
{
return new static("there are no primary key at the {$table} table");
}
}
3 changes: 3 additions & 0 deletions tests/Commands/ConvertCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

namespace Tintnaingwin\KuuPyaung\Tests\Commands;

use Tintnaingwin\KuuPyaung\Tests\TestCase;
use Illuminate\Support\Facades\Artisan;

class ConvertCommandTest extends TestCase
{
/** @test */
Expand Down

0 comments on commit 6308566

Please sign in to comment.