OSDN Git Service

Command: add databaseinit command
authorhylom <hylom@users.sourceforge.jp>
Fri, 17 Aug 2018 11:01:36 +0000 (20:01 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 17 Aug 2018 11:05:19 +0000 (20:05 +0900)
src/newslash_web/lib/Newslash/Command/databaseinit.pm [new file with mode: 0644]
src/newslash_web/lib/Newslash/Model.pm
src/newslash_web/lib/Newslash/Web.pm

diff --git a/src/newslash_web/lib/Newslash/Command/databaseinit.pm b/src/newslash_web/lib/Newslash/Command/databaseinit.pm
new file mode 100644 (file)
index 0000000..f8f1745
--- /dev/null
@@ -0,0 +1,148 @@
+package Newslash::Command::databaseinit;
+use Mojo::Base 'Newslash::Command';
+
+use Mojo::Util 'getopt';
+use Data::Dumper;
+use Encode;
+
+has description => 'Initialize database';
+has usage => sub { shift->extract_usage };
+
+has experimental => 1;
+has danger => 1;
+
+my $table_sql_dir = "data/SQL/create_table";
+my $data_sql_dir = "data/SQL/insert_data";
+
+sub run {
+    my ($self, @args) = @_;
+    return if !$self->check_force_option(\@args);
+
+    getopt \@args,
+      'd|delete' => \my $delete;
+
+    if ($delete) {
+        $self->app->log->info("databaseinit: delete tables...");
+
+        if (!$self->_delete_tables) {
+            $self->app->log->error("databaseinit: cannot delete tables correctly. abort");
+            return;
+        }
+
+        $self->app->log->info("databaseinit: delete tables done.");
+    }
+
+    my $home = $self->app->home;
+    $self->app->log->info("databaseinit: create tables...");
+    if (!$self->_load_and_run_sql($home->child($table_sql_dir))) {
+        $self->app->log->error("databaseinit: cannot create tables correctly. abort");
+        return;
+    }
+    $self->app->log->info("databaseinit: insert data...");
+    if (!$self->_load_and_run_sql($home->child($data_sql_dir))) {
+        $self->app->log->error("databaseinit: cannot insert data correctly. abort");
+        return;
+    }
+    $self->app->log->info("databaseinit: done");
+}
+
+sub _delete_tables {
+    my $self = shift;
+
+    my $db = $self->app->model('database');
+    my $dbh = $db->connect_db;
+
+    # get current tables
+    my $sth = $dbh->prepare("SHOW TABLES");
+    $sth->execute or die "cannot retrieve table data";
+
+    my $rs = $sth->fetchall_arrayref([0]);
+    for my $name (@$rs) {
+        if (!$dbh->do("DROP TABLE `$name->[0]`")) {
+            $self->app->log->error("databaseinit: cannot delete table $name->[0]\n");
+            $db->disconnect_db;
+            return;
+        }
+    }
+
+    $db->disconnect_db;
+    return 1;
+}
+
+sub _load_and_run_sql {
+    my ($self, $dir) = @_;
+
+    my $db = $self->app->model('database');
+    my $dbh = $db->start_transaction;
+
+    for my $sql_file ($dir->list->sort->each) {
+        my $sql = decode('utf-8', $sql_file->slurp);
+        my $result = $dbh->do($sql);
+        if (!$result) {
+            $self->app->log->error("databaseinit: SQL '$sql_file' execute error - $!\n");
+            $dbh->rollback;
+            return;
+        }
+    }
+
+    $db->commit;
+    return 1;
+}
+
+1;
+
+=encoding utf8
+
+=head1 NAME
+
+Newslash::Command::databaseinit - newslash administer command (databaseinit)
+
+=head1 SYNOPSIS
+
+  Usage: APPLICATION databaseinit [OPTIONS]
+
+      ./newslash_web databaseinit -f -d
+
+    Options:
+      -f  if mode is not "test", need this option
+      -d  delete all exist tables
+
+=head1 DESCRIPTION
+
+  L<Newslash::Command::databaseinit> delete and create tables in database.
+
+=head1 ATTRIBUTES
+
+L<Newslash::Command::databaseinit> inherits all attributes from
+L<Newslash::Command> and implements the following new ones.
+
+=head2 description
+
+  my $description = $daemon->description;
+  $daemon         = $daemon->description('Foo');
+
+Short description of this command, used for the command list.
+
+=head2 usage
+
+  my $usage = $daemon->usage;
+  $daemon   = $daemon->usage('Foo');
+
+Usage information for this command, used for the help screen.
+
+=head1 METHODS
+
+L<Newslash::Command::databaseinit> inherits all methods from
+L<Newslash::Command> and implements the following new ones.
+
+=head2 run
+
+  $daemon->run(@ARGV);
+
+Run this command.
+
+=head1 SEE ALSO
+
+L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
+
+=cut
index f742eda..0dc35ee 100644 (file)
@@ -6,6 +6,7 @@ load(boxes => "Newslash::Model::Boxes");
 load(custom_boxes => "Newslash::Model::CustomBoxes");
 load(comments => "Newslash::Model::Comments");
 load(discussions => "Newslash::Model::Discussions");
+load(database => "Newslash::Model::Base");
 load(events => "Newslash::Model::Events");
 load(feeds => "Newslash::Model::Feeds");
 load(firehose => "Newslash::Model::Firehose");
index 7570bd9..ee5f807 100644 (file)
@@ -118,7 +118,13 @@ sub startup {
     my $model_opts = $app->config;
     $model_opts->{Logger} = $app->log;
     $app->helper(model => Newslash::Model::loader($model_opts));
-    Newslash::Model::startup($model_opts, $app);
+
+    if (@ARGV[0] ne "databaseinit") {
+        Newslash::Model::startup($model_opts, $app);
+    }
+    else {
+        $app->log->info("bypassing model startup...");
+    }
 
     # use Easy Cache ($app->ezcache)
     $app->plugin('Newslash::Plugin::EasyCache');