You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2013/02/19 10:11:23 UTC

[3/5] git commit: Core: Added support for migrations to database

Core: Added support for migrations to database

Currently everytime we add or remove attributes from
DB, users need to rerun the 'rake' task to recreate the
database.

This patch will make possible to update the DB schema
without executing the rake task.

The 'migrations' extension in Sequel will check the 'schema_version'
table and apply all migrations >= than the current schema.

Migrations must be prefixed with the schema number in chronological
order and they are stored in server/lib/db/migrations.


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/415a9eab
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/415a9eab
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/415a9eab

Branch: refs/heads/master
Commit: 415a9eabc4f7104ad6825c97db9ff91f7bf126ab
Parents: 2a9e21a
Author: Michal Fojtik <mf...@redhat.com>
Authored: Thu Feb 7 15:31:11 2013 +0100
Committer: Michal fojtik <mf...@redhat.com>
Committed: Tue Feb 19 10:10:57 2013 +0100

----------------------------------------------------------------------
 server/Rakefile                                    |   10 ++++++
 server/bin/deltacloud-db-upgrade                   |   13 ++++++++
 .../migrations/1_add_realm_to_machine_template.rb  |   23 +++++++++++++++
 server/lib/db.rb                                   |    3 +-
 4 files changed, 47 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/415a9eab/server/Rakefile
----------------------------------------------------------------------
diff --git a/server/Rakefile b/server/Rakefile
index 41bd2c1..346df0a 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -46,6 +46,16 @@ Gem::PackageTask.new(spec) do |pkg|
   pkg.need_tar = true
 end
 
+namespace :db do
+  desc 'Execute the database migrations'
+  task 'migrate' do
+    ENV['API_VERBOSE'] = 'true'
+    load File.join(File.dirname(__FILE__), 'lib', 'db.rb')
+    db = Deltacloud.database
+    Sequel::Migrator.apply(db, File.join(File.dirname(__FILE__), 'db', 'migrations'))
+  end
+end
+
 namespace :mock do
   namespace :fixtures do
     desc "Setup Mock driver fixtures"

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/415a9eab/server/bin/deltacloud-db-upgrade
----------------------------------------------------------------------
diff --git a/server/bin/deltacloud-db-upgrade b/server/bin/deltacloud-db-upgrade
new file mode 100755
index 0000000..2698350
--- /dev/null
+++ b/server/bin/deltacloud-db-upgrade
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+
+ENV['API_VERBOSE'] = 'true'
+
+load File.join(File.dirname(__FILE__), '..', 'lib', 'db.rb')
+
+# Initialize the database
+#
+db = Deltacloud.initialize_database
+
+# Apply the migrations
+#
+Sequel::Migrator.apply(db, File.join(File.dirname(__FILE__), '..', 'db', 'migrations'))

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/415a9eab/server/db/migrations/1_add_realm_to_machine_template.rb
----------------------------------------------------------------------
diff --git a/server/db/migrations/1_add_realm_to_machine_template.rb b/server/db/migrations/1_add_realm_to_machine_template.rb
new file mode 100644
index 0000000..8261efc
--- /dev/null
+++ b/server/db/migrations/1_add_realm_to_machine_template.rb
@@ -0,0 +1,23 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+Sequel.migration do
+  up do
+    add_column :entities, :realm, String
+  end
+  down do
+    drop_column :entities, :realm
+  end
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/415a9eab/server/lib/db.rb
----------------------------------------------------------------------
diff --git a/server/lib/db.rb b/server/lib/db.rb
index 5615f50..0cd18e4 100644
--- a/server/lib/db.rb
+++ b/server/lib/db.rb
@@ -15,7 +15,6 @@
 
 module Deltacloud
 
-
   def self.database(opts={})
     if ENV['API_VERBOSE']
       if Deltacloud.respond_to? :config
@@ -81,6 +80,6 @@ module Deltacloud
       column :volume_config, :string
       column :volume_image, :string
     }
+    db
   end
-
 end