From 9bddf73d315de89fc65b74bcc81aca8dfe57a796 Mon Sep 17 00:00:00 2001
From: Joas Schilling <nickvergessen@gmx.de>
Date: Wed, 6 Mar 2013 13:52:11 +0100
Subject: [ticket/9657] Add migration files for updating the database

PHPBB3-9657
---
 .../db/migration/data/310/softdelete_p1.php        | 171 +++++++++++++++++++++
 .../db/migration/data/310/softdelete_p2.php        |  74 +++++++++
 2 files changed, 245 insertions(+)
 create mode 100644 phpBB/includes/db/migration/data/310/softdelete_p1.php
 create mode 100644 phpBB/includes/db/migration/data/310/softdelete_p2.php

(limited to 'phpBB/includes/db/migration/data')

diff --git a/phpBB/includes/db/migration/data/310/softdelete_p1.php b/phpBB/includes/db/migration/data/310/softdelete_p1.php
new file mode 100644
index 0000000000..35f6138ef6
--- /dev/null
+++ b/phpBB/includes/db/migration/data/310/softdelete_p1.php
@@ -0,0 +1,171 @@
+<?php
+/**
+*
+* @package migration
+* @copyright (c) 2012 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2
+*
+*/
+
+class phpbb_db_migration_data_310_softdelete_p1 extends phpbb_db_migration
+{
+	public function effectively_installed()
+	{
+		return $this->db_tools->sql_column_exists($this->table_prefix . 'posts', 'post_visibility');
+	}
+
+	static public function depends_on()
+	{
+		return array('phpbb_db_migration_data_310_dev');
+	}
+
+	public function update_schema()
+	{
+		return array(
+			'add_columns'		=> array(
+				$this->table_prefix . 'forums'		=> array(
+					'forum_posts_approved'		=> array('UINT', 0),
+					'forum_posts_unapproved'	=> array('UINT', 0),
+					'forum_posts_softdeleted'	=> array('UINT', 0),
+					'forum_topics_approved'		=> array('UINT', 0),
+					'forum_topics_unapproved'	=> array('UINT', 0),
+					'forum_topics_softdeleted'	=> array('UINT', 0),
+				),
+				$this->table_prefix . 'posts'		=> array(
+					'post_visibility'		=> array('TINT:3', 0),
+					'post_delete_time'		=> array('TIMESTAMP', 0),
+					'post_delete_reason'	=> array('STEXT_UNI', ''),
+					'post_delete_user'		=> array('UINT', 0),
+				),
+				$this->table_prefix . 'topics'		=> array(
+					'topic_visibility'		=> array('TINT:3', 0),
+					'topic_delete_time'		=> array('TIMESTAMP', 0),
+					'topic_delete_reason'	=> array('STEXT_UNI', ''),
+					'topic_delete_user'		=> array('UINT', 0),
+					'topic_posts_approved'		=> array('UINT', 0),
+					'topic_posts_unapproved'	=> array('UINT', 0),
+					'topic_posts_softdeleted'	=> array('UINT', 0),
+				),
+			),
+			'add_index'		=> array(
+				$this->table_prefix . 'posts'		=> array(
+					'post_visibility'		=> array('post_visibility'),
+				),
+				$this->table_prefix . 'topics'		=> array(
+					'topic_visibility'		=> array('topic_visibility'),
+					'forum_vis_last'		=> array('forum_id', 'topic_visibility', 'topic_last_post_id'),
+				),
+			),
+		);
+	}
+
+	public function revert_schema()
+	{
+		return array(
+			'drop_columns'		=> array(
+				$this->table_prefix . 'forums'		=> array(
+					'forum_posts_approved',
+					'forum_posts_unapproved',
+					'forum_posts_softdeleted',
+					'forum_topics_approved',
+					'forum_topics_unapproved',
+					'forum_topics_softdeleted',
+				),
+				$this->table_prefix . 'posts'		=> array(
+					'post_visibility',
+					'post_delete_time',
+					'post_delete_reason',
+					'post_delete_user',
+				),
+				$this->table_prefix . 'topics'		=> array(
+					'topic_visibility',
+					'topic_delete_time',
+					'topic_delete_reason',
+					'topic_delete_user',
+					'topic_posts_approved',
+					'topic_posts_unapproved',
+					'topic_posts_softdeleted',
+				),
+			),
+			'drop_keys'		=> array(
+				$this->table_prefix . 'posts'		=> array('post_visibility'),
+				$this->table_prefix . 'topics'	=> array('topic_visibility', 'forum_vis_last'),
+			),
+		);
+	}
+
+	public function update_data()
+	{
+		return array(
+			array('custom', array(array($this, 'update_post_visibility'))),
+			array('custom', array(array($this, 'update_topic_visibility'))),
+			array('custom', array(array($this, 'update_topic_forum_counts'))),
+
+			array('permission.add', array('f_softdelete', false)),
+			array('permission.add', array('m_softdelete', false)),
+		);
+	}
+
+	public function update_post_visibility()
+	{
+		$sql = 'UPDATE ' . $this->table_prefix . 'posts
+			SET post_visibility = post_approved';
+		$this->sql_query($sql);
+	}
+
+	public function update_topic_visibility()
+	{
+		$sql = 'UPDATE ' . $this->table_prefix . 'topics
+			SET topic_visibility = topic_approved';
+		$this->sql_query($sql);
+	}
+
+	public function update_topic_forum_counts()
+	{
+		$sql = 'UPDATE ' . $this->table_prefix . 'topics
+			SET topic_posts_approved = topic_replies + 1,
+				topic_posts_unapproved = topic_replies_real - topic_replies
+			WHERE topic_visibility = ' . ITEM_APPROVED;
+		$this->sql_query($sql);
+
+		$sql = 'UPDATE ' . $this->table_prefix . 'topics
+			SET topic_posts_approved = 0,
+				topic_posts_unapproved = (topic_replies_real - topic_replies) + 1
+			WHERE topic_visibility = ' . ITEM_UNAPPROVED;
+		$this->sql_query($sql);
+
+		$sql = 'SELECT forum_id, topic_visibility, COUNT(topic_id) AS sum_topics, SUM(topic_posts) AS sum_posts, SUM(topic_posts_unapproved) AS sum_posts_unapproved
+			FROM ' . $this->table_prefix . 'topics
+			GROUP BY forum_id, topic_visibility';
+		$result = $this->db->sql_query($sql);
+
+		$update_forums = array();
+		while ($row = $this->db->sql_fetchrow($result))
+		{
+			$forum_id = (int) $row['forum_id'];
+			if (!isset($update_forums[$forum_id]))
+			{
+				$update_forums[$forum_id] = array(
+					'forum_posts_approved'		=> 0,
+					'forum_posts_unapproved'	=> 0,
+					'forum_topics_approved'		=> 0,
+					'forum_topics_unapproved'	=> 0,
+				);
+			}
+
+			$update_forums[$forum_id]['forum_posts_approved'] += (int) $row['sum_posts'];
+			$update_forums[$forum_id]['forum_posts_unapproved'] += (int) $row['sum_posts_unapproved'];
+
+			$update_forums[$forum_id][(($row['topic_visibility'] == ITEM_APPROVED) ? 'forum_topics_approved' : 'forum_topics_unapproved')] += (int) $row['sum_topics'];
+		}
+		$this->db->sql_freeresult($result);
+
+		foreach ($update_forums as $forum_id => $forum_data)
+		{
+			$sql = 'UPDATE ' . FORUMS_TABLE . '
+				SET ' . $this->db->sql_build_array('UPDATE', $forum_data) . '
+				WHERE forum_id = ' . $forum_id;
+			$this->sql_query($sql);
+		}
+	}
+}
diff --git a/phpBB/includes/db/migration/data/310/softdelete_p2.php b/phpBB/includes/db/migration/data/310/softdelete_p2.php
new file mode 100644
index 0000000000..15de8e7185
--- /dev/null
+++ b/phpBB/includes/db/migration/data/310/softdelete_p2.php
@@ -0,0 +1,74 @@
+<?php
+/**
+*
+* @package migration
+* @copyright (c) 2012 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2
+*
+*/
+
+class phpbb_db_migration_data_310_softdelete_p2 extends phpbb_db_migration
+{
+	public function effectively_installed()
+	{
+		return !$this->db_tools->sql_column_exists($this->table_prefix . 'posts', 'post_approved');
+	}
+
+	static public function depends_on()
+	{
+		return array(
+			'phpbb_db_migration_data_310_dev',
+			'phpbb_db_migration_data_310_softdelete_p1',
+		);
+	}
+
+	public function update_schema()
+	{
+		return array(
+			'drop_columns'		=> array(
+				$this->table_prefix . 'forums'			=> array('forum_posts', 'forum_topics', 'forum_topics_real'),
+				$this->table_prefix . 'posts'			=> array('post_approved'),
+				$this->table_prefix . 'topics'			=> array('topic_approved', 'topic_replies', 'topic_replies_real'),
+			),
+			'drop_keys'		=> array(
+				$this->table_prefix . 'posts'			=> array('post_approved'),
+				$this->table_prefix . 'topics'			=> array('forum_appr_last'),
+			),
+		);
+	}
+
+	public function revert_schema()
+	{
+		return array(
+			'add_columns'		=> array(
+				$this->table_prefix . 'forums'			=> array(
+					'forum_posts'			=> array('UINT', 0),
+					'forum_topics'			=> array('UINT', 0),
+					'forum_topics_real'		=> array('UINT', 0),
+				),
+				$this->table_prefix . 'posts'			=> array(
+					'post_approved'			=> array('BOOL', 1),
+				),
+				$this->table_prefix . 'topics'		=> array(
+					'topic_approved'			=> array('BOOL', 1),
+					'topic_replies'				=> array('UINT', 0),
+					'topic_replies_real'		=> array('UINT', 0),
+				),
+			),
+			'add_index'		=> array(
+				$this->table_prefix . 'posts'			=> array(
+					'post_approved'		=> array('post_approved'),
+				),
+				$this->table_prefix . 'topics'		=> array(
+					'forum_appr_last'	=> array('forum_id', 'topic_approved', 'topic_last_post_id'),
+				),
+			),
+		);
+	}
+
+	public function update_data()
+	{
+		return array(
+		);
+	}
+}
-- 
cgit v1.2.1


