<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Dave Dash</title>
 <link href="http://davedash.com/tag/mysql/atom.xml" rel="self"/>
 <link href="http://davedash.com/tag/mysql"/>
 <updated>2012-01-17T21:54:19-08:00</updated>
 <id>http://davedash.com/</id>
 <author>
   <name>Dave Dash</name>
   <email>dd+atom1@davedash.com</email>
 </author>

 
 <entry>
   <title>Data Anonymous</title>
   <link href="http://davedash.com/2011/03/02/data-anonymous/"/>
   <updated>2011-03-02T00:00:00-08:00</updated>
   <id>http://davedash.com/2011/03/02/data-anonymous</id>
   <content type="html">&lt;p&gt;I wrote a simple database &lt;a href=&quot;https://github.com/davedash/mysql-anonymous&quot;&gt;scrubber script&lt;/a&gt;.  It takes a &lt;code&gt;yaml&lt;/code&gt; file that
describes what scrubbing needs doing and then outputs &lt;code&gt;sql&lt;/code&gt; that you can send
to &lt;code&gt;mysql&lt;/code&gt;.  It's dreadfully simple and I'd like to see if others can make use
of it.&lt;/p&gt;

&lt;p&gt;At Mozilla we have a lot of contributors and would like them to have access to
realistic data since many of our bugs are based on certain states within the
data.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Making our tests run thrice as fast</title>
   <link href="http://davedash.com/2010/03/16/making-our-tests-run-thrice-as-fast/"/>
   <updated>2010-03-16T00:00:00-07:00</updated>
   <id>http://davedash.com/2010/03/16/making-our-tests-run-thrice-as-fast</id>
   <content type="html">&lt;p&gt;I've written a faster version of &lt;a href=&quot;http://github.com/jbalogh/test-utils/blob/c4c31905a95e59dcc8919c1030b23848ad7fbca6/test_utils/__init__.py#L57&quot;&gt;TransactionTestCase&lt;/a&gt; and packaged it with &lt;a href=&quot;http://github.com/jbalogh/test-utils&quot;&gt;test_utils&lt;/a&gt;.  It's mysql specific since it relies on &lt;code&gt;SET FOREIGN_KEY_CHECKS=0&lt;/code&gt; to flush the database.&lt;/p&gt;

&lt;p&gt;The long story...&lt;/p&gt;

&lt;!-- more--&gt;


&lt;h3&gt;Why speed matters&lt;/h3&gt;

&lt;p&gt;We're closing in on 300 tests for &lt;a href=&quot;http://github.com/jbalogh/zamboni/&quot;&gt;Zamboni&lt;/a&gt;.  As of yesterday, to run our entire test suite it would have taken approximately 5 minutes.  If you run tests before code-reviews, during a code-review, and before you push to master - you've spent about 15 minutes doing tests for a single feature or bug-fix.  We have about 5 developers, so this cycle happens many times in a work day.  In that time many sandwiches can be made and consumed.&lt;/p&gt;

&lt;p&gt;Even shortcuts, like running a subset of tests will only go so far, and ultimately we do want to validate that all our tests pass for any code-change.&lt;/p&gt;

&lt;h3&gt;Testing Sphinx search with &lt;code&gt;TransactionTestCase&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Django recently sped up testing by running tests in a transaction.  However, this means that data never gets committed to the database and therefore external tools, like the Sphinx indexer, will never see any of that data.  So we resort to &lt;code&gt;TransactionTestCase&lt;/code&gt; which &lt;em&gt;will&lt;/em&gt; commit the data.&lt;/p&gt;

&lt;p&gt;Unfortunately &lt;code&gt;TransactionTestCase&lt;/code&gt; is painfully slow.  The accepted practice is to only use &lt;code&gt;TestCase&lt;/code&gt; if you want your tests to be fast.  So, I decided to complain to &lt;a href=&quot;http://blog.ianbicking.org/&quot;&gt;one of our new hires&lt;/a&gt; and he and I decided to tinker in mysql to figure out what was slow.  We discovered the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;delete from [table] is slow&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;truncate [table] is slow&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;... unless you &lt;code&gt;SET FOREIGN_KEY_CHECKS=0&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;So we decided we should do our own tear down.  After some tinkering with &lt;code&gt;cProfiler&lt;/code&gt; I discovered that &lt;code&gt;TransactionTestCase&lt;/code&gt; does a (slow) database &lt;code&gt;flush&lt;/code&gt; on setup for a test case.  This wouldn't do.&lt;/p&gt;

