benwaine/Phabric — pull request #35 by jammus (2012-12-09 20:09:32+00:00)
Title: Track inserts in Postgres databases

Hi Ben,

I noticed that `Phabric#reset()` was not deleting rows when using a Postgres database. This is because the underlying driver requires a sequence name to be passed to `lastInsertId()`. If not suppled it returns false which screws up the internal resetMap.

This patch allows an optional **sequence** parameter to be added to a table mapping. For example:

Phabric:
entities:
users:
tableName: 'users'
primaryKey: 'id'
nameCol: 'email'
sequence: 'users_id_seq'

allows changes to be tracked in a table defined as

Table "public.users"
Column | Type | Modifiers
----------+--------+----------------------------------------------------
id | bigint | not null default nextval('users_id_seq'::regclass)
email | text | not null
password | text |
name | text | not null
Indexes:
"users_pkey" PRIMARY KEY, btree (id)

See: http://php.net/manual/en/pdo.lastinsertid.php for some more details.

Keeping this as a config value rather than trying to auto-generate a name seemed like the right approach as, even though Postgres defaults to `[table]_[column]_seq`, I think it's possible to have anything in there.

I also decided to have the internal table mappings omit the sequence key if none was supplied in order to minimise the number of changes required. An alternative would be to default this to a null entry. I'm happy to update if that style is preferred.

Cheers,
James