Skip to content

Gentoo Portage Alternative

I just came across an alternative to the http://gentoo-portage.com and http://packages.gentoo.org sites: http://znurt.org/

Nice, clean, beautiful and full of information.

Categories: Gentoo.

Tags:

The Sluggish Speed of Gentoo

Gentoo, ffs, get PHP 5.3.1 in Portage ASAP. PHP 5.3 isn’t even in there, yet. What’s the use of your package manager if I just have to manually install stuff?

/rant

Categories: Uncategorized.

Tags: ,

Automatically Setting Modified Date Database Fields

Most of my database tables always have four columns in common:

created_on timestamp
created_by_id int
last_modified_on timestamp
last_modified_by_id int

These columns should be pretty self explanatory. Usually created_on has a default of now() to auto populate it. Populating the last_modified_on column is a little more difficult, especially if you don’t want it to be populated on INSERT (which is what using default now()) would do.

Of course we could just provide for it in our code, especially if using a trusty application framework that takes care of it for us, but I like putting basic logic in the database, and we can’t assume all data updates will come from our application. They could, for example, come from a command line update.

I crafted this quick little database trigger function for taking care of this for me:

CREATE OR REPLACE FUNCTION auto_update_last_modified()
  RETURNS TRIGGER AS
$BODY$
DECLARE
BEGIN
	NEW.last_modified_on:= now();
 
	RETURN NEW;
END;
$BODY$
  LANGUAGE 'plpgsql';

This function simply populates the last_modified_on column of NEW (the new record being updated) with the current timestamp. Now I can add a trigger to every table that has this column:

CREATE TRIGGER auto_update_table_name_last_modified
  BEFORE UPDATE
  ON table_name
  FOR EACH ROW
  EXECUTE PROCEDURE auto_update_last_modified();

And now when updating a record in table_name, it will automatically populate a value for last_modified_on.

Categories: PostgreSQL.

Tags:

User Time Zones in PHP

When writing applications that are meant to be used by users the world over (not just users within a localized network), it’s important to be able to show dates and times relative to the users time zone. If one user posts something at 11:30 EST, and a few minutes later, a user in California checks the post, it would look as if it were made in the future. Not cool.

Lucky, it’s quite easy to localize dates and times using PHP’s built in date classes.

Continued…

Categories: PHP.

Tags:

PostgreSQL 8.4.2-r1 on Gentoo

A new version of PostgreSQL, 8.4.2-r1, is available in Gentoo Portage, but be careful. It appears the server is compiled with HAVE_INT64_TIMESTAMP by default, something not true with previous versions. Because of this, after updating, you might get an error about the data cluster being incompatibile with the server when you restart, unless you were using the pg-intdatetime use flag in previous versions (which has been removed in the newest version).

Continued…

Categories: PostgreSQL.

Tags:

Inheritance in PostgreSQL

Another cool feature I’ve recently discovered in PostgreSQL, although one available and documented for a long time, is table inheritance, which works almost exactly like object inheritance in object oriented programming. This simply means that you can have tables inheriting columns from parent tables.

Continued…

Categories: PostgreSQL.

Tags:

Subversion Commit Logs to Twitter

Twitvn is a small Python script that, when used as a Subversion post-commit hook, will publish commit messages to a Twitter account: http://code.google.com/p/twitvn/

Why would anyone want to do this?

There are many reasons. It would help foster collaboration on a multi-member project, either allowing them to follow the Twitter account with their own, or via Twitter’s RSS feed. It could also allow interested users of the project to follow status updates of what is going on with the project.

Finally, it’s just a damn fun idea.

Categories: Uncategorized.

Tags: ,

Removing Windows Newline Characters in Vim

Sometimes when working on projects in both Linux and Windows, a file will end up with Windows newline characters, which show up as ^M in Vim. Annoying as this is, there’s a quick fix to remove them in Vim using a regular expression replace:

:%s/<control-v><control-m>//g

Magically, all the ugly Windows newline characters are gone.

Categories: Uncategorized.

Tags: ,

Returning in PostgreSQL

I recently came across an amazing feature in PostgreSQL: the RETURNING clause. This clause allows you to return specific data as the result of an INSERT or UPDATE query.

Often, after doing one of these queries, I’m interested in getting some data, such as an autogenerated primary key field, or maybe some defaulted data (like a created timestamp). Normally, one would have to do an additional query to get this data. Using RETURNING, we can do our data manipulation and retrieval in the same query.

Example:

-- Return just the ID:
 
INSERT INTO books(name, author) VALUES('Awesome Book', 'Smart Author') 
    RETURNING book_id;
 
-- Return more than one column:
 
INSERT INTO books(name, author) VALUES ('Sweet Book', 'Other Author') 
    RETURNING book_id, created_on;
 
-- Return all the columns:
 
INSERT INTO books(name, author) VALUES ('Silly Book', 'Airhead Author') 
    RETURNING *;

In our code, we can simply treat the return value of the INSERT or UPDATE code like we would a SELECT statement.

Some things to keep in mind, though:

  1. If the UPDATE query affects multiple rows, multiple rows of data will be returned. If no rows are affected, none will be returned.
  2. Normally the database would return the number of rows affected, but obviously we’re overriding the return value of the query. Obviously we can check the number of rows returned by the query (like we would with a SELECT) to get around this.

Categories: PostgreSQL.

Tags:

A Complete, Polished Look for Gnome

I’m not afraid to admit it: I’m a fan of Gnome. I think KDE looks to cartoonish. I actually prefer using OpenBox with a slew of lightweight apps for window manager addons, but that because too much to maintain, and I have better things to do with my time.

It’s hard to find a nice, clean set of decorations for Gnome that mesh well together. GDM theme, icons, wallpapers, GTK themes, etc that all blend well together to create a complete, polished look.

Recently I found the “Colors” theme set on Gnome Look.

GDM and Wallpapers: http://www.gnome-look.org/content/show.php/Arc-Colors+GDM-Walls?content=88305
GTK: http://www.gnome-look.org/content/show.php/Shiki-Colors?content=86717
Icons: http://www.gnome-look.org/content/show.php/GNOME-colors?content=82562

Nice and simple.

Categories: Uncategorized.

Tags: ,