Skip to content

Commit

Permalink
DB Schema
Browse files Browse the repository at this point in the history
  Design DB UML.
  Generate models and migration.
  Add relationships.
  NB: DB contains two polymorphic tables; comments and tags.
  • Loading branch information
oojewale committed Jan 24, 2016
1 parent dc9f90a commit 33cb90a
Show file tree
Hide file tree
Showing 31 changed files with 412 additions and 15 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
source 'https://rubygems.org'

ruby "2.2.3"

gem 'rails', '4.2.5'

gem 'rails-api'

gem 'spring', :group => :development

gem 'omniauth-oauth2', '~> 1.3.1'
gem 'omniauth-slack'
gem "omniauth-google-oauth2"
gem 'figaro'
gem 'ancestry'
gem 'pry-rails'

group :development, :test do
gem 'sqlite3'
gem "rspec-rails"
gem 'pry-rails'
gem 'pry-nav'
end

group :production do
Expand Down
24 changes: 23 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ GEM
builder (3.2.2)
coderay (1.1.0)
concurrent-ruby (1.0.0)
diff-lcs (1.2.5)
erubis (2.7.0)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
Expand Down Expand Up @@ -92,6 +93,8 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-nav (0.2.4)
pry (>= 0.9.10, < 0.11.0)
pry-rails (0.3.4)
pry (>= 0.9.10)
rack (1.6.4)
Expand Down Expand Up @@ -125,6 +128,23 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.5.0)
rspec-core (3.4.1)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-rails (3.4.0)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
slop (3.6.0)
spring (1.6.2)
sprockets (3.5.2)
Expand All @@ -150,11 +170,13 @@ DEPENDENCIES
omniauth-oauth2 (~> 1.3.1)
omniauth-slack
pg
pry-nav
pry-rails
rails (= 4.2.5)
rails-api
rspec-rails
spring
sqlite3

BUNDLED WITH
1.10.6
1.11.2
5 changes: 5 additions & 0 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Answer < ActiveRecord::Base
has_many :comments, as: :comment_on
belongs_to :user
belongs_to :question
end
3 changes: 3 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Comment < ActiveRecord::Base
belongs_to :comment_on
end
6 changes: 6 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Question < ActiveRecord::Base
has_many :comments, as: :comment_on
has_many :tags, as: :subscriber
has_many :answers
belongs_to :user
end
3 changes: 3 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Tag < ActiveRecord::Base
belongs_to :subscriber
end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class User < ActiveRecord::Base
has_many :comments, as: :comment_on
has_many :tags, as: :subscriber
has_many :questions
has_many :answers
end
11 changes: 11 additions & 0 deletions db/migrate/20160124125414_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :uuid
t.string :provider
t.integer :points

t.timestamps null: false
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20160124130046_create_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.integer :user_id
t.string :title
t.string :content
t.integer :votes

t.timestamps null: false
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20160124130157_create_answers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateAnswers < ActiveRecord::Migration
def change
create_table :answers do |t|
t.integer :user_id
t.integer :question_id
t.string :content
t.integer :votes

t.timestamps null: false
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20160124143352_create_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateTags < ActiveRecord::Migration
def change
create_table :tags do |t|
t.string :name
t.references :subscriber, polymorphic: true, index: true

t.timestamps null: false
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20160124145702_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.integer :user_id
t.string :content
t.integer :votes
t.references :comment_on, polymorphic: true, index: true

t.timestamps null: false
end
end
end
64 changes: 64 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160124145702) do

create_table "answers", force: :cascade do |t|
t.integer "user_id"
t.integer "question_id"
t.string "content"
t.integer "votes"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "comments", force: :cascade do |t|
t.integer "user_id"
t.string "content"
t.integer "votes"
t.integer "comment_on_id"
t.string "comment_on_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "comments", ["comment_on_type", "comment_on_id"], name: "index_comments_on_comment_on_type_and_comment_on_id"

create_table "questions", force: :cascade do |t|
t.integer "user_id"
t.string "title"
t.string "content"
t.integer "votes"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "tags", force: :cascade do |t|
t.string "name"
t.integer "subscriber_id"
t.string "subscriber_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "tags", ["subscriber_type", "subscriber_id"], name: "index_tags_on_subscriber_type_and_subscriber_id"

