Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add users schema

  • Loading branch information...
commit 4d4e89754103a5b13af4d631e314781cb0e9b1d1 1 parent a19b0dc
@DimaD DimaD authored
View
10 lib/perlflow/Model/DB.pm
@@ -0,0 +1,10 @@
+#!/usr/bin/perl -w
+
+package Perlflow::Model::DB;
+
+use base qw/Catalyst::Model::DBIC::Schema/;
+
+__PACKAGE__->config( schema_class => 'Perlflow::Schema' );
+
+
+1;
View
21 lib/perlflow/Schema.pm
@@ -0,0 +1,21 @@
+package Perlflow::Schema;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Schema';
+
+__PACKAGE__->load_classes;
+# Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-02-15 11:30:28
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:nZOJDVGfvVOW4aIgjzHQkA
+
+# You can replace this text with custom content, and it will be preserved on regeneration
+sub sqlt_deploy_hook {
+ my ($self, $sqlt_schema) = @_;
+ my @tables = $sqlt_schema->get_tables;
+ foreach (@tables){
+ $sqlt_schema->drop_table($_->name) if ref $_->name;
+ }
+}
+
+1;
View
47 lib/perlflow/Schema/User.pm
@@ -0,0 +1,47 @@
+#!/usr/bin/perl -w
+
+package Perlflow::Schema::User;
+use strict;
+use utf8;
+
+use base qw/DBIx::Class/;
+__PACKAGE__->load_components(qw/ TimeStamp Core /);
+__PACKAGE__->table('users');
+__PACKAGE__->add_columns(
+ url => {
+ data_type => 'varchar',
+ size => 255,
+ is_nullable => 0,
+ },
+ display => {
+ data_type => 'varchar',
+ size => 255,
+ is_nullable => 0,
+ },
+
+);
+__PACKAGE__->set_primary_key('display');
+__PACKAGE__->resultset_class('Perlflow::Schema::User::ResultSet');
+
+__PACKAGE__->has_many( roles => 'Perlflow::Schema::UserRole', 'user_id' );
+
+sub add_role {
+ my ( $self, $role ) = @_;
+ $self->Roles->find_or_create( { uid => $self->id, role => $role } );
+}
+
+sub is_admin {
+ grep { $_->role eq 'admin' } shift->roles;
+}
+
+package Perlflow::Schema::User::ResultSet;
+use strict;
+use warnings;
+use base qw/DBIx::Class::ResultSet/;
+
+sub auto_create {
+ my ($self, $openid_info) = @_;
+ my $selected = { map { $_ => $openid_info->{$_} } qw/display url/ };
+ $self->create( $selected );
+}
+1;
View
23 lib/perlflow/Schema/UserRole.pm
@@ -0,0 +1,23 @@
+#!/usr/bin/perl -w
+
+package Perlflow::Schema::UserRole;
+use base qw/DBIx::Class/;
+__PACKAGE__->load_components(qw/UTF8Columns Core/);
+__PACKAGE__->table('user_roles');
+__PACKAGE__->add_columns(
+ user_id => {
+ data_type => 'int',
+ is_nullable => 0,
+ is_foreign_key => 1,
+ },
+ role => {
+ data_type => 'varchar',
+ size => 50,
+ is_nullable => 0,
+ },
+);
+__PACKAGE__->set_primary_key(qw/user_id role/);
+__PACKAGE__->utf8_columns(qw/role/);
+
+__PACKAGE__->belongs_to( user => 'Perlflow::Schema::User', 'user_id' );
+1;
View
48 script/sql.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl -w
+use strict;
+use utf8;
+use lib ('./lib');
+use YAML::Syck;
+use Perlflow::Schema;
+
+my $cfg = LoadFile 'perlflow.yml';
+$cfg = $cfg->{'Model::DB'}{connect_info};
+my $schema = Perlflow::Schema->connect(@$cfg);
+$schema = adapt_for_mysql($schema);
+
+my ($next, $prev);
+
+if (@ARGV) {
+ $next = $ARGV[0];
+ $prev = $ARGV[1];
+} else {
+ my @list = <./sql/*.sql>;
+ my $max = 0;
+ foreach (@list) {
+ /^[^-]+-Schema-(\d+)-MySQL\.sql$/ or next;
+ $max = $1 if $max < $1;
+ }
+
+ $next = $max + 1;
+ $prev = $max;
+}
+
+$schema->create_ddl_dir( ['MySQL'], $next, './sql/', $prev );
+
+sub adapt_for_mysql {
+ my ($schema)=@_;
+
+ foreach my $src ($schema->sources){
+ my $rs=$schema->source($src);
+ foreach my $cn ($rs->columns){
+ my $ci=$rs->column_info($cn);
+ if($ci->{data_type} eq 'enum'){
+ $ci->{data_type}='enum('.join(',', map {"'$_'"} @{$ci->{enum_values}}).')';
+ delete $ci->{size};
+ } elsif($ci->{data_type} eq 'DATE') {
+ delete $ci->{size};
+ }
+ }
+ }
+ return $schema;
+}
View
30 sql/Perlflow-Schema-1-MySQL.sql
@@ -0,0 +1,30 @@
+--
+-- Created by SQL::Translator::Producer::MySQL
+-- Created on Wed Oct 15 13:38:59 2008
+--
+SET foreign_key_checks=0;
+
+DROP TABLE IF EXISTS `users`;
+--
+-- Table: `users`
+--
+CREATE TABLE `users` (
+ `url` varchar(255) NOT NULL,
+ `display` varchar(255) NOT NULL,
+ PRIMARY KEY (`display`)
+) ENGINE=InnoDB;
+
+DROP TABLE IF EXISTS `user_roles`;
+--
+-- Table: `user_roles`
+--
+CREATE TABLE `user_roles` (
+ `user_id` integer NOT NULL,
+ `role` varchar(50) NOT NULL,
+ INDEX (`user_id`),
+ PRIMARY KEY (`user_id`, `role`),
+ CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`display`) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB;
+
+SET foreign_key_checks=1;
+
Please sign in to comment.
Something went wrong with that request. Please try again.