Skip to content

Commit

Permalink
Merge pull request #9 from orptech-com/hash-list-partitions
Browse files Browse the repository at this point in the history
Hash list partitions
  • Loading branch information
denizgolbas authored Oct 10, 2022
2 parents d53d9b6 + 976b33a commit eaca44d
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 49 deletions.
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,70 @@ use Illuminate\Support\Facades\Schema;
```

### Template Usage

#### Range Partition

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

Schema::createPartitioned('[YourTableNameHere]', function (Blueprint $table) {
Schema::createRangePartitioned('[YourTableNameHere]', function (Blueprint $table) {
//...
}, '[compositeKeyOne]', '[compositeKeyTwo]', '[rangePartitionKey]');
```

#### List Partition

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

Schema::createListPartitioned('[YourTableNameHere]', function (Blueprint $table) {
//...
}, '[compositeKeyOne]', '[compositeKeyTwo]', '[listPartitionKey]');
```

#### Hash Partition

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

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

### 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)

#### Range Partition

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

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

#### List Partition

```php
Schema::attachPartition('[YourCreatedPartitionTableNameHere]', function (Blueprint $table) {}, '[SubfixForPartition]', '[StartDate]', '[EndDate]');
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

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

#### OR
#### Hash Partition

```php
DB::statement("CREATE TABLE [partition_name_here] PARTITION OF [table_name_here] FOR VALUES FROM [starting_value_here] TO [end_value_here]");
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
use ORPTech\MigrationPartition\Support\Facades\Schema;

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

## Testing
Expand Down
125 changes: 102 additions & 23 deletions src/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,93 @@

namespace ORPTech\MigrationPartition\Database\Schema;

use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Schema\Blueprint as IlluminateBlueprint;

class Blueprint extends \Illuminate\Database\Schema\Blueprint
class Blueprint extends IlluminateBlueprint
{
use Macroable;

/**
* Column key one for creating a composite key.
* Column key one for creating a composite key for a range partitioned table.
*
* @var bool
* @var string
*/
public $pkCompositeOne;

/**
* Column key two for creating a composite key.
* Column key two for creating a composite key for range partitioned table.
*
* @var bool
* @var string
*/
public $pkCompositeTwo;

/**
* Partition range key for creating a partitioned table.
* Partition range key for creating a range partitioned table.
*
* @var bool
* @var string
*/
public $rangeKey;

/**
* Partition range key for creating a partitioned table.
* Partition range key for creating a range partitioned table.
*
* @var bool
* @var string
*/
public $subfixForPartition;

/**
* Partition range key for creating a partitioned table.
* Partition range key for creating a range partitioned table.
*
* @var bool
* @var string
*/
public $startDate;

/**
* Partition range key for creating a partitioned table.
* Partition range key for creating a range partitioned table.
*
* @var bool
* @var string
*/
public $endDate;

/**
* Column key for creating a table with list partition.
*
* @var string
*/
public $listPartitionKey;

/**
* Column key for creating list partitions.
*
* @var string
*/
public $listPartitionValue;

/**
* Column key for creating a table with hash partition.
*
* @var string
*/
public $hashPartitionKey;

/**
* Hashing modulus for creating a hash partition.
*
* @var int
*/
public $hashModulus;

/**
* Hashing reminder for creating a hash partition.
*
* @var int
*/
public $hashRemainder;

/**
* List of commands that trigger the creating function.
*
* @var array
*/
private $creators = ['create', 'createRangePartitioned', 'createListPartitioned', 'createHashPartitioned'];

/**
* Determine if the blueprint has a create command.
Expand All @@ -59,27 +98,67 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
public function creating()
{
return collect($this->commands)->contains(function ($command) {
return $command->name === 'create' || 'createPartitioned';
return in_array($command->name, $this->creators, false);
});
}

/**
* Indicate that the table needs to be created with a partition.
* Indicate that the table needs to be created with a range partition.
*
* @return \Illuminate\Support\Fluent
*/
public function createRangePartitioned()
{
return $this->addCommand('createRangePartitioned');
}

/**
* Create a range partition and attach it to the partitioned table.
*
* @return \Illuminate\Support\Fluent
*/
public function initRangePartition()
{
return $this->addCommand('initRangePartition');
}

/**
* Indicate that the table needs to be created with a list partition.
*
* @return \Illuminate\Support\Fluent
*/
public function createListPartitioned()
{
return $this->addCommand('createListPartitioned');
}

/**
* Create a list partition and attach it to the partitioned table.
*
* @return \Illuminate\Support\Fluent
*/
public function initListPartition()
{
return $this->addCommand('initListPartition');
}

/**
* Indicate that the table needs to be created with a hash partition.
*
* @return \Illuminate\Support\Fluent
*/
public function createPartitioned()
public function createHashPartitioned()
{
return $this->addCommand('createPartitioned');
return $this->addCommand('createHashPartitioned');
}

/**
* Create partition and attach parttioned table.
* Create a hash partition and attach it to the partitioned table.
*
* @return \Illuminate\Support\Fluent
*/
public function attachPartition()
public function initHashPartition()
{
return $this->addCommand('attachPartition');
return $this->addCommand('initHashPartition');
}
}
Loading

0 comments on commit eaca44d

Please sign in to comment.