Mercurial > lada > lada-server
annotate db_schema/setup-db.sh @ 954:379480a94c81
Use ON DELETE CASCADE to ease removing queries from database.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Tue, 24 May 2016 17:15:44 +0200 |
parents | 4d8b8c849935 |
children | bc44dcda6f69 |
rev | line source |
---|---|
933 | 1 #!/bin/sh -e |
930
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
2 # SYNOPSIS |
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
3 # ./setup-db.sh [-c] [ROLE_NAME] [ROLE_PW] [DB_NAME] |
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
4 # -c clean - drop an existing database |
934 | 5 # ROLE_NAME name of db user (default = lada) |
930
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
6 # ROLE_PW login password (default = ROLE_NAME) |
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
7 # DB_NAME name of the databaes (default = ROLE_NAME) |
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
8 # |
934 | 9 # There will be used a remote database server if there exists the |
10 # enviroment variable DB_SRV and optional DB_PORT | |
930
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
11 |
742
17165e710631
Add script to set up database as user postgres.
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
12 DIR=`dirname $0` |
17165e710631
Add script to set up database as user postgres.
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
13 |
932
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
14 while getopts "c" opt; do |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
15 case "$opt" in |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
16 c) |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
17 DROP_DB="true" |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
18 ;; |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
19 esac |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
20 done |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
21 |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
22 shift $((OPTIND-1)) |
930
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
23 |
854
e797391714a0
Allow setting variables via commandline.
Tom Gottfried <tom@intevation.de>
parents:
764
diff
changeset
|
24 ROLE_NAME=${1:-lada} |
934 | 25 echo "ROLE_NAME = $ROLE_NAME" |
854
e797391714a0
Allow setting variables via commandline.
Tom Gottfried <tom@intevation.de>
parents:
764
diff
changeset
|
26 ROLE_PW=${2:-$ROLE_NAME} |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
27 echo "ROLE_PW = $ROLE_PW" |
854
e797391714a0
Allow setting variables via commandline.
Tom Gottfried <tom@intevation.de>
parents:
764
diff
changeset
|
28 DB_NAME=${3:-$ROLE_NAME} |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
29 echo "DB_NAME = $DB_NAME" |
742
17165e710631
Add script to set up database as user postgres.
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
30 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
31 # if variable DB_SRV and otional DB_PORT is set a remote database connection will be used |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
32 if [ -n "$DB_SRV" ] ; then DB_CONNECT_STRING="-h $DB_SRV" ; fi |
934 | 33 if [ -n "$DB_SRV" -a -n "$DB_PORT" ] ; then |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
34 DB_CONNECT_STRING="$DB_CONNECT_STRING -p $DB_PORT" |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
35 fi |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
36 DB_CONNECT_STRING="$DB_CONNECT_STRING -U postgres" |
928
6ab17d8b5558
erase mpr_id values in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
925
diff
changeset
|
37 echo "DB_CONNECT_STRING = $DB_CONNECT_STRING" |
866
d47e6b8f3897
Reorganise database setup scripts.
Tom Gottfried <tom@intevation.de>
parents:
854
diff
changeset
|
38 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
39 if [ `psql $DB_CONNECT_STRING -t --command "SELECT count(*) FROM pg_catalog.pg_user WHERE usename = '$ROLE_NAME'"` -eq 0 ] ; then |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
40 echo create user $ROLE_NAME |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
41 psql $DB_CONNECT_STRING --command "CREATE USER $ROLE_NAME PASSWORD '$ROLE_PW';" |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
42 fi |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
43 |
932
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
44 if [ "$DROP_DB" = "true" ] && psql $DB_CONNECT_STRING -l | grep -q "^ $DB_NAME " ; then |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
45 echo drop db $DB_NAME |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
46 psql $DB_CONNECT_STRING --command "DROP DATABASE $DB_NAME" |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
47 fi |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
48 |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
49 echo create db $DB_NAME |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
50 psql $DB_CONNECT_STRING --command \ |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
51 "CREATE DATABASE $DB_NAME WITH OWNER = $ROLE_NAME ENCODING = 'UTF8'" |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
52 |
934 | 53 echo create postgis extension |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
54 psql $DB_CONNECT_STRING -d $DB_NAME --command \ |
866
d47e6b8f3897
Reorganise database setup scripts.
Tom Gottfried <tom@intevation.de>
parents:
854
diff
changeset
|
55 "CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public" |
d47e6b8f3897
Reorganise database setup scripts.
Tom Gottfried <tom@intevation.de>
parents:
854
diff
changeset
|
56 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
57 echo create stammdaten schema |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
58 psql $DB_CONNECT_STRING -d $DB_NAME -f $DIR/stammdaten_schema.sql |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
59 |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
60 echo create lada schema |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
61 psql $DB_CONNECT_STRING -d $DB_NAME -f $DIR/lada_schema.sql |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
62 echo set grants |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
63 psql $DB_CONNECT_STRING -d $DB_NAME --command \ |
742
17165e710631
Add script to set up database as user postgres.
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
64 "GRANT USAGE ON SCHEMA stammdaten, bund, land TO $ROLE_NAME; |
764
9d2070b84238
Hibernate needs nextval to insert new datasets.
Tom Gottfried <tom@intevation.de>
parents:
752
diff
changeset
|
65 GRANT USAGE |
752
0cf9387c8da2
Added grant to all sequences.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
742
diff
changeset
|
66 ON ALL SEQUENCES IN SCHEMA stammdaten, bund, land TO $ROLE_NAME; |
742
17165e710631
Add script to set up database as user postgres.
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
67 GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES |
17165e710631
Add script to set up database as user postgres.
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
68 ON ALL TABLES IN SCHEMA stammdaten, bund, land TO $ROLE_NAME;" |
908
51051e85348f
Added data dumps.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
866
diff
changeset
|
69 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
70 echo import stammdaten |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
71 psql $DB_CONNECT_STRING -d $DB_NAME -f $DIR/stammdaten_data.sql |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
72 |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
73 echo import lada test data |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
74 psql $DB_CONNECT_STRING -d $DB_NAME -f $DIR/lada_data.sql |