blib/lib/Pinto/Database.pm

filter: Statementstmt 100% 71 / 71 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 5 totaltotal 93.5% 87 / 93 CC 3 SCAR 2.1
CC
3
Cov
99%
get_root_revision
2.0
create_database_schema
2.0
BEGIN
1.0
CRAP 1.2
« blib/lib/Pinto/Server/Router.pm ↑ Summary blib/lib/Pinto/Role/Schema/Result.pm »
1 # ABSTRACT: Interface to the Pinto database
2  
3 package Pinto::Database;
4  
5 58 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 58 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 58 ▶use MooseX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 58 ▶use MooseX::MarkAsMethods ( autoclean => 1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 ▶use MooseX::Types::Moose qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 58 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 58 ▶use Pinto::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 58 ▶use Pinto::Types qw(File);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 58 ▶use Pinto::Util qw(debug throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #-------------------------------------------------------------------------------
18  
19 58 our $VERSION = '0.14'; # VERSION
20  
21 #-------------------------------------------------------------------------------
22  
23 58 has repo => (
24 is => 'ro',
25 isa => 'Pinto::Repository',
26 weak_ref => 1,
27 required => 1,
28 );
29  
30 58 has schema => (
31 is => 'ro',
32 isa => 'Pinto::Schema',
33 builder => '_build_schema',
34 init_arg => undef,
35 lazy => 1,
36 );
37  
38 class_has ddl => (
39 is => 'ro',
40 isa => Str,
41 init_arg => undef,
42 58 default => do { local $/ = undef; <DATA> },
43 lazy => 1,
44 );
45  
46 #-------------------------------------------------------------------------------
47  
48 203 ▶sub _build_schema {
Subroutine _build_schema: called CC=1 SCAR=0.0
49 203 my ($self) = @_;
50  
51 203 my $schema = Pinto::Schema->new;
52  
53 203 my $db_file = $self->repo->config->db_file;
54 203 my $dsn = "dbi:SQLite:$db_file";
55 203 my $xtra = { on_connect_call => 'use_foreign_keys' };
56 203 my @args = ( $dsn, undef, undef, $xtra );
57  
58 203 my $connected = $schema->connect(@args);
59  
60 # Inject attributes thru back door
61 203 $connected->repo( $self->repo );
62  
63 # Tune sqlite (taken from monotone)...
64 203 my $dbh = $connected->storage->dbh;
65 203 $dbh->do('PRAGMA page_size = 8192');
66 203 $dbh->do('PRAGMA cache_size = 4000');
67  
68 # These may be unhelpful or unwise...
69 #$dbh->do('PRAGMA temp_store = MEMORY');
70 #$dbh->do('PRAGMA journal_mode = WAL');
71 #$dbh->do('PRAGMA synchronous = OFF');
72  
73 203 return $connected;
74 }
75  
76 #-------------------------------------------------------------------------------
77 # NB: We used to just let DBIx::Class generate the DDL from its own schema, but
78 # SQL::Translator does not support the COLLATE feature of SQLite. So now, we
79 # ship Pinto with a real copy of the DDL, and feed it into the database when
80 # the repository is initialized.
81 #
82 # Personally, I kinda prefer having a raw DDL file, rather than generating it
83 # because then I know *exactly* what the database schema will be, and we are
84 # no longer exposed to bugs that might exist in SQL::Translator. We don't need
85 # to deploy to different RDBMSes, so we don't really need SQL::Translator to
86 # help with that anyway.
87 #
88 # DBD::SQLite can only process one statement at a time, so we have to parse
89 # the file and "do" each statement separately. Splitting on semicolons is
90 # primitive, but effective (as long as semicolons are only used in statement
91 # terminators).
92 #-------------------------------------------------------------------------------
93  
94 70 ▶sub deploy {
Subroutine deploy: called CC=1 SCAR=0.0
Pod undocumented
95 70 my ($self) = @_;
96  
97 70 my $db_dir = $self->repo->config->db_dir;
98 70 debug("Makding db directory at $db_dir");
99 70 $db_dir->mkpath;
100  
101 70 my $guard = $self->schema->storage->txn_scope_guard;
102 70 $self->create_database_schema;
103 70 $self->create_root_revision;
104 70 $guard->commit;
105  
106 70 return $self;
107 }
108  
109 #-------------------------------------------------------------------------------
110  
111 70 ▶sub create_database_schema {
Subroutine create_database_schema: called CC=2 SCAR=6.9
Pod undocumented
112 70 my ($self) = @_;
113  
114 70 debug("Creating database schema");
115  
116 70 my $dbh = $self->schema->storage->dbh;
117 70 $dbh->do("$_;") for split /;/, $self->ddl;
118  
119 70 return $self;
120 }
121  
122 #-------------------------------------------------------------------------------
123  
124 70 ▶sub create_root_revision {
Subroutine create_root_revision: called CC=1 SCAR=0.0
Pod undocumented
125 70 my ($self) = @_;
126  
127 70 my $attrs = {
128 uuid => $self->root_revision_uuid,
129 message => 'root commit',
130 is_committed => 1
131 };
132  
133 70 debug("Creating root revision");
134  
135 70 return $self->schema->create_revision($attrs);
136 }
137  
138 #-------------------------------------------------------------------------------
139  
140 85 ▶sub get_root_revision {
Subroutine get_root_revision: called CC=2 SCAR=7.0
Pod undocumented
141 85 my ($self) = @_;
142  
143 85 my $where = { uuid => $self->root_revision_uuid };
144 85 my $attrs = { key => 'uuid_unique' };
145  
146 85 ▶ my $revision = $self->schema->find_revision( $where, $attrs )
Branch: 1 branch
BranchTrueFalseTotal
my $revision = $self->schema->find_revision($where, $attrs) or throw("PANIC: No root revision was found") 85 0 85
147 or throw "PANIC: No root revision was found";
148  
149 85 return $revision;
150 }
151  
152 #-------------------------------------------------------------------------------
153  
154 155 ▶sub root_revision_uuid { return '00000000-0000-0000-0000-000000000000' }
Subroutine root_revision_uuid: called CC=1 SCAR=0.0
Pod undocumented
155  
156 #-------------------------------------------------------------------------------
157  
158 58 __PACKAGE__->meta->make_immutable;
159  
160 #-------------------------------------------------------------------------------
161  
162 58 1;
163  
164 =pod
165  
166 =encoding UTF-8
167  
168 =for :stopwords Jeffrey Ryan Thalhammer
169  
170 =head1 NAME
171  
172 Pinto::Database - Interface to the Pinto database
173  
174 =head1 VERSION
175  
176 version 0.14
177  
178 =head1 AUTHOR
179  
180 Jeffrey Ryan Thalhammer <jeff@stratopan.com>
181  
182 =head1 COPYRIGHT AND LICENSE
183  
184 This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
185  
186 This is free software; you can redistribute it and/or modify it under
187 the same terms as the Perl 5 programming language system itself.
188  
189 =cut
190  
191 __DATA__
« blib/lib/Pinto/Server/Router.pm ↑ Summary blib/lib/Pinto/Role/Schema/Result.pm »