File Coverage

lib/UR/DataSource/Meta.pm
Criterion Covered Total %
statement 43 51 84.3
branch 7 14 50.0
condition 1 2 50.0
subroutine 7 9 77.7
pod 0 2 0.0
total 58 78 74.3


line stmt bran cond sub pod time code
1             package UR::DataSource::Meta;
2              
3             # The datasource for metadata describing the tables, columns and foreign
4             # keys in the target datasource
5              
6 39     39   1430 use strict;
  39         55  
  39         1125  
7 39     39   133 use warnings;
  39         50  
  39         1043  
8              
9 39     39   534 use version;
  39         1708  
  39         280  
10 39     39   5944 use DBD::SQLite;
  39         36235  
  39         503  
11              
12 39     39   776 use UR;
  39         57  
  39         201  
13             our $VERSION = "0.46"; # UR $VERSION;
14              
15             UR::Object::Type->define(
16             class_name => 'UR::DataSource::Meta',
17             is => ['UR::DataSource::SQLite'],
18             has_constant => [
19             owner => { value => 'main' },
20             ],
21             );
22              
23             sub _resolve_class_name_for_table_name_fixups {
24 0     0   0 my $self = shift->_singleton_object;
25              
26 0 0       0 if ($_[0] =~ m/Dd/) {
27 0         0 $_[0] = "DataSource::RDBMS::";
28             }
29              
30 0         0 return @_;
31             }
32              
33             # Do a DB dump at commit time
34 0     0 0 0 sub dump_on_commit { 1; }
35              
36             # This is the template for the schema:
37             our $METADATA_DB_SQL =<
38             CREATE TABLE IF NOT EXISTS dd_bitmap_index (
39             data_source varchar NOT NULL,
40             owner varchar,
41             table_name varchar NOT NULL,
42             bitmap_index_name varchar NOT NULL,
43             PRIMARY KEY (data_source, table_name, bitmap_index_name)
44             );
45             CREATE TABLE IF NOT EXISTS dd_fk_constraint (
46             data_source varchar NOT NULL,
47             owner varchar,
48             r_owner varchar,
49             table_name varchar NOT NULL,
50             r_table_name varchar NOT NULL,
51             fk_constraint_name varchar NOT NULL,
52             last_object_revision timestamp NOT NULL,
53             PRIMARY KEY(data_source, table_name, r_table_name, fk_constraint_name)
54             );
55             CREATE TABLE IF NOT EXISTS dd_fk_constraint_column (
56             fk_constraint_name varchar NOT NULL,
57             data_source varchar NOT NULL,
58             owner varchar NOT NULL,
59             table_name varchar NOT NULL,
60             r_table_name varchar NOT NULL,
61             column_name varchar NOT NULL,
62             r_column_name varchar NOT NULL,
63              
64             PRIMARY KEY(data_source, table_name, fk_constraint_name, column_name)
65             );
66             CREATE TABLE IF NOT EXISTS dd_pk_constraint_column (
67             data_source varchar NOT NULL,
68             owner varchar,
69             table_name varchar NOT NULL,
70             column_name varchar NOT NULL,
71             rank integer NOT NULL,
72             PRIMARY KEY (data_source,table_name,column_name,rank)
73             );
74             CREATE TABLE IF NOT EXISTS dd_table (
75             data_source varchar NOT NULL,
76             owner varchar,
77             table_name varchar NOT NULL,
78             table_type varchar NOT NULL,
79             er_type varchar NOT NULL,
80             last_ddl_time timestamp,
81             last_object_revision timestamp NOT NULL,
82             remarks varchar,
83             PRIMARY KEY(data_source, table_name)
84             );
85             CREATE TABLE IF NOT EXISTS dd_table_column (
86             data_source varchar NOT NULL,
87             owner varchar,
88             table_name varchar NOT NULL,
89             column_name varchar NOT NULL,
90             data_type varchar NOT NULL,
91             data_length varchar,
92             nullable varchar NOT NULL,
93             last_object_revision timestamp NOT NULL,
94             remarks varchar,
95             PRIMARY KEY(data_source, table_name, column_name)
96             );
97             CREATE TABLE IF NOT EXISTS dd_unique_constraint_column (
98             data_source varchar NOT NULL,
99             owner varchar,
100             table_name varchar NOT NULL,
101             constraint_name varchar NOT NULL,
102             column_name varchar NOT NULL,
103             PRIMARY KEY (data_source,table_name,constraint_name,column_name)
104             );
105             EOS
106              
107             our $module_template=<
108             package %s;
109              
110             use warnings;
111             use strict;
112              
113             use UR;
114              
115             %s
116              
117             1;
118             EOS
119              
120             # This is a bit ugly until the db cache is symmetrical with the other transactional stuff
121             # It is run by the "ur update schema" command
122             sub generate_for_namespace {
123 5     5 0 182 my $class = shift;
124 5         7 my $namespace_name = shift;
125            
126 5 50       17 Carp::confess('Refusing to make MetaDB for the UR namespace') if $namespace_name eq 'UR';
127              
128 5         22 my $namespace_path = $namespace_name->__meta__->module_path();
129              
130 5         15 my $meta_datasource_name = $namespace_name . '::DataSource::Meta';
131 5         33 my $meta_datasource = UR::Object::Type->define(
132             class_name => $meta_datasource_name,
133             is => 'UR::DataSource::Meta',
134             is_abstract => 0,
135             );
136 5         61 my $meta_datasource_src = $meta_datasource->resolve_module_header_source();
137 5         49 my $meta_datasource_filename = $meta_datasource->module_base_name();
138              
139 5         11 my $meta_datasource_filepath = $namespace_path;
140 5 100       34 return unless defined($meta_datasource_filepath); # This namespace could be fabricated at runtime
141              
142 1         6 $meta_datasource_filepath =~ s/.pm//;
143 1         1 $meta_datasource_filepath .= '/DataSource';
144 1         138 mkdir($meta_datasource_filepath);
145 1 50       17 unless (-d $meta_datasource_filepath) {
146 0         0 die "Failed to create directory $meta_datasource_filepath: $!";
147             }
148 1         3 $meta_datasource_filepath .= '/Meta.pm';
149            
150             # Write the Meta DB datasource Module
151 1 50       15 if (-e $meta_datasource_filepath) {
152 0         0 Carp::croak("Can't create new MetaDB datasource Module $meta_datasource_filepath: File already exists");
153             }
154 1         13 my $fh = IO::File->new("> $meta_datasource_filepath");
155 1 50       136 unless ($fh) {
156 0         0 Carp::croak("Can't create MetaDB datasource Module $meta_datasource_filepath: $!");
157             }
158 1         42 $fh->printf($module_template, $meta_datasource_name, $meta_datasource_src);
159              
160             # Write the skeleton SQLite file
161 1         26 my $meta_db_file = $meta_datasource->class_name->_data_dump_path;
162 1         13 IO::File->new(">$meta_db_file")->print($UR::DataSource::Meta::METADATA_DB_SQL);
163            
164 1         323 return ($meta_datasource, $meta_db_file);
165             }
166              
167             sub _dbi_connect_args {
168 50     50   129 my $self = shift;
169              
170 50         629 my @connection = $self->SUPER::_dbi_connect_args(@_);
171              
172 50 50       1375 if(version->parse($DBD::SQLite::VERSION) >= version->parse('1.38_01')) {
173 50   50     206 my $connect_attr = $connection[3] ||= {};
174 50         147 $connect_attr->{sqlite_use_immediate_transaction} = 0;
175             }
176              
177 50         299 return @connection;
178             }
179              
180             1;
181              
182             =pod
183              
184             =head1 NAME
185              
186             UR::DataSource::Meta - Data source for the MetaDB
187              
188             =head1 SYNOPSIS
189              
190             my $meta_table = UR::DataSource::RDBMS::Table->get(
191             table_name => 'DD_TABLE'
192             namespace => 'UR',
193             );
194              
195             my @myapp_tables = UR::DataSource::RDBMS::Table->get(
196             namespace => 'MyApp',
197             );
198              
199             =head1 DESCRIPTION
200              
201             UR::DataSource::Meta a datasource that contains all table/column meta
202             data for the UR namespace itself. Essentially the schema schema.
203              
204             =head1 INHERITANCE
205              
206             UR::DataSource::Meta is a subclass of L
207              
208             =head1 get() required parameters
209              
210             C or C are required parameters when calling C
211             on any MetaDB-sourced object types.
212              
213             =cut