From 527c99eb377fb18d95544fe3ee09d8504cb04497 Mon Sep 17 00:00:00 2001 From: Nick McDonnough Date: Mon, 5 Jan 2015 10:41:41 -0600 Subject: [PATCH 1/3] Initial commit for active record migrations exercise --- .gitignore | 5 +++++ Gemfile | 5 +++++ Gemfile.lock | 39 ++++++++++++++++++++++++++++++++ README.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ Rakefile | 11 +++++++++ db/config.yml | 11 +++++++++ db/schema.rb | 23 +++++++++++++++++++ 7 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 README.md create mode 100644 Rakefile create mode 100644 db/config.yml create mode 100644 db/schema.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..88d11e1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.swp +*.swo +.DS_Store +.bundle +vendor/bundle diff --git a/Gemfile b/Gemfile new file mode 100644 index 00000000..337e89d6 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gem 'pg' +gem 'activerecord', '~> 4.1.0' +gem 'active_record_tasks', '~> 1.1.0' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 00000000..5fa409d9 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,39 @@ +GEM + remote: https://rubygems.org/ + specs: + active_record_tasks (1.1.0) + activerecord (>= 4.0) + rainbow + rake + activemodel (4.1.8) + activesupport (= 4.1.8) + builder (~> 3.1) + activerecord (4.1.8) + activemodel (= 4.1.8) + activesupport (= 4.1.8) + arel (~> 5.0.0) + activesupport (4.1.8) + i18n (~> 0.6, >= 0.6.9) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.1) + tzinfo (~> 1.1) + arel (5.0.1.20140414130214) + builder (3.2.2) + i18n (0.7.0) + json (1.8.1) + minitest (5.5.0) + pg (0.18.0) + rainbow (2.0.0) + rake (10.4.2) + thread_safe (0.3.4) + tzinfo (1.2.2) + thread_safe (~> 0.1) + +PLATFORMS + ruby + +DEPENDENCIES + active_record_tasks (~> 1.1.0) + activerecord (~> 4.1.0) + pg diff --git a/README.md b/README.md new file mode 100644 index 00000000..63c03461 --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +## Setup + +Run the following commands to clone down the exercise and get your Gems installed. +```console +$ git clone https://github.com/makersquare/ruby-course migrations +$ cd migrations +$ git checkout migrations + +--- now in your VM navigate to the migrations directory + +$ bundle install +``` + +One call Gems are installed you need to use some Rake tasks to create your database (just the DB, not tables yet). You can do this by running `bundle exec rake db:create`. If successful there will be no feedback (isn't that nice?). You can double check that it worked by running `psql -l` in your terminal and looking for databases called `migrations_dev` and `migrations_test`. + + +# Exercise 1 + +In this exercise you will will write migrations (and run them!) to build out a database for a fictional wholesale business. You will use user stories to determine what attributes each item needs which should inform on how to structure your database. + +## User Stories + +1. As a customer I want to place orders for products +2. As a customer I'd like to see the total cost of my order +3. As a seller I'd like to add new products to inventory (description, price) +4. As a seller I'd like to view all orders by date + +Design your schema in any way you'd like. Once complete begin writing your migrations to build the schema you imagined. Remember: we want to build our schema very incrementally so if two changes have absolutely nothing to do with one another then maybe they can go into separate migrations. + +## Updates + +After entering only a few products into inventory we discovered that we had many products with very similar descriptions. For instance two different pairs of brown gloves simply have descriptions from the manufacturer as "BROWN GLOVES" even though one is leather and one is wool. The warehouse team has come up with a product numbering system. You now need to add this new attribute to the products table. + + + +# Exercise 2 + +You are creating a simple blogging platform so users can create simple posts and view other posts by other users. There are some rules on this service and so an administrator + + +## User Stories + +1. As a user I'd like to sign up +2. As a user I'd like to create posts +3. As a user I'd like to organize posts by their title +4. As a user I'd like to create posts with very long bodies +5. As an admin I'd like to view all users over a given age + +## Updates + +Your customer has come back again to make change to the application. Now he tells you that he will no longer ask users for their age. He also says that he wants to be able to find users by their zipcode. + +The senior dev leading your team is worried about people signing up with duplicate email addresses. ActiveRecord provides a way for you to validate uniqueness but he wants to be extra sure by adding a constraint to your the `users` table. + +In the past when a post was deemed to be inappropriate it was simply deleted from the database. The owners of Blogtastic do not like losing this data and have come up with a way to not show inappropriate posts without permanently removing them from the database: flags. When an admin sees an inappropriate post he can flag it. Then anywhere in the application, when retrieving all posts, ones that have been flagged will not be returned. + +There's more... + +## Resources + +You can ask me questions! +You can also refer to [this](http://learn.makersquare.com/courses/ruby/documentation/active_record_configuration.mdx) tutorial. diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..488a5913 --- /dev/null +++ b/Rakefile @@ -0,0 +1,11 @@ +require 'active_record_tasks' + +ActiveRecordTasks.configure do |config| + # These are all the default values + config.db_dir = 'db' + config.db_config_path = 'db/config.yml' + config.env = 'development' +end + +# Run this AFTER you've configured +ActiveRecordTasks.load_tasks diff --git a/db/config.yml b/db/config.yml new file mode 100644 index 00000000..2a90f368 --- /dev/null +++ b/db/config.yml @@ -0,0 +1,11 @@ +development: + adapter: postgresql + database: migrations_dev + pool: 5 + timeout: 5000 + +test: + adapter: postgresql + database: migrations_test + pool: 5 + timeout: 5000 diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..acd5b07c --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# 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: 20150105104003) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "test", force: true do |t| + t.string "name" + end + +end From bd1e6420ba2e36dc40c6758e413d14a97062d14e Mon Sep 17 00:00:00 2001 From: Nick McDonnough Date: Mon, 5 Jan 2015 10:50:51 -0600 Subject: [PATCH 2/3] Instructions update --- README.md | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 63c03461..cb13ef8c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ ## Setup Run the following commands to clone down the exercise and get your Gems installed. + ```console $ git clone https://github.com/makersquare/ruby-course migrations $ cd migrations @@ -13,9 +14,29 @@ $ bundle install One call Gems are installed you need to use some Rake tasks to create your database (just the DB, not tables yet). You can do this by running `bundle exec rake db:create`. If successful there will be no feedback (isn't that nice?). You can double check that it worked by running `psql -l` in your terminal and looking for databases called `migrations_dev` and `migrations_test`. +ActiveRecord generally assumes a single database per application. Even though each exercise is for a totally different application we'll create all the tables in a single database to keep things simple. + # Exercise 1 +When we started our exploration of databases we used the PG gem to create a simple table called `classmates` that contained information about your classmates. Do this again however instead of writing out the raw SQL you will write an ActiveRecord migration. + +Your table should have the following columns. + +* name +* age +* hair_color + +Once you've written *and* run this migration then it's time to make some changes. + +* get rid of the name column +* add a first_name column +* add a last_name column +* add a social security number column with a unique constraint + + +# Exercise 2 + In this exercise you will will write migrations (and run them!) to build out a database for a fictional wholesale business. You will use user stories to determine what attributes each item needs which should inform on how to structure your database. ## User Stories @@ -31,12 +52,12 @@ Design your schema in any way you'd like. Once complete begin writing your migra After entering only a few products into inventory we discovered that we had many products with very similar descriptions. For instance two different pairs of brown gloves simply have descriptions from the manufacturer as "BROWN GLOVES" even though one is leather and one is wool. The warehouse team has come up with a product numbering system. You now need to add this new attribute to the products table. +Note: this new attribute is a unique identifier that we can search by. We would like this attribute to be an index to speed up the search process. -# Exercise 2 - -You are creating a simple blogging platform so users can create simple posts and view other posts by other users. There are some rules on this service and so an administrator +# Exercise 3 +You are rebuilding the Blogtastic platform so users can create simple posts and view other posts by other users. Use the following user stories to design a database schema and then write your migrations. ## User Stories @@ -54,7 +75,6 @@ The senior dev leading your team is worried about people signing up with duplica In the past when a post was deemed to be inappropriate it was simply deleted from the database. The owners of Blogtastic do not like losing this data and have come up with a way to not show inappropriate posts without permanently removing them from the database: flags. When an admin sees an inappropriate post he can flag it. Then anywhere in the application, when retrieving all posts, ones that have been flagged will not be returned. -There's more... ## Resources From 16add2f5f7d5eb5f30ef15eb04b237967913fe66 Mon Sep 17 00:00:00 2001 From: Nick McDonnough Date: Mon, 5 Jan 2015 12:41:26 -0600 Subject: [PATCH 3/3] Deleted stuff lololololzzzz --- README.md | 82 ------------------------------------------------------- 1 file changed, 82 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index cb13ef8c..00000000 --- a/README.md +++ /dev/null @@ -1,82 +0,0 @@ -## Setup - -Run the following commands to clone down the exercise and get your Gems installed. - -```console -$ git clone https://github.com/makersquare/ruby-course migrations -$ cd migrations -$ git checkout migrations - ---- now in your VM navigate to the migrations directory - -$ bundle install -``` - -One call Gems are installed you need to use some Rake tasks to create your database (just the DB, not tables yet). You can do this by running `bundle exec rake db:create`. If successful there will be no feedback (isn't that nice?). You can double check that it worked by running `psql -l` in your terminal and looking for databases called `migrations_dev` and `migrations_test`. - -ActiveRecord generally assumes a single database per application. Even though each exercise is for a totally different application we'll create all the tables in a single database to keep things simple. - - -# Exercise 1 - -When we started our exploration of databases we used the PG gem to create a simple table called `classmates` that contained information about your classmates. Do this again however instead of writing out the raw SQL you will write an ActiveRecord migration. - -Your table should have the following columns. - -* name -* age -* hair_color - -Once you've written *and* run this migration then it's time to make some changes. - -* get rid of the name column -* add a first_name column -* add a last_name column -* add a social security number column with a unique constraint - - -# Exercise 2 - -In this exercise you will will write migrations (and run them!) to build out a database for a fictional wholesale business. You will use user stories to determine what attributes each item needs which should inform on how to structure your database. - -## User Stories - -1. As a customer I want to place orders for products -2. As a customer I'd like to see the total cost of my order -3. As a seller I'd like to add new products to inventory (description, price) -4. As a seller I'd like to view all orders by date - -Design your schema in any way you'd like. Once complete begin writing your migrations to build the schema you imagined. Remember: we want to build our schema very incrementally so if two changes have absolutely nothing to do with one another then maybe they can go into separate migrations. - -## Updates - -After entering only a few products into inventory we discovered that we had many products with very similar descriptions. For instance two different pairs of brown gloves simply have descriptions from the manufacturer as "BROWN GLOVES" even though one is leather and one is wool. The warehouse team has come up with a product numbering system. You now need to add this new attribute to the products table. - -Note: this new attribute is a unique identifier that we can search by. We would like this attribute to be an index to speed up the search process. - - -# Exercise 3 - -You are rebuilding the Blogtastic platform so users can create simple posts and view other posts by other users. Use the following user stories to design a database schema and then write your migrations. - -## User Stories - -1. As a user I'd like to sign up -2. As a user I'd like to create posts -3. As a user I'd like to organize posts by their title -4. As a user I'd like to create posts with very long bodies -5. As an admin I'd like to view all users over a given age - -## Updates - -Your customer has come back again to make change to the application. Now he tells you that he will no longer ask users for their age. He also says that he wants to be able to find users by their zipcode. - -The senior dev leading your team is worried about people signing up with duplicate email addresses. ActiveRecord provides a way for you to validate uniqueness but he wants to be extra sure by adding a constraint to your the `users` table. - -In the past when a post was deemed to be inappropriate it was simply deleted from the database. The owners of Blogtastic do not like losing this data and have come up with a way to not show inappropriate posts without permanently removing them from the database: flags. When an admin sees an inappropriate post he can flag it. Then anywhere in the application, when retrieving all posts, ones that have been flagged will not be returned. - - -## Resources - -You can ask me questions! -You can also refer to [this](http://learn.makersquare.com/courses/ruby/documentation/active_record_configuration.mdx) tutorial.