You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2013/02/18 16:27:37 UTC

[PATCH core 7/7] CIMI: Print warning for pending migrations

From: Michal Fojtik <mf...@redhat.com>

We don't want to run any migrations automatically during
Deltacloud server boot time, to give users chance to backup
data, etc.

This patch will provide a nice warning message if you start
Deltacloud and you have some pending migrations to run.

Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/bin/deltacloud-db-upgrade               | 18 +++++++++++-------
 server/lib/initializers/database_initialize.rb | 19 ++++++++++++++++++-
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/server/bin/deltacloud-db-upgrade b/server/bin/deltacloud-db-upgrade
index 2698350..5b37bbb 100755
--- a/server/bin/deltacloud-db-upgrade
+++ b/server/bin/deltacloud-db-upgrade
@@ -1,13 +1,17 @@
 #!/usr/bin/env ruby
 
-ENV['API_VERBOSE'] = 'true'
+require 'rubygems'
 
-load File.join(File.dirname(__FILE__), '..', 'lib', 'db.rb')
+require 'require_relative' if RUBY_VERSION < '1.9'
 
-# Initialize the database
-#
-db = Deltacloud.initialize_database
+require_relative './../lib/initializers/mock_initialize'
+require_relative './../lib/initializers/database_initialize'
 
-# Apply the migrations
+# The DATABASE_UPGRADE constant is set to true if we have discovered
+# pending migrations in DATABASE_MIGRATIONS_DIR.
 #
-Sequel::Migrator.apply(db, File.join(File.dirname(__FILE__), '..', 'db', 'migrations'))
+
+if DATABASE_UPGRADE
+  puts "Upgrading database schema to the latest version..."
+  Sequel::Migrator.apply(DATABASE, DATABASE_MIGRATIONS_DIR)
+end
diff --git a/server/lib/initializers/database_initialize.rb b/server/lib/initializers/database_initialize.rb
index 32badb2..ae9d247 100644
--- a/server/lib/initializers/database_initialize.rb
+++ b/server/lib/initializers/database_initialize.rb
@@ -12,6 +12,9 @@ require_relative '../db'
 #
 Sequel::Model.plugin :validation_class_methods
 
+# Enable Sequel migrations extension
+Sequel.extension :migration
+
 # For JRuby we need to different Sequel driver
 #
 sequel_driver = (RUBY_PLATFORM=='java') ? 'jdbc:sqlite:' : 'sqlite://'
@@ -25,4 +28,18 @@ sequel_driver = (RUBY_PLATFORM=='java') ? 'jdbc:sqlite:' : 'sqlite://'
 DATABASE_LOCATION = ENV['DATABASE_LOCATION'] ||
   "#{sequel_driver}#{File.join(BASE_STORAGE_DIR, 'db.sqlite')}"
 
-Deltacloud::initialize_database
+DATABASE = Deltacloud::initialize_database
+
+# Detect if there are some pending migrations to run.
+# We don't actually run migrations during server startup, just print
+# a warning to console
+#
+
+DATABASE_MIGRATIONS_DIR = File.join(File.dirname(__FILE__), '..', '..', 'db', 'migrations')
+
+unless Sequel::Migrator.is_current?(DATABASE, DATABASE_MIGRATIONS_DIR)
+  warn "WARNING: The database needs to be upgraded. Run: 'deltacloud-db-upgrade' command."
+  DATABASE_UPGRADE = true
+else
+  DATABASE_UPGRADE = false
+end
-- 
1.8.1.2


Re: [PATCH core 7/7] CIMI: Print warning for pending migrations

Posted by David Lutterkort <lu...@redhat.com>.
On Tue, 2013-02-19 at 10:10 +0100, Michal Fojtik wrote:
> >       * do we really need a separate deltacloud-db-upgrade script rather
> >         than just the rake task ? I don't have an issue with requiring
> >         that people have rake on their prod systems (what's good enough
> >         for rails ought to be good enough for us ;)
> 
> The reason I put deltacloud-db-upgrade here is that the 'gem' users will
> not have access to rake tasks. If you do: gem install deltacloud-core
> or you do gem update deltacloud-core, you need some commands to run
> migrations.

Aah, gem, yes, that makes sense.

David



Re: [PATCH core 7/7] CIMI: Print warning for pending migrations

Posted by Michal Fojtik <mf...@redhat.com>.
On 02/18, David Lutterkort wrote:
> This strikes me as more clever than it needs to be:
>       * we should exit(1) if there are pending migrations - there's no
>         point in starting the server otherwise.

Yes, makes sense to me.

>       * can't deltacloud-db-upgrade just blindly run
>         Sequel::Migrator.run ? There shouldn't be any harm even if there

Fixed :)

