--- /dev/null
+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