create_table "users", force: :cascade do |t|
t.string "uuid"
t.string "provider"
t.integer "points"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
Binary file added docs/.DS_Store
Binary file not shown.
Binary file added docs/Screen Shot 2016-01-24 at 4.03.22 PM.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 118 additions & 0 deletions docs/schema_sql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
-- ---
-- Globals
-- ---

-- SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
-- SET FOREIGN_KEY_CHECKS=0;

-- ---
-- Table 'users'
--
-- ---

DROP TABLE IF EXISTS `users`;

CREATE TABLE `users` (
`id` INTEGER NULL AUTO_INCREMENT DEFAULT NULL,
`uid` INTEGER NOT NULL DEFAULT NULL,
`provider` VARCHAR NOT NULL DEFAULT 'NULL',
`points` INTEGER NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);

-- ---
-- Table 'questions'
--
-- ---

DROP TABLE IF EXISTS `questions`;

CREATE TABLE `questions` (
`id` INTEGER NULL AUTO_INCREMENT DEFAULT NULL,
`user_id` INTEGER NOT NULL DEFAULT NULL,
`content` VARCHAR NOT NULL DEFAULT 'NULL',
`votes` INTEGER NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);

-- ---
-- Table 'comments'
--
-- ---

DROP TABLE IF EXISTS `comments`;

CREATE TABLE `comments` (
`id` INTEGER NULL AUTO_INCREMENT DEFAULT NULL,
`comment_on` INTEGER NOT NULL DEFAULT answer or question,
`subject_id` INTEGER NOT NULL DEFAULT NULL,
`user_id` INTEGER NOT NULL DEFAULT NULL,
`content` VARCHAR NOT NULL DEFAULT 'NULL',
`votes` INTEGER NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);

-- ---
-- Table 'tags'
--
-- ---

DROP TABLE IF EXISTS `tags`;

CREATE TABLE `tags` (
`id` INTEGER NULL AUTO_INCREMENT DEFAULT NULL,
`name` VARCHAR NOT NULL DEFAULT 'NULL',
`subscribed_users` VARCHAR NOT NULL DEFAULT 'NULL' COMMENT 'This will be like a list of the ids of users that have this ',
`subscribed_questions` VARCHAR NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);

-- ---
-- Table 'answers'
--
-- ---

DROP TABLE IF EXISTS `answers`;

CREATE TABLE `answers` (
`id` INTEGER NULL AUTO_INCREMENT DEFAULT NULL,
`question_id` INTEGER NOT NULL DEFAULT NULL,
`content` VARCHAR NOT NULL DEFAULT 'NULL',
`votes` INTEGER NULL DEFAULT NULL,
`user_id` INTEGER NOT NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);

-- ---
-- Foreign Keys
-- ---

ALTER TABLE `questions` ADD FOREIGN KEY (user_id) REFERENCES `users` (`id`);
ALTER TABLE `comments` ADD FOREIGN KEY (user_id) REFERENCES `users` (`id`);
ALTER TABLE `answers` ADD FOREIGN KEY (question_id) REFERENCES `questions` (`id`);
ALTER TABLE `answers` ADD FOREIGN KEY (user_id) REFERENCES `users` (`id`);

-- ---
-- Table Properties
-- ---

-- ALTER TABLE `users` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ALTER TABLE `questions` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ALTER TABLE `comments` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ALTER TABLE `tags` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ALTER TABLE `answers` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ---
-- Test Data
-- ---

-- INSERT INTO `users` (`id`,`uid`,`provider`,`points`) VALUES
-- ('','','','');
-- INSERT INTO `questions` (`id`,`user_id`,`content`,`votes`) VALUES
-- ('','','','');
-- INSERT INTO `comments` (`id`,`comment_on`,`subject_id`,`user_id`,`content`,`votes`) VALUES
-- ('','','','','','');
-- INSERT INTO `tags` (`id`,`name`,`subscribed_users`,`subscribed_questions`) VALUES
-- ('','','','');
-- INSERT INTO `answers` (`id`,`question_id`,`content`,`votes`,`user_id`) VALUES
-- ('','','','','');
5 changes: 5 additions & 0 deletions spec/models/answer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Answer, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Comment, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Question, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Tag, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading

0 comments on commit 33cb90a

Please sign in to comment.