&lt;h3&gt;Making our own &lt;code&gt;TransactionTestCase&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;I decided to make our own &lt;code&gt;TransactionTestCase&lt;/code&gt; and it would just run &lt;code&gt;SET FOREIGN_KEY_CHECKS=0&lt;/code&gt; and &lt;code&gt;TRUNCATE&lt;/code&gt; on each table at tear down time.  It would also not do a &lt;code&gt;flush&lt;/code&gt; on set up.&lt;/p&gt;

&lt;p&gt;We write our tests with the idea that they clean up after themselves.  Rather than having them cleanup after the last test.  This is a requirement for us since &lt;code&gt;django-nose&lt;/code&gt; doesn't reorder tests (nor should it) and a standard &lt;code&gt;django.test.TestCase&lt;/code&gt; assumes a clean database.&lt;/p&gt;

&lt;p&gt;Looking at a single test &lt;code&gt;test_sphinx_indexer&lt;/code&gt;, using &lt;code&gt;django.test.TransactionTestCase&lt;/code&gt; took ~30 seconds.  Using our new &lt;code&gt;TransactionTestCase&lt;/code&gt; it takes ~4 seconds!&lt;/p&gt;

&lt;h3&gt;Fast tests are good&lt;/h3&gt;

&lt;p&gt;We can now run our 275 tests in ~100 seconds versus the ~300 seconds it used to take.  Furthermore, skipping our sphinx tests (which are the only tests that use &lt;code&gt;TransactionTestCase&lt;/code&gt;) only saves us ~10seconds.  That's not a lot of overhead for better coverage.&lt;/p&gt;

&lt;p&gt;This took me the better part of a day, but solving this now, means we're going to more often than not run our sphinx tests all the time rather than skip them.  Our QA team will assure you that search is probably the most regression prone part of our site, so running these tests are vital to quality.&lt;/p&gt;

&lt;p&gt;If you need to use &lt;code&gt;TransactionTestCase&lt;/code&gt; in mysql, &lt;a href=&quot;http://github.com/jbalogh/test-utils&quot;&gt;give ours a try&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Retrieving elements in a specific order in Django and mySQL</title>
   <link href="http://davedash.com/2010/02/11/retrieving-elements-in-a-specific-order-in-django-and-mysql/"/>
   <updated>2010-02-11T00:00:00-08:00</updated>
   <id>http://davedash.com/2010/02/11/retrieving-elements-in-a-specific-order-in-django-and-mysql</id>
   <content type="html">&lt;p&gt;If you have a list of ordered ids and you want to turn them into an ordered result set you can use &lt;code&gt;FIELD()&lt;/code&gt; in mysql:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;SELECT * FROM addons
ORDER BY FIELD(id, 3, 5, 9, 1);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a handy trick if you use an external search engine which gives you an ordered list of ids and you want to pull out entire row sets.&lt;/p&gt;

&lt;p&gt;We do this in &lt;a href=&quot;http://github.com/jbalogh/zamboni&quot;&gt;addons.mozilla.org&lt;/a&gt; using the Django ORM like so:&lt;/p&gt;

&lt;script src=&quot;http://gist.github.com/301162.js&quot;&gt;&lt;/script&gt;


&lt;p&gt;&lt;a href=&quot;http://github.com/jbalogh/zamboni/commit/a0166108e8a62f386b4310cab0ceb3502575d520#L1R219&quot;&gt;The code in action&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Snow Leopard for Macports and Mysql users</title>
   <link href="http://davedash.com/2009/09/02/snow-leopard-for-macports-and-mysql-users/"/>
   <updated>2009-09-02T00:00:00-07:00</updated>
   <id>http://davedash.com/2009/09/02/snow-leopard-for-macports-and-mysql-users</id>
   <content type="html">&lt;p&gt;I use mysql and macports on OSX and both were broken when I upgraded to Snow Leopard.&lt;/p&gt;

&lt;p&gt;Mysql was a quick fix:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ln -s /usr/local/mysql-5.1.35-osx10.5-x86 /usr/local/mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(you're installed version might be different).  It turns out a symlink was removed during the Snow Leopard upgrade.&lt;/p&gt;

&lt;p&gt;As for MacPorts, I had to install Xcode from the Snow Leopard CD, install the Snow Leopard version of MacPorts and then follow &lt;a href=&quot;http://trac.macports.org/wiki/Migration&quot;&gt;this migration guide&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>tunneling and mysql</title>
   <link href="http://davedash.com/2008/10/22/tunneling-and-mysql/"/>
   <updated>2008-10-22T00:00:00-07:00</updated>
   <id>http://davedash.com/2008/10/22/tunneling-and-mysql</id>
   <content type="html">&lt;p&gt;I have a mysql server for &lt;a href=&quot;http://reviewsby.us/&quot;&gt;reviewsby.us&lt;/a&gt; that requires tunneling, and after establishing a tunnel like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;deveshistan:~ dash* ssh user@host -L3307:database_server:3306
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I thought I could just do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mysql -u mysql_user -P3307 -h localhost
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But I was wrong, &lt;code&gt;localhost&lt;/code&gt; should be &lt;code&gt;127.0.0.1&lt;/code&gt;, otherwise mysql will do things in socket mode:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mysql -u mysql_user -P3307 -h 127.0.0.1
&lt;/code&gt;&lt;/pre&gt;
</content>
 </entry>
 
 <entry>
   <title>Unicode or How to deal with f'd up text in Django</title>
   <link href="http://davedash.com/2008/02/11/unicode-or-how-to-deal-with-fd-up-text-in-django/"/>
   <updated>2008-02-11T00:00:00-08:00</updated>
   <id>http://davedash.com/2008/02/11/unicode-or-how-to-deal-with-fd-up-text-in-django</id>
   <content type="html">&lt;p&gt;So I've been going out of my mind trying to figure out why something like:&lt;/p&gt;

&lt;p&gt;Pham's would look like Phamâ€™s&lt;/p&gt;

&lt;p&gt;After searching as much as I could into django and unicode, I found &lt;a href=&quot;http://automatthias.wordpress.com/2006/12/10/mysql-encoding-problems-on-dreamhost/&quot;&gt;this article&lt;/a&gt; which worked.&lt;/p&gt;

&lt;p&gt;As near as my feeble USA-centric &quot;cafes not cafés&quot; understood it, my data was encoded incorrectly, and PHP's mysql ignores encoding and expects UTF-8 whereas django tries to understand the encoding.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Coming soon to reviewsby.us</title>
   <link href="http://davedash.com/2006/10/02/coming-soon-to-reviewsbyus/"/>
   <updated>2006-10-02T00:00:00-07:00</updated>
   <id>http://davedash.com/2006/10/02/coming-soon-to-reviewsbyus</id>
   <content type="html">&lt;p&gt;In August I took a break from &lt;a href=&quot;http://reviewsby.us/&quot;&gt;reviewsby.us&lt;/a&gt; only to be plagued by spam.  In September, I relinquished portions of the project planning to my wife.  We haven't released anything publicly, yet, but there's a lot in development.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I updated the development framework to &lt;a href=&quot;http://symfony-project.com/&quot;&gt;symfony&lt;/a&gt; 1.0 alpha and took care of a whole slew of bugs.&lt;/li&gt;
&lt;li&gt;Katie and I came up with a &lt;a href=&quot;http://flickr.com/photos/davedash/251518309/&quot;&gt;wireframe&lt;/a&gt; that details some of the upcoming changes.&lt;/li&gt;
&lt;li&gt;I upgraded the user logic to take advantage of sfGuardUser, a user management plugin for symfony.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I'm in progress of writing location specific searches.  I'm slow to implement.  It seems that this month is far busier than I'd like, and I can rarely get in a block of enough time to just crank this out.  The problem with geographic-specific searches is mySQL supports those types of queries, but it's not as easy as I'd like.  Zend Search Lucene with some support from PHP, however, may yield some promising results.  As always, I'll share my findings in a forthcoming tutorial.&lt;/p&gt;

&lt;p&gt;Anyway, no visible updates on the actual site, since I didn't want to put alpha software on the live site.  I'm sure by next month symfony will be ready.&lt;/p&gt;
</content>
 </entry>
 

</feed>

