1 package DBIx::Class::Version::Table;
2 use base 'DBIx::Class';
6 __PACKAGE__->load_components(qw/ Core/);
7 __PACKAGE__->table('SchemaVersions');
9 __PACKAGE__->add_columns
11 'data_type' => 'VARCHAR',
12 'is_auto_increment' => 0,
13 'default_value' => undef,
14 'is_foreign_key' => 0,
20 'data_type' => 'VARCHAR',
21 'is_auto_increment' => 0,
22 'default_value' => undef,
23 'is_foreign_key' => 0,
24 'name' => 'Installed',
29 __PACKAGE__->set_primary_key('Version');
31 package DBIx::Class::Version;
32 use base 'DBIx::Class::Schema';
36 __PACKAGE__->register_class('Table', 'DBIx::Class::Version::Table');
39 # ---------------------------------------------------------------------------
40 package DBIx::Class::Schema::Versioned;
44 use base 'DBIx::Class';
48 __PACKAGE__->mk_classdata('_filedata');
49 __PACKAGE__->mk_classdata('upgrade_directory');
54 my $vschema = DBIx::Class::Version->connect(@{$self->storage->connect_info()});
55 my $vtable = $vschema->resultset('Table');
58 if(!$self->exists($vtable))
60 # $vschema->storage->debug(1);
61 $vschema->storage->ensure_connected();
67 my $psearch = $vtable->search(undef,
69 { 'max' => 'Installed' },
73 $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'),
75 $pversion = $pversion->Version if($pversion);
77 # warn("Previous version: $pversion\n");
78 if($pversion eq $self->VERSION)
80 warn "This version is already installed\n";
88 $vtable->create({ Version => $self->VERSION,
89 Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime())
91 ## If we let the user do this, where does the Version table get updated?
92 warn "No previous version found, calling deploy to install this version.\n";
97 my $file = $self->ddl_filename(
98 $self->storage->sqlt_type,
99 $self->upgrade_directory,
104 # No upgrade path between these two versions
108 $file = $self->ddl_filename(
109 $self->storage->sqlt_type,
110 $self->upgrade_directory,
114 # $file =~ s/@{[ $self->VERSION ]}/"${pversion}-" . $self->VERSION/e;
117 warn "Upgrade not possible, no upgrade file found ($file)\n";
122 open $fh, "<$file" or warn("Can't open upgrade file, $file ($!)");
123 my @data = split(/;\n/, join('', <$fh>));
125 @data = grep { $_ && $_ !~ /^-- / } @data;
126 @data = grep { $_ !~ /^(BEGIN TRANACTION|COMMIT)/m } @data;
128 $self->_filedata(\@data);
130 ## Don't do this yet, do only on command?
131 ## If we do this later, where does the Version table get updated??
132 warn "Versions out of sync. This is " . $self->VERSION .
133 ", your database contains version $pversion, please call upgrade on your Schema.\n";
134 # $self->upgrade($pversion, $self->VERSION);
139 my ($self, $rs) = @_;
142 $rs->search({ 1, 0 })->count;
144 return 0 if $@ || !defined $c;
152 ## Make each ::DBI::Foo do this
153 $self->storage->backup();
160 ## overridable sub, per default just run all the commands.
164 $self->run_upgrade(qr/create/i);
165 $self->run_upgrade(qr/alter table .*? add/i);
166 $self->run_upgrade(qr/alter table .*? (?!drop)/i);
167 $self->run_upgrade(qr/alter table .*? drop/i);
168 $self->run_upgrade(qr/drop/i);
169 # $self->run_upgrade(qr//i);
171 my $vschema = DBIx::Class::Version->connect(@{$self->storage->connect_info()});
172 my $vtable = $vschema->resultset('Table');
173 $vtable->create({ Version => $self->VERSION,
174 Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime())
181 my ($self, $stm) = @_;
182 # print "Reg: $stm\n";
183 my @statements = grep { $_ =~ $stm } @{$self->_filedata};
184 # print "Statements: ", join("\n", @statements), "\n";
185 $self->_filedata([ grep { $_ !~ /$stm/i } @{$self->_filedata} ]);
189 $self->storage->debugfh->print("$_\n") if $self->storage->debug;
190 # print "Running \n>>$_<<\n";
191 $self->storage->dbh->do($_) or warn "SQL was:\n $_";
201 DBIx::Class::Versioning - DBIx::Class::Schema plugin for Schema upgrades
205 package Library::Schema;
206 use base qw/DBIx::Class::Schema/;
207 # load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD
208 __PACKAGE__->load_classes(qw/CD Book DVD/);
210 __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Versioned/);
211 __PACKAGE__->upgrade_directory('/path/to/upgrades/');
216 # my special backup process
223 ## overridable sub, per default just runs all the commands.
225 $self->run_upgrade(qr/create/i);
226 $self->run_upgrade(qr/alter table .*? add/i);
227 $self->run_upgrade(qr/alter table .*? (?!drop)/i);
228 $self->run_upgrade(qr/alter table .*? drop/i);
229 $self->run_upgrade(qr/drop/i);
230 $self->run_upgrade(qr//i);
235 This module is a component designed to extend L<DBIx::Class::Schema>
236 classes, to enable them to upgrade to newer schema layouts. To use this
237 module, you need to have called C<create_ddl_dir> on your Schema to
238 create your upgrade files to include with your delivery.
240 A table called I<SchemaVersions> is created and maintained by the
241 module. This contains two fields, 'Version' and 'Installed', which
242 contain each VERSION of your Schema, and the date+time it was installed.
244 If you would like to influence which levels of version change need
245 upgrades in your Schema, you can override the method C<ddl_filename>
246 in L<DBIx::Class::Schema>. Return a false value if there is no upgrade
247 path between the two versions supplied. By default, every change in
248 your VERSION is regarded as needing an upgrade.
250 NB: At the moment, SQLite upgrading is rather spotty, as SQL::Translator::Diff
251 returns SQL statements that SQLite does not support.
258 This is an overwritable method which is called just before the upgrade, to
259 allow you to make a backup of the database. Per default this method attempts
260 to call C<< $self->storage->backup >>, to run the standard backup on each
263 This method should return the name of the backup file, if appropriate.
265 C<backup> is called from C<upgrade>, make sure you call it, if you write your
266 own <upgrade> method.
270 This is an overwritable method used to run your upgrade. The freeform method
271 allows you to run your upgrade any way you please, you can call C<run_upgrade>
272 any number of times to run the actual SQL commands, and in between you can
273 sandwich your data upgrading. For example, first run all the B<CREATE>
274 commands, then migrate your data from old to new tables/formats, then
275 issue the DROP commands when you are finished.
279 $self->run_upgrade(qr/create/i);
281 Runs a set of SQL statements matching a passed in regular expression. The
282 idea is that this method can be called any number of times from your
283 C<upgrade> method, running whichever commands you specify via the
284 regex in the parameter.
288 Jess Robinson <castaway@desert-island.demon.co.uk>