source: trunk/LATMOS-Accounts/bin/la-sql-upgrade.in @ 1123

Last change on this file since 1123 was 1074, checked in by nanardon, 12 years ago

Add 'description' attribute to aliases/revaliases

This attribute allow to add a description to an aliases object.
This attribute is set automatically when such object are created from user
object.

File size: 7.6 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
8use Text::CSV_XS;
9
10my $DATADIR = '@DATADIR@';
11
12=head1 NAME
13
14    la-sql-attreg - Tools to register attributes in LA SQL base
15
16=head1 SYNOPSIS
17
18    la-sql-attreg [options] name
19
20=cut
21
22GetOptions(
23    'c|config=s'     => \my $config,
24    'b|base=s'       => \my $base,
25    'a|attributes=s' => \my $attributes_file,
26    'no-commit'      => \my $nocommit,
27    'v|verbose'      => \my $verbose,
28) or pod2usage();
29
30$attributes_file ||= "$DATADIR/attributes.csv";
31
32=head1 OPTIONS
33
34=over 4
35
36=item -c|--config configdir
37
38Use this configuration directory instead of the default one.
39
40=item -b|--base basename
41
42Query this specific base instead of the default one.
43
44=back
45
46=cut
47
48$| = 1; # autoflush
49
50my $LA = LATMOS::Accounts->new($config, noacl => 1);
51my $labase = $LA->base($base);
52
53my $dbi = $labase->db;
54
55my $rev = $labase->get_global_value('schema_version') || 1;
56
57my @updates = (
58    {
59        ver => 3,
60        sql => [
61            q{ALTER TABLE nethost_attributes_ips DROP CONSTRAINT
62            nethost_is_single_ip},
63            q{DROP TRIGGER IF EXISTS check_zone_name_unity_tg ON nethost},
64            q{DROP TRIGGER IF EXISTS check_zone_name_unity_tg ON netzone},
65            q{DROP FUNCTION IF EXISTS check_zone_name_unity()},
66            q{ALTER TABLE nethost_attributes_ips DROP CONSTRAINT
67            nethost_is_single_ip},
68            q{INSERT INTO settings(varname, val) VALUES ('schema_version', 3)},
69        ],
70    },
71    {
72        ver => 4,
73        sql => [
74            q{
75            CREATE OR REPLACE FUNCTION nethost_sort_fields()
76            RETURNS trigger AS
77            $BODY$BEGIN
78
79            IF (TG_OP='INSERT') then
80            IF (new.attr='ip') THEN
81            insert into nethost_attributes_ips VALUES (new.*);
82            RETURN NULL;
83            END IF;
84
85            IF (new.attr='macaddr') THEN
86            insert into nethost_attributes_macs VALUES (new.*);
87            RETURN NULL;
88            END IF;
89
90            IF (new.attr='owner') THEN
91            insert into nethost_attributes_users VALUES (new.*);
92            RETURN NULL;
93            END IF;
94
95            IF (new.attr='user') THEN
96            insert into nethost_attributes_users VALUES (new.*);
97            RETURN NULL;
98            END IF;
99            end if;
100
101
102            if (TG_OP='DELETE') THEN
103            RETURN old;
104            else
105            RETURN new;
106            end if;
107            END;$BODY$
108            LANGUAGE plpgsql VOLATILE
109            COST 100;
110            },
111        ],
112    },
113    {
114        ver => 5,
115        sql => [
116            q{
117            CREATE OR REPLACE VIEW address_attributes AS
118            ( ( SELECT address."user" AS value, 'user' AS attr, address.rev AS attr_key, address.ikey AS okey
119            FROM address
120            UNION ALL
121            SELECT address.name AS value, 'name' AS attr, address.rev AS attr_key, address.ikey AS okey
122            FROM address )
123            UNION ALL
124            SELECT address_attributes.value, address_attributes.attr, address_attributes.attr_key, address_attributes.okey
125            FROM address_attributes_base address_attributes )
126            UNION ALL
127            SELECT '1' AS value, 'active' AS attr, address.rev AS attr_key, address.ikey AS okey
128            FROM "user" JOIN address ON "user".name = address."user"
129            WHERE "user".exported AND address.exported and
130            ("user".expire IS NULL or "user".expire > now())
131            }
132        ],
133    },
134    {
135        ver => 6,
136        sql => [
137            q{
138            CREATE TABLE request
139            (
140                id serial NOT NULL,
141                "create" timestamp with time zone NOT NULL DEFAULT now(),
142                name text NOT NULL,
143                object text,
144                apply timestamp with time zone NOT NULL DEFAULT now(),
145                done timestamp with time zone,
146                "user" text,
147                CONSTRAINT request_pkey PRIMARY KEY (id ),
148                CONSTRAINT request_name_fkey FOREIGN KEY (name)
149                REFERENCES accreq (name) MATCH SIMPLE
150                ON UPDATE CASCADE ON DELETE CASCADE,
151                CONSTRAINT request_user_fkey FOREIGN KEY ("user")
152                REFERENCES "user" (name) MATCH SIMPLE
153                ON UPDATE CASCADE ON DELETE SET NULL
154            )
155            },
156            q{
157            CREATE INDEX fki_request_name_fkey ON request
158            USING btree (name )
159            },
160            q{
161            CREATE INDEX fki_request_user_fkey ON request
162            USING btree ("user" )
163            },
164            q{
165            CREATE TABLE request_attributes
166            (
167                reqid bigint,
168                attribute text NOT NULL,
169                value text,
170                CONSTRAINT request_id_fkey FOREIGN KEY (reqid)
171                REFERENCES request (id) MATCH SIMPLE
172                ON UPDATE CASCADE ON DELETE CASCADE
173            )
174            },
175            q{
176            CREATE INDEX fki_request_id_fkey ON request_attributes
177            USING btree (reqid )
178            },
179            q{
180            delete from accreq_attributes_list
181            }
182        ],
183    },
184    {
185        ver => 7,
186        sql => [
187            q{
188            ALTER TABLE request ADD COLUMN automated boolean NOT NULL DEFAULT false;
189            },
190            q{
191            ALTER TABLE request ADD COLUMN objrev bigint;
192            },
193            q{
194            ALTER TABLE aliases ADD COLUMN description text;
195            },
196            q{
197            ALTER TABLE revaliases ADD COLUMN description text;
198            },
199        ],
200    },
201);
202
203my @objects = (
204    {
205        name => 'dpmt',
206        otype => 'sutype',
207        attrs => {
208            description => 'Department',
209        },
210    },
211    {
212        name => 'contrattype',
213        otype => 'sutype',
214        attrs => {
215            description => 'Contract',
216        },
217    },
218);
219
220$dbi->rollback;
221
222foreach my $maj (@updates) {
223    if ($rev >= $maj->{ver}) {
224        next;
225    }
226    print "\n";
227    print 'Switching to schema revision: ' . $maj->{ver} . "\n";
228    foreach my $sql (@{ $maj->{sql} }) {
229        {
230            my $sqlv = $sql;
231            $sqlv =~ s/^/  /mg;
232            if ($verbose) {
233                warn '  >' . $sqlv . "\n";
234            } else {
235                print 'x';
236            }
237        }
238        $dbi->do($sql) or die "Erreur :\\" . $dbi->errstr . "\n";
239    }
240
241    print "\n";
242
243    print 'Updating schema_version to ' . $maj->{ver} . "\n";
244
245    $dbi->do(
246        'UPDATE settings SET val = ? where varname = ?',
247        undef,
248        $maj->{ver}, 'schema_version'
249    );
250    print "Done\n\n";
251
252}
253
254print "Loading attribute from: $attributes_file\n";
255
256open my $fh, "<:encoding(utf8)", $attributes_file or die "$attributes_file: $!";
257 
258my $csv = Text::CSV_XS->new();
259 
260while (my $row = $csv->getline($fh)) {
261    my ($otype, $attribute, $comment) = @$row;
262    if ($labase->is_registered_attribute($otype, $attribute)) {
263    } else {
264        $labase->register_attribute($otype, $attribute, $comment)
265            or die "Error, aborting\n";
266        print "Attr. $attribute for object type $otype registred\n";
267    }
268}
269 
270$csv->eof or do {
271    $csv->error_diag();
272    die "Cannot load attribute\n"
273};
274
275foreach (@objects) {
276    if (!$labase->get_object($_->{otype}, $_->{name})) {
277        printf("Creatting object %s/%s\n", $_->{otype}, $_->{name});
278        $labase->create_object($_->{otype}, $_->{name}, %{$_->{attrs} || {}})
279            or die sprintf("cannot create %s/%s\n", $_->{otype}, $_->{name});
280
281    }
282}
283
284if ($nocommit) {
285    $dbi->rollback;
286} else {
287    $dbi->commit;
288}
289print "Process terminated successfully\n";
Note: See TracBrowser for help on using the repository browser.