fix typos
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / 10_Appendices.pod
CommitLineData
d442cc9f 1=head1 NAME
2
3ab6187c 3Catalyst::Manual::Tutorial::10_Appendices - Catalyst Tutorial - Chapter 10: Appendices
d442cc9f 4
5
6=head1 OVERVIEW
7
4b4d3884 8This is B<Chapter 10 of 10> for the Catalyst tutorial.
d442cc9f 9
10L<Tutorial Overview|Catalyst::Manual::Tutorial>
11
12=over 4
13
14=item 1
15
3ab6187c 16L<Introduction|Catalyst::Manual::Tutorial::01_Intro>
d442cc9f 17
18=item 2
19
3ab6187c 20L<Catalyst Basics|Catalyst::Manual::Tutorial::02_CatalystBasics>
d442cc9f 21
22=item 3
23
3ab6187c 24L<More Catalyst Basics|Catalyst::Manual::Tutorial::03_MoreCatalystBasics>
d442cc9f 25
26=item 4
27
3ab6187c 28L<Basic CRUD|Catalyst::Manual::Tutorial::04_BasicCRUD>
d442cc9f 29
30=item 5
31
3ab6187c 32L<Authentication|Catalyst::Manual::Tutorial::05_Authentication>
d442cc9f 33
34=item 6
35
3ab6187c 36L<Authorization|Catalyst::Manual::Tutorial::06_Authorization>
d442cc9f 37
38=item 7
39
3ab6187c 40L<Debugging|Catalyst::Manual::Tutorial::07_Debugging>
d442cc9f 41
42=item 8
43
3ab6187c 44L<Testing|Catalyst::Manual::Tutorial::08_Testing>
d442cc9f 45
46=item 9
47
3ab6187c 48L<Advanced CRUD|Catalyst::Manual::Tutorial::09_AdvancedCRUD>
3533daff 49
50=item 10
51
3ab6187c 52B<10_Appendices>
d442cc9f 53
54=back
55
56
57=head1 DESCRIPTION
58
4b4d3884 59This chapter of the tutorial provides supporting information relevant to
d442cc9f 60the Catalyst tutorial.
61
62
63=head1 APPENDIX 1: CUT AND PASTE FOR POD-BASED EXAMPLES
64
65You may notice that Pod indents example code with four spaces. This
66section provides some quick advice to "un-indent" this text in common
67editors.
68
69=head2 "Un-indenting" with Vi/Vim
70
71When cutting and pasting multi-line text from Pod-based documents, the
72following vi/vim regexs can be helpful to "un-indent" the inserted text
73(do NOT type the quotes, they are only included to show spaces in the
74regex patterns). I<Note that all 3 of the regexs end in 4 spaces>:
75
76=over 4
77
813d364c 78=item *
d442cc9f 79
80":0,$s/^ "
81
82Removes four leading spaces from the entire file (from the first line,
83C<0>, to the last line, C<$>).
84
813d364c 85=item *
d442cc9f 86
87"%s/^ "
88
89A shortcut for the previous item (C<%> specifies the entire file; so
90this removes four leading spaces from every line).
91
813d364c 92=item *
d442cc9f 93
94":.,$s/^ "
95
96Removes the first four spaces from the line the cursor is on at the time
97the regex command is executed (".") to the last line of the file.
98
813d364c 99=item *
d442cc9f 100
101":.,44s/^ "
102
103Removes four leading space from the current line through line 44
104(obviously adjust the C<44> to the appropriate value in your example).
105
106=back
107
108=head2 "Un-indenting" with Emacs
109
325bc0fd 110Although the author has not used Emacs for many years (apologies to
191dee29 111the Emacs fans out there), here is a quick hint to get you started. To
d442cc9f 112replace the leading spaces of every line in a file, use:
113
114 M-x replace-regexp<RET>
115 Replace regexp: ^ <RET>
116 with: <RET>
117
118All of that will occur on the single line at the bottom of your screen.
813d364c 119Note that "<RET>" represents the return key/enter. Also, there are
d442cc9f 120four spaces after the "^" on the "Replace regexp:" line and no spaces
121entered on the last line.
122
123You can limit the replacement operation by selecting text first (depending
813d364c 124on your version of Emacs, you can either use the mouse or experiment with
125commands such as C<C-SPC> to set the mark at the cursor location and
d442cc9f 126C<C-E<lt>> and C<C-E<gt>> to set the mark at the beginning and end of the
127file respectively.
128
325bc0fd 129Also, Stefan Kangas sent in the following tip about an alternate
130approach using the command C<indent-region> to redo the indentation
131for the currently selected region (adhering to indent rules in the
d672dfd7 132current major mode). You can run the command by typing M-x
133indent-region or pressing the default keybinding C-M-\ in cperl-mode.
325bc0fd 134Additional details can be found here:
135
d672dfd7 136L<http://www.gnu.org/software/emacs/manual/html_node/emacs/Indentation-Commands.html>
325bc0fd 137
d442cc9f 138
e179eee0 139=head1 APPENDIX 2: USING POSTGRESQL AND MYSQL
d442cc9f 140
141The main database used in this tutorial is the very simple yet powerful
3dc4813b 142SQLite (L<http://www.sqlite.org>). This section provides information
1eaebcbf 143that can be used to "convert" the tutorial to use
3dc4813b 144PostgreSQL (L<http://www.postgresql.org>) and
145MySQL (L<http://dev.mysql.com>). However, note that part of
d442cc9f 146the beauty of the MVC architecture is that very little database-specific
147code is spread throughout the system (at least when MVC is "done
148right"). Consequently, converting from one database to another is
149relatively painless with most Catalyst applications. In general, you
150just need to adapt the schema definition C<.sql> file you use to
151initialize your database and adjust a few configuration parameters.
152
153Also note that the purpose of the data definition statements for this
154section are not designed to take maximum advantage of the various
155features in each database for issues such as referential integrity and
156field types/constraints.
157
d442cc9f 158
e179eee0 159=head2 PostgreSQL
160
161Use the following steps to adapt the tutorial to PostgreSQL. Thanks
162to Caelum (Rafael Kitover) for assistance with the most recent
163updates, and Louis Moore, Marcello Romani and Tom Lanyon for help with
164earlier versions.
d442cc9f 165
166=over 4
167
168=item *
169
e179eee0 170Chapter 3: More Catalyst Basics
d442cc9f 171
172=over 4
173
174=item *
175
e179eee0 176Install the PostgreSQL server and client and DBD::Pg:
d442cc9f 177
e179eee0 178If you are following along in Debian 5, you can quickly install these
179items via this command:
d442cc9f 180
e179eee0 181 sudo aptitude install postgresql libdbd-pg-perl libdatetime-format-pg-perl
d442cc9f 182
e179eee0 183To configure the permissions, you can open
184C</etc/postgresql/8.3/main/pg_hba.conf> and change this line (near the
185bottom):
d442cc9f 186
e179eee0 187 # "local" is for Unix domain socket connections only
188 local all all ident sameuser
d442cc9f 189
e179eee0 190to:
d442cc9f 191
e179eee0 192 # "local" is for Unix domain socket connections only
193 local all all trust
d442cc9f 194
e179eee0 195And then restart PostgreSQL:
196
197 sudo /etc/init.d/postgresql-8.3 restart
d442cc9f 198
d442cc9f 199
200=item *
201
e179eee0 202Create the database and a user for the database (note that we are
203using "E<lt>catalystE<gt>" to represent the hidden password of
204"catalyst"):
d442cc9f 205
e179eee0 206 $ sudo -u postgres createuser -P catappuser
207 Enter password for new role: <catalyst>
208 Enter it again: <catalyst>
209 Shall the new role be a superuser? (y/n) n
210 Shall the new role be allowed to create databases? (y/n) n
211 Shall the new role be allowed to create more new roles? (y/n) n
212 CREATE ROLE
213 $ sudo -u postgres createdb -O catappuser catappdb
214 CREATE DATABASE
d442cc9f 215
216=item *
217
218Create the C<.sql> file and load the data:
219
220=over 4
221
222=item *
223
e179eee0 224Open the C<myapp01_psql.sql> in your editor and enter:
d442cc9f 225
226 --
e179eee0 227 -- Drops just in case you are reloading
228 ---
229 DROP TABLE IF EXISTS books CASCADE;
230 DROP TABLE IF EXISTS authors CASCADE;
231 DROP TABLE IF EXISTS book_authors CASCADE;
232 DROP TABLE IF EXISTS users CASCADE;
233 DROP TABLE IF EXISTS roles CASCADE;
234 DROP TABLE IF EXISTS user_roles CASCADE;
235
236 --
d442cc9f 237 -- Create a very simple database to hold book and author information
238 --
d442cc9f 239 CREATE TABLE books (
e179eee0 240 id SERIAL PRIMARY KEY,
241 title TEXT ,
242 rating INTEGER,
243 -- Manually add these later
244 -- created TIMESTAMP NOT NULL DEFAULT now(),
245 -- updated TIMESTAMP
d442cc9f 246 );
e179eee0 247
248 CREATE TABLE authors (
249 id SERIAL PRIMARY KEY,
250 first_name TEXT,
251 last_name TEXT
252 );
253
d442cc9f 254 -- 'book_authors' is a many-to-many join table between books & authors
255 CREATE TABLE book_authors (
e179eee0 256 book_id INTEGER REFERENCES books(id) ON DELETE CASCADE ON UPDATE CASCADE,
257 author_id INTEGER REFERENCES authors(id) ON DELETE CASCADE ON UPDATE CASCADE,
258 PRIMARY KEY (book_id, author_id)
d442cc9f 259 );
e179eee0 260
d442cc9f 261 ---
262 --- Load some sample data
263 ---
e179eee0 264 INSERT INTO books (title, rating) VALUES ('CCSP SNRS Exam Certification Guide', 5);
265 INSERT INTO books (title, rating) VALUES ('TCP/IP Illustrated, Volume 1', 5);
266 INSERT INTO books (title, rating) VALUES ('Internetworking with TCP/IP Vol.1', 4);
267 INSERT INTO books (title, rating) VALUES ('Perl Cookbook', 5);
268 INSERT INTO books (title, rating) VALUES ('Designing with Web Standards', 5);
269 INSERT INTO authors (first_name, last_name) VALUES ('Greg', 'Bastien');
270 INSERT INTO authors (first_name, last_name) VALUES ('Sara', 'Nasseh');
271 INSERT INTO authors (first_name, last_name) VALUES ('Christian', 'Degu');
272 INSERT INTO authors (first_name, last_name) VALUES ('Richard', 'Stevens');
273 INSERT INTO authors (first_name, last_name) VALUES ('Douglas', 'Comer');
274 INSERT INTO authors (first_name, last_name) VALUES ('Tom', 'Christiansen');
275 INSERT INTO authors (first_name, last_name) VALUES ('Nathan', 'Torkington');
276 INSERT INTO authors (first_name, last_name) VALUES ('Jeffrey', 'Zeldman');
d442cc9f 277 INSERT INTO book_authors VALUES (1, 1);
278 INSERT INTO book_authors VALUES (1, 2);
279 INSERT INTO book_authors VALUES (1, 3);
280 INSERT INTO book_authors VALUES (2, 4);
281 INSERT INTO book_authors VALUES (3, 5);
282 INSERT INTO book_authors VALUES (4, 6);
283 INSERT INTO book_authors VALUES (4, 7);
284 INSERT INTO book_authors VALUES (5, 8);
285
286=item *
287
288Load the data:
289
e179eee0 290 $ psql -U catappuser -W catappdb -f myapp01_psql.sql
291 Password for user catappuser:
292 psql:myapp01_psql.sql:8: NOTICE: CREATE TABLE will create implicit sequence "books_id_seq" for serial column "books.id"
293 psql:myapp01_psql.sql:8: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "books_pkey" for table "books"
294 CREATE TABLE
295 psql:myapp01_psql.sql:15: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "book_authors_pkey" for table "book_authors"
296 CREATE TABLE
297 psql:myapp01_psql.sql:21: NOTICE: CREATE TABLE will create implicit sequence "authors_id_seq" for serial column "authors.id"
298 psql:myapp01_psql.sql:21: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "authors_pkey" for table "authors"
299 CREATE TABLE
300 INSERT 0 1
301 INSERT 0 1
302 INSERT 0 1
303 ...
d442cc9f 304
305=item *
306
307Make sure the data loaded correctly:
308
e179eee0 309 $ psql -U catappuser -W catappdb
310 Password for user catappuser: <catalyst>
311 Welcome to psql 8.3.7, the PostgreSQL interactive terminal.
d442cc9f 312
e179eee0 313 Type: \copyright for distribution terms
314 \h for help with SQL commands
315 \? for help with psql commands
316 \g or terminate with semicolon to execute query
317 \q to quit
d442cc9f 318
e179eee0 319 catappdb=> \dt
320 List of relations
321 Schema | Name | Type | Owner
322 --------+--------------+-------+------------
323 public | authors | table | catappuser
324 public | book_authors | table | catappuser
325 public | books | table | catappuser
326 (3 rows)
d442cc9f 327
e179eee0 328 catappdb=> select * from books;
329 id | title | rating
330 ----+------------------------------------+--------
331 1 | CCSP SNRS Exam Certification Guide | 5
332 2 | TCP/IP Illustrated, Volume 1 | 5
333 3 | Internetworking with TCP/IP Vol.1 | 4
334 4 | Perl Cookbook | 5
335 5 | Designing with Web Standards | 5
336 (5 rows)
d442cc9f 337
e179eee0 338 catappdb=>
d442cc9f 339
340=back
341
342=item *
343
e179eee0 344After the steps where you:
345
346 edit lib/MyApp.pm
347
348 create lib/MyAppDB.pm
349
350 create lib/MyAppDB/Book.pm
351
352 create lib/MyAppDB/Author.pm
353
354 create lib/MyAppDB/BookAuthor.pm
d442cc9f 355
d442cc9f 356
357=item *
358
e179eee0 359Generate the model using the Catalyst "_create.pl" script:
d442cc9f 360
e179eee0 361 $ rm lib/MyApp/Model/DB.pm # Delete just in case already there
362 $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
363 create=static components=TimeStamp,EncodedColumn \
364 'dbi:Pg:dbname=catappdb' 'catappuser' 'catalyst' '{ AutoCommit => 1 }'
365
366=back
d442cc9f 367
368=item *
369
e179eee0 370Chapter 4: Basic CRUD
d442cc9f 371
e179eee0 372Add Datetime Columns to Our Existing Books Table
d442cc9f 373
e179eee0 374 $ psql -U catappuser -W catappdb
375 ...
376 catappdb=> ALTER TABLE books ADD created TIMESTAMP NOT NULL DEFAULT now();
377 ALTER TABLE
378 catappdb=> ALTER TABLE books ADD updated TIMESTAMP;
379 ALTER TABLE
380 catappdb=> \q
381
382Re-generate the model using the Catalyst "_create.pl" script:
383
384 $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
385 create=static components=TimeStamp,EncodedColumn \
386 'dbi:Pg:dbname=catappdb' 'catappuser' 'catalyst' '{ AutoCommit => 1 }'
d442cc9f 387
d442cc9f 388
389=item *
390
e179eee0 391Chapter 5: Authentication
d442cc9f 392
393=over 4
394
395=item *
396
397Create the C<.sql> file for the user/roles data:
398
e179eee0 399Open C<myapp02_psql.sql> in your editor and enter:
d442cc9f 400
401 --
402 -- Add users and roles tables, along with a many-to-many join table
403 --
e179eee0 404
d442cc9f 405 CREATE TABLE users (
e179eee0 406 id SERIAL PRIMARY KEY,
407 username TEXT,
408 password TEXT,
409 email_address TEXT,
410 first_name TEXT,
411 last_name TEXT,
412 active INTEGER
d442cc9f 413 );
e179eee0 414
d442cc9f 415 CREATE TABLE roles (
e179eee0 416 id SERIAL PRIMARY KEY,
417 role TEXT
d442cc9f 418 );
e179eee0 419
d442cc9f 420 CREATE TABLE user_roles (
e179eee0 421 user_id INTEGER REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
422 role_id INTEGER REFERENCES roles(id) ON DELETE CASCADE ON UPDATE CASCADE,
423 PRIMARY KEY (user_id, role_id)
d442cc9f 424 );
e179eee0 425
d442cc9f 426 --
427 -- Load up some initial test data
428 --
e179eee0 429 INSERT INTO users (username, password, email_address, first_name, last_name, active)
430 VALUES ('test01', 'mypass', 't01@na.com', 'Joe', 'Blow', 1);
431 INSERT INTO users (username, password, email_address, first_name, last_name, active)
432 VALUES ('test02', 'mypass', 't02@na.com', 'Jane', 'Doe', 1);
433 INSERT INTO users (username, password, email_address, first_name, last_name, active)
434 VALUES ('test03', 'mypass', 't03@na.com', 'No', 'Go', 0);
435 INSERT INTO roles (role) VALUES ('user');
436 INSERT INTO roles (role) VALUES ('admin');
d442cc9f 437 INSERT INTO user_roles VALUES (1, 1);
438 INSERT INTO user_roles VALUES (1, 2);
439 INSERT INTO user_roles VALUES (2, 1);
440 INSERT INTO user_roles VALUES (3, 1);
441
442=item *
443
e179eee0 444Load the data:
445
446 $ psql -U catappuser -W catappdb -f myapp02_psql.sql
447 Password for user catappuser: <catalyst>
448 psql:myapp02_psql.sql:13: NOTICE: CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
449 psql:myapp02_psql.sql:13: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
450 CREATE TABLE
451 psql:myapp02_psql.sql:18: NOTICE: CREATE TABLE will create implicit sequence "roles_id_seq" for serial column "roles.id"
452 psql:myapp02_psql.sql:18: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "roles_pkey" for table "roles"
453 CREATE TABLE
454 psql:myapp02_psql.sql:24: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "user_roles_pkey" for table "user_roles"
455 CREATE TABLE
456 INSERT 0 1
457 INSERT 0 1
458 INSERT 0 1
459 INSERT 0 1
460 INSERT 0 1
461 INSERT 0 1
462 INSERT 0 1
463 INSERT 0 1
464 INSERT 0 1
465
466Confirm with:
467
468 $ psql -U catappuser -W catappdb -c "select * from users"
469 Password for user catappuser: <catalyst>
470 id | username | password | email_address | first_name | last_name | active
471 ----+----------+----------+---------------+------------+-----------+--------
472 1 | test01 | mypass | t01@na.com | Joe | Blow | 1
473 2 | test02 | mypass | t02@na.com | Jane | Doe | 1
474 3 | test03 | mypass | t03@na.com | No | Go | 0
475 (3 rows)
d442cc9f 476
d442cc9f 477
478=item *
479
e179eee0 480Modify C<set_hashed_passwords.pl> to match the following (the only difference
481is the C<connect> line):
d442cc9f 482
e179eee0 483 #!/usr/bin/perl
484
485 use strict;
486 use warnings;
487
488 use MyApp::Schema;
489
490 my $schema = MyApp::Schema->connect('dbi:Pg:dbname=catappdb', 'catappuser', 'catalyst');
491
492 my @users = $schema->resultset('Users')->all;
493
494 foreach my $user (@users) {
495 $user->password('mypass');
496 $user->update;
497 }
d442cc9f 498
e179eee0 499Run the C<set_hashed_passwords.pl> as per the "normal" flow of the
500tutorial:
d442cc9f 501
e179eee0 502 $ perl -Ilib set_hashed_passwords.pl
d442cc9f 503
e179eee0 504You can verify that it worked with this command:
505
506 $ psql -U catappuser -W catappdb -c "select * from users"
d442cc9f 507
d442cc9f 508
509=back
510
511=back
512
d442cc9f 513
e179eee0 514=head2 MySQL
515
fb51e5fb 516B<NOTE:> This section is out of date with the rest of the tutorial.
e179eee0 517Consider using SQLite or PostgreSQL since they are current.
518
519Use the following steps to adapt the tutorial to MySQL. Thanks to Jim
520Howard for the help.
3533daff 521
522=over 4
523
524=item *
525
e179eee0 526Chapter 3: Catalyst Basics
3533daff 527
528=over 4
529
530=item *
531
532Install the required software:
533
534=over 4
535
536=item *
537
e179eee0 538The MySQL database server and client utility.
3533daff 539
540=item *
541
e179eee0 542The Perl C<DBD::MySQL> module
3533daff 543
544=back
545
e179eee0 546For CentOS users (see
547L<Catalyst::Manual::Installation::CentOS4|Catalyst::Manual::Installation::CentOS4>),
548you can use the following commands to install the software and start the MySQL
549daemon:
550
551 yum -y install mysql mysql-server
552 service mysqld start
553
3533daff 554=item *
555
e179eee0 556Create the database and set the permissions:
3533daff 557
e179eee0 558 $ mysql
559 Welcome to the MySQL monitor. Commands end with ; or \g.
560 Your MySQL connection id is 2 to server version: 4.1.20
561
562 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
563
564 mysql> create database myapp;
565 Query OK, 1 row affected (0.01 sec)
566
567 mysql> grant all on myapp.* to tutorial@'localhost';
568 Query OK, 0 rows affected (0.00 sec)
569
570 mysql> flush privileges;
571 Query OK, 0 rows affected (0.00 sec)
572
573 mysql> quit
574 Bye
3533daff 575
576=item *
577
578Create the C<.sql> file and load the data:
579
580=over 4
581
582=item *
583
e179eee0 584Open the C<myapp01_mysql.sql> in your editor and enter:
3533daff 585
1390ef0e 586 --
587 -- Create a very simple database to hold book and author information
588 --
e179eee0 589 DROP TABLE IF EXISTS books;
590 DROP TABLE IF EXISTS book_authors;
591 DROP TABLE IF EXISTS authors;
1390ef0e 592 CREATE TABLE books (
e179eee0 593 id INT(11) PRIMARY KEY AUTO_INCREMENT,
594 title TEXT ,
595 rating INT(11)
596 );
1390ef0e 597 -- 'book_authors' is a many-to-many join table between books & authors
598 CREATE TABLE book_authors (
e179eee0 599 book_id INT(11),
600 author_id INT(11),
601 PRIMARY KEY (book_id, author_id)
602 );
1390ef0e 603 CREATE TABLE authors (
e179eee0 604 id INT(11) PRIMARY KEY AUTO_INCREMENT,
605 first_name TEXT,
606 last_name TEXT
1390ef0e 607 );
608 ---
609 --- Load some sample data
610 ---
611 INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', 5);
612 INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5);
613 INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', 4);
614 INSERT INTO books VALUES (4, 'Perl Cookbook', 5);
615 INSERT INTO books VALUES (5, 'Designing with Web Standards', 5);
616 INSERT INTO authors VALUES (1, 'Greg', 'Bastien');
617 INSERT INTO authors VALUES (2, 'Sara', 'Nasseh');
618 INSERT INTO authors VALUES (3, 'Christian', 'Degu');
619 INSERT INTO authors VALUES (4, 'Richard', 'Stevens');
620 INSERT INTO authors VALUES (5, 'Douglas', 'Comer');
621 INSERT INTO authors VALUES (6, 'Tom', 'Christiansen');
e179eee0 622 INSERT INTO authors VALUES (7, ' Nathan', 'Torkington');
1390ef0e 623 INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman');
624 INSERT INTO book_authors VALUES (1, 1);
625 INSERT INTO book_authors VALUES (1, 2);
626 INSERT INTO book_authors VALUES (1, 3);
627 INSERT INTO book_authors VALUES (2, 4);
628 INSERT INTO book_authors VALUES (3, 5);
629 INSERT INTO book_authors VALUES (4, 6);
630 INSERT INTO book_authors VALUES (4, 7);
631 INSERT INTO book_authors VALUES (5, 8);
3533daff 632
633=item *
634
635Load the data:
636
e179eee0 637 mysql -ututorial myapp < myapp01_mysql.sql
3533daff 638
639=item *
640
641Make sure the data loaded correctly:
642
e179eee0 643 $ mysql -ututorial myapp
644 Reading table information for completion of table and column names
645 You can turn off this feature to get a quicker startup with -A
1390ef0e 646
e179eee0 647 Welcome to the MySQL monitor. Commands end with ; or \g.
648 Your MySQL connection id is 4 to server version: 4.1.20
1390ef0e 649
e179eee0 650 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
651
652 mysql> show tables;
653 +-----------------+
654 | Tables_in_myapp |
655 +-----------------+
656 | authors |
657 | book_authors |
658 | books |
659 +-----------------+
660 3 rows in set (0.00 sec)
661
662 mysql> select * from books;
663 +----+------------------------------------+--------+
664 | id | title | rating |
665 +----+------------------------------------+--------+
666 | 1 | CCSP SNRS Exam Certification Guide | 5 |
667 | 2 | TCP/IP Illustrated, Volume 1 | 5 |
668 | 3 | Internetworking with TCP/IP Vol.1 | 4 |
669 | 4 | Perl Cookbook | 5 |
670 | 5 | Designing with Web Standards | 5 |
671 +----+------------------------------------+--------+
672 5 rows in set (0.00 sec)
673
674 mysql>
3533daff 675
676=back
677
678=item *
679
e179eee0 680Update the model:
3533daff 681
e179eee0 682=over 4
683
684=item *
685
686Delete the existing model:
3533daff 687
e179eee0 688 rm lib/MyApp/Model/MyAppDB.pm
3533daff 689
690=item *
691
e179eee0 692Regenerate the model using the Catalyst "_create.pl" script:
3533daff 693
e179eee0 694 script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
695 dbi:mysql:myapp '_username_here_' '_password_here_' '{ AutoCommit => 1 }'
3533daff 696
e179eee0 697=back
3533daff 698
699=back
700
701=item *
702
e179eee0 703Chapter 5: Authentication
3533daff 704
705=over 4
706
707=item *
708
709Create the C<.sql> file for the user/roles data:
710
e179eee0 711Open C<myapp02_mysql.sql> in your editor and enter:
3533daff 712
713 --
714 -- Add users and roles tables, along with a many-to-many join table
715 --
3533daff 716 CREATE TABLE users (
e179eee0 717 id INT(11) PRIMARY KEY,
3533daff 718 username TEXT,
719 password TEXT,
720 email_address TEXT,
721 first_name TEXT,
722 last_name TEXT,
e179eee0 723 active INT(11)
3533daff 724 );
3533daff 725 CREATE TABLE roles (
e179eee0 726 id INTEGER PRIMARY KEY,
3533daff 727 role TEXT
728 );
3533daff 729 CREATE TABLE user_roles (
e179eee0 730 user_id INT(11),
731 role_id INT(11),
3533daff 732 PRIMARY KEY (user_id, role_id)
733 );
3533daff 734 --
735 -- Load up some initial test data
736 --
737 INSERT INTO users VALUES (1, 'test01', 'mypass', 't01@na.com', 'Joe', 'Blow', 1);
738 INSERT INTO users VALUES (2, 'test02', 'mypass', 't02@na.com', 'Jane', 'Doe', 1);
739 INSERT INTO users VALUES (3, 'test03', 'mypass', 't03@na.com', 'No', 'Go', 0);
740 INSERT INTO roles VALUES (1, 'user');
741 INSERT INTO roles VALUES (2, 'admin');
742 INSERT INTO user_roles VALUES (1, 1);
743 INSERT INTO user_roles VALUES (1, 2);
744 INSERT INTO user_roles VALUES (2, 1);
745 INSERT INTO user_roles VALUES (3, 1);
746
747=item *
748
e179eee0 749Load the user/roles data:
3533daff 750
e179eee0 751 mysql -ututorial myapp < myapp02_mysql.sql
3533daff 752
753=item *
754
755Create the C<.sql> file for the hashed password data:
756
e179eee0 757Open C<myapp03_mysql.sql> in your editor and enter:
3533daff 758
759 --
760 -- Convert passwords to SHA-1 hashes
761 --
762 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 1;
763 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 2;
764 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 3;
765
766=item *
767
e179eee0 768Load the user/roles data:
3533daff 769
e179eee0 770 mysql -ututorial myapp < myapp03_mysql.sql
3533daff 771
772=back
773
774=back
d442cc9f 775
776
d442cc9f 777=head1 AUTHOR
778
779Kennedy Clark, C<hkclark@gmail.com>
780
781Please report any errors, issues or suggestions to the author. The
782most recent version of the Catalyst Tutorial can be found at
59884771 783L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.80/trunk/lib/Catalyst/Manual/Tutorial/>.
d442cc9f 784
45c7830f 785Copyright 2006-2008, Kennedy Clark, under Creative Commons License
95674086 786(L<http://creativecommons.org/licenses/by-sa/3.0/us/>).