Skip to content

Commit

Permalink
Merge pull request #12 from orptech-com/v3-beta
Browse files Browse the repository at this point in the history
A lot of refactoring, new commands, updated README.md and much more.
  • Loading branch information
denizgolbas authored Oct 11, 2022
2 parents 4dbc26a + 392ac49 commit 754f122
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 220 deletions.
100 changes: 60 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ composer require orptech/laravel-migration-partition
- SQLite 3.8.8+

## Usage
This package currently, only supports PostgreSQL.

## PostgreSQL
PostgreSQL also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance.

### PostgreSQL Range Partitioning
### Range Partitioning
Instead of importing Illuminate's Schema import this package's schema:
```php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
```

### Template Usage
## Template Usage

#### I. Range Partition
### Range Partition

```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
Expand All @@ -56,37 +59,25 @@ Schema::createRangePartitioned('[YourTableNameHere]', function (Blueprint $table
}, '[compositeKeyOne]', '[compositeKeyTwo]', '[rangePartitionKey]');
```

##### I.I Range Partition Pre-defined Partition Adding
##### Creating a Range Partition for a Partitioned Table

```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

Schema::createRangePartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[subfix]', '[startDate]', '[endDate]');
Schema::createRangePartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[suffixForPartition]', '[startDate]', '[endDate]');
```

##### I.II Range Partition Post-defined Partition Adding
##### Attaching a Range Partition to a Partitioned Table

```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

Schema::attachRangePartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[partitionTableName]', '[startDate]', '[endDate]');
Schema::attachRangePartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[suffixForPartition]', '[startDate]', '[endDate]');
```

##### I.III Add Partition for Each Range Partitioned Tables

```bash
php artisan partition:range-init-all
```
- This command will create migration files for partition tables for each range partitioned tables.
- You can create year base (or other time base) periodic tables.
- After this command run:
```bash
php artisan migrate
```

#### II. List Partition
### List Partition

```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
Expand All @@ -97,64 +88,93 @@ Schema::createListPartitioned('[YourTableNameHere]', function (Blueprint $table)
}, '[compositeKeyOne]', '[compositeKeyTwo]', '[listPartitionKey]');
```

#### III. Hash Partition
##### Creating a List Partition for a Partitioned Table

```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

Schema::createHashPartitioned('[YourTableNameHere]', function (Blueprint $table) {
//...
}, '[compositeKeyOne]', '[compositeKeyTwo]', '[hashPartitionKey]');
Schema::createListPartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[suffixForPartition]', '[listPartitionValue]');
```

##### Partition Removing
##### Attaching a List Partition to a Partitioned Table

```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

Schema::detachPartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[partitionTableName]');
Schema::attachListPartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[suffixForPartition]', '[listPartitionValue]');
```

##### List all partitions for a partitioned table
### Hash Partition

```bash
php artisan partition:list
```
```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

### Important
- This package currently supports PostgreSQL Range Partitions.
- You shouldn't define any primary keys in your migration. The package creates a composite key while setting up the table.
- You need to create an initial partition to start using the tables. (PostgreSQL)
Schema::createHashPartitioned('[YourTableNameHere]', function (Blueprint $table) {
//...
}, '[compositeKeyOne]', '[compositeKeyTwo]', '[hashPartitionKey]');
```

#### Range Partition
##### Creating a Hash Partition for a Partitioned Table

```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

Schema::initRangePartition('[YourCreatedPartitionTableNameHere]', function (Blueprint $table) {}, '[SubfixForPartition]', '[StartDate]', '[EndDate]');
Schema::createHashPartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[suffixForPartition]', '[hashModulus]', '[hashRemainder]');
```

#### List Partition
##### Attaching a Hash Partition to a Partitioned Table

```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

Schema::initListPartition('[YourCreatedPartitionTableNameHere]', function (Blueprint $table) {}, '[SubfixForPartition]', '[listPartitionValue]');
Schema::attachHashPartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[suffixForPartition]', '[hashModulus]', '[hashRemainder]');
```

#### Hash Partition
#### Removing a Partition

