Revision 6a30f836
Von Sven Schöling vor mehr als 9 Jahren hinzugefügt
scripts/rose_auto_create_model.pl | ||
---|---|---|
23 | 23 |
use SL::DBUtils; |
24 | 24 |
use SL::DB; |
25 | 25 |
use SL::Form; |
26 |
use SL::InstanceConfiguration; |
|
26 | 27 |
use SL::Locale; |
27 | 28 |
use SL::LXDebug; |
28 | 29 |
use SL::LxOfficeConf; |
... | ... | |
37 | 38 |
our %lx_office_conf; |
38 | 39 |
|
39 | 40 |
our $script = __FILE__; |
40 |
$script =~ s:.*/::;
|
|
41 |
$script =~ s{.*/}{};
|
|
41 | 42 |
|
42 | 43 |
$OUTPUT_AUTOFLUSH = 1; |
43 | 44 |
$Data::Dumper::Sortkeys = 1; |
... | ... | |
80 | 81 |
|
81 | 82 |
SL::LxOfficeConf->read; |
82 | 83 |
|
83 |
my $client = $config{client} || $::lx_office_conf{devel}{client}; |
|
84 |
my $client = $config{client} || $::lx_office_conf{devel}{client}; |
|
85 |
my $new_client = $config{new_client}; |
|
84 | 86 |
|
85 |
if (!$client) { |
|
87 |
if (!$client && !$new_client) {
|
|
86 | 88 |
error("No client found in config. Please provide a client:"); |
87 | 89 |
usage(); |
88 | 90 |
} |
89 | 91 |
|
90 |
$::lxdebug = LXDebug->new(); |
|
91 |
$::locale = Locale->new("de"); |
|
92 |
$::form = new Form; |
|
93 |
$form->{script} = 'rose_meta_data.pl'; |
|
94 |
$::auth = SL::Auth->new(); |
|
92 |
$::lxdebug = LXDebug->new(); |
|
93 |
$::lxdebug->disable_sub_tracing; |
|
94 |
$::locale = Locale->new("de"); |
|
95 |
$::form = new Form; |
|
96 |
$::instance_conf = SL::InstanceConfiguration->new; |
|
97 |
$form->{script} = 'rose_meta_data.pl'; |
|
98 |
|
|
99 |
if ($new_client) { |
|
100 |
$::auth = SL::Auth->new(unit_tests_database => 1); |
|
101 |
$client = 1; |
|
102 |
drop_and_create_test_database(); |
|
103 |
} else { |
|
104 |
$::auth = SL::Auth->new(); |
|
105 |
} |
|
95 | 106 |
|
96 | 107 |
if (!$::auth->set_client($client)) { |
97 | 108 |
error("No client with ID or name '$client' found in config. Please provide a client:"); |
... | ... | |
280 | 291 |
my ($options) = @_; |
281 | 292 |
GetOptions( |
282 | 293 |
'client=s' => \ my $client, |
294 |
'test-client' => \ my $use_test_client, |
|
283 | 295 |
all => \ my $all, |
284 | 296 |
'db=s' => \ my $db, |
285 | 297 |
'no-commit|dry-run' => \ my $nocommit, |
... | ... | |
288 | 300 |
diff => \ my $diff, |
289 | 301 |
); |
290 | 302 |
|
291 |
$options->{client} = $client; |
|
292 |
$options->{all} = $all; |
|
293 |
$options->{db} = $db; |
|
294 |
$options->{nocommit} = $nocommit; |
|
295 |
$options->{quiet} = $quiet; |
|
296 |
$options->{color} = -t STDOUT ? 1 : 0; |
|
303 |
$options->{client} = $client; |
|
304 |
$options->{new_client} = $use_test_client; |
|
305 |
$options->{all} = $all; |
|
306 |
$options->{db} = $db; |
|
307 |
$options->{nocommit} = $nocommit; |
|
308 |
$options->{quiet} = $quiet; |
|
309 |
$options->{color} = -t STDOUT ? 1 : 0; |
|
297 | 310 |
|
298 | 311 |
if ($diff) { |
299 | 312 |
if (eval { require Text::Diff; 1 }) { |
... | ... | |
374 | 387 |
} |
375 | 388 |
} |
376 | 389 |
|
390 |
sub drop_and_create_test_database { |
|
391 |
my $db_cfg = $::lx_office_conf{'testing/database'} || die 'testing/database missing'; |
|
392 |
|
|
393 |
my @dbi_options = ( |
|
394 |
'dbi:Pg:dbname=' . $db_cfg->{template} . ';host=' . $db_cfg->{host} . ';port=' . $db_cfg->{port}, |
|
395 |
$db_cfg->{user}, |
|
396 |
$db_cfg->{password}, |
|
397 |
SL::DBConnect->get_options, |
|
398 |
); |
|
399 |
|
|
400 |
$::auth->reset; |
|
401 |
my $dbh_template = SL::DBConnect->connect(@dbi_options) || BAIL_OUT("No database connection to the template database: " . $DBI::errstr); |
|
402 |
my $auth_dbh = $::auth->dbconnect(1); |
|
403 |
|
|
404 |
if ($auth_dbh) { |
|
405 |
notice("Database exists; dropping"); |
|
406 |
$auth_dbh->disconnect; |
|
407 |
|
|
408 |
dbh_do($dbh_template, "DROP DATABASE \"" . $db_cfg->{db} . "\"", message => "Database could not be dropped"); |
|
409 |
|
|
410 |
$::auth->reset; |
|
411 |
} |
|
412 |
|
|
413 |
notice("Creating database"); |
|
414 |
|
|
415 |
dbh_do($dbh_template, "CREATE DATABASE \"" . $db_cfg->{db} . "\" TEMPLATE \"" . $db_cfg->{template} . "\" ENCODING 'UNICODE'", message => "Database could not be created"); |
|
416 |
$dbh_template->disconnect; |
|
417 |
|
|
418 |
notice("Creating initial schema"); |
|
419 |
|
|
420 |
@dbi_options = ( |
|
421 |
'dbi:Pg:dbname=' . $db_cfg->{db} . ';host=' . $db_cfg->{host} . ';port=' . $db_cfg->{port}, |
|
422 |
$db_cfg->{user}, |
|
423 |
$db_cfg->{password}, |
|
424 |
SL::DBConnect->get_options(PrintError => 0, PrintWarn => 0), |
|
425 |
); |
|
426 |
|
|
427 |
my $dbh = SL::DBConnect->connect(@dbi_options) || BAIL_OUT("Database connection failed: " . $DBI::errstr); |
|
428 |
$::auth->{dbh} = $dbh; |
|
429 |
my $dbupdater = SL::DBUpgrade2->new(form => $::form, return_on_error => 1, silent => 1); |
|
430 |
my $coa = 'Germany-DATEV-SKR03EU'; |
|
431 |
|
|
432 |
apply_dbupgrade($dbupdater, $dbh, "sql/lx-office.sql"); |
|
433 |
apply_dbupgrade($dbupdater, $dbh, "sql/${coa}-chart.sql"); |
|
434 |
|
|
435 |
dbh_do($dbh, qq|UPDATE defaults SET coa = '${coa}', accounting_method = 'cash', profit_determination = 'income', inventory_system = 'periodic', curr = 'EUR'|); |
|
436 |
dbh_do($dbh, qq|CREATE TABLE schema_info (tag TEXT, login TEXT, itime TIMESTAMP DEFAULT now(), PRIMARY KEY (tag))|); |
|
437 |
|
|
438 |
notice("Creating initial auth schema"); |
|
439 |
|
|
440 |
$dbupdater = SL::DBUpgrade2->new(form => $::form, return_on_error => 1, auth => 1); |
|
441 |
apply_dbupgrade($dbupdater, $dbh, 'sql/auth_db.sql'); |
|
442 |
|
|
443 |
apply_upgrades(auth => 1, dbh => $dbh); |
|
444 |
|
|
445 |
notice("Creating client, user, group and employee"); |
|
446 |
|
|
447 |
dbh_do($dbh, qq|DELETE FROM auth.clients|); |
|
448 |
dbh_do($dbh, qq|INSERT INTO auth.clients (id, name, dbhost, dbport, dbname, dbuser, dbpasswd, is_default) VALUES (1, 'Unit-Tests', ?, ?, ?, ?, ?, TRUE)|, |
|
449 |
bind => [ @{ $db_cfg }{ qw(host port db user password) } ]); |
|
450 |
dbh_do($dbh, qq|INSERT INTO auth."user" (id, login) VALUES (1, 'unittests')|); |
|
451 |
dbh_do($dbh, qq|INSERT INTO auth."group" (id, name) VALUES (1, 'Vollzugriff')|); |
|
452 |
dbh_do($dbh, qq|INSERT INTO auth.clients_users (client_id, user_id) VALUES (1, 1)|); |
|
453 |
dbh_do($dbh, qq|INSERT INTO auth.clients_groups (client_id, group_id) VALUES (1, 1)|); |
|
454 |
dbh_do($dbh, qq|INSERT INTO auth.user_group (user_id, group_id) VALUES (1, 1)|); |
|
455 |
|
|
456 |
my %config = ( |
|
457 |
default_printer_id => '', |
|
458 |
template_format => '', |
|
459 |
default_media => '', |
|
460 |
email => 'unit@tester', |
|
461 |
tel => '', |
|
462 |
dateformat => 'dd.mm.yy', |
|
463 |
show_form_details => '', |
|
464 |
name => 'Unit Tester', |
|
465 |
signature => '', |
|
466 |
hide_cvar_search_options => '', |
|
467 |
numberformat => '1.000,00', |
|
468 |
vclimit => 0, |
|
469 |
favorites => '', |
|
470 |
copies => '', |
|
471 |
menustyle => 'v3', |
|
472 |
fax => '', |
|
473 |
stylesheet => 'lx-office-erp.css', |
|
474 |
mandatory_departments => 0, |
|
475 |
countrycode => 'de', |
|
476 |
); |
|
477 |
|
|
478 |
my $sth = $dbh->prepare(qq|INSERT INTO auth.user_config (user_id, cfg_key, cfg_value) VALUES (1, ?, ?)|) || BAIL_OUT($dbh->errstr); |
|
479 |
dbh_do($dbh, $sth, bind => [ $_, $config{$_} ]) for sort keys %config; |
|
480 |
$sth->finish; |
|
481 |
|
|
482 |
$sth = $dbh->prepare(qq|INSERT INTO auth.group_rights (group_id, "right", granted) VALUES (1, ?, TRUE)|) || BAIL_OUT($dbh->errstr); |
|
483 |
dbh_do($dbh, $sth, bind => [ $_ ]) for sort $::auth->all_rights; |
|
484 |
$sth->finish; |
|
485 |
|
|
486 |
dbh_do($dbh, qq|INSERT INTO employee (id, login, name) VALUES (1, 'unittests', 'Unit Tester')|); |
|
487 |
|
|
488 |
$::auth->set_client(1) || BAIL_OUT("\$::auth->set_client(1) failed"); |
|
489 |
%::myconfig = $::auth->read_user(login => 'unittests'); |
|
490 |
|
|
491 |
apply_upgrades(dbh => $dbh); |
|
492 |
} |
|
493 |
|
|
494 |
sub apply_upgrades { |
|
495 |
my %params = @_; |
|
496 |
my $dbupdater = SL::DBUpgrade2->new(form => $::form, return_on_error => 1, auth => $params{auth}); |
|
497 |
my @unapplied_scripts = $dbupdater->unapplied_upgrade_scripts($params{dbh}); |
|
498 |
|
|
499 |
my $all = @unapplied_scripts; |
|
500 |
my $i; |
|
501 |
for my $script (@unapplied_scripts) { |
|
502 |
++$i; |
|
503 |
print "\rUpgrade $i/$all"; |
|
504 |
apply_dbupgrade($dbupdater, $params{dbh}, $script); |
|
505 |
} |
|
506 |
print " - done.\n"; |
|
507 |
} |
|
508 |
|
|
509 |
sub apply_dbupgrade { |
|
510 |
my ($dbupdater, $dbh, $control_or_file) = @_; |
|
511 |
|
|
512 |
my $file = ref($control_or_file) ? ("sql/Pg-upgrade2" . ($dbupdater->{auth} ? "-auth" : "") . "/$control_or_file->{file}") : $control_or_file; |
|
513 |
my $control = ref($control_or_file) ? $control_or_file : undef; |
|
514 |
|
|
515 |
my $error = $dbupdater->process_file($dbh, $file, $control); |
|
516 |
|
|
517 |
die("Error applying $file: $error") if $error; |
|
518 |
} |
|
519 |
|
|
520 |
sub dbh_do { |
|
521 |
my ($dbh, $query, %params) = @_; |
|
522 |
|
|
523 |
if (ref($query)) { |
|
524 |
return if $query->execute(@{ $params{bind} || [] }); |
|
525 |
die($dbh->errstr); |
|
526 |
} |
|
527 |
|
|
528 |
return if $dbh->do($query, undef, @{ $params{bind} || [] }); |
|
529 |
|
|
530 |
die($params{message} . ": " . $dbh->errstr) if $params{message}; |
|
531 |
die("Query failed: " . $dbh->errstr . " ; query: $query"); |
|
532 |
} |
|
533 |
|
|
377 | 534 |
parse_args(\%config); |
378 | 535 |
setup(); |
379 | 536 |
check_errors_in_package_names(); |
... | ... | |
403 | 560 |
|
404 | 561 |
=head1 SYNOPSIS |
405 | 562 |
|
406 |
scripts/rose_auto_create_model.pl --client name-or-id [db1:]table1 [[db2:]table2 ...] |
|
407 |
scripts/rose_auto_create_model.pl --client name-or-id [--all|-a] |
|
563 |
scripts/rose_auto_create_model.pl OPTIONS TARGET |
|
564 |
|
|
565 |
# use other client than devel.client |
|
566 |
scripts/rose_auto_create_model.pl --test-client TARGET |
|
567 |
scripts/rose_auto_create_model.pl --client name-or-id TARGET |
|
408 | 568 |
|
569 |
# TARGETS: |
|
409 | 570 |
# updates all models |
410 |
scripts/rose_auto_create_model.pl --client name-or-id --all [--db db]
|
|
571 |
scripts/rose_auto_create_model.pl --all [--db db] |
|
411 | 572 |
|
412 | 573 |
# updates only customer table, login taken from config |
413 | 574 |
scripts/rose_auto_create_model.pl customer |
... | ... | |
474 | 635 |
|
475 | 636 |
=over 4 |
476 | 637 |
|
638 |
=item C<--test-client, -t> |
|
639 |
|
|
640 |
Use the C<testing/database> to create a new testing database, and connect to |
|
641 |
the first client there. Overrides C<client>. |
|
642 |
|
|
643 |
If neither C<test-client> nor C<client> are set, the config key C<devel/client> |
|
644 |
will be used. |
|
645 |
|
|
477 | 646 |
=item C<--client, -c CLIENT> |
478 | 647 |
|
479 |
Provide a client whose database settings are used. If not present the |
|
480 |
client is loaded from the config key C<devel/client>. If that too is |
|
481 |
not found, an error is thrown. |
|
648 |
Provide a client whose database settings are used. C<CLIENT> can be either a |
|
649 |
database ID or a client's name. |
|
482 | 650 |
|
483 |
Note that C<CLIENT> can be either a database ID or a client's name. |
|
651 |
If neither C<test-client> nor C<client> are set, the config key C<devel/client> |
|
652 |
will be used. |
|
484 | 653 |
|
485 | 654 |
=item C<--all, -a> |
486 | 655 |
|
Auch abrufbar als: Unified diff
rose_auto_create_model: Optional auf frischer Datenbank arbeiten
Beim mergen hat man öfter das Problem, dass die Datenbank schon weit
mehr Upgrades enthält aber trotzdem aktuelle MetaSetups fixen muss.
Mit
scripts/rose_auto_create_model.pl --test-client --all
werden jetzt alle Operationen auf einer frisch angelegten Testdatenbank
gemacht. Nachteil ist natürlich, dass das Anlegen gute 20s extra dauert.