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