From d05a72aec7175f4d1446935405f1a78611f3017d Mon Sep 17 00:00:00 2001 From: Walid Mujahid Date: Thu, 3 Oct 2024 22:58:54 -0400 Subject: [PATCH] feat: add '--prepare-only' option to 'create' command This commit adds a '--prepare-only' option to the 'ledgersmb-admin create' command, allowing users to prepare an existing database without attempting to create it. This is particularly useful in deployment scenarios where the database is pre-created, such as when using managed database services like DigitalOcean Managed Databases. The '--prepare-only' option: - Skips the database creation step, avoiding errors when the database already exists. - Performs the preparation steps by loading the base schema, applying changes, and loading modules into the specified database. - Provides a consistent user experience by integrating the functionality into the existing 'create' command. This addition addresses the need to prepare existing databases in situations where the 'create' command cannot be used because it attempts to create the database, which may not be possible or desired in certain environments. This is inspired from the workaround for issue: #8411 --- lib/LedgerSMB/Admin/Command/create.pm | 45 ++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/lib/LedgerSMB/Admin/Command/create.pm b/lib/LedgerSMB/Admin/Command/create.pm index 1fa3ddb7dc..a27a4a78f6 100644 --- a/lib/LedgerSMB/Admin/Command/create.pm +++ b/lib/LedgerSMB/Admin/Command/create.pm @@ -17,10 +17,26 @@ use Moose; extends 'LedgerSMB::Admin::Command'; use namespace::autoclean; +use Getopt::Long qw(GetOptionsFromArray); use Feature::Compat::Try; +has options => (is => 'ro', default => sub { {} }); + +sub _option_spec { + my ($self, $command) = @_; + return ( + 'prepare-only' => \$self->options->{'prepare-only'}, + ); +} + sub run { - my ($self, $dbname) = @_; + my ($self, @args) = @_; + my $options = {}; + + Getopt::Long::Configure(qw(bundling require_order)); + GetOptionsFromArray(\@args, $self->options, $self->_option_spec()); + + my $dbname = shift @args; return $self->help('create') if !$dbname || $dbname eq 'help'; @@ -41,11 +57,16 @@ sub run { my $log = LedgerSMB::Database::loader_log_filename; my $errlog = LedgerSMB::Database::loader_log_filename; try { - $self->db->create_and_load( - { - log => $log, - errlog => $errlog, - }); + unless ($self->options->{'prepare-only'}) { + $self->db->create(); + $logger->warn('Database successfully created'); + } else { + $logger->warn('Preparing existing database'); + } + $self->db->load_base_schema(); + $self->db->apply_changes(); + $self->db->load_modules('LOADORDER'); + $logger->warn('Database successfully created and prepared'); } catch ($e) { ###TODO remove database after failed creation @@ -68,7 +89,7 @@ __END__ =head1 SYNOPSIS - ledgersmb-admin create + ledgersmb-admin create [options] =head1 DESCRIPTION @@ -78,6 +99,16 @@ C. The resulting database does not have any setup, settings or users. See the C, C and C commands. +=head1 OPTIONS + +=over + +=item B<--prepare-only> + +Prepares an existing database without attempting to create it. This is useful when the database has been pre-created and you only need to prepare it for use with LedgerSMB. + +=back + =head1 SUBCOMMANDS None