Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / scripts / rose_auto_create_model.pl
index 54693cb..2ab34cb 100755 (executable)
@@ -3,8 +3,10 @@
 use strict;
 
 BEGIN {
-  unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
-  push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
+  use FindBin;
+
+  unshift(@INC, $FindBin::Bin . '/../modules/override'); # Use our own versions of various modules (e.g. YAML).
+  push   (@INC, $FindBin::Bin . '/..');                  # '.' will be removed from @INC soon.
 }
 
 use CGI qw( -no_xhtml);
@@ -13,7 +15,7 @@ use Data::Dumper;
 use Digest::MD5 qw(md5_hex);
 use English qw( -no_match_vars );
 use Getopt::Long;
-use List::MoreUtils qw(none);
+use List::MoreUtils qw(apply none uniq);
 use List::UtilsBy qw(partition_by);
 use Pod::Usage;
 use Rose::DB::Object 0.809;
@@ -30,6 +32,8 @@ use SL::LxOfficeConf;
 use SL::DB::Helper::ALL;
 use SL::DB::Helper::Mappings;
 
+chdir($FindBin::Bin . '/..');
+
 my %blacklist     = SL::DB::Helper::Mappings->get_blacklist;
 my %package_names = SL::DB::Helper::Mappings->get_package_names;
 
@@ -70,11 +74,16 @@ our %foreign_key_name_map     = (
     orderitems                => { parts_id => 'part', trans_id => 'order', },
     delivery_order_items      => { parts_id => 'part' },
     invoice                   => { parts_id => 'part' },
-    follow_ups                => { created_for_user => 'created_for', created_by => 'created_by', },
+    follow_ups                => { created_for_user => 'created_for_employee', created_by => 'created_by_employee', },
     follow_up_access          => { who => 'with_access', what => 'to_follow_ups_by', },
 
-    periodic_invoices_configs => { oe_id => 'order' },
+    periodic_invoices_configs => { oe_id => 'order', email_recipient_contact_id => 'email_recipient_contact' },
     reconciliation_links      => { acc_trans_id => 'acc_trans' },
+
+    assembly                  => { parts_id => 'part', id => 'assembly_part' },
+    assortment_items          => { parts_id => 'part' },
+
+    dunning                   => { trans_id => 'invoice', fee_interest_ar_id => 'fee_interest_invoice' },
   },
 );
 
@@ -340,6 +349,22 @@ sub usage {
   pod2usage(verbose => 99, sections => 'SYNOPSIS');
 }
 
+sub list_all_tables {
+  my ($db) = @_;
+
+  my @schemas = (undef, uniq apply { s{\..*}{} } grep { m{\.} } keys %{ $package_names{KIVITENDO} });
+  my @tables;
+
+  foreach my $schema (@schemas) {
+    $db->schema($schema);
+    push @tables, map { $schema ? "${schema}.${_}" : $_ } $db->list_tables;
+  }
+
+  $db->schema(undef);
+
+  return @tables;
+}
+
 sub make_tables {
   my %tables_by_domain;
   if ($config{all}) {
@@ -347,7 +372,7 @@ sub make_tables {
 
     foreach my $domain (@domains) {
       my $db  = SL::DB::create(undef, $domain);
-      $tables_by_domain{$domain} = [ grep { my $table = $_; none { $_ eq $table } @{ $blacklist{$domain} } } $db->list_tables ];
+      $tables_by_domain{$domain} = [ grep { my $table = $_; none { $_ eq $table } @{ $blacklist{$domain} } } list_all_tables($db) ];
       $db->disconnect;
     }
 
@@ -406,8 +431,6 @@ sub drop_and_create_test_database {
     $auth_dbh->disconnect;
 
     dbh_do($dbh_template, "DROP DATABASE \"" . $db_cfg->{db} . "\"", message => "Database could not be dropped");
-
-    $::auth->reset;
   }
 
   notice("Creating database");
@@ -442,6 +465,8 @@ sub drop_and_create_test_database {
 
   apply_upgrades(auth => 1, dbh => $dbh);
 
+  $::auth->reset;
+
   notice("Creating client, user, group and employee");
 
   dbh_do($dbh, qq|DELETE FROM auth.clients|);
@@ -465,7 +490,6 @@ sub drop_and_create_test_database {
     signature                => '',
     hide_cvar_search_options => '',
     numberformat             => '1.000,00',
-    vclimit                  => 0,
     favorites                => '',
     copies                   => '',
     menustyle                => 'v3',