]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/build-db.sh
Apply patch from Aaron S. Joyner to improve user friendliness of database build script
[Evergreen.git] / Open-ILS / src / sql / Pg / build-db.sh
1 #!/bin/sh
2
3 # ---------------------------------------------------------------------------
4 # Store command line args for later use
5 # args: {db-host} {db-port} {db-name} {db-user} {db-password}
6 # ---------------------------------------------------------------------------
7 PGHOST=$1
8 PGPORT=$2
9 PGDATABASE=$3
10 PGUSER=$4
11 PGPASSWORD=$5
12 export PGHOST PGPORT PGDATABASE PGUSER PGPASSWORD
13
14 # ---------------------------------------------------------------------------
15 # Lookup the database version from the PostgreSQL server.
16 # ---------------------------------------------------------------------------
17 DB_VERSION=`psql -qtc 'show server_version;' | xargs | cut -c1,3`
18 if [ -z "$DB_VERSION" ] || [ `echo $DB_VERSION | grep -c '[^0-9]'` != 0 ]; then
19   cat <<EOM
20 ********************************************************************************
21 * Could not determine the version of PostgreSQL you have installed.  Our best  *
22 * guess was:                                                                   *
23 * $DB_VERSION
24 * which didn't make any sense.  For assistance, please email                   *
25 * open-ils-general@list.georgialibraries.org or join #OpenILS-Evergreen on the *
26 * freenode IRC network.                                                        *
27 ********************************************************************************
28 EOM
29   exit 1
30 fi
31
32 # ---------------------------------------------------------------------------
33 # Validate fts-config file is available for specified DB_VERSION.
34 # ---------------------------------------------------------------------------
35 if [ -e "000.english.pg$DB_VERSION.fts-config.sql" ]; then
36   fts_config_file="000.english.pg$DB_VERSION.fts-config.sql"
37 else
38   # -------------------------------------------------------------------------
39   # Attempt to auto-detect the latest available config file.
40   # -------------------------------------------------------------------------
41   last_ver=""
42   for i in $(seq 80 99 | sort -rn); do
43     if [ -e "000.english.pg$i.fts-config.sql" ]; then
44       last_ver=$i
45       break
46     fi
47   done
48   if [ -z "$last_ver" ]; then
49     cat <<EOM
50 ********************************************************************************
51 * Cannot locate any configuration files for  full text search config.  This    *
52 * may indicate a problem with your copy of the source files.  We attempted to  *
53 * find files like 000.english.pg83.fts-config.sql in this directory:           *
54 * `pwd` 
55 * but were unsuccessful.  Aborting.                                            *
56 ********************************************************************************
57 EOM
58     exit 1
59   fi
60
61   a=$DB_VERSION  # preserves the text alignment below, in a cheap fashion
62   b=$last_ver    # assuming of course two character DB_VERSION and last_ver
63 cat <<EOM
64 ********************************************************************************
65 * There is no configuration for full text search config, for the database      *
66 * version you have installed ($a).  If you're not really sure why, you should  *
67 * proabably press 'Control-C' now, and abort.  To continue using the latest    *
68 * available version ($b), press enter. For assistance, please email            *
69 * open-ils-general@list.georgialibraries.org or join #OpenILS-Evergreen on the *
70 * freenode IRC network.                                                        *
71 ********************************************************************************
72 EOM
73   read unused
74   fts_config_file="000.english.pg$last_ver.fts-config.sql"
75 fi
76
77 # ---------------------------------------------------------------------------
78 # This describes the order in which the SQL files will be eval'd by psql.
79 # ---------------------------------------------------------------------------
80 ordered_file_list="
81   $fts_config_file
82   001.schema.offline.sql
83   002.schema.config.sql
84   005.schema.actors.sql
85   006.schema.permissions.sql
86   010.schema.biblio.sql
87   011.schema.authority.sql
88   020.schema.functions.sql
89   030.schema.metabib.sql
90   040.schema.asset.sql
91   070.schema.container.sql
92   080.schema.money.sql
93   090.schema.action.sql
94
95   300.schema.staged_search.sql
96
97   500.view.cross-schema.sql
98
99   800.fkeys.sql
100   900.audit-functions.sql
101   901.audit-tables.sql
102
103   reporter-schema.sql
104 "
105
106 # ---------------------------------------------------------------------------
107 # Import files via psql, warn user on error, suggest abort.
108 # ---------------------------------------------------------------------------
109 for sql_file in $ordered_file_list; do
110   # It would be wise to turn this on only if confidence is high that errors in
111   # scripts will result in terminal failures.  Currently, there are a couple
112   # that seem benign.  --asjoyner
113   # export ON_ERROR_STOP=1
114
115   export PGHOST PGPORT PGDATABASE PGUSER PGPASSWORD
116   psql -f $sql_file
117   if [ $? != 0 ]; then
118     cat <<EOM
119 ********************************************************************************
120 * There was an error with a database configuration file:                       *
121 * $sql_file
122 * It is very likely that your installation will be unsuccessful because of     *
123 * this error.  Press Control-C to abort, or press enter to charge ahead.       *
124 ********************************************************************************
125 EOM
126     read unused
127   fi
128 done
129