Skip to content

Commit

Permalink
Test eager loading
Browse files Browse the repository at this point in the history
  • Loading branch information
cyangle committed Oct 4, 2022
1 parent ec6ccd5 commit 566fec0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
26 changes: 26 additions & 0 deletions scripts/migrations/20221004003152781_add_user_tables.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class AddUserTables < Jennifer::Migration::Base
def up
create_table(:test_users) do |t|
t.integer :id, {:primary => true, :null => false, :auto_increment => true}
t.string :email
end

create_table(:teams) do |t|
t.integer :id, {:primary => true, :null => false, :auto_increment => true}
t.string :name, {:null => false}
end

create_table(:team_members) do |t|
t.integer :id, {:primary => true, :null => false, :auto_increment => true}
t.integer :role, {:null => false}
t.reference :test_user, :integer, {:null => false}
t.reference :team, :integer, {:null => false}
end
end

def down
drop_table :test_users
drop_table :teams
drop_table :team_members
end
end
5 changes: 3 additions & 2 deletions spec/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ def set_default_configuration

Jennifer::Config.configure do |conf|
conf.read(File.join(__DIR__, "../scripts/database.yml"), Spec.adapter)
conf.logger = Spec.logger
conf.logger = ::Log.for("db", :debug)
# conf.logger.level = :debug
conf.user = ENV["DB_USER"] if ENV["DB_USER"]?
conf.password = ENV["DB_PASSWORD"] if ENV["DB_PASSWORD"]?
conf.verbose_migrations = false
conf.local_time_zone_name = "Europe/Kiev"
end

Log.setup "db", :debug, Spec.logger_backend
Log.setup(:debug)
# Log.setup "db", :debug, Spec.logger_backend
# Log.setup "db", :debug, Log::IOBackend.new(formatter: Jennifer::DBFormat)
end

Expand Down
40 changes: 40 additions & 0 deletions spec/test_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "../spec/spec_helper"

class TestUser < Jennifer::Model::Base
mapping(
id: Primary32,
email: String?,
)
has_many :memberships, TeamMember
end

class TeamMember < Jennifer::Model::Base
mapping(
id: Primary32,
test_user_id: Int32,
team_id: Int32,
role: Int32,
)
belongs_to :team, Team
belongs_to :test_user, TestUser
end

class Team < Jennifer::Model::Base
mapping(
id: Primary32,
name: String,
)
has_many :members, TeamMember
end

it "eager loads" do
puts 1
u = TestUser.new({email: "test@example.com"})
u.save
t = Team.new({name: "team"})
t.save
tm = TeamMember.new({test_user_id: u.id, team_id: t.id, role: 1})
tm.save
debugger
all = Team.all.eager_load(members: [:test_user]).to_a
end

0 comments on commit 566fec0

Please sign in to comment.