[jifty-devel] Proposed change to SchemaGenerator

Andrew Sterling Hanenkamp andrew.hanenkamp at boomer.com
Thu Feb 22 11:18:43 EST 2007

Before I push this back into Subversion, I want to make sure it's an
okay idea. The patch makes some changes to the behavior of
SchemaGenerator, which is fundamental and I want to clear it first.
One issue I've had with renaming columns has to do with the fact that if
I put something into my model schema like:
column 'tasty' =>
  type is 'boolean',
  is immutable,
  till '0.0.2';
column 'really_tasty' =>
  type is 'boolean',
  is immutable,
  since '0.0.2';

and then I add the rename statement to TestApp::Upgrade:

since '0.0.2' => sub {
    table  => 'TestApp::Model::User',
    column => 'tasty',
    to     => 'really_tasty';

I get an error that looks like:
WARN - Jifty::Handle=HASH(0x1e4e12c) couldn't prepare the query 'CREATE
TABLE users_new_7706 (
  name text NOT NULL  ,
  email text NOT NULL  ,
  really_tasty boolean   ,
  really_tasty boolean   ,
  password text NOT NULL  ,
  created_on datetime  DEFAULT '2007-02-22T14:49:55' 
)'duplicate column name: really_tasty(1) at dbdimp.c line 271

This problem occurs because the SchemaGenerator creates a 'tasty' and a
'really_tasty' column because it has no notion of what an application
version is.

Basically, if I were doing this on a specific production application
instance, upgrade should work just great since the original schema would
have been created without the new column. 

However, if I create the application from scratch (e.g., testing) then
it blows up because both the new and old column are created initially
and then the upgrade attempts to do the above on SQLite. If it were
another driver, it would be an error about using a column rename to an
existing column. This is because SchemaGenerator has no notion of what
an application version is an ignores "till" and "since".

I propose to add a new accessor to Jifty::DBI::SchemaGenerator named
"app_version" and a constructor argument to set the app_version. The
app_version to the constructor is optional. 

If app_version is not given, then it assumes the latest application
version, which means that any column with "till" specified in the schema
will not be created, but all others will be. 

If app_version is given, columns will be created if and only if the
app_version >= since and app_version < till.

This should cause SchemaGenerator to initially match the behavior that
Jifty::Script::Schema gradually applies. Thus, this feature has a very
low impact on the API and behavior and makes Jifty::DBI::SchemaGenerator
behave more consistently with Jifty::Script::Schema.

I've attached a patch including the complete modification with 5
additional subtests to 10schema.t and a slightly modified
Sample::Address schema. The existing tests passed without modification
because the new column in the sample schema gets ignored be default as
it uses "till". The new tests basically just retest the schema
generation with a bunch of different application versions.

This also adds a new dependency on the version pragma.

Andrew Sterling Hanenkamp
Interaction Developer
Boomer Consulting, Inc.
1.785.537.2358 ext. 17
1.888.266.6375 ext. 17
1.785.537.4545 (fax)
610 Humboldt
Manhattan, KS 66502
andrew.hanenkamp at boomer.com <http://www.boomer.com/> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SchemaGeneratorWithVersions.patch
Type: application/octet-stream
Size: 6126 bytes
Desc: SchemaGeneratorWithVersions.patch
Url : http://lists.bestpractical.com/pipermail/jifty-devel/attachments/20070222/9f21c81e/SchemaGeneratorWithVersions.obj

More information about the jifty-devel mailing list