Back in the SQL Saddle Again

Dark Magic Demand

Dark Magic has a PostgreSQL back-end, and I have to say that it's very nice to be back in the land of SQL after having been fighting with CouchDB and it's problems recently. If Couch just didn't fall over on us and refuse connections, then I'd probably be a little more inclined to use it, but this is getting a little out of hand.

Anyway… I am back in PostgreSQL, and it's great. This morning I just whipped up a little view that didn't take 24 hours to generate, and gave me the latest uploads by division and source that have gone into the database:

  CREATE OR REPLACE VIEW latest_uploads AS
    SELECT d.permalink, s.name, ds.loaded_at
      FROM demand_sets ds, divisions d, SOURCE s,
           ( SELECT source_id, division_id, MAX(loaded_at) AS loaded_at
               FROM demand_sets
             GROUP BY source_id, demand_id ) latest
     WHERE ds.loaded_at = latest.loaded_at
       AND ds.source_id = latest.source_id
       AND ds.division_id = latest.division_id
       AND d.id = ds.division_id
       AND s.id = ds.source_id

After this, I can simply query this view and get the latest of each of the divisions and sources and the time it was loaded. Very nice debugging tool.

More importantly, it's SQL, and that's so much easier to deal with for stable schemas.