From effb73658cb64bd03d356fc54d2f7258fb8ee3bb Mon Sep 17 00:00:00 2001 From: Antonio Larrosa Date: Tue, 2 Dec 2014 18:15:26 +0100 Subject: [PATCH 1/2] Added a --force option that allows to overwrite the generated files for an schema. Also shows some help text if the script notices the user may want to use --force. --- script/initdb | 23 +++++++++++++++++++---- script/upgradedb | 12 ++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/script/initdb b/script/initdb index 1fd71a63761..77317606be8 100755 --- a/script/initdb +++ b/script/initdb @@ -29,15 +29,18 @@ use POSIX qw/getuid getgid setuid setgid/; use openqa (); use Schema::Schema; use Getopt::Long; +use IO::Dir; my $help=0; my $prepare_init=0; my $init_database=0; +my $force=0; my $result = GetOptions( - "help" => \$help, - "prepare_init" => \$prepare_init, - "init_database" => \$init_database + "help" => \$help, + "prepare_init" => \$prepare_init, + "init_database" => \$init_database, + "force" => \$force ); if (!$prepare_init and !$init_database) { @@ -51,6 +54,7 @@ if ($help) { print " and note those files should be commited to the source repo.\n"; print " --init_database : Use the generated deployment files created with --prepare_init\n"; print " to actually initialize a database.\n"; + print " --force : Force overwriting existing data.\n"; print " --help : This help message.\n"; exit; } @@ -82,11 +86,22 @@ my $dh = DBIx::Class::DeploymentHandler->new( script_directory => $script_directory, databases => 'SQLite', sql_translator_args => { add_drop_table => 1, producer_args => { sqlite_version => '3.7' } }, - force_overwrite => 0, + force_overwrite => $force, } ); +my $version = $dh->schema_version; + +my %deploy_dir; +tie %deploy_dir, 'IO::Dir', "$script_directory/SQLite/deploy"; + if ($prepare_init) { + if ( exists $deploy_dir{$version} and not $force) { + print "The deploy directory already contains the schema for the current version.\n"; + print "Use the --force option if you want to overwrite the contents of the $script_directory/SQLite/upgrade/$version directory\n"; + exit 1; + } + $dh->prepare_install; } if ($init_database) { diff --git a/script/upgradedb b/script/upgradedb index b5780e0c0de..02a9b4e6d9d 100755 --- a/script/upgradedb +++ b/script/upgradedb @@ -36,12 +36,14 @@ my $prepare_upgrades = 0; my $upgrade_database = 0; my $help = 0; my $from_uninitialized_database = 0; +my $force = 0; my $result = GetOptions( "help" => \$help, "from_uninitialized_database" => \$from_uninitialized_database, "prepare_upgrades" => \$prepare_upgrades, - "upgrade_database" => \$upgrade_database + "upgrade_database" => \$upgrade_database, + "force" => \$force ); if (!$prepare_upgrades and !$upgrade_database) { @@ -57,6 +59,7 @@ if ($help) { print " to actually upgrade a database.\n"; print " --from_uninitialized_database : Create and populate an existing database as if it was\n"; print " initialized with the initdb script.\n"; + print " --force : Force overwriting existing data.\n"; print " --help : This help message.\n"; exit; } @@ -71,6 +74,7 @@ my $dh = DH->new( script_directory => $script_directory, databases => 'SQLite', sql_translator_args => { add_drop_table => 0, producer_args => { sqlite_version => '3.7' } }, + force_overwrite => $force, } ); @@ -89,9 +93,9 @@ my %deploy_dir; tie %deploy_dir, 'IO::Dir', "$script_directory/SQLite/deploy"; if ($prepare_upgrades) { - if ( exists $upgrade_dir{$upgrade_directory} ) { - print "The current version $version already has upgrade data generated. Nothing to upgrade\n"; - print "Remove the $script_directory/SQLite/upgrade/$upgrade_directory if you want to regenerate it\n"; + if ( exists $upgrade_dir{$upgrade_directory} and not $force) { + print "The upgrade directory already contains files to upgrade to the current version ($version)\n"; + print "Use the --force option if you want to overwrite the contents of the $script_directory/SQLite/upgrade/$upgrade_directory directory\n"; exit 1; } From b93672b046797d1c32438f38e8153b2cf3568d5a Mon Sep 17 00:00:00 2001 From: Antonio Larrosa Date: Tue, 2 Dec 2014 18:16:26 +0100 Subject: [PATCH 2/2] Fixes some documentation lines by removing them and referencing the right docs --- lib/OpenQA/modules/Schema/Schema.pm | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/OpenQA/modules/Schema/Schema.pm b/lib/OpenQA/modules/Schema/Schema.pm index bc856eb425a..3c7a99c9ee9 100644 --- a/lib/OpenQA/modules/Schema/Schema.pm +++ b/lib/OpenQA/modules/Schema/Schema.pm @@ -20,11 +20,8 @@ use IO::Dir; use SQL::SplitStatement; use Fcntl ':mode'; -# after bumping the version ... -# - run script/initdb --prepare -# - run script/upgradedb --prepare -# - edit dbicdh/SQLite/upgrade/$old-$new/001-auto.sql and add missing triggers -# - optionally add migration script dbicdh/_common/upgrade/$old-$new/... +# after bumping the version please look at the instructions in the docs/Contributing.asciidoc file +# on what scripts should be run and how our $VERSION = '15'; __PACKAGE__->load_namespaces;