Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished first version of the SqlLoader module, which loads the sql o…
…bjects required for each object type like Friend or TeamMember, which uses views/rules. need to hook it into the application
- Loading branch information
1 parent
3d530af
commit c589142
Showing
17 changed files
with
251 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
# Load the rails application | ||
require File.expand_path('../application', __FILE__) | ||
require File.expand_path('../../lib/devise_names', __FILE__) | ||
|
||
# Load the Sinatra GuestApp | ||
require File.expand_path('../../lib/guest/guest_app', __FILE__) | ||
|
||
# Load the SqlLoader Module to help create extra SQL objects | ||
require File.expand_path('../../db/sql_loader', __FILE__) | ||
|
||
# Initialize the rails application | ||
Sk8::Application.initialize! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#this is way too fragile, but I'm not sure | ||
# how to specify the order in which objects are created/dropped | ||
# short of creating a configuration yaml file that | ||
# specifies the dependencies between each object | ||
|
||
#there's probably a million better ways to do this | ||
# but it's my first time creating a module like this, | ||
# sue me | ||
|
||
module SqlLoader | ||
class SqlLoaderBase | ||
CREATE_START = 'Creating objects for' | ||
CREATE_ERRORS = 'Encountered errors creating objects:' | ||
CREATE_DELIMETER = '=================================' | ||
NO_CREATE_ERRORS = 'All objects created successfully!' | ||
|
||
DROP_START = 'Dropping objects for' | ||
DROP_ERRORS = 'Encountered errors dropping objects:' | ||
DROP_DELIMETER = '=================================' | ||
NO_DROP_ERRORS = 'All objects dropped successfully!' | ||
|
||
SQL_LOADER_ROOT = File.join(Rails.root, 'db/sql_loader') | ||
Dir[File.join(SQL_LOADER_ROOT, "*.rb")].each {|rb| require rb } | ||
|
||
def self.relative_name | ||
name.to_s.split('::').last | ||
end | ||
|
||
def self.get_sql_files | ||
#for each type, append it's sql files into an array in an order that can be executed | ||
sql_object_types = %w[table view index rule function] | ||
|
||
sql_object_types.each_with_object([]) do |type, files| | ||
files.concat(Dir[File.join(SQL_LOADER_ROOT, relative_name.underscore, "#{type}_*.sql")]) | ||
end | ||
end | ||
|
||
def self.create | ||
errors = [] | ||
|
||
puts "#{CREATE_START} #{relative_name}" | ||
|
||
get_sql_files.each do |sql| | ||
begin | ||
ActiveRecord::Base.connection.execute(IO.read(sql)) | ||
rescue | ||
errors << $! | ||
end | ||
end | ||
|
||
create_error_output(errors) | ||
end | ||
|
||
def self.drop | ||
errors = [] | ||
|
||
puts "#{DROP_START} #{relative_name}" | ||
|
||
get_sql_files.reverse.each do |f| | ||
sql = drop_statement(f) | ||
|
||
begin | ||
puts "============= " + sql | ||
ActiveRecord::Base.connection.execute(sql) | ||
rescue | ||
errors << $! | ||
end | ||
end | ||
|
||
drop_error_output(errors) | ||
end | ||
|
||
def self.drop_statement(filename) | ||
case File.basename(filename, '.sql') | ||
when /(^table.*)/i | ||
return "DROP TABLE IF EXISTS#{$1};" | ||
when /(^view.*)/i | ||
return "DROP VIEW IF EXISTS #{$1};" | ||
when /(^rule_(.*)_(insert|delete|update))/i | ||
return "DROP RULE IF EXISTS #{$1} ON #{$2};" | ||
end | ||
end | ||
|
||
# ERROR OUTPUT | ||
|
||
def self.create_error_output errors | ||
out = "Creating #{relative_name} Objects: " | ||
|
||
if errors.any? | ||
out += "#{CREATE_ERRORS}\n" | ||
errors.each {|e| out += "\n\n#{CREATE_DELIMETER}\n#{e}\n" } | ||
|
||
puts out | ||
raise "SqlLoader Exception" | ||
else | ||
out += NO_CREATE_ERRORS | ||
puts out | ||
end | ||
end | ||
|
||
def self.drop_error_output errors | ||
out = "Dropping #{relative_name} Objects: " | ||
|
||
if errors.any? | ||
out += "#{DROP_ERRORS}\n" | ||
errors.each {|e| out += "\n\n#{DROP_DELIMETER}\n#{e}\n" } | ||
|
||
puts out | ||
raise "SqlLoader Exception" | ||
else | ||
out += NO_DROP_ERRORS | ||
puts out | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module SqlLoader | ||
class Friend < SqlLoaderBase | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE RULE rule_view_friends_delete AS | ||
ON DELETE TO view_friends DO INSTEAD | ||
|
||
UPDATE friendships SET active = false | ||
WHERE id = OLD.friendship_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE VIEW view_friends AS | ||
|
||
SELECT m.id as member_id, f.id as friendship_id, m2.* | ||
FROM members m | ||
JOIN friendships f | ||
ON (m.id = f.member_requested_id) OR (m.id = f.member_requesting_id) | ||
JOIN members m2 | ||
ON ((f.member_requested_id = m2.id) OR (f.member_requesting_id = m2.id)) | ||
AND (m.id != m2.id) | ||
WHERE f.active = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module SqlLoader | ||
class TeamCaptain < SqlLoaderBase | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module SqlLoader | ||
class TeamMember < SqlLoaderBase | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
CREATE VIEW view_team_members AS | ||
|
||
SELECT t.id as team_id, tm.id as team_request_id, this_member.id as member_id, team_mems.* | ||
FROM members this_member | ||
INNER JOIN team_requests active_tm | ||
ON (this_member.id = active_tm.member_requesting_id | ||
OR this_member.id = active_tm.member_requested_id) | ||
AND active_tm.active = true | ||
INNER JOIN teams t | ||
ON active_tm.team_id = t.id | ||
INNER JOIN team_requests tm | ||
ON t.id = tm.team_id | ||
AND tm.active = true | ||
INNER JOIN members team_mems | ||
ON (team_mems.id = tm.member_requesting_id and t.creator_id = this_member.id) | ||
OR (team_mems.id = tm.member_requested_id and t.creator_id != this_member.id) | ||
WHERE (this_member.id != team_mems.id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters