first pass RT4 merge, RT#13852
[freeside.git] / rt / etc / upgrade / 3.9.7 / content
1 my $move_attributes = sub {
2     my ($table, $type, $column) = @_;
3     my $query = "UPDATE $table SET $column = (SELECT Content FROM Attributes WHERE"
4         ." Name = ? AND ObjectType = ? AND $table.id = Attributes.ObjectId)";
5
6     my $res = $RT::Handle->SimpleQuery( $query, $column, $type );
7     unless ( $res ) {
8         $RT::Logger->error("Failed to move $column on $type from Attributes into $table table");
9         return;
10     }
11
12     $query = 'DELETE FROM Attributes WHERE Name = ? AND ObjectType = ?';
13     $res = $RT::Handle->SimpleQuery( $query, $column, $type );
14     unless ( $res ) {
15         $RT::Logger->error("Failed to delete $column on $type from Attributes");
16         return;
17     }
18     return 1;
19 };
20
21 @Initial = (
22     sub {
23         return $move_attributes->( 'Users', 'RT::User', 'AuthToken');
24     },
25     sub {
26         return $move_attributes->( 'CustomFields', 'RT::CustomField', 'RenderType');
27     },
28     sub {
29         my $cfs = RT::CustomFields->new($RT::SystemUser);
30         $cfs->UnLimit;
31         $cfs->FindAllRows;
32         while ( my $cf = $cfs->Next ) {
33             # Explicitly remove 'ORDER BY id asc' to emulate the
34             # previous functionality, where Pg might return the the
35             # rows in arbitrary order
36             $cf->Attributes->OrderByCols();
37
38             my $attr = $cf->FirstAttribute('BasedOn');
39             next unless $attr;
40             $cf->SetBasedOn($attr->Content);
41         }
42         $query = 'DELETE FROM Attributes WHERE Name = ? AND ObjectType = ?';
43         $res = $RT::Handle->SimpleQuery( $query, 'BasedOn', 'RT::CustomField' );
44         unless ( $res ) {
45             $RT::Logger->error("Failed to delete BasedOn CustomFields from Attributes");
46             return;
47         }
48         return 1;
49     },
50     sub {
51         $move_attributes->( 'CustomFields', 'RT::CustomField', 'ValuesClass')
52             or return;
53
54         my $query = "UPDATE CustomFields SET ValuesClass = NULL WHERE ValuesClass = ?";
55         my $res = $RT::Handle->SimpleQuery( $query, 'RT::CustomFieldValues' );
56         unless ( $res ) {
57             $RT::Logger->error("Failed to replace default with NULLs");
58             return;
59         }
60         return 1;
61     },
62     sub {
63         my $attr = RT->System->FirstAttribute('BrandedSubjectTag');
64         return 1 unless $attr;
65
66         my $map = $attr->Content || {};
67         while ( my ($qid, $tag) = each %$map ) {
68             my $queue = RT::Queue->new( RT->SystemUser );
69             $queue->Load( $qid );
70             unless ( $queue->id ) {
71                 $RT::Logger->warning("Couldn't load queue #$qid. Skipping...");
72                 next;
73             }
74
75             my ($status, $msg) = $queue->SetSubjectTag($tag);
76             unless ( $status ) {
77                 $RT::Logger->error("Couldn't set subject tag for queue #$qid: $msg");
78                 next;
79             }
80         }
81     },
82 );