File: initdb

package info (click to toggle)
openqa 5.1764349525.ffb594867-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,264 kB
  • sloc: perl: 57,599; sql: 26,462; javascript: 8,466; xml: 2,229; sh: 1,705; makefile: 443; python: 249
file content (113 lines) | stat: -rwxr-xr-x 3,625 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env perl

# Copyright 2014-2021 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

use FindBin qw($RealBin);
use lib "$RealBin/../lib";

use strict;
use warnings;
use DBIx::Class::DeploymentHandler;
use File::Basename qw(dirname);
use POSIX qw(getuid getgid setuid setgid);
use OpenQA::Schema;
use Getopt::Long;
use IO::Dir;

my $help = 0;
my $prepare_init = 0;
my $init_database = 0;
my $force = 0;
my $user;
my $default_script_directory = "$FindBin::RealBin/../dbicdh";
my $script_directory = $default_script_directory;

my $result = GetOptions(
    "help" => \$help,
    "prepare_init" => \$prepare_init,
    "init_database" => \$init_database,
    "force" => \$force,
    "user=s" => \$user,
    "dir=s" => \$script_directory,
);

sub usage {
    print "Usage: $0 [flags]\n\n";
    print "  --prepare_init   : Create the deployment files used to initialize the database.\n";
    print "                     Don't forget to increase the version before using this\n";
    print "                     and note those files should be committed to the source repo.\n";
    print "  --init_database  : Use the generated deployment files created with --prepare_init\n";
    print "                     to actually initialize a database (or upgrade an outdated one).\n";
    print "                     Exit code 4 indicates database already exists and is updated and\n";
    print "                     --force was not used.\n";
    print "  --force          : Force overwriting existing data.\n";
    print "  --user=login     : Change uid before connecting the DB.\n";
    print "  --dir=directory  : Create deployment and migration scripts in this directory.\n";
    print "                     Default is '$default_script_directory'\n";
    print "  --help           : This help message.\n";
    exit $_[0];
}

usage 0 if $help;
usage 1 unless $result or $prepare_init or $init_database;

if ($user) {
    my $uid = getpwnam($user) || die "No such login $user";
    my $gid = getgrnam($user);
    if ($gid) {
        setgid($gid) || die "can't sgid to $user group";
    }
    setuid($uid) || die "can't suid to $user";
}

my @databases = qw( PostgreSQL );
my $schema = OpenQA::Schema::connect_db(deploy => 0, silent => 1, from_script => 1);

if ($prepare_init) {
    my $dh = DBIx::Class::DeploymentHandler->new(
        {
            schema => $schema,
            script_directory => $script_directory,
            databases => \@databases,
            sql_translator_args => {add_drop_table => 0, quote_identifiers => 0},
            force_overwrite => $force,
        });

    my $version = $dh->schema_version;

    my %deploy_dir;
    foreach my $db (@databases) {
        tie %deploy_dir, 'IO::Dir', "$script_directory/$db/deploy";

        if (exists $deploy_dir{$version} and not $force) {
            print "The deploy directory already contains the schema for the current version.\n";
            print "Use the --force option if you want to overwrite the contents of the ";
            print "$script_directory/$db/deploy/$version directory\n";
            exit 1;
        }
    }

    $dh->prepare_install;
}
if ($init_database) {
    # Create the schema
    my $ret = $schema->deploy($force);
    if ($ret == 0) {
        print "Database already exists and schema is up to date.\n";
        exit 4;
    }
    elsif ($ret == 2) {
        print "Database initialized.\n";
        exit 0;
    }
    elsif ($ret == 1) {
        print "Database already exists, but schema was upgraded.\n";
        exit 0;
    }
    else {
        print "Unexpected result from deployment!\n";
        exit 1;
    }
}