.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Catalyst::Manual::Tutorial::10_Appendices 3pm" .TH Catalyst::Manual::Tutorial::10_Appendices 3pm 2024-03-30 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Catalyst::Manual::Tutorial::10_Appendices \- Catalyst Tutorial \- Chapter 10: Appendices .SH OVERVIEW .IX Header "OVERVIEW" This is \fBChapter 10 of 10\fR for the Catalyst tutorial. .PP Tutorial Overview .IP 1. 4 Introduction .IP 2. 4 Catalyst Basics .IP 3. 4 More Catalyst Basics .IP 4. 4 Basic CRUD .IP 5. 4 Authentication .IP 6. 4 Authorization .IP 7. 4 Debugging .IP 8. 4 Testing .IP 9. 4 Advanced CRUD .IP 10. 4 \&\fB10_Appendices\fR .SH DESCRIPTION .IX Header "DESCRIPTION" This chapter of the tutorial provides supporting information relevant to the Catalyst tutorial. .SH "APPENDIX 1: CUT AND PASTE FOR POD-BASED EXAMPLES" .IX Header "APPENDIX 1: CUT AND PASTE FOR POD-BASED EXAMPLES" You may notice that Pod indents example code with four spaces. This section provides some quick advice to "un-indent" this text in common editors. .SS """Un-indenting"" with Vi/Vim" .IX Subsection """Un-indenting"" with Vi/Vim" When cutting and pasting multi-line text from Pod-based documents, the following vi/vim regexs can be helpful to "un-indent" the inserted text (do NOT type the quotes, they are only included to show spaces in the regex patterns). \fINote that all 3 of the regexs end in 4 spaces\fR: .IP \(bu 4 \&\f(CW":0,$s/^ "\fR .Sp Removes four leading spaces from the entire file (from the first line, \&\f(CW0\fR, to the last line, \f(CW\*(C`$\*(C'\fR). .IP \(bu 4 \&\f(CW"%s/^ "\fR .Sp A shortcut for the previous item (\f(CW\*(C`%\*(C'\fR specifies the entire file; so this removes four leading spaces from every line). .IP \(bu 4 \&\f(CW":.,$s/^ "\fR .Sp Removes the first four spaces from the line the cursor is on at the time the regex command is executed (".") to the last line of the file. .IP \(bu 4 \&\f(CW":.,44s/^ "\fR .Sp Removes four leading space from the current line through line 44 (obviously adjust the \f(CW44\fR to the appropriate value in your example). .SS """Un-indenting"" with Emacs" .IX Subsection """Un-indenting"" with Emacs" Although the author has not used Emacs for many years (apologies to the Emacs fans out there), here is a quick hint to get you started. To replace the leading spaces of every line in a file, use: .PP .Vb 3 \& M\-x replace\-regexp \& Replace regexp: ^ \& with: .Ve .PP All of that will occur on the single line at the bottom of your screen. Note that "" represents the return key/enter. Also, there are four spaces after the "^" on the "Replace regexp:" line and no spaces entered on the last line. .PP You can limit the replacement operation by selecting text first (depending on your version of Emacs, you can either use the mouse or experiment with commands such as \f(CW\*(C`C\-SPC\*(C'\fR to set the mark at the cursor location and \&\f(CW\*(C`C\-<\*(C'\fR and \f(CW\*(C`C\->\*(C'\fR to set the mark at the beginning and end of the file respectively. .PP Also, Stefan Kangas sent in the following tip about an alternate approach using the command \f(CW\*(C`indent\-region\*(C'\fR to redo the indentation for the currently selected region (adhering to indent rules in the current major mode). You can run the command by typing M\-x indent-region or pressing the default keybinding C\-M\-\e in cperl-mode. Additional details can be found here: .PP .SH "APPENDIX 2: USING POSTGRESQL AND MYSQL" .IX Header "APPENDIX 2: USING POSTGRESQL AND MYSQL" The main database used in this tutorial is the very simple yet powerful SQLite . This section provides information that can be used to "convert" the tutorial to use PostgreSQL and MySQL . However, note that part of the beauty of the MVC architecture is that very little database-specific code is spread throughout the system (at least when MVC is "done right"). Consequently, converting from one database to another is relatively painless with most Catalyst applications. In general, you just need to adapt the schema definition \f(CW\*(C`.sql\*(C'\fR file you use to initialize your database and adjust a few configuration parameters. .PP Also note that the purpose of the data definition statements for this section are not designed to take maximum advantage of the various features in each database for issues such as referential integrity and field types/constraints. .SS PostgreSQL .IX Subsection "PostgreSQL" Use the following steps to adapt the tutorial to PostgreSQL. Thanks to Caelum (Rafael Kitover) for assistance with the most recent updates, and Louis Moore, Marcello Romani and Tom Lanyon for help with earlier versions. .IP \(bu 4 Chapter 3: More Catalyst Basics .RS 4 .IP \(bu 4 Install the PostgreSQL server and client and DBD::Pg: .Sp If you are following along in Debian 6, you can quickly install these items via this command: .Sp .Vb 1 \& sudo aptitude install postgresql libdbd\-pg\-perl libdatetime\-format\-pg\-perl .Ve .Sp To configure the permissions, you can open \&\fI/etc/postgresql/8.3/main/pg_hba.conf\fR and change this line (near the bottom): .Sp .Vb 2 \& # "local" is for Unix domain socket connections only \& local all all ident sameuser .Ve .Sp to: .Sp .Vb 2 \& # "local" is for Unix domain socket connections only \& local all all trust .Ve .Sp And then restart PostgreSQL: .Sp .Vb 1 \& sudo /etc/init.d/postgresql\-8.3 restart .Ve .IP \(bu 4 Create the database and a user for the database (note that we are using "" to represent the hidden password of "catalyst"): .Sp .Vb 9 \& $ sudo \-u postgres createuser \-P catappuser \& Enter password for new role: \& Enter it again: \& Shall the new role be a superuser? (y/n) n \& Shall the new role be allowed to create databases? (y/n) n \& Shall the new role be allowed to create more new roles? (y/n) n \& CREATE ROLE \& $ sudo \-u postgres createdb \-O catappuser catappdb \& CREATE DATABASE .Ve .IP \(bu 4 Create the \f(CW\*(C`.sql\*(C'\fR file and load the data: .RS 4 .IP \(bu 4 Open the \fImyapp01_psql.sql\fR in your editor and enter: .Sp .Vb 9 \& \-\- \& \-\- Drops just in case you are reloading \& \-\-\- \& DROP TABLE IF EXISTS books CASCADE; \& DROP TABLE IF EXISTS authors CASCADE; \& DROP TABLE IF EXISTS book_authors CASCADE; \& DROP TABLE IF EXISTS users CASCADE; \& DROP TABLE IF EXISTS roles CASCADE; \& DROP TABLE IF EXISTS user_roles CASCADE; \& \& \-\- \& \-\- Create a very simple database to hold book and author information \& \-\- \& CREATE TABLE books ( \& id SERIAL PRIMARY KEY, \& title TEXT , \& rating INTEGER, \& \-\- Manually add these later \& \-\- created TIMESTAMP NOT NULL DEFAULT now(), \& \-\- updated TIMESTAMP \& ); \& \& CREATE TABLE authors ( \& id SERIAL PRIMARY KEY, \& first_name TEXT, \& last_name TEXT \& ); \& \& \-\- \*(Aqbook_authors\*(Aq is a many\-to\-many join table between books & authors \& CREATE TABLE book_authors ( \& book_id INTEGER REFERENCES books(id) ON DELETE CASCADE ON UPDATE CASCADE, \& author_id INTEGER REFERENCES authors(id) ON DELETE CASCADE ON UPDATE CASCADE, \& PRIMARY KEY (book_id, author_id) \& ); \& \& \-\-\- \& \-\-\- Load some sample data \& \-\-\- \& INSERT INTO books (title, rating) VALUES (\*(AqCCSP SNRS Exam Certification Guide\*(Aq, 5); \& INSERT INTO books (title, rating) VALUES (\*(AqTCP/IP Illustrated, Volume 1\*(Aq, 5); \& INSERT INTO books (title, rating) VALUES (\*(AqInternetworking with TCP/IP Vol.1\*(Aq, 4); \& INSERT INTO books (title, rating) VALUES (\*(AqPerl Cookbook\*(Aq, 5); \& INSERT INTO books (title, rating) VALUES (\*(AqDesigning with Web Standards\*(Aq, 5); \& INSERT INTO authors (first_name, last_name) VALUES (\*(AqGreg\*(Aq, \*(AqBastien\*(Aq); \& INSERT INTO authors (first_name, last_name) VALUES (\*(AqSara\*(Aq, \*(AqNasseh\*(Aq); \& INSERT INTO authors (first_name, last_name) VALUES (\*(AqChristian\*(Aq, \*(AqDegu\*(Aq); \& INSERT INTO authors (first_name, last_name) VALUES (\*(AqRichard\*(Aq, \*(AqStevens\*(Aq); \& INSERT INTO authors (first_name, last_name) VALUES (\*(AqDouglas\*(Aq, \*(AqComer\*(Aq); \& INSERT INTO authors (first_name, last_name) VALUES (\*(AqTom\*(Aq, \*(AqChristiansen\*(Aq); \& INSERT INTO authors (first_name, last_name) VALUES (\*(AqNathan\*(Aq, \*(AqTorkington\*(Aq); \& INSERT INTO authors (first_name, last_name) VALUES (\*(AqJeffrey\*(Aq, \*(AqZeldman\*(Aq); \& INSERT INTO book_authors VALUES (1, 1); \& INSERT INTO book_authors VALUES (1, 2); \& INSERT INTO book_authors VALUES (1, 3); \& INSERT INTO book_authors VALUES (2, 4); \& INSERT INTO book_authors VALUES (3, 5); \& INSERT INTO book_authors VALUES (4, 6); \& INSERT INTO book_authors VALUES (4, 7); \& INSERT INTO book_authors VALUES (5, 8); .Ve .IP \(bu 4 Load the data: .Sp .Vb 10 \& $ psql \-U catappuser \-W catappdb \-f myapp01_psql.sql \& Password for user catappuser: \& psql:myapp01_psql.sql:8: NOTICE: CREATE TABLE will create implicit sequence "books_id_seq" for serial column "books.id" \& psql:myapp01_psql.sql:8: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "books_pkey" for table "books" \& CREATE TABLE \& psql:myapp01_psql.sql:15: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "book_authors_pkey" for table "book_authors" \& CREATE TABLE \& psql:myapp01_psql.sql:21: NOTICE: CREATE TABLE will create implicit sequence "authors_id_seq" for serial column "authors.id" \& psql:myapp01_psql.sql:21: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "authors_pkey" for table "authors" \& CREATE TABLE \& INSERT 0 1 \& INSERT 0 1 \& INSERT 0 1 \& ... .Ve .IP \(bu 4 Make sure the data loaded correctly: .Sp .Vb 3 \& $ psql \-U catappuser \-W catappdb \& Password for user catappuser: \& Welcome to psql 8.3.7, the PostgreSQL interactive terminal. \& \& Type: \ecopyright for distribution terms \& \eh for help with SQL commands \& \e? for help with psql commands \& \eg or terminate with semicolon to execute query \& \eq to quit \& \& catappdb=> \edt \& List of relations \& Schema | Name | Type | Owner \& \-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\- \& public | authors | table | catappuser \& public | book_authors | table | catappuser \& public | books | table | catappuser \& (3 rows) \& \& catappdb=> select * from books; \& id | title | rating \& \-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\- \& 1 | CCSP SNRS Exam Certification Guide | 5 \& 2 | TCP/IP Illustrated, Volume 1 | 5 \& 3 | Internetworking with TCP/IP Vol.1 | 4 \& 4 | Perl Cookbook | 5 \& 5 | Designing with Web Standards | 5 \& (5 rows) \& \& catappdb=> .Ve .RE .RS 4 .RE .IP \(bu 4 After the steps where you: .Sp .Vb 1 \& edit lib/MyApp.pm \& \& create lib/MyAppDB.pm \& \& create lib/MyAppDB/Book.pm \& \& create lib/MyAppDB/Author.pm \& \& create lib/MyAppDB/BookAuthor.pm .Ve .IP \(bu 4 Generate the model using the Catalyst "_create.pl" script: .Sp .Vb 4 \& $ rm lib/MyApp/Model/DB.pm # Delete just in case already there \& $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \e \& create=static components=TimeStamp,PassphraseColumn \e \& \*(Aqdbi:Pg:dbname=catappdb\*(Aq \*(Aqcatappuser\*(Aq \*(Aqcatalyst\*(Aq \*(Aq{ AutoCommit => 1 }\*(Aq .Ve .RE .RS 4 .RE .IP \(bu 4 Chapter 4: Basic CRUD .Sp Add Datetime Columns to Our Existing Books Table .Sp .Vb 7 \& $ psql \-U catappuser \-W catappdb \& ... \& catappdb=> ALTER TABLE books ADD created TIMESTAMP NOT NULL DEFAULT now(); \& ALTER TABLE \& catappdb=> ALTER TABLE books ADD updated TIMESTAMP; \& ALTER TABLE \& catappdb=> \eq .Ve .Sp Re-generate the model using the Catalyst "_create.pl" script: .Sp .Vb 3 \& $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \e \& create=static components=TimeStamp,PassphraseColumn \e \& \*(Aqdbi:Pg:dbname=catappdb\*(Aq \*(Aqcatappuser\*(Aq \*(Aqcatalyst\*(Aq \*(Aq{ AutoCommit => 1 }\*(Aq .Ve .IP \(bu 4 Chapter 5: Authentication .RS 4 .IP \(bu 4 Create the \f(CW\*(C`.sql\*(C'\fR file for the user/roles data: .Sp Open \fImyapp02_psql.sql\fR in your editor and enter: .Sp .Vb 3 \& \-\- \& \-\- Add users and roles tables, along with a many\-to\-many join table \& \-\- \& \& CREATE TABLE users ( \& id SERIAL PRIMARY KEY, \& username TEXT, \& password TEXT, \& email_address TEXT, \& first_name TEXT, \& last_name TEXT, \& active INTEGER \& ); \& \& CREATE TABLE roles ( \& id SERIAL PRIMARY KEY, \& role TEXT \& ); \& \& CREATE TABLE user_roles ( \& user_id INTEGER REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE, \& role_id INTEGER REFERENCES roles(id) ON DELETE CASCADE ON UPDATE CASCADE, \& PRIMARY KEY (user_id, role_id) \& ); \& \& \-\- \& \-\- Load up some initial test data \& \-\- \& INSERT INTO users (username, password, email_address, first_name, last_name, active) \& VALUES (\*(Aqtest01\*(Aq, \*(Aqmypass\*(Aq, \*(Aqt01@na.com\*(Aq, \*(AqJoe\*(Aq, \*(AqBlow\*(Aq, 1); \& INSERT INTO users (username, password, email_address, first_name, last_name, active) \& VALUES (\*(Aqtest02\*(Aq, \*(Aqmypass\*(Aq, \*(Aqt02@na.com\*(Aq, \*(AqJane\*(Aq, \*(AqDoe\*(Aq, 1); \& INSERT INTO users (username, password, email_address, first_name, last_name, active) \& VALUES (\*(Aqtest03\*(Aq, \*(Aqmypass\*(Aq, \*(Aqt03@na.com\*(Aq, \*(AqNo\*(Aq, \*(AqGo\*(Aq, 0); \& INSERT INTO roles (role) VALUES (\*(Aquser\*(Aq); \& INSERT INTO roles (role) VALUES (\*(Aqadmin\*(Aq); \& INSERT INTO user_roles VALUES (1, 1); \& INSERT INTO user_roles VALUES (1, 2); \& INSERT INTO user_roles VALUES (2, 1); \& INSERT INTO user_roles VALUES (3, 1); .Ve .IP \(bu 4 Load the data: .Sp .Vb 10 \& $ psql \-U catappuser \-W catappdb \-f myapp02_psql.sql \& Password for user catappuser: \& psql:myapp02_psql.sql:13: NOTICE: CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id" \& psql:myapp02_psql.sql:13: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users" \& CREATE TABLE \& psql:myapp02_psql.sql:18: NOTICE: CREATE TABLE will create implicit sequence "roles_id_seq" for serial column "roles.id" \& psql:myapp02_psql.sql:18: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "roles_pkey" for table "roles" \& CREATE TABLE \& psql:myapp02_psql.sql:24: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "user_roles_pkey" for table "user_roles" \& CREATE TABLE \& INSERT 0 1 \& INSERT 0 1 \& INSERT 0 1 \& INSERT 0 1 \& INSERT 0 1 \& INSERT 0 1 \& INSERT 0 1 \& INSERT 0 1 \& INSERT 0 1 .Ve .Sp Confirm with: .Sp .Vb 8 \& $ psql \-U catappuser \-W catappdb \-c "select * from users" \& Password for user catappuser: \& id | username | password | email_address | first_name | last_name | active \& \-\-\-\-+\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\- \& 1 | test01 | mypass | t01@na.com | Joe | Blow | 1 \& 2 | test02 | mypass | t02@na.com | Jane | Doe | 1 \& 3 | test03 | mypass | t03@na.com | No | Go | 0 \& (3 rows) .Ve .IP \(bu 4 Modify \fIset_hashed_passwords.pl\fR to match the following (the only difference is the \f(CW\*(C`connect\*(C'\fR line): .Sp .Vb 1 \& #!/usr/bin/perl \& \& use strict; \& use warnings; \& \& use MyApp::Schema; \& \& my $schema = MyApp::Schema\->connect(\*(Aqdbi:Pg:dbname=catappdb\*(Aq, \*(Aqcatappuser\*(Aq, \*(Aqcatalyst\*(Aq); \& \& my @users = $schema\->resultset(\*(AqUsers\*(Aq)\->all; \& \& foreach my $user (@users) { \& $user\->password(\*(Aqmypass\*(Aq); \& $user\->update; \& } .Ve .Sp Run the \fIset_hashed_passwords.pl\fR as per the "normal" flow of the tutorial: .Sp .Vb 1 \& $ perl \-Ilib set_hashed_passwords.pl .Ve .Sp You can verify that it worked with this command: .Sp .Vb 1 \& $ psql \-U catappuser \-W catappdb \-c "select * from users" .Ve .RE .RS 4 .RE .SS MySQL .IX Subsection "MySQL" Use the following steps to adapt the tutorial to MySQL. Thanks to Jim Howard for the help and Zsolt Zemancsik for the up to date fixes. .IP \(bu 4 Chapter 3: Catalyst Basics .RS 4 .IP \(bu 4 Install the required software: .RS 4 .IP \(bu 4 The MySQL database server and client utility. .IP \(bu 4 The Perl DBD::MySQL module .RE .RS 4 .Sp For CentOS users (see Catalyst::Manual::Installation::CentOS4), you can use the following commands to install the software and start the MySQL daemon: .Sp .Vb 2 \& yum \-y install mysql mysql\-server \& service mysqld start .Ve .Sp For Debian users you can use the following commands to install the software and start the MySQL daemon: .Sp .Vb 2 \& apt\-get install mysql\-client mysql\-server \& /etc/init.d/mysql start .Ve .Sp \&\fBNOTE:\fR The tutorial is based on Foreign Keys in database which is supported by InnoDB. Only MySQL 5.0 and above supports InnoDB storage Engine so you need to have InnoDB support in you MySQL. You can simply figure out that your install supports it or not: .Sp .Vb 3 \& # mysql \-u root \-p \& Enter password: \& Welcome to the MySQL monitor. Commands end with ; or \eg. \& \& Type \*(Aqhelp;\*(Aq or \*(Aq\eh\*(Aq for help. Type \*(Aq\ec\*(Aq to clear the current input \& statement. \& \& mysql> SHOW VARIABLES LIKE \*(Aqhave_innodb\*(Aq; \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+ \& | Variable_name | Value | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+ \& | have_innodb | YES | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+ \& 1 row in set (0.01 sec) \& \& mysql> exit \& Bye .Ve .Sp If the Value is "YES" you can use your setup (Debian based mysql supports it by default). Else, you need to configure your my.cnf or start your MySQL daemon without \-\-skip\-innodb option. .RE .IP \(bu 4 Create the database and set the permissions: .Sp .Vb 3 \& # mysql \-u root \-p \& Enter password: \& Welcome to the MySQL monitor. Commands end with ; or \eg. \& \& Type \*(Aqhelp;\*(Aq or \*(Aq\eh\*(Aq for help. Type \*(Aq\ec\*(Aq to clear the current input statement. \& \& mysql> CREATE DATABASE \`myapp\`; \& Query OK, 1 row affected (0.01 sec) \& \& mysql> GRANT ALL PRIVILEGES ON myapp.* TO \*(Aqtutorial\*(Aq@\*(Aqlocalhost\*(Aq IDENTIFIED BY \*(Aqyourpassword\*(Aq; \& Query OK, 0 rows affected (0.00 sec) \& \& mysql> FLUSH PRIVILEGES; \& Query OK, 0 rows affected (0.00 sec) \& \& mysql> exit \& Bye .Ve .IP \(bu 4 Create the \f(CW\*(C`.sql\*(C'\fR file and load the data: .RS 4 .IP \(bu 4 Open the \fImyapp01_mysql.sql\fR in your editor and enter: .Sp .Vb 10 \& \-\- \& \-\- Create a very simple database to hold book and author information \& \-\- \& CREATE TABLE IF NOT EXISTS \`books\` ( \& \`id\` int(11) NOT NULL AUTO_INCREMENT, \& \`title\` text CHARACTER SET utf8, \& \`rating\` int(11) DEFAULT NULL, \& PRIMARY KEY (\`id\`) \& ) ENGINE=InnoDB DEFAULT CHARSET=utf8; \& \-\- \*(Aqbook_authors\*(Aq is a many\-to\-many join table between books & authors \& CREATE TABLE IF NOT EXISTS \`book_authors\` ( \& \`book_id\` int(11) NOT NULL DEFAULT \*(Aq0\*(Aq, \& \`author_id\` int(11) NOT NULL DEFAULT \*(Aq0\*(Aq, \& PRIMARY KEY (\`book_id\`,\`author_id\`), \& KEY \`author_id\` (\`author_id\`) \& ) ENGINE=InnoDB DEFAULT CHARSET=utf8; \& CREATE TABLE IF NOT EXISTS \`authors\` ( \& \`id\` int(11) NOT NULL AUTO_INCREMENT, \& \`first_name\` text CHARACTER SET utf8, \& \`last_name\` text CHARACTER SET utf8, \& PRIMARY KEY (\`id\`) \& ) ENGINE=InnoDB DEFAULT CHARSET=utf8; \& \-\-\- \& \-\-\- Load some sample data \& \-\-\- \& INSERT INTO \`books\` (\`id\`, \`title\`, \`rating\`) VALUES \& (1, \*(AqCCSP SNRS Exam Certification Guide\*(Aq, 5), \& (2, \*(AqTCP/IP Illustrated, Volume 1\*(Aq, 5), \& (3, \*(AqInternetworking with TCP/IP Vol.1\*(Aq, 4), \& (4, \*(AqPerl Cookbook\*(Aq, 5), \& (5, \*(AqDesigning with Web Standards\*(Aq, 5); \& \& INSERT INTO \`book_authors\` (\`book_id\`, \`author_id\`) VALUES \& (1, 1), \& (1, 2), \& (1, 3), \& (2, 4), \& (3, 5), \& (4, 6), \& (4, 7), \& (5, 8); \& \& INSERT INTO \`authors\` (\`id\`, \`first_name\`, \`last_name\`) VALUES \& (1, \*(AqGreg\*(Aq, \*(AqBastien\*(Aq), \& (2, \*(AqSara\*(Aq, \*(AqNasseh\*(Aq), \& (3, \*(AqChristian\*(Aq, \*(AqDegu\*(Aq), \& (4, \*(AqRichard\*(Aq, \*(AqStevens\*(Aq), \& (5, \*(AqDouglas\*(Aq, \*(AqComer\*(Aq), \& (6, \*(AqTom\*(Aq, \*(AqChristiansen\*(Aq), \& (7, \*(AqNathan\*(Aq, \*(AqTorkington\*(Aq), \& (8, \*(AqJeffrey\*(Aq, \*(AqZeldman\*(Aq); \& \& ALTER TABLE \`book_authors\` \& ADD CONSTRAINT \`book_author_ibfk_2\` FOREIGN KEY (\`author_id\`) REFERENCES \`authors\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE, \& ADD CONSTRAINT \`book_author_ibfk_1\` FOREIGN KEY (\`book_id\`) REFERENCES \`books\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE; .Ve .IP \(bu 4 Load the data: .Sp .Vb 1 \& mysql \-u tutorial \-p myapp < myapp01_mysql.sql .Ve .IP \(bu 4 Make sure the data loaded correctly: .Sp .Vb 3 \& $ mysql \-u tutorial \-p myapp \& Reading table information for completion of table and column names \& You can turn off this feature to get a quicker startup with \-A \& \& Welcome to the MySQL monitor. Commands end with ; or \eg. \& \& Type \*(Aqhelp;\*(Aq or \*(Aq\eh\*(Aq for help. Type \*(Aq\ec\*(Aq to clear the buffer. \& \& mysql> show tables; \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& | Tables_in_myapp | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& | authors | \& | book_authors | \& | books | \& +\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& 3 rows in set (0.00 sec) \& \& mysql> select * from books; \& +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+ \& | id | title | rating | \& +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+ \& | 1 | CCSP SNRS Exam Certification Guide | 5 | \& | 2 | TCP/IP Illustrated, Volume 1 | 5 | \& | 3 | Internetworking with TCP/IP Vol.1 | 4 | \& | 4 | Perl Cookbook | 5 | \& | 5 | Designing with Web Standards | 5 | \& +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-+ \& 5 rows in set (0.00 sec) \& \& mysql> .Ve .RE .RS 4 .RE .IP \(bu 4 Update the model: .RS 4 .IP \(bu 4 Delete the existing model: .Sp .Vb 1 \& rm lib/MyApp/Model/MyAppDB.pm .Ve .IP \(bu 4 Regenerate the model using the Catalyst "_create.pl" script: .Sp .Vb 2 \& script/myapp_create.pl model DB DBIC::Schema MyApp::Schema create=static \e \& dbi:mysql:myapp \*(Aqtutorial\*(Aq \*(Aqyourpassword\*(Aq \*(Aq{ AutoCommit => 1 }\*(Aq .Ve .RE .RS 4 .RE .RE .RS 4 .RE .IP \(bu 4 Chapter 5: Authentication .RS 4 .IP \(bu 4 Create the \f(CW\*(C`.sql\*(C'\fR file for the user/roles data: .Sp Open \fImyapp02_mysql.sql\fR in your editor and enter: .Sp .Vb 10 \& \-\- \& \-\- Add users and roles tables, along with a many\-to\-many join table \& \-\- \& CREATE TABLE IF NOT EXISTS \`roles\` ( \& \`id\` int(11) NOT NULL, \& \`role\` text CHARACTER SET utf8, \& PRIMARY KEY (\`id\`) \& ) ENGINE=InnoDB DEFAULT CHARSET=utf8; \& CREATE TABLE IF NOT EXISTS \`users\` ( \& \`id\` int(11) NOT NULL, \& \`username\` text CHARACTER SET utf8, \& \`password\` text CHARACTER SET utf8, \& \`email_address\` text CHARACTER SET utf8, \& \`first_name\` text CHARACTER SET utf8, \& \`last_name\` text CHARACTER SET utf8, \& \`active\` int(11) DEFAULT NULL, \& PRIMARY KEY (\`id\`) \& ) ENGINE=InnoDB DEFAULT CHARSET=utf8; \& CREATE TABLE IF NOT EXISTS \`user_roles\` ( \& \`user_id\` int(11) NOT NULL DEFAULT \*(Aq0\*(Aq, \& \`role_id\` int(11) NOT NULL DEFAULT \*(Aq0\*(Aq, \& PRIMARY KEY (\`user_id\`,\`role_id\`), \& KEY \`role_id\` (\`role_id\`) \& ) ENGINE=InnoDB DEFAULT CHARSET=utf8; \& \-\- \& \-\- Load up some initial test data \& \-\- \& INSERT INTO \`roles\` (\`id\`, \`role\`) VALUES \& (1, \*(Aquser\*(Aq), \& (2, \*(Aqadmin\*(Aq); \& \& INSERT INTO \`users\` (\`id\`, \`username\`, \`password\`, \`email_address\`, \`first_name\`, \`last_name\`, \`active\`) VALUES \& (1, \*(Aqtest01\*(Aq, \*(Aqmypass\*(Aq, \*(Aqt01@na.com\*(Aq, \*(AqJoe\*(Aq, \*(AqBlow\*(Aq, 1), \& (2, \*(Aqtest02\*(Aq, \*(Aqmypass\*(Aq, \*(Aqt02@na.com\*(Aq, \*(AqJane\*(Aq, \*(AqDoe\*(Aq, 1), \& (3, \*(Aqtest03\*(Aq, \*(Aqmypass\*(Aq, \*(Aqt03@na.com\*(Aq, \*(AqNo\*(Aq, \*(AqGo\*(Aq, 0); \& \& INSERT INTO \`user_roles\` (\`user_id\`, \`role_id\`) VALUES \& (1, 1), \& (2, 1), \& (3, 1), \& (1, 2); \& \& ALTER TABLE \`user_roles \& ADD CONSTRAINT \`user_role_ibfk_2\` FOREIGN KEY (\`role_id\`) REFERENCES \`roles\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE, \& ADD CONSTRAINT \`user_role_ibfk_1\` FOREIGN KEY (\`user_id\`) REFERENCES \`users\` (\`id\`) ON DELETE CASCADE ON UPDATE CASCADE; .Ve .IP \(bu 4 Load the user/roles data: .Sp .Vb 1 \& mysql \-u tutorial \-p myapp < myapp02_mysql.sql .Ve .IP \(bu 4 Update the model: .RS 4 .IP \(bu 4 Regenerate the model using the Catalyst "_create.pl" script: .Sp .Vb 2 \& script/myapp_create.pl model DB DBIC::Schema MyApp::Schema create=static \e \& components=TimeStamp,PassphraseColumn dbi:mysql:myapp \*(Aqtutorial\*(Aq \*(Aqyourpassword\*(Aq \*(Aq{ AutoCommit => 1 }\*(Aq .Ve .RE .RS 4 .RE .IP \(bu 4 Create the \f(CW\*(C`.sql\*(C'\fR file for the hashed password data: .Sp Open \fImyapp03_mysql.sql\fR in your editor and enter: .Sp .Vb 6 \& \-\- \& \-\- Convert passwords to SHA\-1 hashes \& \-\- \& UPDATE users SET password = \*(Aq{SSHA}esgz64CpHMo8pMfgIIszP13ft23z/zio04aCwNdm0wc6MDeloMUH4g==\*(Aq WHERE id = 1; \& UPDATE users SET password = \*(Aq{SSHA}FpGhpCJus+Ea9ne4ww8404HH+hJKW/fW+bAv1v6FuRUy2G7I2aoTRQ==\*(Aq WHERE id = 2; \& UPDATE users SET password = \*(Aq{SSHA}ZyGlpiHls8qFBSbHr3r5t/iqcZE602XLMbkSVRRNl6rF8imv1abQVg==\*(Aq WHERE id = 3; .Ve .IP \(bu 4 Load the user/roles data: .Sp .Vb 1 \& mysql \-u tutorial \-p myapp < myapp03_mysql.sql .Ve .RE .RS 4 .RE .SH AUTHOR .IX Header "AUTHOR" Kennedy Clark, \f(CW\*(C`hkclark@gmail.com\*(C'\fR .PP Feel free to contact the author for any errors or suggestions, but the best way to report issues is via the CPAN RT Bug system at . .PP Copyright 2006\-2011, Kennedy Clark, under the Creative Commons Attribution Share-Alike License Version 3.0 ().