From 60c0da8b54e5347e472d2ce97e756623717fd011 Mon Sep 17 00:00:00 2001
From: Joas Schilling <nickvergessen@gmx.de>
Date: Thu, 7 Mar 2013 13:15:55 +0100
Subject: [ticket/9657] Remove empty update_data() method from p2 migration

PHPBB3-9657
---
 phpBB/includes/db/migration/data/310/softdelete_p2.php | 6 ------
 1 file changed, 6 deletions(-)

(limited to 'phpBB/includes/db/migration/data')

diff --git a/phpBB/includes/db/migration/data/310/softdelete_p2.php b/phpBB/includes/db/migration/data/310/softdelete_p2.php
index 15de8e7185..7320a2c2bf 100644
--- a/phpBB/includes/db/migration/data/310/softdelete_p2.php
+++ b/phpBB/includes/db/migration/data/310/softdelete_p2.php
@@ -65,10 +65,4 @@ class phpbb_db_migration_data_310_softdelete_p2 extends phpbb_db_migration
 			),
 		);
 	}
-
-	public function update_data()
-	{
-		return array(
-		);
-	}
 }
-- 
cgit v1.2.1


From bff6cf40ba03320247c777d651d47673b3d36d2b Mon Sep 17 00:00:00 2001
From: Joas Schilling <nickvergessen@gmx.de>
Date: Thu, 7 Mar 2013 14:02:11 +0100
Subject: [ticket/9657] Fix colum name in migration file

