4 use vars qw( @ISA $DEBUG $me );
6 use DBIx::DBSchema::Table;
7 use DBIx::DBSchema::Column;
8 use FS::Record qw( qsearch qsearchs dbh dbdef );
11 @ISA = qw(FS::Record);
17 FS::reason - Object methods for reason records
23 $record = new FS::reason \%hash;
24 $record = new FS::reason { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::reason object represents a reason message. FS::reason inherits from
37 FS::Record. The following fields are currently supported:
41 =item reasonnum - primary key
43 =item reason_type - index into FS::reason_type
45 =item reason - text of the reason
47 =item disabled - 'Y' or ''
58 Creates a new reason. To add the example to the database, see L<"insert">.
60 Note that this stores the hash reference, not a distinct copy of the hash it
61 points to. You can ask the object for a copy with the I<hash> method.
65 sub table { 'reason'; }
69 Adds this record to the database. If there is an error, returns the error,
70 otherwise returns false.
76 Delete this record from the database.
80 =item replace OLD_RECORD
82 Replaces the OLD_RECORD with this one in the database. If there is an error,
83 returns the error, otherwise returns false.
89 Checks all fields to make sure this is a valid reason. If there is
90 an error, returns the error, otherwise returns false. Called by the insert
99 $self->ut_numbern('reasonnum')
100 || $self->ut_text('reason')
102 return $error if $error;
109 Returns the reason_type (see <I>FS::reason_type</I>) associated with this reason.
114 qsearchs( 'reason_type', { 'typenum' => shift->reason_type } );
119 # Used by FS::Upgrade to migrate to a new database.
123 sub _upgrade_data { # class method
124 my ($self, %opts) = @_;
127 warn "$me upgrading $self\n" if $DEBUG;
129 my $column = dbdef->table($self->table)->column('reason');
130 unless ($column->type eq 'text') { # assume history matches main table
132 # ideally this would be supported in DBIx-DBSchema and friends
133 warn "$me Shifting reason column to type 'text'\n" if $DEBUG;
134 foreach my $table ( $self->table, 'h_'. $self->table ) {
137 $column = dbdef->table($self->table)->column('reason');
138 my $columndef = $column->line($dbh);
139 $columndef =~ s/varchar\(\d+\)/text/i;
141 if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
143 my $notnull = $columndef =~ s/not null//i;
144 push @sql,"ALTER TABLE $table RENAME reason TO freeside_upgrade_reason";
145 push @sql,"ALTER TABLE $table ADD $columndef";
146 push @sql,"UPDATE $table SET reason = freeside_upgrade_reason";
147 push @sql,"ALTER TABLE $table ALTER reason SET NOT NULL"
149 push @sql,"ALTER TABLE $table DROP freeside_upgrade_reason";
151 } elsif ( $dbh->{Driver}->{Name} =~ /^mysql/i ){
153 #crap, this isn't working
154 #push @sql,"ALTER TABLE $table MODIFY reason ". $column->line($dbh);
155 warn "WARNING: reason table upgrade not yet supported for mysql, sorry";
158 die "watchu talkin' 'bout, Willis? (unsupported database type)";
162 my $sth = $dbh->prepare($_) or die $dbh->errstr;
163 $sth->execute or die $sth->errstr;
175 Here be termintes. Don't use on wooden computers.
179 L<FS::Record>, schema.html from the base documentation.