Permalink
Browse files

Added Skiima Migrations

  • Loading branch information...
1 parent f872112 commit 079fb182c766ccca3bf4b021c91dc4315dde54c2 @dcunited001 committed Mar 19, 2012
View
@@ -1,56 +1,29 @@
-# SQLite version 3.x
-# gem install sqlite3-ruby (not necessary on OS X Leopard)
-#development:
-# adapter: mysql
-# encoding: utf8
-# reconnect: false
-# database: testapp_dev
-# pool: 5
-# username: root
-# password: maffman
-# host: localhost
-
development:
adapter: postgresql
encoding: utf8
reconnect: false
database: skate_dev
pool: 5
- username: testapp
- password: testapp
+ username: <%= ENV['SKATE_DB_USER'] || 'user' %>
+ password: <%= ENV['SKATE_DB_PASS'] || 'pass' %>
host: localhost
-#development:
-# adapter: sqlite3
-# dbfile: db/testapp_test.rb
-# database: testapp_test
-
-
test:
adapter: postgresql
encoding: utf8
reconnect: false
database: skate_test
pool: 5
- username: testapp
- password: testapp
+ username: <%= ENV['SKATE_DB_USER'] || 'user' %>
+ password: <%= ENV['SKATE_DB_PASS'] || 'pass' %>
host: localhost
-
-# Warning: The database defined as "test" will be erased and
-# re-generated from your development database when you run "rake".
-# Do not set this db to the same as development or production.
-#test:
-# adapter: sqlite3
-# dbfile: db/testapp_test.rb
-# database: testapp_test
-
production:
adapter: postgresql
encoding: utf8
reconnect: false
database: skate_prod
pool: 5
- username: testapp
- password: testapp
+ username: <%= ENV['SKATE_DB_USER'] || 'user' %>
+ password: <%= ENV['SKATE_DB_PASS'] || 'pass' %>
host: localhost
@@ -0,0 +1,6 @@
+Skiima.setup do |config|
+ config.root_path = Rails.root
+ config.config_path = 'config'
+ config.scripts_path = 'db/skiima'
+ config.locale = :en
+end
@@ -0,0 +1,13 @@
+class AddFriendsObjects < ActiveRecord::Migration
+ def up
+ ski = Skiima.new(Rails.env.to_sym)
+ ski.up(:friends)
+ # Skiima.up(Rails.env.to_sym, :friends)
+ end
+
+ def down
+ ski = Skiima.new(Rails.env.to_sym)
+ ski.down(:friends)
+ # Skiima.up(Rails.env.to_sym, :friends)
+ end
+end
@@ -0,0 +1,11 @@
+class AddTeamMembersObjects < ActiveRecord::Migration
+ def up
+ ski = Skiima.new(Rails.env.to_sym)
+ ski.up(:team_members)
+ end
+
+ def down
+ ski = Skiima.new(Rails.env.to_sym)
+ ski.down(:team_members)
+ end
+end
View
@@ -1,3 +1,4 @@
+# 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.
@@ -10,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121223235959) do
+ActiveRecord::Schema.define(:version => 20120319104323) do
create_table "addresses", :force => true do |t|
t.integer "addressable_id"
@@ -55,8 +56,8 @@
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
- t.datetime "created_at"
- t.datetime "updated_at"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
t.string "alias"
t.string "first_name"
t.string "last_name"
@@ -129,8 +130,8 @@
t.integer "role_id"
t.integer "rollable_id"
t.string "rollable_type"
- t.datetime "created_at"
- t.datetime "updated_at"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
end
create_table "states", :force => true do |t|
View
@@ -0,0 +1,14 @@
+---
+friends:
+ postgresql:
+ current:
+ - view.view_members_clean
+ - view.view_friends
+ - rule.rule_view_friends_d.view_friends
+ - rule.rule_view_friends_i.view_friends
+team_members:
+ postgresql:
+ current:
+ - view.view_member_teams
+ - view.view_team_members
+ - view.view_team_mates
@@ -0,0 +1,6 @@
+DROP RULE IF EXISTS rule_view_friends_d on view_friends;
+CREATE RULE rule_view_friends_d AS
+ON DELETE TO view_friends DO INSTEAD
+
+UPDATE friendships SET active = false
+WHERE id = OLD.friendship_id;
@@ -0,0 +1,8 @@
+DROP RULE IF EXISTS rule_view_friends_i on view_friends;
+CREATE RULE rule_view_friends_insert AS
+ON INSERT TO view_friends DO INSTEAD
+
+-- WHY YOU NO WORKING ??
+INSERT INTO friendships
+(member_requesting_id, member_requested_id, approved, rejected, active, created_at, updated_at) VALUES
+(NEW.member_id, NEW.id, true, false, true, current_timestamp, current_timestamp);
@@ -0,0 +1,11 @@
+DROP VIEW IF EXISTS view_friends;
+CREATE VIEW view_friends AS
+
+SELECT m.id as member_id, f.id as friendship_id, m2.*
+FROM view_members_clean m
+ JOIN friendships f
+ ON (m.id = f.member_requested_id) OR (m.id = f.member_requesting_id)
+JOIN view_members_clean m2
+ ON ((f.member_requested_id = m2.id) OR (f.member_requesting_id = m2.id))
+ AND (m.id != m2.id)
+WHERE f.active = true;
@@ -0,0 +1,30 @@
+DROP VIEW IF EXISTS view_friends;
+CREATE VIEW view_members_clean AS
+
+SELECT
+ id,
+ email,
+ sign_in_count,
+ current_sign_in_at,
+ last_sign_in_at,
+ current_sign_in_ip,
+ last_sign_in_ip,
+ created_at,
+ updated_at,
+ alias,
+ first_name,
+ last_name,
+ rink_id,
+ birthday,
+ phone,
+ verified,
+ original_verified_date,
+ last_verified_date,
+ current_member,
+ original_membership_date,
+ last_membership_date,
+ renewal_months,
+ pro_skater,
+ suspended,
+ suspended_until
+FROM members;
@@ -0,0 +1,14 @@
+DROP VIEW IF EXISTS view_member_teams;
+CREATE VIEW view_member_teams AS
+
+SELECT m.id as member_id, t.*
+FROM members m
+
+INNER JOIN team_requests active_tm
+ ON ((m.id = active_tm.member_requesting_id and active_tm.incoming = true)
+ OR (m.id = active_tm.member_requested_id and active_tm.incoming = false))
+
+INNER JOIN teams t
+ ON active_tm.team_id = t.id
+
+WHERE active_tm.approved = true
@@ -0,0 +1,27 @@
+DROP VIEW IF EXISTS view_team_mates;
+CREATE VIEW view_team_mates AS
+
+SELECT m.id as member_id, vtm.*
+FROM members m
+
+INNER JOIN team_requests active_tm
+ ON ((m.id = active_tm.member_requesting_id and active_tm.incoming = true)
+ OR (m.id = active_tm.member_requested_id and active_tm.incoming = false))
+ AND active_tm.active = true
+
+INNER JOIN teams t
+ ON active_tm.team_id = t.id
+
+INNER JOIN view_team_members vtm
+ ON t.id = vtm.team_id
+
+WHERE m.id != vtm.id
+
+-- =============================================================================
+-- team members probably should not be able to be deleted from here
+-- CREATE RULE rule_view_team_mates_delete AS
+-- ON DELETE TO view_team_mates DO INSTEAD
+
+-- UPDATE team_requests SET active = false
+-- WHERE id = OLD.team_request_id;
+-- =============================================================================
@@ -0,0 +1,55 @@
+DROP VIEW IF EXISTS view_team_members;
+CREATE VIEW view_team_members AS
+
+SELECT t.id as team_id, tm.id as team_request_id, team_mems.*
+FROM teams t
+
+INNER JOIN team_requests tm
+ ON t.id = tm.team_id
+ AND tm.active = true
+
+INNER JOIN view_members_clean team_mems
+ --if it's an incoming request, the member requesting is the team member
+ --otherwise the member requested is the team member
+ ON (team_mems.id = tm.member_requesting_id AND tm.incoming = true)
+ OR (team_mems.id = tm.member_requested_id AND tm.incoming = false)
+
+-- =============================================================================
+-- CREATE RULE rule_view_team_members_delete AS
+-- ON DELETE TO view_team_members DO INSTEAD
+
+-- UPDATE team_requests SET active = false
+-- WHERE id = OLD.team_request_id;
+-- =============================================================================
+
+-- i'll remove these comments as I determine my strategy here.
+-- what to do if the team creator changes over the life span of the team??
+-- store an original_creator_id field in the team and team_requests to map everything
+-- or simply refuse to allow teams to change 'ownership'?? (which is lame)
+
+-- I've been trying to avoid storing the "direction" of the team request
+-- this was possible with the friends relationship
+-- but it's looking like this is impossible here
+-- since you have to key on the team creator_id
+-- to implicitly determine the "direction" of the team request
+-- and if i want to allow team's to change their creator or owner later
+-- that's going to open me up to some particularly nasty bugs
+-- which would probably silently ruin the integrity of my data
+
+-- changing team_requests table to use an "incoming" field to determine the team_member
+
+-- =============================================================================
+
+--goal of this view:
+-- to take each team
+-- and list it's members' individual team_request records (the easy part)
+-- then take each team_request record and determine
+-- which field to consider the team_member (the hard part)
+-- member_requesting_id or member_requested_id?
+
+-- this was moderately difficult to do with members and friends
+-- but appears to be very much more complex with teams and team_members
+-- although i got it to work in the original application
+-- using string substitution in the commented query below
+
+-- =============================================================================

0 comments on commit 079fb18

Please sign in to comment.