Closed smurthys closed 6 years ago
All tasks except creating a database are now accomplished in initializedDB.sql
in v1.0.
A possible solution is to build on the .psql batch script developed in #99.
I agree that prepareDB.psql
is the right place to do database creation. One possible approach is to have the user supply a desired database name prior to running prepareDB.psql
. This can be accomplished using psql's script variables.
The user would set the value of a script variable (in this example, DB_NAME) by either invoking the -v
switch when starting psql (-v DB_NAME=<databaseName>
), or running the meta-command \set
(\set DB_NAME <databaseName>
) once inside psql.
Then, the following code added to the beginning of prepareDB.psql
will create a new database with the name stored in DB_NAME
, and then connect to it prior to running the ClassDB preparation scripts.
CREATE DATABASE :DB_NAME WITH OWNER = classDB;
\connect :DB_NAME
I did a quick test of this code, and it appears to work. If we do use this method, it will be necessary to add the CREATEDB
assertion to prepareDB.psql
, as mentioned in the first comment.
I like the approach to using the -v
psql switch. However, I highly recommend using the variable name NEW_DB_NAME
, because DB_NAME
is too close to the psql variable DBNAME
.
After much thinking and research, I have identified two alternative approaches: a simple alternative and a rigorous alternative. I recommend taking the simple alternative for M2. Also, because the existing solution does everything except create the database, this issue gets low priority in the interest of time: it should be addressed only if no other issues of higher priority exist.
The simple alternative is to create a createAndPrepareDB.psql
which blindly creates the new database, switches connection, and invokes the existing prepareDB.psql
. Any error in creating the new DB and connecting to it will just stop the script. The most likely error is that the specified DB already exists.
The rigorous alternative takes the following approach:
prepareDB.psql
as the starting pointNEW_DB_NAME
is non-empty no database with that name already exists. The conditional execution is necessary to accommodate the cases where the current DB is the DB to be initialized (which is actually the current usage)initializeDB.sql
. This way initializeDB.sql
will be independent of psql (that script presently works on current_database()
)Here are some considerations:
-l
switch prints a list of databases and exits psql. An OS script can grep the result to see if a database with a given name already existsCREATE DATABASE
cannot be executed in a transaction
CREATE DATABASE
can be executed inside a PL/pgSQL IF
block: this assumption should be tested first before proceedingSELECT COUNT(*) FROM pg_database WHERE datname=':NEW_DB_NAME';
CREATE DATABASE
can be conditionally executed, I propose we put the DB creation and the connection switching in a separate script createDB.psql
psql
file because it will contain a psql meta commandRemoving from M2 due to time constraint.
I am closing this issue because creating a database simply requires executing the CREATE DATABASE and the installation scripts in ClassDB 2.0 make it extremely easy to add ClassDB to any database.
I propose a
createClassDB.sql
to create a DB with the roleClassDB
as owner. This script should be run after runningprepareClassServer.sql
but before runningprepareClassDB.sql
.CREATEDB
privilegeprepareClassDB.sql
)ClassDB
Parameterizing the script to accept a database name probably fixes
psql
as the client, but it can be instructive to consult the docs for thecreatedb
program.