Commit c9ef265

mo khan <mo@mokhan.ca>
2017-01-26 00:36:38
create rake task to create database and schema.
1 parent 1366c29
db/migrations/0001_create_businesses.rb
@@ -0,0 +1,13 @@
+Sequel.migration do
+  up do
+    create_table :businesses do
+      primary_key :id
+      String :name, null: false
+      Integer :business_relationship_id, null: false
+    end
+  end
+
+  down do
+    drop_table :businesses
+  end
+end
db/migrations/0002_create_computer.rb
@@ -0,0 +1,13 @@
+Sequel.migration do
+  up do
+    create_table :computers do
+      primary_key :id
+      TrueClass :active, null: false, default: true
+      foreign_key :business_id, :businesses
+    end
+  end
+
+  down do
+    drop_table :computers
+  end
+end
db/migrations/003_create_events.rb
@@ -0,0 +1,15 @@
+Sequel.migration do
+  up do
+    create_table :events do
+      primary_key :id
+      foreign_key :computer_id, :computers
+      String :type, null: false
+      String :data, text: true
+      DateTime :occurred_at, null: false
+    end
+  end
+
+  down do
+    drop_table :events
+  end
+end
Gemfile
@@ -1,4 +1,6 @@
 # frozen_string_literal: true
 source "https://rubygems.org"
 
+gem 'mysql2'
+gem 'rake'
 gem 'sequel'
Gemfile.lock
@@ -1,12 +1,16 @@
 GEM
   remote: https://rubygems.org/
   specs:
+    mysql2 (0.4.5)
+    rake (12.0.0)
     sequel (4.42.1)
 
 PLATFORMS
   ruby
 
 DEPENDENCIES
+  mysql2
+  rake
   sequel
 
 BUNDLED WITH
Rakefile
@@ -0,0 +1,46 @@
+require 'rubygems'
+require 'bundler'
+Bundler.require(:default)
+
+DATABASE_NAME = 'sql_bootcamp'
+
+#| BUSINESSES               |              | COMPUTERS   |         | EVENTS      |              |
+#| id                       | int          | id          | int     | id          | int          |
+#| name                     | varchar(255) | active      | tinyint | computer_id | int          |
+#| business_relationship_id | int          | business_id | int     | occurred_at | datetime     |
+#|                          |              |             |         | type        | varchar(255) |
+#|                          |              |             |         | data        | text         |
+
+namespace :db do
+  def pipe_to_mysql(command)
+    `echo "#{command}" | mysql -u root`
+  end
+
+  desc "create database"
+  task :create do
+    pipe_to_mysql("CREATE DATABASE #{DATABASE_NAME}")
+  end
+
+  desc "drop database"
+  task :drop do
+    pipe_to_mysql("DROP DATABASE IF EXISTS #{DATABASE_NAME}")
+  end
+
+  desc "Run migrations"
+  task :migrate, [:version] do |t, args|
+    Sequel.extension :migration
+    db = Sequel.connect("mysql2://root@localhost/#{DATABASE_NAME}")
+
+    if args[:version]
+      puts "Migrating to version #{args[:version]}"
+      Sequel::Migrator.run(db, "db/migrations", target: args[:version].to_i)
+    else
+      puts "Migrating to latest"
+      Sequel::Migrator.run(db, "db/migrations")
+    end
+  end
+
+  task :reset => [:drop, :create, :migrate] do
+
+  end
+end