<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James&#039; Tools and Tricks &#187; SQL</title>
	<atom:link href="http://jrudd.org/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://jrudd.org</link>
	<description>Tools, Tips and Hints for managing a network.</description>
	<lastBuildDate>Sun, 08 Jan 2012 03:50:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Moodle Structure Problems</title>
		<link>http://jrudd.org/2009/06/moodle-structure-problems/</link>
		<comments>http://jrudd.org/2009/06/moodle-structure-problems/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 07:10:44 +0000</pubDate>
		<dc:creator>James Rudd</dc:creator>
				<category><![CDATA[Moodle]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[Meld]]></category>
		<category><![CDATA[mysqldump]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://jrudd.org/wordpress/?p=106</guid>
		<description><![CDATA[Recently I discovered our Moodle gradebook was playing up and not correctly recording marks from assignments as well as been unable to assign categories from quizes.
I tracked the problem down to the Moodle database losing all its default entries for its tables.]]></description>
			<content:encoded><![CDATA[<p>WARNING: DO NOT ATTEMPT ANY OF THIS IF YOU DO NOT FULLY UNDERSTAND IT. YOU CAN DESTROY YOUR MOODLE SITE.</p>
<p>Recently I discovered our Moodle gradebook was playing up and not correctly recording marks from assignments as well as been unable to assign categories from quizes.</p>
<p>I tracked the problem down to the Moodle database losing all its default entries for its tables. The cause is unknown but is quite likely the <a title="UTF Convert" href="http://moodle.org/mod/forum/discuss.php?d=97592">UTF conversion utility</a> I ran previously to make the system compatible with Asian languages for LOTE.</p>
<p>To fix this problem I needed to recreate the Moodle DB structure and then reimport the data.</p>
<h2>Backup</h2>
<p>First make a backup. I made a mistake the first time I tried repairing this and the backup I made saved me.</p>
<pre class="brush: bash; title: ; notranslate">mysqldump -u backupacc -p -Q --opt moodle &gt; moodleBackup.sql</pre>
<p>I created a duplicate site with a clean DB (copy the moodle folder and create a new DB) and went through the install. This needs to have all your modules and blocks to ensure all tables are created.</p>
<p>I then dumped the structure from this new DB and the old DB and compared them using Meld (or any other diffing program will work).</p>
<pre class="brush: bash; title: ; notranslate"> mysqldump -u backupacc -p --no-data --skip-add-drop-table moodletest2 &gt; moodleGoodStruc.sql
mysqldump -u backupacc -p -d --skip-add-drop-table moodle &gt; moodleOldStruc.sql</pre>
<p>Using Meld determine if there are any extra tables or fields in your current system that don&#8217;t exist in the clean one.</p>
<p>Then you need to determine if it is safe to delete those tables / field in your main DB or if they need to be added to the clean system. e.g. grade_items_history.decimals and grade_items_history.display were in the main DB but not the new one. After searching for it discovered they can be removed (<a href="http://tracker.moodle.org/browse/MDL-15985">MDL-15985</a>).</p>
<p>After getting them to match you need to dump your Moodle data from your main site.</p>
<p>You can speed up the next steps by reducing the size of backup_log if you wish. Just do a</p>
<pre class="brush: sql; title: ; notranslate">
Delete
FROM `mdl_backup_log`
WHERE `time` &lt;1242777600
</pre>
<p>Where the time code is <a href="http://converteverything.com/conversions/unix_time.htm">calculated as a unix time code</a> from about a month before todays date. This can reduce alot of space of your DB, and improve import/export times.</p>
<pre class="brush: bash; title: ; notranslate">mysqldump -u backupacc -p  --no-create-info --extended-insert --complete-insert moodle &gt; moodleData200906171559.sql</pre>
<p>You need the <strong>&#8211;complete-insert</strong> option to ensure each insert is labelled with field name, in case field order is different. I didn&#8217;t do this the first time and needed to restore the backup.</p>
<h2>Testing Merge</h2>
<p>Now you need to test it. Create a new empty DB and import the Structure then import your data.</p>
<pre class="brush: bash; title: ; notranslate">mysql -u root -p moodleTest3 &lt; moodleStruc.sql
mysql -u root -p moodleTest3 &lt; moodleData200906171559.sql</pre>
<p>Check that no errors occurred during import, this is when I discovered most of my problems with extra non existent field.</p>
<h2>Prep for Applying</h2>
<p>Put your site in Maintenance mode so no users can login while you are testing this. (i.e. when you restore data site will already be in maintenance mode)</p>
<p>When everything is OK do another backup, as after this you are going to <strong>delete it all</strong>. (I recommend taking your site completely offline for this, not just maintenance mode. Edit your config.php and temporarily change either the username, password or db name or your database settings.)</p>
<p>Now re-export the Data again (assuming your site has been running while you have been doing tests)</p>
<pre class="brush: bash; title: ; notranslate">mysqldump -u backupacc -p  --no-create-info --extended-insert --complete-insert moodle &gt; moodleData200906171559.sql</pre>
<h2>Applying to Site</h2>
<p><strong>WARNING: HAVE A GOOD RELIABLE BACKUP BEFORE THIS STEP.  THIS WILL DESTROY YOUR MOODLE SITE.</strong></p>
<p>Now use phpMyAdmin to drop every table in the Moodle main DB. Just use Check All at the bottom and select Drop.</p>
<p>Now you need to redo your test restore above but to your main DB.</p>
<p>Once complete reconnect your config.php file and check everything on your site looks the same. Open some courses, check some gradebooks, have a look around.</p>
<p>When its OK take out of Maintenance mode and you should be good to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://jrudd.org/2009/06/moodle-structure-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some useful Moodle SQL Queries</title>
		<link>http://jrudd.org/2007/12/some-useful-moodle-sql-queries/</link>
		<comments>http://jrudd.org/2007/12/some-useful-moodle-sql-queries/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 14:28:55 +0000</pubDate>
		<dc:creator>James Rudd</dc:creator>
				<category><![CDATA[Moodle]]></category>
		<category><![CDATA[Bulk Operations]]></category>
		<category><![CDATA[Delete]]></category>
		<category><![CDATA[Roles]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://jrudd.org/wordpress/2007/12/15/some-useful-moodle-sql-queries/</guid>
		<description><![CDATA[I occasionally use the following SQL statements to clean-up our Moodle system and identify old courses. I recommend backing up your database before using any and knowing enough SQL to understand what they are doing before running them. I usually use phpMyAdmin for execution and export, but use what ever works best for you. Find <a href='http://jrudd.org/2007/12/some-useful-moodle-sql-queries/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I occasionally use the following SQL statements to clean-up our Moodle system and identify old courses. I recommend backing up your database before using any and knowing enough SQL to understand what they are doing before running them. I usually use <a href="http://www.phpmyadmin.net/" target="_blank">phpMyAdmin</a> for execution and export, but use what ever works best for you.</p>
<h2>Find Empty Moodle Courses</h2>
<p>If you use an auto-enrolment module you will often find you have lots of courses created that are never used. We have used the LDAP and Database enrolment before and both have created large numbers of courses. Some are not used by teachers,others are just used as child courses for year wide meta courses. Either way at the end of the year you may wish to identify any class that hasn&#8217;t been used. (Perhaps to delete using the <a href="http://jrudd.org//2007/12/14/moodle-bulk-deletion-operations/" target="_blank">Bulk Deletion</a> options).</p>
<h3>No Content</h3>
<p>Following SQL code I&#8217;ve written will generate a list containing a sum of most of the resources and activities a course has, allowing you to quickly identify those with little to no content. It bases this count on having no labels, resources, assignments, etc.</p>
<pre class="brush: sql; title: ; notranslate">
SELECT m.id, m.`shortname` , m.fullname, cCount.totalcount
FROM mdl_course m
LEFT JOIN (

SELECT courseCount.course, sum( courseCount.subcount ) AS totalcount
FROM (

SELECT course, count( * ) AS subcount
FROM mdl_resource
GROUP BY course
UNION ALL SELECT course, count( * ) AS subcount
FROM mdl_quiz
GROUP BY course
UNION ALL SELECT course, count( * ) AS subcount
FROM mdl_assignment
GROUP BY course
UNION ALL SELECT course, count( * ) AS subcount
FROM mdl_survey
GROUP BY course
UNION ALL SELECT course, count( * ) AS subcount
FROM mdl_label
GROUP BY course
UNION ALL SELECT course, count( * ) AS subcount
FROM mdl_glossary
GROUP BY course
UNION ALL SELECT course, count( * ) AS subcount
FROM mdl_homework
GROUP BY course
UNION ALL SELECT course, count( * ) AS subcount
FROM mdl_wiki
GROUP BY course
) AS courseCount
GROUP BY courseCount.course
) AS cCount ON cCount.course = m.id
</pre>
<p>You may need to edit above code if you do not have an activity installed (eg homework).</p>
<h3>No Users</h3>
<p>The following code generates a list of all your courses together with how many students are enrolled in each. Useful to find out if you have any courses with no one enrolled.</p>
<pre class="brush: sql; title: ; notranslate">

SELECT cr.shortname, cr.fullname, count( ra.id ) AS enrolled
FROM `mdl_course` cr
JOIN `mdl_context` ct ON ( ct.instanceid = cr.id )
LEFT JOIN `mdl_role_assignments` ra ON ( ra.contextid = ct.id )
WHERE ct.contextlevel =50
GROUP BY cr.shortname, cr.fullname
ORDER BY `enrolled` ASC
</pre>
<p>You can export either of the above queries into Excel and manipulate it from there. Order by the counts then copy the short names&#8217; of courses to delete, paste into a text file and upload to the <a href="http://jrudd.org//2007/12/14/moodle-bulk-deletion-operations/" target="_blank">Bulk Course Deletion</a> addon. Goodbye excess courses.</p>
<h2>Data Cleanup: Roles without Users</h2>
<p>Sometimes when a user gets deleted Moodle doesn&#8217;t clean up after it self as well as it should. The following code will list all the rows in your <span class="syntax"><span class="syntax_quote syntax_quote_backtick">role assignments </span></span>table that no longer match to a user:</p>
<pre class="brush: sql; title: ; notranslate">SELECT *
FROM `mdl_role_assignments`
WHERE `userid` NOT
IN (
SELECT id
FROM mdl_user
)
</pre>
<p>If you wish to the delete all of these just run following. As always make sure you have a good backup before deleting anything from DB.</p>
<pre class="brush: sql; title: ; notranslate">DELETE
FROM `mdl_role_assignments`
WHERE `userid` NOT
IN (
SELECT id
FROM mdl_user
)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://jrudd.org/2007/12/some-useful-moodle-sql-queries/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

