From 15a1b6e7e38a72241074434bb2c216ce5e8bc51c Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 24 Dec 2021 14:43:22 +1100 Subject: [PATCH 1/6] this makes oracle materialised views and DDL amend correctly, including propietary oracle mv refresh statements --- lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm index 9b5d7c650..a51b881f5 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm @@ -415,12 +415,20 @@ sub _dbh_column_info { sub _view_definition { my ($self, $view) = @_; - - return scalar $self->schema->storage->dbh->selectrow_array(<<'EOF', {}, $view->schema, $view->name); + my $viewdef = scalar $self->schema->storage->dbh + ->selectrow_array(<<'EOF', {}, $view->schema, $view->name); SELECT text FROM all_views WHERE owner = ? AND view_name = ? EOF + return $viewdef if $viewdef; + + my $mview = eval { scalar $self->schema->storage->dbh + ->selectrow_array(<<'EOF', {}, $view->name, $view->schema); +select dbms_metadata.get_ddl('MATERIALIZED_VIEW', ?, ?) from dual +EOF + }; + return $mview; } =head1 SEE ALSO From 4b2a96d7d19fe572ca83f924eda8dc085470592f Mon Sep 17 00:00:00 2001 From: John Smith Date: Wed, 29 Dec 2021 14:53:26 +1100 Subject: [PATCH 2/6] code review suggestion from robk --- lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm index a51b881f5..c0c0bb0b3 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm @@ -416,10 +416,10 @@ sub _dbh_column_info { sub _view_definition { my ($self, $view) = @_; my $viewdef = scalar $self->schema->storage->dbh - ->selectrow_array(<<'EOF', {}, $view->schema, $view->name); + ->selectrow_array(<<'EOF', {}, $view->name, $view->schema); SELECT text FROM all_views -WHERE owner = ? AND view_name = ? +WHERE view_name = ? AND owner = ? EOF return $viewdef if $viewdef; From fa9b628c78a2aa58be12bf980b337cf6137e7ecb Mon Sep 17 00:00:00 2001 From: John Smith Date: Wed, 29 Dec 2021 14:53:51 +1100 Subject: [PATCH 3/6] Warn if we are using too old a DBD::Oracle and will not be able to find the materialised views --- lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm index c0c0bb0b3..96189c42d 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm @@ -422,7 +422,10 @@ FROM all_views WHERE view_name = ? AND owner = ? EOF return $viewdef if $viewdef; - + if ($DBD::Oracle::VERSION > 1.81) { + warn "DBD::Oracle must be version 1.81 or greater to be able to find materialised views\n"; + return ''; + } my $mview = eval { scalar $self->schema->storage->dbh ->selectrow_array(<<'EOF', {}, $view->name, $view->schema); select dbms_metadata.get_ddl('MATERIALIZED_VIEW', ?, ?) from dual From d9362df04272843e2751502c1c1de8f270ffb4e8 Mon Sep 17 00:00:00 2001 From: John Smith Date: Thu, 30 Dec 2021 08:11:01 +1100 Subject: [PATCH 4/6] squish warnings that can occur when ingesting a materialised view --- lib/DBIx/Class/Schema/Loader/Base.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index 4fdc9e79c..7d2706aa2 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -2696,7 +2696,7 @@ sub _setup_src_meta { } my @non_nullable_uniqs = grep { - all { $col_info->{$_}{is_nullable} == 0 } @{ $_->[1] } + all { exists $col_info->{$_} && $col_info->{$_}{is_nullable} == 0 } @{ $_->[1] } } @uniqs; if ($self->uniq_to_primary && (not @$pks) && @non_nullable_uniqs) { From f769ba5487b05a909e70de29213e232c25c2ff63 Mon Sep 17 00:00:00 2001 From: John Smith Date: Thu, 30 Dec 2021 08:11:44 +1100 Subject: [PATCH 5/6] do not add spurious virtual column constraints to materialised view result classes - they are irellevant and can cause spurious warnings / failures --- lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm index 96189c42d..c4a10199b 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm @@ -113,14 +113,19 @@ EOF sub _table_uniq_info { my ($self, $table) = @_; + ## Oracle's DDL for materialised views makes it really easy to conflate + ## with tables. Adding the left join here means don't pick up + ## spurious virtual column constraints for mvs my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1); SELECT ac.constraint_name, acc.column_name FROM all_constraints ac, all_cons_columns acc +LEFT JOIN all_mviews mv on mv.owner = acc.owner and mv.mview_name = acc.table_name WHERE acc.table_name=? AND acc.owner = ? AND ac.table_name = acc.table_name AND ac.owner = acc.owner AND acc.constraint_name = ac.constraint_name AND ac.constraint_type = 'U' AND ac.status = 'ENABLED' + AND mv.mview_name is null ORDER BY acc.position EOF From 57667195d4dc6ba4a6de1a6eb3f8336323504e20 Mon Sep 17 00:00:00 2001 From: John Smith Date: Mon, 28 Feb 2022 11:13:42 +1100 Subject: [PATCH 6/6] fix version number thinko --- lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm index c4a10199b..34f0fe387 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Oracle.pm @@ -427,7 +427,7 @@ FROM all_views WHERE view_name = ? AND owner = ? EOF return $viewdef if $viewdef; - if ($DBD::Oracle::VERSION > 1.81) { + if ($DBD::Oracle::VERSION < 1.81) { warn "DBD::Oracle must be version 1.81 or greater to be able to find materialised views\n"; return ''; }