Skip to content

Commit eaca44d

Browse files
authored
Merge pull request #9 from orptech-com/hash-list-partitions
Hash list partitions
2 parents d53d9b6 + 976b33a commit eaca44d

File tree

5 files changed

+331
-49
lines changed

5 files changed

+331
-49
lines changed

README.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,70 @@ use Illuminate\Support\Facades\Schema;
4444
```
4545

4646
### Template Usage
47+
48+
#### Range Partition
49+
4750
```php
4851
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
4952
use ORPTech\MigrationPartition\Support\Facades\Schema;
5053

51-
Schema::createPartitioned('[YourTableNameHere]', function (Blueprint $table) {
54+
Schema::createRangePartitioned('[YourTableNameHere]', function (Blueprint $table) {
5255
//...
5356
}, '[compositeKeyOne]', '[compositeKeyTwo]', '[rangePartitionKey]');
5457
```
5558

59+
#### List Partition
60+
61+
```php
62+
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
63+
use ORPTech\MigrationPartition\Support\Facades\Schema;
64+
65+
Schema::createListPartitioned('[YourTableNameHere]', function (Blueprint $table) {
66+
//...
67+
}, '[compositeKeyOne]', '[compositeKeyTwo]', '[listPartitionKey]');
68+
```
69+
70+
#### Hash Partition
71+
72+
```php
73+
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
74+
use ORPTech\MigrationPartition\Support\Facades\Schema;
75+
76+
Schema::createHashPartitioned('[YourTableNameHere]', function (Blueprint $table) {
77+
//...
78+
}, '[compositeKeyOne]', '[compositeKeyTwo]', '[hashPartitionKey]');
79+
```
80+
5681
### Important
5782
- This package currently supports PostgreSQL Range Partitions.
5883
- You shouldn't define any primary keys in your migration. The package creates a composite key while setting up the table.
5984
- You need to create an initial partition to start using the tables. (PostgreSQL)
6085

86+
#### Range Partition
87+
88+
```php
89+
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
90+
use ORPTech\MigrationPartition\Support\Facades\Schema;
91+
92+
Schema::initRangePartition('[YourCreatedPartitionTableNameHere]', function (Blueprint $table) {}, '[SubfixForPartition]', '[StartDate]', '[EndDate]');
93+
```
94+
95+
#### List Partition
96+
6197
```php
62-
Schema::attachPartition('[YourCreatedPartitionTableNameHere]', function (Blueprint $table) {}, '[SubfixForPartition]', '[StartDate]', '[EndDate]');
98+
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
99+
use ORPTech\MigrationPartition\Support\Facades\Schema;
100+
101+
Schema::initListPartition('[YourCreatedPartitionTableNameHere]', function (Blueprint $table) {}, '[SubfixForPartition]', '[listPartitionValue]');
63102
```
64103

65-
#### OR
104+
#### Hash Partition
66105

67106
```php
68-
DB::statement("CREATE TABLE [partition_name_here] PARTITION OF [table_name_here] FOR VALUES FROM [starting_value_here] TO [end_value_here]");
107+
use ORPTech\MigrationPartition\Database\Schema\Blueprint;
108+
use ORPTech\MigrationPartition\Support\Facades\Schema;
109+
110+
Schema::initHashPartition('[YourCreatedPartitionTableNameHere]', function (Blueprint $table) {}, '[SubfixForPartition]', '[hashModulus]', '[hashRemainder]');
69111
```
70112

71113
## Testing

src/Database/Schema/Blueprint.php

Lines changed: 102 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,93 @@
22

33
namespace ORPTech\MigrationPartition\Database\Schema;
44

5-
use Illuminate\Support\Traits\Macroable;
5+
use Illuminate\Database\Schema\Blueprint as IlluminateBlueprint;
66

7-
class Blueprint extends \Illuminate\Database\Schema\Blueprint
7+
class Blueprint extends IlluminateBlueprint
88
{
9-
use Macroable;
10-
119
/**
12-
* Column key one for creating a composite key.
10+
* Column key one for creating a composite key for a range partitioned table.
1311
*
14-
* @var bool
12+
* @var string
1513
*/
1614
public $pkCompositeOne;
1715

1816
/**
19-
* Column key two for creating a composite key.
17+
* Column key two for creating a composite key for range partitioned table.
2018
*
21-
* @var bool
19+
* @var string
2220
*/
2321
public $pkCompositeTwo;
2422

2523
/**
26-
* Partition range key for creating a partitioned table.
24+
* Partition range key for creating a range partitioned table.
2725
*
28-
* @var bool
26+
* @var string
2927
*/
3028
public $rangeKey;
3129

3230
/**
33-
* Partition range key for creating a partitioned table.
31+
* Partition range key for creating a range partitioned table.
3432
*
35-
* @var bool
33+
* @var string
3634
*/
3735
public $subfixForPartition;
3836

3937
/**
40-
* Partition range key for creating a partitioned table.
38+
* Partition range key for creating a range partitioned table.
4139
*
42-
* @var bool
40+
* @var string
4341
*/
4442
public $startDate;
4543

4644
/**
47-
* Partition range key for creating a partitioned table.
45+
* Partition range key for creating a range partitioned table.
4846
*
49-
* @var bool
47+
* @var string
5048
*/
5149
public $endDate;
5250

51+
/**
52+
* Column key for creating a table with list partition.
53+
*
54+
* @var string
55+
*/
56+
public $listPartitionKey;
57+
58+
/**
59+
* Column key for creating list partitions.
60+
*
61+
* @var string
62+
*/
63+
public $listPartitionValue;
64+
65+
/**
66+
* Column key for creating a table with hash partition.
67+
*
68+
* @var string
69+
*/
70+
public $hashPartitionKey;
71+
72+
/**
73+
* Hashing modulus for creating a hash partition.
74+
*
75+
* @var int
76+
*/
77+
public $hashModulus;
78+
79+
/**
80+
* Hashing reminder for creating a hash partition.
81+
*
82+
* @var int
83+
*/
84+
public $hashRemainder;
85+
86+
/**
87+
* List of commands that trigger the creating function.
88+
*
89+
* @var array
90+
*/
91+
private $creators = ['create', 'createRangePartitioned', 'createListPartitioned', 'createHashPartitioned'];
5392

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

66105
/**
67-
* Indicate that the table needs to be created with a partition.
106+
* Indicate that the table needs to be created with a range partition.
107+
*
108+
* @return \Illuminate\Support\Fluent
109+
*/
110+
public function createRangePartitioned()
111+
{
112+
return $this->addCommand('createRangePartitioned');
113+
}
114+
115+
/**
116+
* Create a range partition and attach it to the partitioned table.
117+
*
118+
* @return \Illuminate\Support\Fluent
119+
*/
120+
public function initRangePartition()
121+
{
122+
return $this->addCommand('initRangePartition');
123+
}
124+
125+
/**
126+
* Indicate that the table needs to be created with a list partition.
127+
*
128+
* @return \Illuminate\Support\Fluent
129+
*/
130+
public function createListPartitioned()
131+
{
132+
return $this->addCommand('createListPartitioned');
133+
}
134+
135+
/**
136+
* Create a list partition and attach it to the partitioned table.
137+
*
138+
* @return \Illuminate\Support\Fluent
139+
*/
140+
public function initListPartition()
141+
{
142+
return $this->addCommand('initListPartition');
143+
}
144+
145+
/**
146+
* Indicate that the table needs to be created with a hash partition.
68147
*
69148
* @return \Illuminate\Support\Fluent
70149
*/
71-
public function createPartitioned()
150+
public function createHashPartitioned()
72151
{
73-
return $this->addCommand('createPartitioned');
152+
return $this->addCommand('createHashPartitioned');
74153
}
75154

76155
/**
77-
* Create partition and attach parttioned table.
156+
* Create a hash partition and attach it to the partitioned table.
78157
*
79158
* @return \Illuminate\Support\Fluent
80159
*/
81-
public function attachPartition()
160+
public function initHashPartition()
82161
{
83-
return $this->addCommand('attachPartition');
162+
return $this->addCommand('initHashPartition');
84163
}
85164
}

0 commit comments

Comments
 (0)