From c241c4ff0ccf9a711caab4fc7a42448b42a53ce9 Mon Sep 17 00:00:00 2001
From: Mauricio Giacomini Girardello <mauriciogiacomini4@gmail.com>
Date: Mon, 31 Aug 2015 11:35:20 -0300
Subject: [PATCH] adding orient db migrations

---
 lib/orient_db/migration.rb                    | 18 ++++++++++++++++++
 lib/orient_db/migrations.rb                   | 13 +++++++++++++
 lib/orient_db/migrations/create_country.rb    |  7 +++++++
 lib/orient_db/migrations/create_highlight.rb  |  7 +++++++
 lib/orient_db/migrations/create_university.rb |  7 +++++++
 lib/orient_db/migrations/create_user.rb       |  7 +++++++
 lib/tasks/orientdb.rake                       | 17 +++--------------
 7 files changed, 62 insertions(+), 14 deletions(-)
 create mode 100644 lib/orient_db/migration.rb
 create mode 100644 lib/orient_db/migrations.rb
 create mode 100644 lib/orient_db/migrations/create_country.rb
 create mode 100644 lib/orient_db/migrations/create_highlight.rb
 create mode 100644 lib/orient_db/migrations/create_university.rb
 create mode 100644 lib/orient_db/migrations/create_user.rb

diff --git a/lib/orient_db/migration.rb b/lib/orient_db/migration.rb
new file mode 100644
index 00000000..37c5cecd
--- /dev/null
+++ b/lib/orient_db/migration.rb
@@ -0,0 +1,18 @@
+module OrientDb
+  class Migration
+
+    def initialize(client)
+      @client = client
+    end
+
+    def create_class(klass)
+      unless @client.class_exists? klass
+        @client.create_class(klass) do |c|
+          if block_given?
+            yield c
+          end
+        end
+      end
+    end
+  end
+end
\ No newline at end of file
diff --git a/lib/orient_db/migrations.rb b/lib/orient_db/migrations.rb
new file mode 100644
index 00000000..b721b8d6
--- /dev/null
+++ b/lib/orient_db/migrations.rb
@@ -0,0 +1,13 @@
+class OrientDb::Migrations
+  def initialize(client)
+    @migrations = []
+    @migrations << CreateCountry.new(client)
+    @migrations << CreateHighlight.new(client)
+    @migrations << CreateUniversity.new(client)
+    @migrations << CreateUser.new(client)
+  end
+
+  def run
+    @migrations.each { |m| m.up }
+  end
+end
\ No newline at end of file
diff --git a/lib/orient_db/migrations/create_country.rb b/lib/orient_db/migrations/create_country.rb
new file mode 100644
index 00000000..430b2e49
--- /dev/null
+++ b/lib/orient_db/migrations/create_country.rb
@@ -0,0 +1,7 @@
+class OrientDb::Migrations::CreateCountry < OrientDb::Migration
+
+  def up
+    create_class 'Country'
+  end
+
+end
\ No newline at end of file
diff --git a/lib/orient_db/migrations/create_highlight.rb b/lib/orient_db/migrations/create_highlight.rb
new file mode 100644
index 00000000..8fcc0b45
--- /dev/null
+++ b/lib/orient_db/migrations/create_highlight.rb
@@ -0,0 +1,7 @@
+class OrientDb::Migrations::CreateHighlight < OrientDb::Migration
+
+  def up
+    create_class 'Highlight'
+  end
+
+end
\ No newline at end of file
diff --git a/lib/orient_db/migrations/create_university.rb b/lib/orient_db/migrations/create_university.rb
new file mode 100644
index 00000000..ec6df665
--- /dev/null
+++ b/lib/orient_db/migrations/create_university.rb
@@ -0,0 +1,7 @@
+class OrientDb::Migrations::CreateUniversity < OrientDb::Migration
+
+  def up
+    create_class 'University'
+  end
+
+end
\ No newline at end of file
diff --git a/lib/orient_db/migrations/create_user.rb b/lib/orient_db/migrations/create_user.rb
new file mode 100644
index 00000000..50136f70
--- /dev/null
+++ b/lib/orient_db/migrations/create_user.rb
@@ -0,0 +1,7 @@
+class OrientDb::Migrations::CreateUser < OrientDb::Migration
+
+  def up
+    create_class 'User'
+  end
+
+end
\ No newline at end of file
diff --git a/lib/tasks/orientdb.rake b/lib/tasks/orientdb.rake
index ba2b89f7..504aa1f1 100644
--- a/lib/tasks/orientdb.rake
+++ b/lib/tasks/orientdb.rake
@@ -4,19 +4,8 @@ namespace :orientdb do
   task migrate: :environment do
     desc "Migrate orient db schema"
 
-    client = OrientDb::Client.instance
-
-    # User vertice
-    client.command 'create class User extends V'
-
-    # Highlight vertice
-    client.command 'create class Highlight extends V'
-
-    # Country vertice
-    client.command 'create class Country extends V'
-
-    # University vertice
-    client.command 'create class University extends V'
+    migrations = OrientDb::Migrations.new(OrientDb::Client.instance)
+    migrations.run
   end
 
-end
+end
\ No newline at end of file
-- 
GitLab