Skip to content

Commit

Permalink
feat: add '--prepare-only' option to 'create' command
Browse files Browse the repository at this point in the history
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:  ledgersmb#8411
  • Loading branch information
walidmujahid committed Oct 4, 2024
1 parent 405b0f0 commit 1d01a34
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions lib/LedgerSMB/Admin/Command/create.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand All @@ -68,7 +89,7 @@ __END__
=head1 SYNOPSIS
ledgersmb-admin create <db-uri>
ledgersmb-admin create [options] <db-uri>
=head1 DESCRIPTION
Expand All @@ -78,6 +99,16 @@ C<db-uri>.
The resulting database does not have any setup, settings or users. See the
C<setup>, C<setting> and C<user> 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
Expand Down

0 comments on commit 1d01a34

Please sign in to comment.