Define a prefix for database table names.

This commit is contained in:
Olivier PEREZ 2014-12-31 15:19:15 +01:00
parent c9be94bc19
commit 1111b86e2a
6 changed files with 52 additions and 76 deletions

View File

@ -78,7 +78,7 @@ class FramaDB {
}
function findPollById($poll_id) {
$prepared = $this->prepare('SELECT * FROM poll WHERE id = ?');
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('poll') . ' WHERE id = ?');
$prepared->execute([$poll_id]);
$poll = $prepared->fetch();
@ -88,40 +88,40 @@ class FramaDB {
}
function updatePoll($poll) {
$prepared = $this->prepare('UPDATE poll SET title=?, admin_mail=?, comment=?, active=?, editable=? WHERE id = ?');
$prepared = $this->prepare('UPDATE ' . Utils::table('poll') . ' SET title=?, admin_mail=?, comment=?, active=?, editable=? WHERE id = ?');
return $prepared->execute([$poll->title, $poll->admin_mail, $poll->comment, $poll->active, $poll->editable, $poll->id]);
}
function allCommentsByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM comment WHERE poll_id = ? ORDER BY id');
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('comment') . ' WHERE poll_id = ? ORDER BY id');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
function allUserVotesByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM vote WHERE poll_id = ? ORDER BY id');
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('vote') . ' WHERE poll_id = ? ORDER BY id');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
function allSlotsByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM slot WHERE poll_id = ? ORDER BY title');
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('slot') . ' WHERE poll_id = ? ORDER BY title');
$prepared->execute(array($poll_id));
return $prepared->fetchAll();
}
function insertDefaultVote($poll_id, $insert_position) {
$prepared = $this->prepare('UPDATE vote SET choices = CONCAT(SUBSTRING(choices, 1, ?), "0", SUBSTRING(choices, ?)) WHERE poll_id = ?');
$prepared = $this->prepare('UPDATE ' . Utils::table('vote') . ' SET choices = CONCAT(SUBSTRING(choices, 1, ?), "0", SUBSTRING(choices, ?)) WHERE poll_id = ?');
return $prepared->execute([$insert_position, $insert_position + 1, $poll_id]);
}
function insertVote($poll_id, $name, $choices) {
$prepared = $this->prepare('INSERT INTO vote (poll_id, name, choices) VALUES (?,?,?)');
$prepared = $this->prepare('INSERT INTO ' . Utils::table('vote') . ' (poll_id, name, choices) VALUES (?,?,?)');
$prepared->execute([$poll_id, $name, $choices]);
$newVote = new \stdClass();
@ -134,7 +134,7 @@ class FramaDB {
}
function deleteVote($poll_id, $vote_id) {
$prepared = $this->prepare('DELETE FROM vote WHERE poll_id = ? AND id = ?');
$prepared = $this->prepare('DELETE FROM ' . Utils::table('vote') . ' WHERE poll_id = ? AND id = ?');
return $prepared->execute([$poll_id, $vote_id]);
}
@ -146,7 +146,7 @@ class FramaDB {
* @return bool|null true if action succeeded.
*/
function deleteVotesByPollId($poll_id) {
$prepared = $this->prepare('DELETE FROM vote WHERE poll_id = ?');
$prepared = $this->prepare('DELETE FROM ' . Utils::table('vote') . ' WHERE poll_id = ?');
return $prepared->execute([$poll_id]);
}
@ -159,7 +159,7 @@ class FramaDB {
* @return bool|null true if action succeeded.
*/
function deleteVotesByIndex($poll_id, $index) {
$prepared = $this->prepare('UPDATE vote SET choices = CONCAT(SUBSTR(choices, 1, ?), SUBSTR(choices, ?)) WHERE poll_id = ?');
$prepared = $this->prepare('UPDATE ' . Utils::table('vote') . ' SET choices = CONCAT(SUBSTR(choices, 1, ?), SUBSTR(choices, ?)) WHERE poll_id = ?');
return $prepared->execute([$index, $index + 2, $poll_id]);
}
@ -172,7 +172,7 @@ class FramaDB {
* @return mixed Object The slot found, or null
*/
function findSlotByPollIdAndDatetime($poll_id, $datetime) {
$prepared = $this->prepare('SELECT * FROM slot WHERE poll_id = ? AND SUBSTRING_INDEX(title, \'@\', 1) = ?');
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('slot') . ' WHERE poll_id = ? AND SUBSTRING_INDEX(title, \'@\', 1) = ?');
$prepared->execute([$poll_id, $datetime]);
$slot = $prepared->fetch();
@ -190,7 +190,7 @@ class FramaDB {
* @return bool true if action succeeded
*/
function insertSlot($poll_id, $title, $moments) {
$prepared = $this->prepare('INSERT INTO slot (poll_id, title, moments) VALUES (?,?,?)');
$prepared = $this->prepare('INSERT INTO ' . Utils::table('slot') . ' (poll_id, title, moments) VALUES (?,?,?)');
return $prepared->execute([$poll_id, $title, $moments]);
}
@ -204,7 +204,7 @@ class FramaDB {
* @return bool|null true if action succeeded.
*/
function updateSlot($poll_id, $datetime, $newMoments) {
$prepared = $this->prepare('UPDATE slot SET moments = ? WHERE poll_id = ? AND title = ?');
$prepared = $this->prepare('UPDATE ' . Utils::table('slot') . ' SET moments = ? WHERE poll_id = ? AND title = ?');
return $prepared->execute([$newMoments, $poll_id, $datetime]);
}
@ -216,12 +216,12 @@ class FramaDB {
* @param $datetime mixed The datetime of the slot
*/
function deleteSlot($poll_id, $datetime) {
$prepared = $this->prepare('DELETE FROM slot WHERE poll_id = ? AND title = ?');
$prepared = $this->prepare('DELETE FROM ' . Utils::table('slot') . ' WHERE poll_id = ? AND title = ?');
$prepared->execute([$poll_id, $datetime]);
}
function deleteSlotsByPollId($poll_id) {
$prepared = $this->prepare('DELETE FROM slot WHERE poll_id = ?');
$prepared = $this->prepare('DELETE FROM ' . Utils::table('slot') . ' WHERE poll_id = ?');
return $prepared->execute([$poll_id]);
}
@ -233,31 +233,31 @@ class FramaDB {
* @return bool|null true if action succeeded.
*/
function deleteCommentsByPollId($poll_id) {
$prepared = $this->prepare('DELETE FROM comment WHERE poll_id = ?');
$prepared = $this->prepare('DELETE FROM ' . Utils::table('comment') . ' WHERE poll_id = ?');
return $prepared->execute([$poll_id]);
}
function updateVote($poll_id, $vote_id, $choices) {
$prepared = $this->prepare('UPDATE vote SET choices = ? WHERE poll_id = ? AND id = ?');
$prepared = $this->prepare('UPDATE ' . Utils::table('vote') . ' SET choices = ? WHERE poll_id = ? AND id = ?');
return $prepared->execute([$choices, $poll_id, $vote_id]);
}
function insertComment($poll_id, $name, $comment) {
$prepared = $this->prepare('INSERT INTO comment (poll_id, name, comment) VALUES (?,?,?)');
$prepared = $this->prepare('INSERT INTO ' . Utils::table('comment') . ' (poll_id, name, comment) VALUES (?,?,?)');
return $prepared->execute([$poll_id, $name, $comment]);
}
function deleteComment($poll_id, $comment_id) {
$prepared = $this->prepare('DELETE FROM comment WHERE poll_id = ? AND id = ?');
$prepared = $this->prepare('DELETE FROM ' . Utils::table('comment') . ' WHERE poll_id = ? AND id = ?');
return $prepared->execute([$poll_id, $comment_id]);
}
function deletePollById($poll_id) {
$prepared = $this->prepare('DELETE FROM poll WHERE id = ?');
$prepared = $this->prepare('DELETE FROM ' . Utils::table('poll') . ' WHERE id = ?');
return $prepared->execute([$poll_id]);
}
@ -268,7 +268,7 @@ class FramaDB {
* @return array Array of old polls
*/
public function findOldPolls() {
$prepared = $this->prepare('SELECT * FROM poll WHERE end_date < NOW() LIMIT 20');
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('poll') . ' WHERE end_date < NOW() LIMIT 20');
$prepared->execute([]);
return $prepared->fetchAll();

View File

@ -1,6 +1,8 @@
<?php
namespace Framadate\Migration;
use Framadate\Utils;
/**
* This class executes the aciton in database to migrate data from version 0.8 to 0.9.
*
@ -11,6 +13,12 @@ class From_0_8_to_0_9_Migration implements Migration {
function __construct() {
}
/**
* This methode is called only one time in the migration page.
*
* @param \PDO $pdo The connection to database
* @return bool true is the execution succeeded
*/
function execute(\PDO $pdo) {
$this->createPollTable($pdo);
$this->migrateFromSondageToPoll($pdo);
@ -31,7 +39,7 @@ class From_0_8_to_0_9_Migration implements Migration {
private function createPollTable(\PDO $pdo) {
$pdo->exec('
CREATE TABLE IF NOT EXISTS `poll` (
CREATE TABLE IF NOT EXISTS `' . Utils::table('poll') . '` (
`id` CHAR(16) NOT NULL,
`admin_id` CHAR(24) NOT NULL,
`title` TEXT NOT NULL,
@ -52,7 +60,7 @@ CREATE TABLE IF NOT EXISTS `poll` (
private function migrateFromSondageToPoll(\PDO $pdo) {
$pdo->exec('
INSERT INTO `poll`
INSERT INTO `' . Utils::table('poll') . '`
(`id`, `admin_id`, `title`, `description`, `admin_name`, `admin_mail`, `creation_date`, `end_date`, `format`, `editable`, `receiveNewVotes`, `active`)
SELECT
`id_sondage`,
@ -76,7 +84,7 @@ INSERT INTO `poll`
private function createSlotTable(\PDO $pdo) {
$pdo->exec('
CREATE TABLE IF NOT EXISTS `slot` (
CREATE TABLE IF NOT EXISTS `' . Utils::table('slot') . '` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`poll_id` CHAR(16) NOT NULL,
`title` TEXT,
@ -98,7 +106,7 @@ CREATE TABLE IF NOT EXISTS `slot` (
$slots = array_merge($slots, $newSlots);
}
$prepared = $pdo->prepare('INSERT INTO slot (`poll_id`, `title`, `moments`) VALUE (?,?,?)');
$prepared = $pdo->prepare('INSERT INTO ' . Utils::table('slot') . ' (`poll_id`, `title`, `moments`) VALUE (?,?,?)');
foreach ($slots as $slot) {
$prepared->execute([$slot->poll_id, $slot->title, $slot->moments]);
}
@ -106,7 +114,7 @@ CREATE TABLE IF NOT EXISTS `slot` (
private function createCommentTable(\PDO $pdo) {
$pdo->exec('
CREATE TABLE IF NOT EXISTS `comment` (
CREATE TABLE IF NOT EXISTS `' . Utils::table('comment') . '` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`poll_id` CHAR(16) NOT NULL,
`name` TEXT,
@ -120,7 +128,7 @@ CREATE TABLE IF NOT EXISTS `comment` (
private function migrateFromCommentsToComment(\PDO $pdo) {
$pdo->exec('
INSERT INTO `comment`
INSERT INTO `' . Utils::table('comment') . '`
(`poll_id`, `name`, `comment`)
SELECT
`id_sondage`,
@ -131,7 +139,7 @@ INSERT INTO `comment`
private function createVoteTable(\PDO $pdo) {
$pdo->exec('
CREATE TABLE IF NOT EXISTS `vote` (
CREATE TABLE IF NOT EXISTS `' . Utils::table('vote') . '` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`poll_id` CHAR(16) NOT NULL,
`name` VARCHAR(64) NOT NULL,
@ -145,7 +153,7 @@ CREATE TABLE IF NOT EXISTS `vote` (
private function migrateFromUserStudsToVote(\PDO $pdo) {
$pdo->exec('
INSERT INTO `vote`
INSERT INTO `' . Utils::table('vote') . '`
(`poll_id`, `name`, `choices`)
SELECT
`id_sondage`,

View File

@ -133,13 +133,13 @@ class PollService {
$this->connect->beginTransaction();
// TODO Extract this to FramaDB (or repository layer)
$sql = 'INSERT INTO poll
$sql = 'INSERT INTO ' . Utils::table('poll') . '
(id, admin_id, title, description, admin_name, admin_mail, end_date, format, editable, receiveNewVotes)
VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?)';
$prepared = $this->connect->prepare($sql);
$prepared->execute(array($poll_id, $admin_poll_id, $form->title, $form->description, $form->admin_name, $form->admin_mail, $form->end_date, $form->format, $form->editable, $form->receiveNewVotes));
$prepared = $this->connect->prepare('INSERT INTO slot (poll_id, title, moments) VALUES (?, ?, ?)');
$prepared = $this->connect->prepare('INSERT INTO ' . Utils::table('slot') . ' (poll_id, title, moments) VALUES (?, ?, ?)');
foreach ($form->getChoices() as $choice) {

View File

@ -123,46 +123,6 @@ class Utils
return $url;
}
/**
* Completly delete data about the given poll
* TODO Move this function to FramaDB
*/
public static function removeSondage($poll_id) {
global $connect;
$prepared = $connect->prepare('DELETE FROM sujet_studs WHERE id_sondage = ?');
$prepared->execute(array($poll_id));
$prepared = $connect->prepare('DELETE FROM user_studs WHERE id_sondage = ?');
$prepared->execute(array($poll_id));
$prepared = $connect->prepare('DELETE FROM comments WHERE id_sondage = ?');
$prepared->execute(array($poll_id));
$prepared = $connect->prepare('DELETE FROM sondage WHERE poll_id = ?');
$prepared->execute(array($poll_id));
}
/**
* Clean old poll (end_date < now).
* TODO Move this function to PurgePollService
*/
public static function cleaningOldPolls($log_txt) {
global $connect;
$resultSet = $connect->query('SELECT poll_id, format, admin_name FROM sondage WHERE end_date < NOW() LIMIT 20');
$toClean = $resultSet->fetchAll(\PDO::FETCH_CLASS);
$connect->beginTransaction();
foreach ($toClean as $row) {
if (self::removeSondage($row->poll_id)) {
error_log(date('H:i:s d/m/Y:') . ' EXPIRATION: '. $row->poll_id."\t".$row->format."\t".$row->admin_name."\n", 3, $log_txt);
}
}
$connect->commit();
}
/**
* This method pretty prints an object to the page framed by pre tags.
* @param mixed $object The object to print.
@ -172,4 +132,8 @@ class Utils
print_r($object);
echo '</pre>';
}
public static function table($tableName) {
return TABLENAME_PREFIX . $tableName;
}
}

View File

@ -44,6 +44,9 @@ const DB_CONNECTION_STRING = 'mysql:host=<database host>;dbname=<database name>;
// Name of the table that store migration script already executed
const MIGRATION_TABLE = 'framadate_migration';
// Table name prefix
const TABLENAME_PREFIX = 'fd_';
// Default Language using POSIX variant of BC P47 standard (choose in $ALLOWED_LANGUAGES)
const LANGUE = 'fr_FR';

View File

@ -18,10 +18,11 @@ $migrations = [
// Check if MIGRATION_TABLE already exists
$tables = $connect->allTables();
$pdo = $connect->getPDO();
$prefixedMigrationTable = Utils::table(MIGRATION_TABLE);
if (!in_array(MIGRATION_TABLE, $tables)) {
if (!in_array($prefixedMigrationTable, $tables)) {
$pdo->exec('
CREATE TABLE IF NOT EXISTS `' . MIGRATION_TABLE . '` (
CREATE TABLE IF NOT EXISTS `' . $prefixedMigrationTable . '` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` TEXT NOT NULL,
`execute_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@ -30,11 +31,11 @@ CREATE TABLE IF NOT EXISTS `' . MIGRATION_TABLE . '` (
ENGINE = MyISAM
DEFAULT CHARSET = utf8;');
output('Table ' . MIGRATION_TABLE . ' created.');
output('Table ' . $prefixedMigrationTable . ' created.');
}
$selectStmt = $pdo->prepare('SELECT id FROM ' . MIGRATION_TABLE . ' WHERE name=?');
$insertStmt = $pdo->prepare('INSERT INTO ' . MIGRATION_TABLE . ' (name) VALUES (?)');
$selectStmt = $pdo->prepare('SELECT id FROM ' . $prefixedMigrationTable . ' WHERE name=?');
$insertStmt = $pdo->prepare('INSERT INTO ' . $prefixedMigrationTable . ' (name) VALUES (?)');
$countSucceeded = 0;
$countFailed = 0;
$countSkipped = 0;