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');
50 __PACKAGE__->mk_classdata('backup_directory');
51 __PACKAGE__->mk_classdata('do_backup');
55 my $class = ref($self)||$self;
59 $version = ${"${class}::VERSION"};
66 $self->next::method(@_);
74 $self->{vschema} = DBIx::Class::Version->connect(@{$self->storage->connect_info()});
75 my $vtable = $self->{vschema}->resultset('Table');
78 if(!$self->_source_exists($vtable))
80 # $self->{vschema}->storage->debug(1);
81 $self->{vschema}->storage->ensure_connected();
82 $self->{vschema}->deploy();
87 $pversion = $self->get_db_version();
89 # warn("Previous version: $pversion\n");
90 if($pversion eq $self->schema_version)
92 warn "This version is already installed\n";
100 $vtable->create({ Version => $self->schema_version,
101 Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime())
103 ## If we let the user do this, where does the Version table get updated?
104 warn "No previous version found, calling deploy to install this version.\n";
109 my $file = $self->ddl_filename(
110 $self->storage->sqlt_type,
111 $self->upgrade_directory,
112 $self->schema_version
116 # No upgrade path between these two versions
120 $file = $self->ddl_filename(
121 $self->storage->sqlt_type,
122 $self->upgrade_directory,
123 $self->schema_version,
126 # $file =~ s/@{[ $self->schema_version ]}/"${pversion}-" . $self->schema_version/e;
129 warn "Upgrade not possible, no upgrade file found ($file)\n";
134 open $fh, "<$file" or warn("Can't open upgrade file, $file ($!)");
135 my @data = split(/[;\n]/, join('', <$fh>));
137 @data = grep { $_ && $_ !~ /^-- / } @data;
138 @data = grep { $_ !~ /^(BEGIN TRANACTION|COMMIT)/m } @data;
140 $self->_filedata(\@data);
142 ## Don't do this yet, do only on command?
143 ## If we do this later, where does the Version table get updated??
144 warn "Versions out of sync. This is " . $self->schema_version .
145 ", your database contains version $pversion, please call upgrade on your Schema.\n";
146 # $self->upgrade($pversion, $self->schema_version);
151 my ($self, $rs) = @_;
153 my $vtable = $self->{vschema}->resultset('Table');
154 my $psearch = $vtable->search(undef,
156 { 'max' => 'Installed' },
158 as => ['maxinstall'],
160 my $pversion = $vtable->search({ Installed => $psearch->get_column('maxinstall'),
162 $pversion = $pversion->Version if($pversion);
168 my ($self, $rs) = @_;
171 $rs->search({ 1, 0 })->count;
173 return 0 if $@ || !defined $c;
181 ## Make each ::DBI::Foo do this
182 $self->storage->backup($self->backup_directory());
189 $self->backup() if($self->do_backup);
195 my $vtable = $self->{vschema}->resultset('Table');
196 $vtable->create({ Version => $self->schema_version,
197 Installed => strftime("%Y-%m-%d %H:%M:%S", gmtime())
206 ## overridable sub, per default just run all the commands.
207 $self->run_upgrade(qr/create/i);
208 $self->run_upgrade(qr/alter table .*? add/i);
209 $self->run_upgrade(qr/alter table .*? (?!drop)/i);
210 $self->run_upgrade(qr/alter table .*? drop/i);
211 $self->run_upgrade(qr/drop/i);
216 my ($self, $stm) = @_;
217 # print "Reg: $stm\n";
218 my @statements = grep { $_ =~ $stm } @{$self->_filedata};
219 # print "Statements: ", join("\n", @statements), "\n";
220 $self->_filedata([ grep { $_ !~ /$stm/i } @{$self->_filedata} ]);
224 $self->storage->debugobj->query_start($_) if $self->storage->debug;
225 $self->storage->dbh->do($_) or warn "SQL was:\n $_";
226 $self->storage->debugobj->query_end($_) if $self->storage->debug;
236 DBIx::Class::Schema::Versioned - DBIx::Class::Schema plugin for Schema upgrades
240 package Library::Schema;
241 use base qw/DBIx::Class::Schema/;
242 # load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD
243 __PACKAGE__->load_classes(qw/CD Book DVD/);
245 __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Versioned/);
246 __PACKAGE__->upgrade_directory('/path/to/upgrades/');
247 __PACKAGE__->backup_directory('/path/to/backups/');
252 # my special backup process
259 ## overridable sub, per default just runs all the commands.
261 $self->run_upgrade(qr/create/i);
262 $self->run_upgrade(qr/alter table .*? add/i);
263 $self->run_upgrade(qr/alter table .*? (?!drop)/i);
264 $self->run_upgrade(qr/alter table .*? drop/i);
265 $self->run_upgrade(qr/drop/i);
266 $self->run_upgrade(qr//i);
271 This module is a component designed to extend L<DBIx::Class::Schema>
272 classes, to enable them to upgrade to newer schema layouts. To use this
273 module, you need to have called C<create_ddl_dir> on your Schema to
274 create your upgrade files to include with your delivery.
276 A table called I<SchemaVersions> is created and maintained by the
277 module. This contains two fields, 'Version' and 'Installed', which
278 contain each VERSION of your Schema, and the date+time it was installed.
280 If you would like to influence which levels of version change need
281 upgrades in your Schema, you can override the method C<ddl_filename>
282 in L<DBIx::Class::Schema>. Return a false value if there is no upgrade
283 path between the two versions supplied. By default, every change in
284 your VERSION is regarded as needing an upgrade.
286 The actual upgrade is called manually by calling C<upgrade> on your
287 schema object. Code is run at connect time to determine whether an
288 upgrade is needed, if so, a warning "Versions out of sync" is
291 NB: At the moment, SQLite upgrading is rather spotty, as SQL::Translator::Diff
292 returns SQL statements that SQLite does not support.
299 This is an overwritable method which is called just before the upgrade, to
300 allow you to make a backup of the database. Per default this method attempts
301 to call C<< $self->storage->backup >>, to run the standard backup on each
304 This method should return the name of the backup file, if appropriate..
308 This is the main upgrade method which calls the overridable do_upgrade and
309 also handles the backups and updating of the SchemaVersion table.
313 This is an overwritable method used to run your upgrade. The freeform method
314 allows you to run your upgrade any way you please, you can call C<run_upgrade>
315 any number of times to run the actual SQL commands, and in between you can
316 sandwich your data upgrading. For example, first run all the B<CREATE>
317 commands, then migrate your data from old to new tables/formats, then
318 issue the DROP commands when you are finished.
322 $self->run_upgrade(qr/create/i);
324 Runs a set of SQL statements matching a passed in regular expression. The
325 idea is that this method can be called any number of times from your
326 C<upgrade> method, running whichever commands you specify via the
327 regex in the parameter.
329 =head2 upgrade_directory
331 Use this to set the directory your upgrade files are stored in.
333 =head2 backup_directory
335 Use this to set the directory you want your backups stored in.
337 =head2 schema_version
339 Returns the current schema class' $VERSION; does -not- use $schema->VERSION
340 since that varies in results depending on if version.pm is installed, and if
341 so the perl or XS versions. If you want this to change, bug the version.pm
342 author to make vpp and vxs behave the same.
346 Jess Robinson <castaway@desert-island.demon.co.uk>