[3733] | 1 | @echo off
|
---|
| 2 | rem Build the DAViCal database
|
---|
| 3 |
|
---|
| 4 | rem BAT file changes are internal only
|
---|
| 5 | setlocal
|
---|
| 6 |
|
---|
| 7 | if db%1 EQU db (
|
---|
| 8 | echo Usage: create-database dbnameprefix [adminpassword [pguser]]
|
---|
| 9 | exit /B 1
|
---|
| 10 | )
|
---|
| 11 | set DBNAME=%1-davical
|
---|
| 12 | set ADMINPW=%2
|
---|
| 13 |
|
---|
| 14 | set DBADIR=%CD%\..
|
---|
| 15 |
|
---|
| 16 | rem Attempt to locate the AWL directory
|
---|
| 17 | set AWLDIR=%DBADIR%\..\..\awl\dba
|
---|
| 18 | echo %AWLDIR%
|
---|
| 19 | if EXIST %AWLDIR%\awl-tables.sql (
|
---|
| 20 | rem awldir=%AWLDIR%
|
---|
| 21 | ) ELSE (
|
---|
| 22 | echo awl directory not found
|
---|
| 23 | exit /B 2
|
---|
| 24 | )
|
---|
| 25 |
|
---|
| 26 | rem Set DB user, web user, DB config directory, Windows DB config directory
|
---|
| 27 | set AWL_DBAUSER=davical_dba
|
---|
| 28 | set AWL_APPUSER=davical_app
|
---|
| 29 | set DBA=%AWL_DBAUSER%
|
---|
| 30 |
|
---|
| 31 | rem Need PostgreSQL location
|
---|
| 32 | if DEFINED %PGDIR% (
|
---|
| 33 | rem Use existing variable
|
---|
| 34 | ) ELSE (
|
---|
| 35 | if EXIST "c:\Program Files\PostgreSQL\8.3\bin\createuser" (
|
---|
| 36 | set PGDIR="c:\Program Files\PostgreSQL\8.3\bin"
|
---|
| 37 | )
|
---|
| 38 | )
|
---|
| 39 | echo PGDIR=%PGDIR%
|
---|
| 40 | rem set PGDIR=%PGLOCALEDIR%\..\..\bin
|
---|
| 41 |
|
---|
| 42 | rem Get the major version for PostgreSQL
|
---|
| 43 | rem set DBVERSION="`%PGDIR\psql -qAt -c "SELECT version();" template1 | cut -f2 -d' ' | cut -f1-2 -d'.'`"
|
---|
| 44 |
|
---|
| 45 | rem Show general info
|
---|
| 46 | IF usr%3 NEQ usr ( set USERNAME=%3 )
|
---|
| 47 | echo username=%USERNAME%
|
---|
| 48 |
|
---|
| 49 | rem Create DB user, web user
|
---|
| 50 | %PGDIR%\createuser -U %USERNAME% --no-createdb --no-createrole --no-superuser %AWL_DBAUSER%
|
---|
| 51 | %PGDIR%\createuser -U %USERNAME% --no-createdb --no-createrole --no-superuser %AWL_APPUSER%
|
---|
| 52 |
|
---|
| 53 | echo Creating DB=%DBNAME%
|
---|
| 54 | %PGDIR%\createdb -E UTF8 -T template0 -U %USERNAME% %DBNAME%
|
---|
| 55 | if %ERRORLEVEL% NEQ 0 (
|
---|
| 56 | echo Unable to create database
|
---|
| 57 | exit /B 2
|
---|
| 58 | )
|
---|
| 59 |
|
---|
| 60 | rem This will fail if the language already exists, but it should not
|
---|
| 61 | rem because we created from template0.
|
---|
| 62 | %PGDIR%\createlang -U %USERNAME% plpgsql %DBNAME%
|
---|
| 63 |
|
---|
| 64 | rem Test if egrep is available
|
---|
| 65 | rem You can download egrep.exe for Windows e.g. from UnxUtils: http://unxutils.sourceforge.net/):
|
---|
| 66 | egrep 2>NULL
|
---|
| 67 | echo egrep results: %ERRORLEVEL%
|
---|
| 68 | if %ERRORLEVEL% EQU 3 (
|
---|
| 69 | rem No egrep
|
---|
| 70 |
|
---|
| 71 | rem Load the AWL base tables and schema management tables
|
---|
| 72 | echo load windows\awl-tables.sql [no egrep]
|
---|
| 73 | %PGDIR%\psql -q -f %AWLDIR/awl-tables.sql %DBNAME% %USERNAME% 2>&1
|
---|
| 74 |
|
---|
| 75 | echo load windows\schema-management.sql [no egrep]
|
---|
| 76 | %PGDIR%\psql -q -f %AWLDIR%/schema-management.sql %DBNAME% %USERNAME% 2>&1
|
---|
| 77 |
|
---|
| 78 | rem Load the DAViCal tables
|
---|
| 79 | echo load davical [no egrep]
|
---|
| 80 | %PGDIR%\psql -q -f %DBADIR%\davical.sql %DBNAME% %USERNAME% 2>&1
|
---|
| 81 |
|
---|
| 82 | ) ELSE (
|
---|
| 83 | rem egrep is available
|
---|
| 84 |
|
---|
| 85 | rem Load the AWL base tables and schema management tables
|
---|
| 86 | echo load windows\awl-tables [egrep]
|
---|
| 87 | %PGDIR%\psql -q -f %AWLDIR%/awl-tables.sql %DBNAME% %USERNAME% 2>&1 | egrep -v "(^CREATE |^GRANT|^BEGIN|^COMMIT| NOTICE: )"
|
---|
| 88 | echo load WINDOWS schema-management [egrep]
|
---|
| 89 | %PGDIR%\psql -q -f %AWLDIR%/schema-management.sql %DBNAME% %USERNAME% 2>&1 | egrep -v "(^CREATE |^GRANT|^BEGIN|^COMMIT| NOTICE: |^t$)"
|
---|
| 90 |
|
---|
| 91 | rem Load the DAViCal tables
|
---|
| 92 | echo load davical [egrep]
|
---|
| 93 | %PGDIR%\psql -q -f %DBADIR%/davical.sql %DBNAME% %USERNAME% 2>&1 | egrep -v "(^CREATE |^GRANT|^BEGIN|^COMMIT| NOTICE: |^t$)"
|
---|
| 94 | )
|
---|
| 95 | del NULL
|
---|
| 96 |
|
---|
| 97 | echo load caldav_functions
|
---|
| 98 | %PGDIR%\psql -q -f %DBADIR%/caldav_functions.sql %DBNAME% %USERNAME%
|
---|
| 99 |
|
---|
| 100 | echo TBD: Set permissions for the application DB user on the database
|
---|
| 101 | rem if EXIST %DBADIR%\update-davical-database (
|
---|
| 102 | rem %DBADIR%\update-davical-database --dbname %DBNAME% --appuser %AWL_APPUSER% --nopatch --owner %AWL_DBAUSER%
|
---|
| 103 | rem ) ELSE (
|
---|
| 104 | rem if EXIST %DBADIR%\..\update-davical-database (
|
---|
| 105 | rem %DBADIR%\..\update-davical-database --dbname %DBNAME% --appuser %AWL_APPUSER% --nopatch --owner %AWL_DBAUSER%
|
---|
| 106 | rem ) ELSE (
|
---|
| 107 | rem echo Could not find update-davical-database...ignoring
|
---|
| 108 | rem )
|
---|
| 109 | rem )
|
---|
| 110 | rem if %ERRORLEVEL% NEQ 0 (
|
---|
| 111 | rem echo * * * * ERROR * * * *
|
---|
| 112 | rem echo The database administration utility failed. This is usually due to the Perl YAML
|
---|
| 113 | rem echo or the Perl DBD::Pg libraries not being available.
|
---|
| 114 |
|
---|
| 115 | rem echo See: http://wiki.davical.org/w/Install_Errors/No_Perl_YAML
|
---|
| 116 |
|
---|
| 117 | rem exit /B 2
|
---|
| 118 | rem )
|
---|
| 119 |
|
---|
| 120 | rem Load the required base data
|
---|
| 121 | echo load base-data
|
---|
| 122 | %PGDIR%\psql -q -f %DBADIR%/base-data.sql %DBNAME% %USERNAME%
|
---|
| 123 |
|
---|
| 124 | rem We can override the admin password generation for regression testing predictability
|
---|
| 125 | rem if [ %ADMINPW}" = "" ] ; then
|
---|
| 126 | rem #
|
---|
| 127 | rem # Generate a random administrative password. If pwgen is available we'll use that,
|
---|
| 128 | rem # otherwise try and hack something up using a few standard utilities
|
---|
| 129 | rem ADMINPW="`pwgen -Bcny 2>/dev/null | tr \"\\\'\" '^='`"
|
---|
| 130 | rem fi
|
---|
| 131 | rem
|
---|
| 132 | rem if [ "$ADMINPW" = "" ] ; then
|
---|
| 133 | rem # OK. They didn't supply one, and pwgen didn't work, so we hack something
|
---|
| 134 | rem # together from /dev/random ...
|
---|
| 135 | rem ADMINPW="`dd if=/dev/urandom bs=512 count=1 2>/dev/null | tr -c -d "[:alnum:]" | cut -c2-9`"
|
---|
| 136 | rem fi
|
---|
| 137 | rem
|
---|
| 138 | rem # Right. We're getting desperate now. We'll have to use a default password
|
---|
| 139 | rem # and hope that they change it to something more sensible.
|
---|
| 140 | IF pw%ADMINPW% EQU pw ( set ADMINPW=please change this password )
|
---|
| 141 | rem fi
|
---|
| 142 |
|
---|
| 143 | %PGDIR%\psql -q -c "UPDATE usr SET password = '**%ADMINPW%' WHERE user_no = 1;" %DBNAME% %USERNAME%
|
---|
| 144 |
|
---|
| 145 | echo The password for the 'admin' user has been set to "%ADMINPW%"
|
---|
| 146 |
|
---|
| 147 | rem The supported locales are in a separate file to make them easier to upgrade
|
---|
| 148 | %PGDIR%\psql -q -f %DBADIR%/supported_locales.sql %DBNAME% %USERNAME%
|
---|
| 149 |
|
---|
| 150 | echo DONE
|
---|
| 151 |
|
---|
| 152 | :END
|
---|
| 153 |
|
---|
| 154 | endlocal
|
---|
| 155 |
|
---|