From bd2f488e2c0b632dbf370eb1202701f04f89c95e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 29 Jan 2020 10:53:06 +0100 Subject: [PATCH] Updated entity mappings so that schema an table prefixes can be eventually provided --- .../Shlinkio.Shlink.Core.Entity.Domain.php | 4 ++-- .../Shlinkio.Shlink.Core.Entity.ShortUrl.php | 6 +++--- .../Shlinkio.Shlink.Core.Entity.Tag.php | 4 ++-- .../Shlinkio.Shlink.Core.Entity.Visit.php | 4 ++-- .../Shlinkio.Shlink.Core.Entity.VisitLocation.php | 4 ++-- module/Core/functions/functions.php | 14 ++++++++++++++ .../Shlinkio.Shlink.Rest.Entity.ApiKey.php | 8 +++++--- phpstan.neon | 1 - 8 files changed, 30 insertions(+), 15 deletions(-) diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Domain.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Domain.php index 1c751d17..c6349b74 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Domain.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Domain.php @@ -8,10 +8,10 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; use Doctrine\ORM\Mapping\ClassMetadata; -return static function (ClassMetadata $metadata): void { +return static function (ClassMetadata $metadata, array $emConfig): void { $builder = new ClassMetadataBuilder($metadata); - $builder->setTable('domains'); + $builder->setTable(determineTableName('domains', $emConfig)); $builder->createField('id', Types::BIGINT) ->columnName('id') diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php index a8bcf7ea..a4aef29f 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.ShortUrl.php @@ -9,10 +9,10 @@ use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; use Doctrine\ORM\Mapping\ClassMetadata; use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType; -return static function (ClassMetadata $metadata): void { +return static function (ClassMetadata $metadata, array $emConfig): void { $builder = new ClassMetadataBuilder($metadata); - $builder->setTable('short_urls') + $builder->setTable(determineTableName('short_urls', $emConfig)) ->setCustomRepositoryClass(Repository\ShortUrlRepository::class); $builder->createField('id', Types::BIGINT) @@ -57,7 +57,7 @@ return static function (ClassMetadata $metadata): void { ->build(); $builder->createManyToMany('tags', Entity\Tag::class) - ->setJoinTable('short_urls_in_tags') + ->setJoinTable(determineTableName('short_urls_in_tags', $emConfig)) ->addInverseJoinColumn('tag_id', 'id', true, false, 'CASCADE') ->addJoinColumn('short_url_id', 'id', true, false, 'CASCADE') ->build(); diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Tag.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Tag.php index 7d310eb8..214396bd 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Tag.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Tag.php @@ -8,10 +8,10 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; use Doctrine\ORM\Mapping\ClassMetadata; -return static function (ClassMetadata $metadata): void { +return static function (ClassMetadata $metadata, array $emConfig): void { $builder = new ClassMetadataBuilder($metadata); - $builder->setTable('tags') + $builder->setTable(determineTableName('tags', $emConfig)) ->setCustomRepositoryClass(Repository\TagRepository::class); $builder->createField('id', Types::BIGINT) diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php index 5d8f6ff4..803b9790 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.Visit.php @@ -10,10 +10,10 @@ use Doctrine\ORM\Mapping\ClassMetadata; use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType; use Shlinkio\Shlink\Core\Model\Visitor; -return static function (ClassMetadata $metadata): void { +return static function (ClassMetadata $metadata, array $emConfig): void { $builder = new ClassMetadataBuilder($metadata); - $builder->setTable('visits') + $builder->setTable(determineTableName('visits', $emConfig)) ->setCustomRepositoryClass(Repository\VisitRepository::class); $builder->createField('id', Types::BIGINT) diff --git a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php index b9652a08..fde00abc 100644 --- a/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php +++ b/module/Core/config/entities-mappings/Shlinkio.Shlink.Core.Entity.VisitLocation.php @@ -8,10 +8,10 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; use Doctrine\ORM\Mapping\ClassMetadata; -return static function (ClassMetadata $metadata): void { +return static function (ClassMetadata $metadata, array $emConfig): void { $builder = new ClassMetadataBuilder($metadata); - $builder->setTable('visit_locations'); + $builder->setTable(determineTableName('visit_locations', $emConfig)); $builder->createField('id', Types::BIGINT) ->columnName('id') diff --git a/module/Core/functions/functions.php b/module/Core/functions/functions.php index 1384549f..7ab5ebbb 100644 --- a/module/Core/functions/functions.php +++ b/module/Core/functions/functions.php @@ -8,6 +8,8 @@ use Cake\Chronos\Chronos; use DateTimeInterface; use PUGX\Shortid\Factory as ShortIdFactory; +use function sprintf; + function generateRandomShortCode(int $length = 5): string { static $shortIdFactory; @@ -39,3 +41,15 @@ function parseDateField($date): ?Chronos return Chronos::parse($date); } + +function determineTableName(string $tableName, array $emConfig = []): string +{ + $schema = $emConfig['connection']['schema'] ?? null; +// $tablePrefix = $emConfig['connection']['table_prefix'] ?? null; // TODO + + if ($schema === null) { + return $tableName; + } + + return sprintf('%s.%s', $schema, $tableName); +} diff --git a/module/Rest/config/entities-mappings/Shlinkio.Shlink.Rest.Entity.ApiKey.php b/module/Rest/config/entities-mappings/Shlinkio.Shlink.Rest.Entity.ApiKey.php index a79bd642..a5084cee 100644 --- a/module/Rest/config/entities-mappings/Shlinkio.Shlink.Rest.Entity.ApiKey.php +++ b/module/Rest/config/entities-mappings/Shlinkio.Shlink.Rest.Entity.ApiKey.php @@ -9,10 +9,12 @@ use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; use Doctrine\ORM\Mapping\ClassMetadata; use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType; -return static function (ClassMetadata $metadata): void { +use function Shlinkio\Shlink\Core\determineTableName; + +return static function (ClassMetadata $metadata, array $emConfig): void { $builder = new ClassMetadataBuilder($metadata); - $builder->setTable('api_keys'); + $builder->setTable(determineTableName('api_keys', $emConfig)); $builder->createField('id', Types::BIGINT) ->makePrimaryKey() @@ -31,5 +33,5 @@ return static function (ClassMetadata $metadata): void { ->build(); $builder->createField('enabled', Types::BOOLEAN) - ->build(); + ->build(); }; diff --git a/phpstan.neon b/phpstan.neon index b6c65f7f..d983a985 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,6 +2,5 @@ parameters: checkMissingIterableValueType: false checkGenericClassInNonGenericObjectType: false ignoreErrors: - - '#Undefined variable: \$metadata#' - '#AbstractQuery::setParameters()#' - '#mustRun()#'