```php
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

Schema::initHashPartition('[YourCreatedPartitionTableNameHere]', function (Blueprint $table) {}, '[SubfixForPartition]', '[hashModulus]', '[hashRemainder]');
Schema::detachPartition('[YourPartitionedTableNameHere]', function (Blueprint $table) {}, '[partitionTableName]');
```

## Commands

##### New Series of Range Partition Migrations
This command will create a new series of migrations for all range partitioned tables.
```bash
php artisan partition:range
```

##### New Series of List Partition Migrations
This command will create a new series of migrations for all list partitioned tables.
```bash
php artisan partition:list
```

##### New Series of Hash Partition Migrations
This command will create a new series of migrations for all hash partitioned tables.
```bash
php artisan partition:hash
```

##### Listing Partitions
This command will list all the partitioned tables.
```bash
php artisan partition:partitions
```

### Important
- This package currently supports PostgreSQL Range Partitions.
- You shouldn't define any primary keys in your migration. The package creates a composite key while setting up the table.
- You need to create an initial partition to start using the tables. (PostgreSQL)

## Testing

```bash
Expand Down
172 changes: 172 additions & 0 deletions src/Commands/InitHashPartitionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

namespace ORPTech\MigrationPartition\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Pluralizer;
use ORPTech\MigrationPartition\Support\Facades\Schema;

class InitHashPartitionCommand extends Command
{
/**
* Filesystem instance
* @var Filesystem
*/
protected Filesystem $files;

/**
* Create a new command instance.
* @param Filesystem $files
*/
public function __construct(Filesystem $files)
{
parent::__construct();

$this->files = $files;
}

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'partition:hash';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This command will create a new series of migrations for all range partitioned table.';

/**
* The table name.
*
* @var string
*/
protected $table;

/**
* The suffix for the table.
*
* @var string
*/
protected $suffix;

/**
* Modulus value for the partition.
*
* @var string
*/
protected $modulus;

/**
* Remainder value for the partition.
*
* @var string
*/
protected $remainder;


/**
* Handler for the command.
*
*/
public function handle()
{
$this->suffix = $this->ask('Define your table suffix');
$this->modulus = $this->ask('Modulus value');
$this->remainder = $this->ask('Remainder value');
$tables = Schema::getAllHashPartitionedTables();
foreach($tables as $table){
$this->table = $table;
$path = $this->getSourceFilePath();
$contents = $this->getSourceFile();
$this->files->put($path, $contents);
$this->info("File : {$path} created.");
}
}

/**
* Get the full path of generate class.
*
* @return string
*/
public function getSourceFilePath()
{
return base_path('database/migrations') . '/'.now()->format('Y_m_d_His').'_create_partition_' . $this->table.'_'.$this->suffix . '_table.php';
}

/**
* Build the directory for the class if necessary.
*
* @param string $path
* @return string
*/
protected function makeDirectory($path)
{
if (!$this->files->isDirectory($path)) {
$this->files->makeDirectory($path, 0755, true, true);
}

return $path;
}

/**
* Get the stub path and the stub variables.
*
* @return array|false|string|string[]
*
*/
public function getSourceFile()
{
return $this->getStubContents($this->getStubPath(), $this->getStubVariables());
}

/**
* Replace the stub variables(key) with the desire value.
*
* @param $stub
* @param array $stubVariables
* @return array|false|string|string[]
*/
public function getStubContents($stub, array $stubVariables = [])
{
$contents = file_get_contents($stub);

foreach ($stubVariables as $search => $replace) {
$contents = str_replace('$' . $search . '$', $replace, $contents);
}

return $contents;

}

/**
* Return the stub file path.
* @return string
*
*/
public function getStubPath()
{
return __DIR__.'/Stubs/hash-partition-migration.stub';
}

/**
**
* Map the stub variables present in stub to its value.
*
* @return array
*
*/
public function getStubVariables()
{
return [
'TABLE' => $this->table,
'SUFFIX' => $this->suffix,
'MODULUS' => $this->modulus,
'REMAINDER' => $this->remainder
];
}
}
Loading

0 comments on commit 754f122

Please sign in to comment.