aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2015-04-01 21:53:48 +0200
committerNicolas LÅ“uillet <nicolas@loeuillet.org>2015-04-01 21:53:48 +0200
commitd6fa2f70ac3c798b28ce33c6c143f45c09c74062 (patch)
tree40769c29279d189dfe0dbb8aa513122f9172ebde
parent1a93ee423b072ec3bcb0c437cbf9b488bdea245c (diff)
parent276a1e9d3fb7e83569d16bcb37a67607d4ace8be (diff)
downloadwallabag-d6fa2f70ac3c798b28ce33c6c143f45c09c74062.tar.gz
wallabag-d6fa2f70ac3c798b28ce33c6c143f45c09c74062.tar.zst
wallabag-d6fa2f70ac3c798b28ce33c6c143f45c09c74062.zip
Merge pull request #1165 from wallabag/v2-prefix-db
Prefix table
-rw-r--r--app/config/config.yml5
-rw-r--r--app/config/parameters.yml.dist1
-rw-r--r--build.xml7
-rw-r--r--src/Wallabag/CoreBundle/Command/InstallCommand.php5
-rw-r--r--src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php75
-rw-r--r--src/Wallabag/CoreBundle/Entity/Config.php2
-rw-r--r--src/Wallabag/CoreBundle/Entity/Entry.php4
-rw-r--r--src/Wallabag/CoreBundle/Entity/Tag.php2
-rw-r--r--src/Wallabag/CoreBundle/Entity/User.php2
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml4
10 files changed, 99 insertions, 8 deletions
diff --git a/app/config/config.yml b/app/config/config.yml
index 367aa276..0807b06a 100644
--- a/app/config/config.yml
+++ b/app/config/config.yml
@@ -75,7 +75,10 @@ doctrine:
75 75
76 orm: 76 orm:
77 auto_generate_proxy_classes: "%kernel.debug%" 77 auto_generate_proxy_classes: "%kernel.debug%"
78 auto_mapping: true 78 entity_managers:
79 default:
80 naming_strategy: wallabag_core.doctrine.prefixed_naming_strategy
81 auto_mapping: true
79 82
80# Swiftmailer Configuration 83# Swiftmailer Configuration
81swiftmailer: 84swiftmailer:
diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist
index 73318013..f80f65ad 100644
--- a/app/config/parameters.yml.dist
+++ b/app/config/parameters.yml.dist
@@ -7,6 +7,7 @@ parameters:
7 database_user: root 7 database_user: root
8 database_password: ~ 8 database_password: ~
9 database_path: "%kernel.root_dir%/../data/db/wallabag.sqlite" 9 database_path: "%kernel.root_dir%/../data/db/wallabag.sqlite"
10 database_table_prefix: wallabag_
10 11
11 mailer_transport: smtp 12 mailer_transport: smtp
12 mailer_host: 127.0.0.1 13 mailer_host: 127.0.0.1
diff --git a/build.xml b/build.xml
index cd2dfa3c..30ed2fa1 100644
--- a/build.xml
+++ b/build.xml
@@ -40,4 +40,11 @@
40 <arg value="--env=test"/> 40 <arg value="--env=test"/>
41 </exec> 41 </exec>
42 </target> 42 </target>
43
44 <target name="phpunit" description="Run unit tests with PHPUnit + HTML Coverage">
45 <exec executable="phpunit" failonerror="true">
46 <arg value="--coverage-html"/>
47 <arg value="build/coverage"/>
48 </exec>
49 </target>
43</project> 50</project>
diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php
index bba2607d..493842f7 100644
--- a/src/Wallabag/CoreBundle/Command/InstallCommand.php
+++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php
@@ -299,7 +299,8 @@ class InstallCommand extends ContainerAwareCommand
299 } 299 }
300 300
301 /** 301 /**
302 * Check if the schema is already created 302 * Check if the schema is already created.
303 * If we found at least oen table, it means the schema exists
303 * 304 *
304 * @return boolean 305 * @return boolean
305 */ 306 */
@@ -307,6 +308,6 @@ class InstallCommand extends ContainerAwareCommand
307 { 308 {
308 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager(); 309 $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
309 310
310 return $schemaManager->tablesExist(array('entry')); 311 return count($schemaManager->listTableNames()) > 0 ? true : false;
311 } 312 }
312} 313}
diff --git a/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php b/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php
new file mode 100644
index 00000000..861a60ea
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Doctrine/Mapping/PrefixedNamingStrategy.php
@@ -0,0 +1,75 @@
1<?php
2
3namespace Wallabag\CoreBundle\Doctrine\Mapping;
4
5use Doctrine\ORM\Mapping\NamingStrategy;
6
7/**
8 * Puts a prefix to each table.
9 *
10 * Solution from :
11 * - http://stackoverflow.com/a/23860613/569101
12 * - http://doctrine-orm.readthedocs.org/en/latest/reference/namingstrategy.html
13 */
14class PrefixedNamingStrategy implements NamingStrategy
15{
16 protected $prefix = '';
17
18 public function __construct($prefix)
19 {
20 $this->prefix = (string) $prefix;
21 }
22
23 /**
24 * {@inheritdoc}
25 */
26 public function classToTableName($className)
27 {
28 return strtolower($this->prefix . substr($className, strrpos($className, '\\') + 1));
29 }
30
31 /**
32 * {@inheritdoc}
33 */
34 public function propertyToColumnName($propertyName, $className = null)
35 {
36 return $propertyName;
37 }
38
39 /**
40 * {@inheritdoc}
41 */
42 public function referenceColumnName()
43 {
44 return 'id';
45 }
46
47 /**
48 * {@inheritdoc}
49 */
50 public function joinColumnName($propertyName)
51 {
52 return $propertyName . '_' . $this->referenceColumnName();
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
59 {
60 // for join table we don't want to have both table concatenated AND prefixed
61 // we just want the whole table to prefixed once
62 // ie: not "wallabag_entry_wallabag_tag" but "wallabag_entry_tag"
63 $target = substr($targetEntity, strrpos($targetEntity, '\\') + 1);
64
65 return strtolower($this->classToTableName($sourceEntity) . '_' .$target);
66 }
67
68 /**
69 * {@inheritdoc}
70 */
71 public function joinKeyColumnName($entityName, $referencedColumnName = null)
72 {
73 return strtolower($this->classToTableName($entityName) . '_' .($referencedColumnName ?: $this->referenceColumnName()));
74 }
75}
diff --git a/src/Wallabag/CoreBundle/Entity/Config.php b/src/Wallabag/CoreBundle/Entity/Config.php
index 9f079656..62ea637e 100644
--- a/src/Wallabag/CoreBundle/Entity/Config.php
+++ b/src/Wallabag/CoreBundle/Entity/Config.php
@@ -9,7 +9,7 @@ use Symfony\Component\Validator\Constraints as Assert;
9 * Config 9 * Config
10 * 10 *
11 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository") 11 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\ConfigRepository")
12 * @ORM\Table(name="config") 12 * @ORM\Table
13 * @ORM\Entity 13 * @ORM\Entity
14 */ 14 */
15class Config 15class Config
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php
index 75aeae84..15af105d 100644
--- a/src/Wallabag/CoreBundle/Entity/Entry.php
+++ b/src/Wallabag/CoreBundle/Entity/Entry.php
@@ -13,7 +13,7 @@ use JMS\Serializer\Annotation\XmlRoot;
13 * 13 *
14 * @XmlRoot("entry") 14 * @XmlRoot("entry")
15 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository") 15 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
16 * @ORM\Table(name="entry") 16 * @ORM\Table
17 * @ORM\HasLifecycleCallbacks() 17 * @ORM\HasLifecycleCallbacks()
18 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())") 18 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
19 */ 19 */
@@ -121,7 +121,7 @@ class Entry
121 121
122 /** 122 /**
123 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) 123 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
124 * @ORM\JoinTable(name="entry_tags") 124 * @ORM\JoinTable
125 */ 125 */
126 private $tags; 126 private $tags;
127 127
diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php
index 9ae5867c..9d3c7a32 100644
--- a/src/Wallabag/CoreBundle/Entity/Tag.php
+++ b/src/Wallabag/CoreBundle/Entity/Tag.php
@@ -12,7 +12,7 @@ use Doctrine\Common\Collections\ArrayCollection;
12 * Tag 12 * Tag
13 * 13 *
14 * @XmlRoot("tag") 14 * @XmlRoot("tag")
15 * @ORM\Table(name="tag") 15 * @ORM\Table
16 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository") 16 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
17 * @ExclusionPolicy("all") 17 * @ExclusionPolicy("all")
18 */ 18 */
diff --git a/src/Wallabag/CoreBundle/Entity/User.php b/src/Wallabag/CoreBundle/Entity/User.php
index e75e3a83..1652170f 100644
--- a/src/Wallabag/CoreBundle/Entity/User.php
+++ b/src/Wallabag/CoreBundle/Entity/User.php
@@ -13,8 +13,8 @@ use JMS\Serializer\Annotation\Expose;
13/** 13/**
14 * User 14 * User
15 * 15 *
16 * @ORM\Table(name="user")
17 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\UserRepository") 16 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\UserRepository")
17 * @ORM\Table
18 * @ORM\HasLifecycleCallbacks() 18 * @ORM\HasLifecycleCallbacks()
19 * @ExclusionPolicy("all") 19 * @ExclusionPolicy("all")
20 */ 20 */
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml
index 0f4db94e..cea6c0df 100644
--- a/src/Wallabag/CoreBundle/Resources/config/services.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/services.yml
@@ -43,3 +43,7 @@ services:
43 - { name: request.param_converter, converter: username_rsstoken_converter } 43 - { name: request.param_converter, converter: username_rsstoken_converter }
44 arguments: 44 arguments:
45 - @doctrine 45 - @doctrine
46
47 wallabag_core.doctrine.prefixed_naming_strategy:
48 class: Wallabag\CoreBundle\Doctrine\Mapping\PrefixedNamingStrategy
49 arguments: [%database_table_prefix%]