-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38c74a7
commit c0aa376
Showing
6 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateAdminUsersView::V20201202225520 < Avram::Migrator::Migration::V1 | ||
def migrate | ||
execute <<-SQL | ||
CREATE VIEW admin_users AS | ||
SELECT users.* | ||
FROM users | ||
JOIN admins on admins.name = users.name; | ||
SQL | ||
end | ||
|
||
def rollback | ||
execute "DROP VIEW admin_users;" | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
db/migrations/20201202231134_create_nickname_infos_view.cr
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,14 @@ | ||
class CreateNicknameInfosView::V20201202231134 < Avram::Migrator::Migration::V1 | ||
def migrate | ||
execute <<-SQL | ||
CREATE VIEW nickname_infos AS | ||
SELECT users.nickname, COUNT(nickname) | ||
FROM users | ||
GROUP BY nickname; | ||
SQL | ||
end | ||
|
||
def rollback | ||
execute "DROP VIEW nickname_infos;" | ||
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,16 @@ | ||
# an AdminUser is a User who's name is also found in the Admin table | ||
class AdminUser < BaseModel | ||
view do | ||
primary_key id : Int64 | ||
timestamps | ||
column name : String | ||
column age : Int32 | ||
column year_born : Int16? | ||
column nickname : String? | ||
column joined_at : Time | ||
column total_score : Int64? | ||
column average_score : Float64? | ||
column available_for_hire : Bool? | ||
has_one sign_in_credential : SignInCredential? | ||
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,6 @@ | ||
class NicknameInfo < BaseModel | ||
view do | ||
column nickname : String | ||
column count : Int64 | ||
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,23 @@ | ||
require "./spec_helper" | ||
|
||
include LazyLoadHelpers | ||
|
||
describe "views" do | ||
it "works with a primary key" do | ||
user = UserBox.create | ||
admin = AdminBox.new.name(user.name).create | ||
admin_user = AdminUser::BaseQuery.find(user.id) | ||
|
||
admin_user.name.should eq user.name | ||
end | ||
|
||
it "works without a primary key" do | ||
UserBox.new.nickname("Johnny").create | ||
UserBox.new.nickname("Johnny").create | ||
UserBox.new.nickname("Johnny").create | ||
nickname_info = NicknameInfo::BaseQuery.first | ||
|
||
nickname_info.nickname.should eq "Johnny" | ||
nickname_info.count.should eq 3 | ||
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