Skip to content

Instantly share code, notes, and snippets.

View boutell's full-sized avatar

Tom Boutell boutell

View GitHub Profile
@boutell
boutell / pages_yml_ideas.yml
Created January 24, 2012 20:06
Structuring information about page templates and their contents in Apostrophe (2?)
templates:
home:
areas:
header
body
footer
slots:
splash:
type: aImage
logo:
@boutell
boutell / gist:1811024
Created February 12, 2012 21:51
Executing the same query in MongoDB and MySQL
I'm going to examine a plausible, nontrivial query that comes up for client sites and
consider how it would be implemented in both MongoDB and a MySQL back end that attempts
to support a lot of the capabilities we like in MongoDB.
I'm not going to look at how we retrieve the inner contents of a page (nested contents)
or information about subpages (related contents) because I have a pretty good idea how
we want to cope with those bits in both cases.
The query syntax here is just pseudocode, I'm not proposing it:
@boutell
boutell / profile.php
Created April 7, 2012 14:11
Profile CPU performance at 1-minute intervals, compare it to reported load average
<?php
$file = dirname(__FILE__) . '/profile.csv';
$out = fopen($file, 'a');
$load = @file_get_contents('/proc/loadavg');
$load = preg_split('/ /', $load);
$load = $load[0];
@boutell
boutell / mongoose-unique-index-error-test.js
Created April 14, 2012 15:15
Test: retrying save after a unique index error does not work
/**
* Create a situation where a unique index error will occur. Try to save the
* object again after making the relevant field unique. Expected behavior:
* object saves second (or third...) time and we wind up with ten objects. Actual behavior:
* we get just one object and there is a unique index error after all ten save callbacks
* have already been invoked "successfully"! This is really strange, even stranger than I
* thought. Maybe the index is not applied yet somehow and I need to wait on the schema
* being ready?
*
@boutell
boutell / mongoose-save-error-test-2.js
Created April 14, 2012 19:52
New Mongoose retry-when-save-fails test
/**
* Create a situation where a unique index error will occur. Try to save the
* object again after making the relevant field unique. Then do an update on one of the objects.
*
* Expected behavior: each object after the first saves successfully the second (or third...) time and we wind
* up with ten objects. Actual behavior with current Mongoose: we wind up with one object because the
* isNew flag is never reset to true when an insert fails. Behavior with my pull request: the isNew flag
* is reset if an insert (not an update) fails, allowing save handlers to try again on error after doing things
* like making slugs more unique.
*
@boutell
boutell / short_slugs_for_mongoose.js
Created April 15, 2012 01:42
Guarantee short yet unique slugs for any model
// Extend any Mongoose model that has a unique index on a 'slug' field so that
// if a unique index error occurs on 'slug', a random digit is added to the slug
// and the save operation is retried until it works. This is concurrency-safe even
// if you have lots of inserts going on from multiple machines etc.
//
// By Tom Boutell, tom@punkave.com
//
// NOTE: YOU MUST HAVE THIS PULL REQUEST... https://github.com/LearnBoost/mongoose/pull/837
//
// (As of this writing that commit is not in LearnBoost/mongoose yet.)
@boutell
boutell / gist:2764357
Created May 21, 2012 20:10
Rasmus test
<?php
define('NUM_TESTS', 1000);
$before = memory_get_usage(true);
$test = array();
class Foo
{
macintosh-5:~ boutell$ set | grep PATH
MANPATH=/opt/local/share/man:
PATH=/Users/boutell/mongodb/bin:/usr/local/bin:.:/Users/boutell/bin:/Users/boutell/.gem/bin:/opt/symfony1.4/data/bin:/opt/local/bin:/opt/local/sbin:/opt/local/apache2/bin:/Users/boutell/personalassistant/festival/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/pktools/bin
macintosh-5:~ boutell$
@boutell
boutell / gist:3151698
Created July 20, 2012 16:30
Count rows without throwing an exception if there are none
// If there are no matches, getSingleScalarResult will throw an exception
try
{
$this->numResults = $countQb->getQuery()->getSingleScalarResult();
} catch (\Doctrine\ORM\NoResultException $e)
{
// This happens when zero rows are returned
$this->numResults = 0;
}
@boutell
boutell / gist:3183250
Created July 26, 2012 17:06
Deleting search objects related many-to-one to a given posting
// In my Search entity class:
/**
* @ORM\ManyToOne(targetEntity="Posting", inversedBy="words")
* @ORM\JoinColumn(name="posting_id", referencedColumnName="id", onDelete="cascade")
*/
protected $posting;
// Later, when I want to delete the Search objects for a given Posting entity: