1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use File::Slurp; |
---|
9 | use DateTime; |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | la-build_orig_schema - Tools to generate initial SQL schema |
---|
14 | |
---|
15 | =head1 SYNOPSIS |
---|
16 | |
---|
17 | la-build_orig_schema |
---|
18 | |
---|
19 | =head1 DESCRIPTION |
---|
20 | |
---|
21 | |
---|
22 | =cut |
---|
23 | |
---|
24 | GetOptions( |
---|
25 | 'c|config=s' => \my $config, |
---|
26 | 'b|base=s' => \my $base, |
---|
27 | 'help' => sub { pod2usage(0) }, |
---|
28 | ) or pod2usage(); |
---|
29 | |
---|
30 | =head1 OPTIONS |
---|
31 | |
---|
32 | =over 4 |
---|
33 | |
---|
34 | =item -c|--config configdir |
---|
35 | |
---|
36 | Use this configuration directory instead of the default one. |
---|
37 | |
---|
38 | =item -b|--base basename |
---|
39 | |
---|
40 | Query this specific base instead of the default one. |
---|
41 | |
---|
42 | =back |
---|
43 | |
---|
44 | =cut |
---|
45 | |
---|
46 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
47 | my $labase = $LA->base($base); |
---|
48 | $labase && $labase->load or die l("Cannot load base %s\n", $base); |
---|
49 | |
---|
50 | my $LAVersion = LATMOS::Accounts->VERSION; |
---|
51 | my $version = $labase->get_global_value('schema_version'); |
---|
52 | my $date = DateTime->now->iso8601; |
---|
53 | |
---|
54 | my $text = <<EOF; |
---|
55 | --- DataBase Schema for LATMOS-Accounts $LAVersion |
---|
56 | --- Schema version $version build on $date |
---|
57 | |
---|
58 | --- To properly deploy as Postgresql Admin run: |
---|
59 | --- CREATE ROLE laccounts LOGIN PASSWORD 'laccounts' ; |
---|
60 | --- CREATE DATABASE laccounts OWNER laccounts encoding 'UTF-8' ; |
---|
61 | |
---|
62 | EOF |
---|
63 | |
---|
64 | my $command = sprintf( |
---|
65 | "pg_dump -x -O -s -h %s -p %d -U %s %s", |
---|
66 | $labase->{_db}->{pg_host}, |
---|
67 | $labase->{_db}->{pg_port}, |
---|
68 | $labase->{_db}->{pg_user}, |
---|
69 | $labase->{_db}->{pg_db}, |
---|
70 | ); |
---|
71 | |
---|
72 | warn $command; |
---|
73 | |
---|
74 | open(my $handle, "$command |") or die "Cannot run pg_dump"; |
---|
75 | while (<$handle>) { |
---|
76 | $text .= $_; |
---|
77 | } |
---|
78 | close($handle); |
---|
79 | |
---|
80 | $text .= "\n"; |
---|
81 | |
---|
82 | $text .= "INSERT INTO settings (varname, val) VALUES ('schema_version', '$version');\n"; |
---|
83 | |
---|
84 | print $text; |
---|