Merge branch 'master' into topic/tracking_transformation
[sgn.git] / db / 00157 / AlterDescriptionToText.pm
blob00c89ca0bc7923ff4e0f2dda19f9e247777c39b8
1 #!/usr/bin/env perl
4 =head1 NAME
6 AlterDescriptionToText.pm
8 =head1 SYNOPSIS
10 mx-run AlterDescriptionToText [options] -H hostname -D dbname -u username [-F]
12 this is a subclass of L<CXGN::Metadata::Dbpatch>
13 see the perldoc of parent class for more details.
15 =head1 DESCRIPTION
17 Alters project table description column from varchar(255) to text
19 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
21 =head1 AUTHOR
23 Bryan Ellerbrock<bje24@cornell.edu>
25 =head1 COPYRIGHT & LICENSE
27 Copyright 2010 Boyce Thompson Institute for Plant Research
29 This program is free software; you can redistribute it and/or modify
30 it under the same terms as Perl itself.
32 =cut
35 package AlterDescriptionToText;
37 use Moose;
38 extends 'CXGN::Metadata::Dbpatch';
41 has '+description' => ( default => <<'' );
42 Alters project table description column from varchar(255) to text
43 has '+prereq' => (
44 default => sub {
45 [''],
49 sub patch {
50 my $self=shift;
52 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
54 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
56 print STDOUT "\nExecuting the SQL commands.\n";
58 $self->dbh->do(<<EOSQL);
59 --do your SQL here
61 Running just "ALTER TABLE project ALTER COLUMN description TYPE text"
62 produces
64 ERROR: cannot alter type of a column used by a view or rule
65 DETAIL: rule _RETURN on materialized view materialized_phenotype_jsonb_table depends on column "description"
67 so the following function drops the view, does the edit, then restores the view
70 do \$\$
71 DECLARE phenotype_jsonb_def text;
72 DECLARE exec_text text;
73 begin
74 phenotype_jsonb_def = pg_get_viewdef('materialized_phenotype_jsonb_table');
75 DROP MATERIALIZED VIEW public.materialized_phenotype_jsonb_table;
77 ALTER TABLE project ALTER COLUMN description TYPE text;
79 exec_text = FORMAT('CREATE MATERIALIZED VIEW public.materialized_phenotype_jsonb_table AS %s',
80 phenotype_jsonb_def);
81 EXECUTE exec_text;
82 CREATE UNIQUE INDEX materialized_phenotype_jsonb_table_obsunit_stock_idx ON public.materialized_phenotype_jsonb_table(observationunit_stock_id) WITH (fillfactor=100);
83 CREATE INDEX materialized_phenotype_jsonb_table_obsunit_uniquename_idx ON public.materialized_phenotype_jsonb_table(observationunit_uniquename) WITH (fillfactor=100);
84 CREATE INDEX materialized_phenotype_jsonb_table_germplasm_stock_idx ON public.materialized_phenotype_jsonb_table(germplasm_stock_id) WITH (fillfactor=100);
85 CREATE INDEX materialized_phenotype_jsonb_table_germplasm_uniquename_idx ON public.materialized_phenotype_jsonb_table(germplasm_uniquename) WITH (fillfactor=100);
86 CREATE INDEX materialized_phenotype_jsonb_table_trial_idx ON public.materialized_phenotype_jsonb_table(trial_id) WITH (fillfactor=100);
87 CREATE INDEX materialized_phenotype_jsonb_table_trial_name_idx ON public.materialized_phenotype_jsonb_table(trial_name) WITH (fillfactor=100);
88 ALTER MATERIALIZED VIEW public.materialized_phenotype_jsonb_table OWNER TO web_usr;
89 end \$\$;
92 EOSQL
94 print "You're done!\n";
98 ####
99 1; #
100 ####