PHPBB3-9657
---
 phpBB/includes/db/migration/data/310/softdelete_p1.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

(limited to 'phpBB/includes/db/migration/data')

diff --git a/phpBB/includes/db/migration/data/310/softdelete_p1.php b/phpBB/includes/db/migration/data/310/softdelete_p1.php
index 35f6138ef6..84f8eebd4a 100644
--- a/phpBB/includes/db/migration/data/310/softdelete_p1.php
+++ b/phpBB/includes/db/migration/data/310/softdelete_p1.php
@@ -134,7 +134,7 @@ class phpbb_db_migration_data_310_softdelete_p1 extends phpbb_db_migration
 			WHERE topic_visibility = ' . ITEM_UNAPPROVED;
 		$this->sql_query($sql);
 
-		$sql = 'SELECT forum_id, topic_visibility, COUNT(topic_id) AS sum_topics, SUM(topic_posts) AS sum_posts, SUM(topic_posts_unapproved) AS sum_posts_unapproved
+		$sql = 'SELECT forum_id, topic_visibility, COUNT(topic_id) AS sum_topics, SUM(topic_posts_approved) AS sum_posts_approved, SUM(topic_posts_unapproved) AS sum_posts_unapproved
 			FROM ' . $this->table_prefix . 'topics
 			GROUP BY forum_id, topic_visibility';
 		$result = $this->db->sql_query($sql);
@@ -153,7 +153,7 @@ class phpbb_db_migration_data_310_softdelete_p1 extends phpbb_db_migration
 				);
 			}
 
-			$update_forums[$forum_id]['forum_posts_approved'] += (int) $row['sum_posts'];
+			$update_forums[$forum_id]['forum_posts_approved'] += (int) $row['sum_posts_approved'];
 			$update_forums[$forum_id]['forum_posts_unapproved'] += (int) $row['sum_posts_unapproved'];
 
 			$update_forums[$forum_id][(($row['topic_visibility'] == ITEM_APPROVED) ? 'forum_topics_approved' : 'forum_topics_unapproved')] += (int) $row['sum_topics'];
-- 
cgit v1.2.1