diff options
| author | mo khan <mo@mokhan.ca> | 2017-01-25 17:36:38 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2017-01-25 17:36:38 -0700 |
| commit | c9ef265c99611ac0a49bfd4541684e5af968ac5f (patch) | |
| tree | 934d2847b9ce71c68a975f9d96453aebbb22d925 | |
| parent | 1366c29d7698890a540847bc88b01d83aba30016 (diff) | |
create rake task to create database and schema.
| -rw-r--r-- | Gemfile | 2 | ||||
| -rw-r--r-- | Gemfile.lock | 4 | ||||
| -rw-r--r-- | Rakefile | 46 | ||||
| -rw-r--r-- | db/migrations/0001_create_businesses.rb | 13 | ||||
| -rw-r--r-- | db/migrations/0002_create_computer.rb | 13 | ||||
| -rw-r--r-- | db/migrations/003_create_events.rb | 15 |
6 files changed, 93 insertions, 0 deletions
@@ -1,4 +1,6 @@ # frozen_string_literal: true source "https://rubygems.org" +gem 'mysql2' +gem 'rake' gem 'sequel' diff --git a/Gemfile.lock b/Gemfile.lock index 4c8ba28..3b0a47b 100644 --- a/Gemfile.lock +++ b/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 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..26443b5 --- /dev/null +++ b/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 diff --git a/db/migrations/0001_create_businesses.rb b/db/migrations/0001_create_businesses.rb new file mode 100644 index 0000000..6bbd26b --- /dev/null +++ b/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 diff --git a/db/migrations/0002_create_computer.rb b/db/migrations/0002_create_computer.rb new file mode 100644 index 0000000..4a61ce8 --- /dev/null +++ b/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 diff --git a/db/migrations/003_create_events.rb b/db/migrations/003_create_events.rb new file mode 100644 index 0000000..f9273a2 --- /dev/null +++ b/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 |
