Line 0
Link Here
|
|
|
1 |
use Modern::Perl; |
2 |
use Koha::Installer::Output qw(say_warning say_failure say_success say_info); |
3 |
|
4 |
return { |
5 |
bug_number => "38489", |
6 |
description => "Migrate EDI transport configuration to use the new file_transports system", |
7 |
up => sub { |
8 |
my ($args) = @_; |
9 |
my ( $dbh, $out ) = @$args{qw(dbh out)}; |
10 |
|
11 |
# Check if file_transports table exists (should exist from Bug 39190) |
12 |
my $sth = $dbh->prepare("SHOW TABLES LIKE 'file_transports'"); |
13 |
$sth->execute(); |
14 |
if ( !$sth->fetchrow_array ) { |
15 |
say_failure( $out, "file_transports table not found. Please ensure Bug 39190 is applied first." ); |
16 |
return; |
17 |
} |
18 |
|
19 |
# Add file_transport_id column to vendor_edi_accounts |
20 |
if ( !column_exists( 'vendor_edi_accounts', 'file_transport_id' ) ) { |
21 |
$dbh->do( |
22 |
q{ |
23 |
ALTER TABLE vendor_edi_accounts |
24 |
ADD COLUMN file_transport_id int(11) DEFAULT NULL AFTER plugin, |
25 |
ADD KEY `vendor_edi_accounts_file_transport_id` (`file_transport_id`), |
26 |
ADD CONSTRAINT `vendor_edi_accounts_ibfk_file_transport` |
27 |
FOREIGN KEY (`file_transport_id`) REFERENCES `file_transports` (`file_transport_id`) |
28 |
ON DELETE SET NULL ON UPDATE CASCADE |
29 |
} |
30 |
); |
31 |
say_success( $out, "Added file_transport_id column to vendor_edi_accounts table" ); |
32 |
} |
33 |
|
34 |
# Add 'local' to the transport enum in file_transports table |
35 |
$dbh->do( |
36 |
q{ |
37 |
ALTER TABLE file_transports |
38 |
MODIFY COLUMN transport ENUM('ftp','sftp','local') NOT NULL DEFAULT 'sftp' |
39 |
} |
40 |
); |
41 |
say_success( $out, "Added 'local' transport type to file_transports table" ); |
42 |
|
43 |
# Migrate existing EDI transport configurations to file_transports |
44 |
my $migration_count = 0; |
45 |
my $edi_accounts_sth = $dbh->prepare( |
46 |
q{ |
47 |
SELECT id, description, host, username, password, upload_port, download_port, |
48 |
upload_directory, download_directory, transport |
49 |
FROM vendor_edi_accounts |
50 |
WHERE file_transport_id IS NULL |
51 |
AND host IS NOT NULL |
52 |
AND host != '' |
53 |
} |
54 |
); |
55 |
$edi_accounts_sth->execute(); |
56 |
|
57 |
my $insert_transport_sth = $dbh->prepare( |
58 |
q{ |
59 |
INSERT INTO file_transports (name, host, port, transport, user_name, password, |
60 |
upload_directory, download_directory, auth_mode, passive, debug) |
61 |
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'password', 1, 0) |
62 |
} |
63 |
); |
64 |
|
65 |
my $update_edi_account_sth = $dbh->prepare( |
66 |
q{ |
67 |
UPDATE vendor_edi_accounts SET file_transport_id = ? WHERE id = ? |
68 |
} |
69 |
); |
70 |
|
71 |
while ( my $row = $edi_accounts_sth->fetchrow_hashref ) { |
72 |
|
73 |
# Determine appropriate port (default to SFTP=22, FTP=21, Local=NULL) |
74 |
my $port = $row->{upload_port} || $row->{download_port}; |
75 |
unless ($port) { |
76 |
if ( uc( $row->{transport} ) eq 'SFTP' ) { |
77 |
$port = 22; |
78 |
} elsif ( uc( $row->{transport} ) eq 'FILE' ) { |
79 |
$port = undef; # Local transport doesn't use ports |
80 |
} else { |
81 |
$port = 21; # FTP default |
82 |
} |
83 |
} |
84 |
|
85 |
# Map transport type (normalize case and handle FILE -> local) |
86 |
my $transport_type = lc( $row->{transport} ); |
87 |
$transport_type = 'local' if $transport_type eq 'file'; |
88 |
$transport_type = 'ftp' unless $transport_type =~ /^(sftp|local)$/; |
89 |
|
90 |
# Create transport name from EDI account description |
91 |
my $transport_name = sprintf( "EDI Transport for %s", $row->{description} ); |
92 |
|
93 |
# Handle host for local transport |
94 |
my $host = $row->{host}; |
95 |
$host = 'localhost' if $transport_type eq 'local' && !$host; |
96 |
|
97 |
# Insert new file transport |
98 |
$insert_transport_sth->execute( |
99 |
$transport_name, |
100 |
$host, |
101 |
$port, |
102 |
$transport_type, |
103 |
$row->{username}, |
104 |
$row->{password}, # Password is already encrypted in EDI accounts |
105 |
$row->{upload_directory}, |
106 |
$row->{download_directory} |
107 |
); |
108 |
|
109 |
my $transport_id = $dbh->last_insert_id( undef, undef, 'file_transports', 'file_transport_id' ); |
110 |
|
111 |
# Update EDI account to reference the new transport |
112 |
$update_edi_account_sth->execute( $transport_id, $row->{id} ); |
113 |
|
114 |
$migration_count++; |
115 |
} |
116 |
|
117 |
if ( $migration_count > 0 ) { |
118 |
say_success( |
119 |
$out, |
120 |
"Successfully migrated $migration_count EDI transport configurations to file_transports" |
121 |
); |
122 |
} else { |
123 |
say_info( $out, "No EDI transport configurations found to migrate" ); |
124 |
} |
125 |
|
126 |
# Drop the old transport-related columns from vendor_edi_accounts |
127 |
my @columns_to_drop = |
128 |
qw(host username password upload_port download_port upload_directory download_directory transport); |
129 |
|
130 |
for my $column (@columns_to_drop) { |
131 |
if ( column_exists( 'vendor_edi_accounts', $column ) ) { |
132 |
$dbh->do("ALTER TABLE vendor_edi_accounts DROP COLUMN $column"); |
133 |
say_success( $out, "Dropped column '$column' from vendor_edi_accounts table" ); |
134 |
} |
135 |
} |
136 |
|
137 |
say_success( $out, "EDI transport migration completed successfully" ); |
138 |
}, |
139 |
}; |