source: contrib/davical/dba/windows/create-database.bat @ 3733

Revision 3733, 5.2 KB checked in by gabriel.malheiros, 13 years ago (diff)

Ticket #1541 - <Davical customizado para o Expresso.Utiliza Caldav e CardDav?>

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