This commit is contained in:
2024-10-14 00:08:40 +02:00
parent dbfba56f66
commit 1462d52e13
4572 changed files with 2658864 additions and 0 deletions

View File

@@ -0,0 +1,602 @@
# --
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::Console::Command::Dev::Tools::Database::RandomDataInsert;
use strict;
use warnings;
use Kernel::System::VariableCheck qw(:all);
use parent qw(Kernel::System::Console::BaseCommand);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::Cache',
'Kernel::System::CustomerUser',
'Kernel::System::CustomerCompany',
'Kernel::System::DB',
'Kernel::System::DynamicField',
'Kernel::System::DynamicField::Backend',
'Kernel::System::Group',
'Kernel::System::Queue',
'Kernel::System::Ticket',
'Kernel::System::Ticket::Article',
'Kernel::System::User',
'Kernel::System::Priority',
);
sub Configure {
my ( $Self, %Param ) = @_;
$Self->Description('Insert random data into the OTRS database for testing purposes.');
$Self->AddOption(
Name => 'generate-tickets',
Description => "Specify how many tickets should be generated.",
Required => 1,
HasValue => 1,
ValueRegex => qr/^\d+$/smx,
);
$Self->AddOption(
Name => 'articles-per-ticket',
Description => "Specify how many articles should be generated per ticket.",
Required => 0,
HasValue => 1,
ValueRegex => qr/^\d+$/smx,
);
$Self->AddOption(
Name => 'generate-users',
Description => "Specify how many users should be generated.",
Required => 0,
HasValue => 1,
ValueRegex => qr/^\d+$/smx,
);
$Self->AddOption(
Name => 'generate-customer-users',
Description => "Specify how many customer users should be generated.",
Required => 0,
HasValue => 1,
ValueRegex => qr/^\d+$/smx,
);
$Self->AddOption(
Name => 'generate-customer-companies',
Description => "Specify how many customer companies should be generated.",
Required => 0,
HasValue => 1,
ValueRegex => qr/^\d+$/smx,
);
$Self->AddOption(
Name => 'generate-groups',
Description => "Specify how many groups should be generated.",
Required => 0,
HasValue => 1,
ValueRegex => qr/^\d+$/smx,
);
$Self->AddOption(
Name => 'generate-queues',
Description => "Specify how many queues should be generated.",
Required => 0,
HasValue => 1,
ValueRegex => qr/^\d+$/smx,
);
$Self->AddOption(
Name => 'mark-tickets-as-seen',
Description => "Specify if the generated tickets should be marked as seen.",
Required => 0,
HasValue => 0,
);
$Self->AdditionalHelp("<red>Please don't use this command in production environments.</red>\n");
return;
}
sub Run {
my ( $Self, %Param ) = @_;
# set dummy sendmail module to avoid notifications
$Kernel::OM->Get('Kernel::Config')->Set(
Key => 'SendmailModule',
Value => 'Kernel::System::Email::DoNotSendEmail',
);
$Kernel::OM->Get('Kernel::Config')->Set(
Key => 'CheckEmailAddresses',
Value => 0,
);
# Turn off persistent cache to speed up the inserts.
$Kernel::OM->Get('Kernel::System::Cache')->Configure(
CacheInMemory => 1,
CacheInBackend => 0,
);
# Refresh common objects after a certain number of loop iterations.
# This will call event handlers and clean up caches to avoid excessive mem usage.
my $CommonObjectRefresh = 50;
# get dynamic fields
my $TicketDynamicField = $Kernel::OM->Get('Kernel::System::DynamicField')->DynamicFieldListGet(
Valid => 1,
ObjectType => ['Ticket'],
);
my $ArticleDynamicField = $Kernel::OM->Get('Kernel::System::DynamicField')->DynamicFieldListGet(
Valid => 1,
ObjectType => ['Article'],
);
# groups
my @GroupIDs;
if ( !$Self->GetOption('generate-groups') ) {
@GroupIDs = GroupGet();
}
else {
@GroupIDs = GroupCreate( $Self->GetOption('generate-groups') );
}
# users
my @UserIDs;
if ( !$Self->GetOption('generate-users') ) {
@UserIDs = UserGet();
}
else {
@UserIDs = UserCreate( $Self->GetOption('generate-users'), \@GroupIDs );
}
# queues
my @QueueIDs;
if ( !$Self->GetOption('generate-queues') ) {
@QueueIDs = QueueGet();
}
else {
@QueueIDs = QueueCreate( $Self->GetOption('generate-queues'), \@GroupIDs );
}
# customer users
if ( $Self->GetOption('generate-customer-users') ) {
CustomerCreate( $Self->GetOption('generate-customer-users') );
}
# customer companies
if ( $Self->GetOption('generate-customer-companies') ) {
CompanyCreate( $Self->GetOption('generate-customer-companies') );
}
my $Counter = 1;
# create tickets
my @TicketIDs;
for ( 1 .. $Self->GetOption('generate-tickets') ) {
my $TicketUserID =
my $TicketID = $Kernel::OM->Get('Kernel::System::Ticket')->TicketCreate(
Title => RandomSubject(),
QueueID => $QueueIDs[ int( rand($#QueueIDs) ) ],
Lock => 'unlock',
Priority => PriorityGet(),
State => 'new',
CustomerNo => int( rand(1000) ),
CustomerUser => RandomAddress(),
OwnerID => $UserIDs[ int( rand($#UserIDs) ) ],
UserID => $UserIDs[ int( rand($#UserIDs) ) ],
);
if ( $Self->GetOption('mark-tickets-as-seen') ) {
# bulk-insert the flags directly for improved performance
my $SQL = 'INSERT INTO ticket_flag (ticket_id, ticket_key, ticket_value, create_time, create_by) VALUES ';
my @Values;
for my $UserID (@UserIDs) {
push @Values, "($TicketID, 'Seen', 1, current_timestamp, $UserID)";
}
while ( my @ValuesPart = splice( @Values, 0, 50 ) ) {
$Kernel::OM->Get('Kernel::System::DB')->Do( SQL => $SQL . join( ',', @ValuesPart ) );
}
}
if ($TicketID) {
print "Ticket with ID '$TicketID' created.\n";
for ( 1 .. $Self->GetOption('articles-per-ticket') // 10 ) {
my $ArticleBackendObject = $Kernel::OM->Get('Kernel::System::Ticket::Article')->BackendForChannel(
ChannelName => 'Internal',
);
my $ArticleID = $ArticleBackendObject->ArticleCreate(
TicketID => $TicketID,
IsVisibleForCustomer => 1,
SenderType => 'customer',
From => RandomAddress(),
To => RandomAddress(),
Cc => RandomAddress(),
Subject => RandomSubject(),
Body => RandomBody(),
ContentType => 'text/plain; charset=ISO-8859-15',
HistoryType => 'AddNote',
HistoryComment => 'Some free text!',
UserID => $UserIDs[ int( rand($#UserIDs) ) ],
NoAgentNotify => 1, # if you don't want to send agent notifications
);
if ( $Self->GetOption('mark-tickets-as-seen') ) {
# bulk-insert the flags directly for improved performance
my $SQL
= 'INSERT INTO article_flag (article_id, article_key, article_value, create_time, create_by) VALUES ';
my @Values;
for my $UserID (@UserIDs) {
push @Values, "($ArticleID, 'Seen', 1, current_timestamp, $UserID)";
}
while ( my @ValuesPart = splice( @Values, 0, 50 ) ) {
$Kernel::OM->Get('Kernel::System::DB')->Do( SQL => $SQL . join( ',', @ValuesPart ) );
}
}
DYNAMICFIELD:
for my $DynamicFieldConfig ( @{$ArticleDynamicField} ) {
next DYNAMICFIELD if !IsHashRefWithData($DynamicFieldConfig);
next DYNAMICFIELD if $DynamicFieldConfig->{ObjectType} ne 'Article';
next DYNAMICFIELD if $DynamicFieldConfig->{InternalField};
# set a random value
my $Result = $Kernel::OM->Get('Kernel::System::DynamicField::Backend')->RandomValueSet(
DynamicFieldConfig => $DynamicFieldConfig,
ObjectID => $ArticleID,
UserID => $UserIDs[ int( rand($#UserIDs) ) ],
);
if ( $Result->{Success} ) {
print "Article with ID '$ArticleID' set dynamic field "
. "$DynamicFieldConfig->{Name}: $Result->{Value}.\n";
}
}
print "New Article '$ArticleID' created for Ticket '$TicketID'.\n";
}
DYNAMICFIELD:
for my $DynamicFieldConfig ( @{$TicketDynamicField} ) {
next DYNAMICFIELD if !IsHashRefWithData($DynamicFieldConfig);
next DYNAMICFIELD if $DynamicFieldConfig->{ObjectType} ne 'Ticket';
next DYNAMICFIELD if $DynamicFieldConfig->{InternalField};
# set a random value
my $Result = $Kernel::OM->Get('Kernel::System::DynamicField::Backend')->RandomValueSet(
DynamicFieldConfig => $DynamicFieldConfig,
ObjectID => $TicketID,
UserID => $UserIDs[ int( rand($#UserIDs) ) ],
);
if ( $Result->{Success} ) {
print "Ticket with ID '$TicketID' set dynamic field "
. "$DynamicFieldConfig->{Name}: $Result->{Value}.\n";
}
}
push( @TicketIDs, $TicketID );
if ( $Counter++ % $CommonObjectRefresh == 0 ) {
$Kernel::OM->ObjectsDiscard(
Objects => ['Kernel::System::Ticket'],
);
}
}
}
return $Self->ExitCodeOk();
}
#
# Helper functions below
#
sub RandomAddress {
my $Name = int( rand(1_000) );
my @Domain = (
'example.com',
'example-sales.com',
'example-service.com',
'example.net',
'example-sales.net',
'example-service.net',
'company.com',
'company-sales.com',
'company-service.com',
'fast-company-example.com',
'fast-company-example-sales.com',
'fast-company-example-service.com',
'slow-company-example.com',
'slow-company-example-sales.com',
'slow-company-example-service.com',
);
return $Name . '@' . $Domain[ int( rand( $#Domain + 1 ) ) ];
}
sub RandomSubject {
my @Text = (
'some subject alalal',
'Re: subject alalal 1234',
'Re: Some Problem with my ...',
'Re: RE: AW: subject with very long lines',
'and we go an very long way to home',
'Ask me about performance',
'What is the real questions?',
'Do not get out of your house!',
'Some other smart subject!',
'Why am I here?',
'No problem, everything is ok!',
'Good morning!',
'Hello again!',
'What a wonderful day!',
'1237891234123412784 2314 test testsetsetset set set',
);
return $Text[ int( rand( $#Text + 1 ) ) ];
}
sub RandomBody {
my $Body = '';
my @Text = (
'some body alalal',
'Re: body alalal 1234',
'and we go an very long way to home',
'here are some other lines with what ever on information',
'Re: RE: AW: body with very long lines body with very long lines body with very long lines body with very long lines ',
'1237891234123412784 2314 test testsetsetset set set',
"alal \t lala",
'Location of the City and County of San Francisco, California',
'The combination of cold ocean water and the high heat of the California mainland create',
' fall, which are the warmest months of the year. Due to its sharp topography',
'climate with little seasonal temperature variation',
'wet winters and dry summers.[31] In addition, since it is surrounded on three sides by water,',
'San Francisco is located on the west coast of the U.S. at the tip of the San Francisco Peninsula',
'Urban planning projects in the 1950s and 1960s saw widespread destruction and redevelopment of westside ',
'The license explicitly separates any kind of "Document" from "Secondary Sections", which may not be integrated with the Document',
'any subject matter itself. While the Document itself is wholly editable',
"\n",
"\r",
'The goal of invariant sections, ever since the 80s when we first made the GNU',
'Manifesto an invariant section in the Emacs Manual, was to make',
'sure they could not be removed. Specifically, to make sure that distributors',
' Emacs that also distribute non-free software could not remove the statements of our philosophy,',
'which they might think of doing because those statements criticize their actions.',
'The definition of a "transparent" format is complicated, and may be difficult to apply. For example, drawings are required to be in a format that allows',
'them to be revised straightforwardly with "some widely available drawing editor." The definition',
'of "widely available" may be difficult to interpret, and may change over time, since, e.g., the open-source',
'Inkscape editor is rapidly maturing, but has not yet reached version 1.0. This section, which was rewritten somewhat between versions 1.1 and 1.2 of the license, uses the terms',
'"widely available" and "proprietary" inconsistently and without defining them. According to a strict interpretation of the license, the references to "generic text editors" ',
'could be interpreted as ruling out any non-human-readable format even if used by an open-source ',
'word-processor; according to a loose interpretation, however, Microsoft Word .doc format could',
'qualify as transparent, since a subset of .doc files can be edited perfectly using ',
'and the format therefore is not one "that can be read and edited only by proprietary word processors.',
'In addition to being an encyclopedic reference, Wikipedia has received major media attention as an online source of breaking',
'news as it is constantly updated.[14][15] When Time Magazine recognized "You" as their Person of',
'the Year 2006, praising the accelerating success of on-line collaboration and interaction by millions',
'of users around the world, Wikipedia was the first particular "Web 2.0" service mentioned.',
'Repeating thoughts and actions is an essential part of learning. ',
'Thinking about a specific memory will make it easy to recall. This is the reason why reviews are',
'such an integral part of education. On first performing a task, it is difficult as there is no path',
'from axon to dendrite. After several repetitions a pathway begins to form and the',
'task becomes easier. When the task becomes so easy that you can perform it at any time, the pathway',
'is fully formed. The speed at which a pathway is formed depends on the individual, but ',
'is usually localised resulting in talents.[citation needed]',
);
for ( 1 .. 50 ) {
$Body .= $Text[ int( rand( $#Text + 1 ) ) ] . "\n";
}
return $Body;
}
sub PriorityGet {
my %PriorityList = $Kernel::OM->Get('Kernel::System::Priority')->PriorityList(
Valid => 1,
);
my @Priorities;
for my $PriorityID ( sort keys %PriorityList ) {
push @Priorities, $PriorityList{$PriorityID};
}
return $Priorities[ int( rand( $#Priorities + 1 ) ) ];
}
sub QueueGet {
my @QueueIDs;
my %Queues = $Kernel::OM->Get('Kernel::System::Queue')->GetAllQueues();
for ( sort keys %Queues ) {
push @QueueIDs, $_;
}
return @QueueIDs;
}
sub QueueCreate {
my $Count = shift || return;
my @GroupIDs = @{ shift() };
my @QueueIDs;
for ( 1 .. $Count ) {
my $Name = 'fill-up-queue' . int( rand(100_000_000) );
my $ID = $Kernel::OM->Get('Kernel::System::Queue')->QueueAdd(
Name => $Name,
ValidID => 1,
GroupID => $GroupIDs[ int( rand( scalar @GroupIDs ) ) ],
FirstResponseTime => 0,
UpdateTime => 0,
SolutionTime => 0,
SystemAddressID => 1,
SalutationID => 1,
SignatureID => 1,
UserID => 1,
MoveNotify => 0,
StateNotify => 0,
LockNotify => 0,
OwnerNotify => 0,
Comment => 'Some Comment',
);
if ($ID) {
print "Queue '$Name' with ID '$ID' created.\n";
push( @QueueIDs, $ID );
}
}
return @QueueIDs;
}
sub GroupGet {
my @GroupIDs;
my %Groups = $Kernel::OM->Get('Kernel::System::Group')->GroupList( Valid => 1 );
for ( sort keys %Groups ) {
push @GroupIDs, $_;
}
return @GroupIDs;
}
sub GroupCreate {
my $Count = shift || return;
my @GroupIDs;
for ( 1 .. $Count ) {
my $Name = 'fill-up-group' . int( rand(100_000_000) );
my $ID = $Kernel::OM->Get('Kernel::System::Group')->GroupAdd(
Name => $Name,
ValidID => 1,
UserID => 1,
);
if ($ID) {
print "Group '$Name' with ID '$ID' created.\n";
push( @GroupIDs, $ID );
# add root to every group
$Kernel::OM->Get('Kernel::System::Group')->PermissionGroupUserAdd(
GID => $ID,
UID => 1,
Permission => {
ro => 1,
move_into => 1,
create => 1,
owner => 1,
priority => 0,
rw => 1,
},
UserID => 1,
);
}
}
return @GroupIDs;
}
sub UserGet {
my @UserIDs;
my %Users = $Kernel::OM->Get('Kernel::System::User')->UserList(
Type => 'Short', # Short|Long
Valid => 1, # not required
);
for ( sort keys %Users ) {
push @UserIDs, $_;
}
return @UserIDs;
}
sub UserCreate {
my $Count = shift || return;
my @GroupIDs = @{ shift() };
my @UserIDs;
for ( 1 .. $Count ) {
my $Name = 'fill-up-user' . int( rand(100_000_000) );
my $ID = $Kernel::OM->Get('Kernel::System::User')->UserAdd(
UserFirstname => "$Name-Firstname",
UserLastname => "$Name-Lastname",
UserLogin => $Name,
UserEmail => $Name . '@example.com',
ValidID => 1,
ChangeUserID => 1,
);
if ($ID) {
print "User '$Name' with ID '$ID' created.\n";
push( @UserIDs, $ID );
for my $GroupID (@GroupIDs) {
my $GroupAdd = int( rand(3) );
if ( $GroupAdd == 2 ) {
$Kernel::OM->Get('Kernel::System::Group')->PermissionGroupUserAdd(
GID => $GroupID,
UID => $ID,
Permission => {
ro => 1,
move_into => 1,
create => 1,
owner => 1,
priority => 0,
rw => 1,
},
UserID => 1,
);
}
elsif ( $GroupAdd == 1 ) {
$Kernel::OM->Get('Kernel::System::Group')->PermissionGroupUserAdd(
GID => $GroupID,
UID => $ID,
Permission => {
ro => 1,
move_into => 1,
create => 1,
owner => 0,
priority => 0,
rw => 0,
},
UserID => 1,
);
}
}
}
}
return @UserIDs;
}
sub CustomerCreate {
my $Count = shift || return;
for ( 1 .. $Count ) {
my $Name = 'fill-up-user' . int( rand(100_000_000) );
my $UserLogin = $Kernel::OM->Get('Kernel::System::CustomerUser')->CustomerUserAdd(
Source => 'CustomerUser', # CustomerUser source config
UserFirstname => $Name,
UserLastname => $Name,
UserCustomerID => $Name,
UserLogin => $Name,
UserEmail => $Name . '@example2.com',
ValidID => 1,
UserID => 1,
);
print "CustomerUser '$Name' created.\n";
}
return;
}
sub CompanyCreate {
my $Count = shift || return;
for ( 1 .. $Count ) {
my $Name = 'fill-up-company' . int( rand(100_000_000) );
my $CustomerID = $Kernel::OM->Get('Kernel::System::CustomerCompany')->CustomerCompanyAdd(
Source => 'CustomerCompany', # CustomerCompany source config
CustomerID => $Name . '_CustomerID',
CustomerCompanyName => $Name,
CustomerCompanyStreet => '5201 Blue Lagoon Drive',
CustomerCompanyZIP => '33126',
CustomerCompanyCity => 'Miami',
CustomerCompanyCountry => 'USA',
CustomerCompanyURL => 'http://www.example.org',
CustomerCompanyComment => 'some comment',
ValidID => 1,
UserID => 1,
);
print "CustomerCompany '$Name' created.\n";
}
return;
}
1;

View File

@@ -0,0 +1,223 @@
# --
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::Console::Command::Dev::Tools::Database::XML2SQL;
use strict;
use warnings;
use parent qw(Kernel::System::Console::BaseCommand);
## nofilter(TidyAll::Plugin::OTRS::Perl::ObjectManagerCreation)
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::DB',
'Kernel::System::Main',
'Kernel::System::XML',
);
sub Configure {
my ( $Self, %Param ) = @_;
$Self->Description('Convert OTRS database XML to SQL.');
$Self->AddOption(
Name => 'database-type',
Description => "Specify the database to generate SQL for (mysql|postgresql|oracle|all).",
Required => 1,
HasValue => 1,
ValueRegex => qr/^(mysql|postgresql|oracle|all)$/smx,
);
$Self->AddOption(
Name => 'source-path',
Description => "Read XML from the specified file (otherwise STDIN will be used).",
Required => 0,
HasValue => 1,
ValueRegex => qr/.*/smx,
);
$Self->AddOption(
Name => 'target-directory',
Description => "Specify the output directory (otherwise the result will be printed on the console).",
Required => 0,
HasValue => 1,
ValueRegex => qr/.*/smx,
);
$Self->AddOption(
Name => 'target-filename',
Description => "Specify the output filename.",
Required => 0,
HasValue => 1,
ValueRegex => qr/.*/smx,
);
$Self->AddOption(
Name => 'split-files',
Description => "Split foreign key creation into a separate (post) SQL file.",
Required => 0,
HasValue => 0,
);
return;
}
sub PreRun {
my ( $Self, %Param ) = @_;
my $TargetDirectory = $Self->GetOption('target-directory');
if ($TargetDirectory) {
if ( !-d $TargetDirectory ) {
die "Directory $TargetDirectory does not exist.\n";
}
my $TargetFilename = $Self->GetOption('target-filename');
if ( !$TargetFilename ) {
die "Please provide the option 'target-filename'.\n";
}
}
my $SourceFilename = $Self->GetOption('source-path');
if ( $SourceFilename && !-r $SourceFilename ) {
die "Source file $SourceFilename does not exist / cannot be read.\n";
}
return;
}
sub Run {
my ( $Self, %Param ) = @_;
my @DatabaseType = ( $Self->GetOption('database-type') );
if ( $Self->GetOption('database-type') eq 'all' ) {
@DatabaseType = qw(mysql postgresql oracle);
}
my $SourceFilename = $Self->GetOption('source-path');
my $SourceXML;
if ($SourceFilename) {
my $FileStringRef = $Kernel::OM->Get('Kernel::System::Main')->FileRead(
Location => $SourceFilename,
Mode => 'utf8',
Type => 'Local',
Result => 'SCALAR',
DisableWarnings => 1,
);
$SourceXML = ${$FileStringRef};
}
else {
# read xml data from STDIN
$SourceXML = do { local $/; <> };
}
for my $DatabaseType (@DatabaseType) {
local $Kernel::OM = Kernel::System::ObjectManager->new();
$Kernel::OM->Get('Kernel::Config')->Set(
Key => 'Database::Type',
Value => $DatabaseType,
);
$Kernel::OM->Get('Kernel::Config')->Set(
Key => 'Database::ShellOutput',
Value => 1,
);
# parse xml package
my @XMLARRAY = $Kernel::OM->Get('Kernel::System::XML')->XMLParse( String => $SourceXML );
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
my $Head = $DBObject->{Backend}->{'DB::Comment'}
. "----------------------------------------------------------\n";
$Head .= $DBObject->{Backend}->{'DB::Comment'}
. " driver: $DatabaseType\n";
$Head .= $DBObject->{Backend}->{'DB::Comment'}
. "----------------------------------------------------------\n";
# get sql from parsed xml
my @SQL;
if ( $DBObject->{Backend}->{'DB::ShellConnect'} ) {
push @SQL, $DBObject->{Backend}->{'DB::ShellConnect'};
}
push @SQL, $DBObject->SQLProcessor( Database => \@XMLARRAY );
# get port sql from parsed xml
my @SQLPost;
if ( $DBObject->{Backend}->{'DB::ShellConnect'} ) {
push @SQLPost, $DBObject->{Backend}->{'DB::ShellConnect'};
}
push @SQLPost, $DBObject->SQLProcessorPost();
my $TargetFilename = $Self->GetOption('target-filename');
my $TargetFilenamePost = $Self->GetOption('target-filename');
if ($TargetFilename) {
$TargetFilename = $Self->GetOption('target-directory') . "/$TargetFilename.$DatabaseType.sql";
$TargetFilenamePost = $Self->GetOption('target-directory') . "/$TargetFilenamePost-post.$DatabaseType.sql";
}
if ( $Self->GetOption('split-files') ) {
# write create script
$Self->Dump(
$TargetFilename,
\@SQL,
$Head,
$DBObject->{Backend}->{'DB::ShellCommit'},
);
# write post script
$Self->Dump(
$TargetFilenamePost,
\@SQLPost,
$Head,
$DBObject->{Backend}->{'DB::ShellCommit'},
);
}
else {
$Self->Dump(
$TargetFilename,
[ @SQL, @SQLPost ],
$Head,
$DBObject->{Backend}->{'DB::ShellCommit'},
);
}
}
return $Self->ExitCodeOk();
}
sub Dump {
my ( $Self, $Filename, $SQL, $Head, $Commit ) = @_;
if ($Filename) {
my $Content = $Head;
for my $Item ( @{$SQL} ) {
$Content .= $Item . $Commit . "\n";
}
$Self->Print("Writing: <yellow>$Filename</yellow>\n");
my $Written = $Kernel::OM->Get('Kernel::System::Main')->FileWrite(
Location => $Filename,
Content => \$Content,
Mode => 'utf8',
Type => 'Local',
);
if ( !$Written ) {
$Self->PrintError("Could not write $Filename.");
}
}
else {
print $Head;
for my $Item ( @{$SQL} ) {
print $Item . $Commit . "\n";
}
}
return 1;
}
1;

View File

@@ -0,0 +1,101 @@
# --
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::Console::Command::Dev::Tools::Database::XMLExecute;
use strict;
use warnings;
use parent qw(Kernel::System::Console::BaseCommand);
our @ObjectDependencies = (
'Kernel::System::DB',
'Kernel::System::Main',
'Kernel::System::XML',
);
sub Configure {
my ( $Self, %Param ) = @_;
$Self->Description('Convert an OTRS database XML file to SQL and execute it in the current database.');
$Self->AddArgument(
Name => 'source-path',
Description => "Specify the location of the database XML file to be executed.",
Required => 1,
ValueRegex => qr/.*/smx,
);
$Self->AddOption(
Name => 'sql-part',
Description => "Generate only 'pre' or 'post' SQL",
Required => 0,
HasValue => 1,
ValueRegex => qr/^(pre|post)$/smx,
);
return;
}
sub PreRun {
my ( $Self, %Param ) = @_;
my $SourcePath = $Self->GetArgument('source-path');
if ( !-r $SourcePath ) {
die "Source file $SourcePath does not exist / is not readable.\n";
}
return;
}
sub Run {
my ( $Self, %Param ) = @_;
my $XML = $Kernel::OM->Get('Kernel::System::Main')->FileRead(
Location => $Self->GetArgument('source-path'),
);
if ( !$XML ) {
$Self->PrintError("Could not read XML source.");
return $Self->ExitCodeError();
}
my @XMLArray = $Kernel::OM->Get('Kernel::System::XML')->XMLParse( String => $XML );
my @SQL = $Kernel::OM->Get('Kernel::System::DB')->SQLProcessor(
Database => \@XMLArray,
);
if ( !@SQL ) {
$Self->PrintError("Could not generate SQL.");
return $Self->ExitCodeError();
}
my @SQLPost = $Kernel::OM->Get('Kernel::System::DB')->SQLProcessorPost();
my $SQLPart = $Self->GetOption('sql-part') || 'both';
my @SQLCollection;
if ( $SQLPart eq 'both' ) {
push @SQLCollection, @SQL, @SQLPost;
}
elsif ( $SQLPart eq 'pre' ) {
push @SQLCollection, @SQL;
}
elsif ( $SQLPart eq 'post' ) {
push @SQLCollection, @SQLPost;
}
for my $SQL (@SQLCollection) {
$Self->Print("$SQL\n");
my $Success = $Kernel::OM->Get('Kernel::System::DB')->Do( SQL => $SQL );
if ( !$Success ) {
$Self->PrintError("Database action failed. Exiting.");
return $Self->ExitCodeError();
}
}
$Self->Print("<green>Done.</green>\n");
return $Self->ExitCodeOk();
}
1;