>         are no migrations to apply
>       * do we really need a separate deltacloud-db-upgrade script rather
>         than just the rake task ? I don't have an issue with requiring
>         that people have rake on their prod systems (what's good enough
>         for rails ought to be good enough for us ;)

The reason I put deltacloud-db-upgrade here is that the 'gem' users will
not have access to rake tasks. If you do: gem install deltacloud-core
or you do gem update deltacloud-core, you need some commands to run
migrations.

  -- Michal

-- 
Michal Fojtik <mf...@redhat.com>
Deltacloud API, CloudForms

Re: [PATCH core 7/7] CIMI: Print warning for pending migrations

Posted by David Lutterkort <lu...@redhat.com>.
On Mon, 2013-02-18 at 16:27 +0100, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> We don't want to run any migrations automatically during
> Deltacloud server boot time, to give users chance to backup
> data, etc.
> 
> This patch will provide a nice warning message if you start
> Deltacloud and you have some pending migrations to run.
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/bin/deltacloud-db-upgrade               | 18 +++++++++++-------
>  server/lib/initializers/database_initialize.rb | 19 ++++++++++++++++++-
>  2 files changed, 29 insertions(+), 8 deletions(-)
> 
> diff --git a/server/bin/deltacloud-db-upgrade b/server/bin/deltacloud-db-upgrade
> index 2698350..5b37bbb 100755
> --- a/server/bin/deltacloud-db-upgrade
> +++ b/server/bin/deltacloud-db-upgrade
> @@ -1,13 +1,17 @@
>  #!/usr/bin/env ruby
>  
> -ENV['API_VERBOSE'] = 'true'
> +require 'rubygems'
>  
> -load File.join(File.dirname(__FILE__), '..', 'lib', 'db.rb')
> +require 'require_relative' if RUBY_VERSION < '1.9'
>  
> -# Initialize the database
> -#
> -db = Deltacloud.initialize_database
> +require_relative './../lib/initializers/mock_initialize'
> +require_relative './../lib/initializers/database_initialize'
>  
> -# Apply the migrations
> +# The DATABASE_UPGRADE constant is set to true if we have discovered
> +# pending migrations in DATABASE_MIGRATIONS_DIR.
>  #
> -Sequel::Migrator.apply(db, File.join(File.dirname(__FILE__), '..', 'db', 'migrations'))
> +
> +if DATABASE_UPGRADE
> +  puts "Upgrading database schema to the latest version..."
> +  Sequel::Migrator.apply(DATABASE, DATABASE_MIGRATIONS_DIR)
> +end
>
> diff --git a/server/lib/initializers/database_initialize.rb b/server/lib/initializers/database_initialize.rb
> index 32badb2..ae9d247 100644
> --- a/server/lib/initializers/database_initialize.rb
> +++ b/server/lib/initializers/database_initialize.rb
> @@ -12,6 +12,9 @@ require_relative '../db'
>  #
>  Sequel::Model.plugin :validation_class_methods
>  
> +# Enable Sequel migrations extension
> +Sequel.extension :migration
> +
>  # For JRuby we need to different Sequel driver
>  #
>  sequel_driver = (RUBY_PLATFORM=='java') ? 'jdbc:sqlite:' : 'sqlite://'
> @@ -25,4 +28,18 @@ sequel_driver = (RUBY_PLATFORM=='java') ? 'jdbc:sqlite:' : 'sqlite://'
>  DATABASE_LOCATION = ENV['DATABASE_LOCATION'] ||
>    "#{sequel_driver}#{File.join(BASE_STORAGE_DIR, 'db.sqlite')}"
>  
> -Deltacloud::initialize_database
> +DATABASE = Deltacloud::initialize_database
> +
> +# Detect if there are some pending migrations to run.
> +# We don't actually run migrations during server startup, just print
> +# a warning to console
> +#
> +
> +DATABASE_MIGRATIONS_DIR = File.join(File.dirname(__FILE__), '..', '..', 'db', 'migrations')
> +
> +unless Sequel::Migrator.is_current?(DATABASE, DATABASE_MIGRATIONS_DIR)
> +  warn "WARNING: The database needs to be upgraded. Run: 'deltacloud-db-upgrade' command."
> +  DATABASE_UPGRADE = true
> +else
> +  DATABASE_UPGRADE = false
> +end

This strikes me as more clever than it needs to be:
      * we should exit(1) if there are pending migrations - there's no
        point in starting the server otherwise.
      * can't deltacloud-db-upgrade just blindly run
        Sequel::Migrator.run ? There shouldn't be any harm even if there
        are no migrations to apply
      * do we really need a separate deltacloud-db-upgrade script rather
        than just the rake task ? I don't have an issue with requiring
        that people have rake on their prod systems (what's good enough
        for rails ought to be good enough for us ;)

But ACK anyway; would be nice though to address these.

David