-
Notifications
You must be signed in to change notification settings - Fork 8
[#42387] Split out schema creation #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3562292
[#42387] Split out schema creation
kimpepper 23d6796
Alway create a new in memory db
kimpepper 74818c5
Adds indexes
kimpepper c31f08f
Split out validate function
kimpepper e29de2c
Remove unused imports
kimpepper ac64b4d
Rename create and drop schema
kimpepper c44bc88
Optimise imports
kimpepper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| namespace PNX\NestedSet; | ||
|
|
||
| /** | ||
| * Provides Nested Set schema operations. | ||
| */ | ||
| interface NestedSetSchemaInterface { | ||
|
|
||
| /** | ||
| * Creates the nested set table. | ||
| */ | ||
| public function create(); | ||
|
|
||
| /** | ||
| * Drops the nested set table. | ||
| */ | ||
| public function drop(); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| namespace PNX\NestedSet\Storage; | ||
|
|
||
| use Doctrine\DBAL\Connection; | ||
|
|
||
| /** | ||
| * Abstract base class for DBAL storage classes. | ||
| */ | ||
| abstract class BaseDbalStorage { | ||
|
|
||
| /** | ||
| * The regex for validating table names. | ||
| */ | ||
| const VALID_TABLE_REGEX = '/^[a-zA-Z]\w{1,64}$/'; | ||
|
|
||
| /** | ||
| * The database connection. | ||
| * | ||
| * @var \Doctrine\DBAL\Connection | ||
| */ | ||
| protected $connection; | ||
|
|
||
| /** | ||
| * The table name to use for storing the nested set. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $tableName; | ||
|
|
||
| /** | ||
| * DbalTree constructor. | ||
| * | ||
| * @param \Doctrine\DBAL\Connection $connection | ||
| * The database connection. | ||
| * @param string $tableName | ||
| * (optional) The table name to use. | ||
| */ | ||
| public function __construct(Connection $connection, $tableName = 'tree') { | ||
| $this->connection = $connection; | ||
| if (!$this->validTableName($tableName)) { | ||
| throw new \InvalidArgumentException("Table name must match the regex " . self::VALID_TABLE_REGEX); | ||
| } | ||
| $this->tableName = $tableName; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the table name is valid. | ||
| * | ||
| * Table names must: | ||
| * - start with a letter | ||
| * - only contain letters, numbers, and underscores | ||
| * - be maximum 64 characters. | ||
| * | ||
| * @param string $tableName | ||
| * The table name. | ||
| * | ||
| * @return bool | ||
| * TRUE if the table name is valid. | ||
| */ | ||
| protected function validTableName($tableName) { | ||
| return (bool) preg_match(self::VALID_TABLE_REGEX, $tableName); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| namespace PNX\NestedSet\Storage; | ||
|
|
||
| use Doctrine\DBAL\Schema\Schema; | ||
| use PNX\NestedSet\NestedSetSchemaInterface; | ||
|
|
||
| /** | ||
| * Provides DBAL Nested set schema operations. | ||
| */ | ||
| class DbalNestedSetSchema extends BaseDbalStorage implements NestedSetSchemaInterface { | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function create() { | ||
| $schema = new Schema(); | ||
| $tree = $schema->createTable($this->tableName); | ||
| $tree->addColumn("id", "integer", ["unsigned" => TRUE]); | ||
| $tree->addColumn("revision_id", "integer", ["unsigned" => TRUE]); | ||
| $tree->addColumn("left_pos", "integer", ["unsigned" => TRUE]); | ||
| $tree->addColumn("right_pos", "integer", ["unsigned" => TRUE]); | ||
| $tree->addColumn("depth", "integer", ["unsigned" => TRUE]); | ||
|
|
||
| $tree->setPrimaryKey(['id', 'revision_id']); | ||
| $tree->addIndex(['id', 'revision_id', 'left_pos', 'right_pos', 'depth']); | ||
| $tree->addIndex(['left_pos', 'right_pos']); | ||
| $tree->addIndex(['right_pos']); | ||
|
|
||
| foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) { | ||
| $this->connection->exec($sql); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function drop() { | ||
| $schema = new Schema(); | ||
| $schema->dropTable($this->tableName); | ||
| foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) { | ||
| $this->connection->exec($sql); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need indexes on any column you use in where clauses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah!