From b5ab6702deb3b1223aa6788cf942de60870007c5 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Thu, 13 Oct 2011 18:49:39 -0600 Subject: [PATCH] modified schema to support chat rooms --- db/schema.sql | 12 +++- lib/Chatty/Schema/Result/Account.pm | 32 +++++++++- lib/Chatty/Schema/Result/Message.pm | 35 ++++++++++- lib/Chatty/Schema/Result/Room.pm | 94 +++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 lib/Chatty/Schema/Result/Room.pm diff --git a/db/schema.sql b/db/schema.sql index 7fd9824..70a5a24 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -1,18 +1,26 @@ PRAGMA foreign_keys = ON; +CREATE TABLE room ( + id INTEGER PRIMARY KEY, + name TEXT, + created TIMESTAMP DEFAULT NOW +); + CREATE TABLE account ( id INTEGER PRIMARY KEY, email TEXT, username TEXT UNIQUE, password TEXT NOT NULL, - status TEXT DEFAULT 'active' + status TEXT DEFAULT 'active', + current_room INTEGER REFERENCES room(id) ); CREATE TABLE message ( id INTEGER PRIMARY KEY, - posted TIMESTAMP, + posted TIMESTAMP DEFAULT NOW, author INTEGER REFERENCES account(id), + room INTEGER REFERENCES room(id), content TEXT ); diff --git a/lib/Chatty/Schema/Result/Account.pm b/lib/Chatty/Schema/Result/Account.pm index 8e55393..1432ed5 100644 --- a/lib/Chatty/Schema/Result/Account.pm +++ b/lib/Chatty/Schema/Result/Account.pm @@ -50,6 +50,12 @@ __PACKAGE__->table("account"); default_value: 'active' is_nullable: 1 +=head2 current_room + + data_type: 'integer' + is_foreign_key: 1 + is_nullable: 1 + =cut __PACKAGE__->add_columns( @@ -63,12 +69,34 @@ __PACKAGE__->add_columns( { data_type => "text", is_nullable => 0 }, "status", { data_type => "text", default_value => "active", is_nullable => 1 }, + "current_room", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->add_unique_constraint("username_unique", ["username"]); =head1 RELATIONS +=head2 current_room + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "current_room", + "Chatty::Schema::Result::Room", + { id => "current_room" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "CASCADE", + }, +); + =head2 messages Type: has_many @@ -85,8 +113,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-13 16:46:39 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:pPJdUbHgHvUo4FxblDaJ2g +# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-13 18:47:53 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:l1E3sAYHA5mK6RiNszOTzA # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/lib/Chatty/Schema/Result/Message.pm b/lib/Chatty/Schema/Result/Message.pm index 796a797..792713d 100644 --- a/lib/Chatty/Schema/Result/Message.pm +++ b/lib/Chatty/Schema/Result/Message.pm @@ -32,6 +32,7 @@ __PACKAGE__->table("message"); =head2 posted data_type: 'timestamp' + default_value: NOW is_nullable: 1 =head2 author @@ -40,6 +41,12 @@ __PACKAGE__->table("message"); is_foreign_key: 1 is_nullable: 1 +=head2 room + + data_type: 'integer' + is_foreign_key: 1 + is_nullable: 1 + =head2 content data_type: 'text' @@ -51,9 +58,11 @@ __PACKAGE__->add_columns( "id", { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, "posted", - { data_type => "timestamp", is_nullable => 1 }, + { data_type => "timestamp", default_value => \"NOW", is_nullable => 1 }, "author", { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, + "room", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, "content", { data_type => "text", is_nullable => 1 }, ); @@ -61,6 +70,26 @@ __PACKAGE__->set_primary_key("id"); =head1 RELATIONS +=head2 room + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "room", + "Chatty::Schema::Result::Room", + { id => "room" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "CASCADE", + }, +); + =head2 author Type: belongs_to @@ -82,8 +111,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-13 16:46:39 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dgMXiWuIhmCQeExkpcxorA +# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-13 18:47:53 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:q1cq2bWftQPoo8huOftzMQ # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/lib/Chatty/Schema/Result/Room.pm b/lib/Chatty/Schema/Result/Room.pm new file mode 100644 index 0000000..7ae5643 --- /dev/null +++ b/lib/Chatty/Schema/Result/Room.pm @@ -0,0 +1,94 @@ +package Chatty::Schema::Result::Room; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +use strict; +use warnings; + +use Moose; +use MooseX::NonMoose; +use namespace::autoclean; +extends 'DBIx::Class::Core'; + +__PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp"); + +=head1 NAME + +Chatty::Schema::Result::Room + +=cut + +__PACKAGE__->table("room"); + +=head1 ACCESSORS + +=head2 id + + data_type: 'integer' + is_auto_increment: 1 + is_nullable: 0 + +=head2 name + + data_type: 'text' + is_nullable: 1 + +=head2 created + + data_type: 'timestamp' + default_value: NOW + is_nullable: 1 + +=cut + +__PACKAGE__->add_columns( + "id", + { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, + "name", + { data_type => "text", is_nullable => 1 }, + "created", + { data_type => "timestamp", default_value => \"NOW", is_nullable => 1 }, +); +__PACKAGE__->set_primary_key("id"); + +=head1 RELATIONS + +=head2 accounts + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "accounts", + "Chatty::Schema::Result::Account", + { "foreign.current_room" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +=head2 messages + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "messages", + "Chatty::Schema::Result::Message", + { "foreign.room" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-13 18:47:53 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:2QSf3vZfv8xVbUKtsKsvDg + + +# You can replace this text with custom code or comments, and it will be preserved on regeneration +__PACKAGE__->meta->make_immutable; +1; -- 2.45.2