|
|
@ -81,7 +81,7 @@ poetry add setuptools || { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
# Install PostgreSQL adapter |
|
|
|
# Install PostgreSQL adapter (inside backend directory) |
|
|
|
echo "Installing PostgreSQL adapter (psycopg2-binary)..." |
|
|
|
poetry add psycopg2-binary || { |
|
|
|
echo "Failed to install psycopg2-binary through Poetry." |
|
|
@ -110,6 +110,161 @@ echo |
|
|
|
|
|
|
|
cd "${ROOT_DIR}" || exit |
|
|
|
|
|
|
|
# Database setup function |
|
|
|
setup_postgres() { |
|
|
|
echo |
|
|
|
echo "=== PostgreSQL Database Setup ===" |
|
|
|
|
|
|
|
# Check if psql is installed |
|
|
|
if ! command -v psql &> /dev/null; then |
|
|
|
echo "Error: PostgreSQL client (psql) is not installed or not in PATH" |
|
|
|
echo "Please install PostgreSQL: https://www.postgresql.org/download/" |
|
|
|
return 1 |
|
|
|
fi |
|
|
|
|
|
|
|
echo "PostgreSQL client found: $(which psql)" |
|
|
|
|
|
|
|
# Check if PostgreSQL server is running |
|
|
|
if ! pg_isready &> /dev/null; then |
|
|
|
echo "Error: PostgreSQL server is not running" |
|
|
|
echo "Please start your PostgreSQL server" |
|
|
|
return 1 |
|
|
|
fi |
|
|
|
|
|
|
|
echo "PostgreSQL server is running" |
|
|
|
|
|
|
|
# Default values |
|
|
|
local PG_USER="doccano_admin" |
|
|
|
local PG_PASS="doccano_pass" |
|
|
|
local PG_DB="doccano" |
|
|
|
local PG_HOST="localhost" |
|
|
|
local PG_PORT="5432" |
|
|
|
|
|
|
|
echo "Setting up PostgreSQL database with the following configuration:" |
|
|
|
echo " Host: ${PG_HOST}:${PG_PORT}" |
|
|
|
echo " Database: ${PG_DB}" |
|
|
|
echo " User: ${PG_USER}" |
|
|
|
echo " Password: ${PG_PASS}" |
|
|
|
echo |
|
|
|
|
|
|
|
# Ask for confirmation |
|
|
|
read -p "Continue with these settings? (y/n): " -n 1 -r |
|
|
|
echo |
|
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
|
|
|
echo "Database setup cancelled" |
|
|
|
return 1 |
|
|
|
fi |
|
|
|
|
|
|
|
# Create user if it doesn't exist |
|
|
|
echo "Creating user ${PG_USER} if it doesn't exist..." |
|
|
|
psql -h ${PG_HOST} -p ${PG_PORT} -c "SELECT 1 FROM pg_roles WHERE rolname='${PG_USER}'" | grep -q 1 |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
psql -h ${PG_HOST} -p ${PG_PORT} -c "CREATE USER ${PG_USER} WITH PASSWORD '${PG_PASS}';" |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
echo "Error: Failed to create user ${PG_USER}" |
|
|
|
return 1 |
|
|
|
fi |
|
|
|
echo "User ${PG_USER} created successfully" |
|
|
|
else |
|
|
|
echo "User ${PG_USER} already exists" |
|
|
|
fi |
|
|
|
|
|
|
|
# Grant privileges to user |
|
|
|
echo "Granting necessary privileges to ${PG_USER}..." |
|
|
|
psql -h ${PG_HOST} -p ${PG_PORT} -c "ALTER USER ${PG_USER} WITH CREATEDB;" || { |
|
|
|
echo "Error: Failed to grant CREATEDB privilege to ${PG_USER}" |
|
|
|
return 1 |
|
|
|
} |
|
|
|
|
|
|
|
# Create database if it doesn't exist |
|
|
|
echo "Creating database ${PG_DB} if it doesn't exist..." |
|
|
|
psql -h ${PG_HOST} -p ${PG_PORT} -c "SELECT 1 FROM pg_database WHERE datname='${PG_DB}'" | grep -q 1 |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
psql -h ${PG_HOST} -p ${PG_PORT} -c "CREATE DATABASE ${PG_DB} OWNER ${PG_USER};" |
|
|
|
if [ $? -ne 0 ]; then |
|
|
|
echo "Error: Failed to create database ${PG_DB}" |
|
|
|
return 1 |
|
|
|
fi |
|
|
|
echo "Database ${PG_DB} created successfully" |
|
|
|
else |
|
|
|
echo "Database ${PG_DB} already exists" |
|
|
|
|
|
|
|
# Ensure the existing database is owned by our user |
|
|
|
psql -h ${PG_HOST} -p ${PG_PORT} -c "ALTER DATABASE ${PG_DB} OWNER TO ${PG_USER};" || { |
|
|
|
echo "Warning: Failed to change ownership of existing database ${PG_DB}" |
|
|
|
} |
|
|
|
fi |
|
|
|
|
|
|
|
# Grant privileges on database |
|
|
|
echo "Granting all privileges on database ${PG_DB} to ${PG_USER}..." |
|
|
|
psql -h ${PG_HOST} -p ${PG_PORT} -c "GRANT ALL PRIVILEGES ON DATABASE ${PG_DB} TO ${PG_USER};" || { |
|
|
|
echo "Error: Failed to grant privileges on ${PG_DB} to ${PG_USER}" |
|
|
|
return 1 |
|
|
|
} |
|
|
|
|
|
|
|
echo "PostgreSQL database setup completed successfully" |
|
|
|
echo "Connection URL: postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}/${PG_DB}?sslmode=disable" |
|
|
|
echo |
|
|
|
|
|
|
|
# Store the config in a .env file |
|
|
|
echo "DATABASE_URL=postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}/${PG_DB}?sslmode=disable" > "${ROOT_DIR}/.env" |
|
|
|
echo "Database configuration saved to ${ROOT_DIR}/.env" |
|
|
|
|
|
|
|
# Create a .env file also in the backend directory for direct access |
|
|
|
echo "DATABASE_URL=postgres://${PG_USER}:${PG_PASS}@${PG_HOST}:${PG_PORT}/${PG_DB}?sslmode=disable" > "${BACKEND_DIR}/.env" |
|
|
|
echo "Database configuration also saved to ${BACKEND_DIR}/.env" |
|
|
|
|
|
|
|
return 0 |
|
|
|
} |
|
|
|
|
|
|
|
# Docker PostgreSQL setup function |
|
|
|
show_docker_command() { |
|
|
|
echo |
|
|
|
echo "=== PostgreSQL Docker Setup Command ===" |
|
|
|
echo "To set up PostgreSQL in a Docker container, run:" |
|
|
|
echo |
|
|
|
echo "docker run -d \\" |
|
|
|
echo " --name doccano-postgres \\" |
|
|
|
echo " -e POSTGRES_USER=doccano_admin \\" |
|
|
|
echo " -e POSTGRES_PASSWORD=doccano_pass \\" |
|
|
|
echo " -e POSTGRES_DB=doccano \\" |
|
|
|
echo " -v doccano-db:/var/lib/postgresql/data \\" |
|
|
|
echo " -p 5432:5432 \\" |
|
|
|
echo " postgres:13.8-alpine" |
|
|
|
echo |
|
|
|
echo "After running the container, press any key to continue setup..." |
|
|
|
echo |
|
|
|
read -n 1 -s -r |
|
|
|
# After showing the command and waiting for user to run it, save the connection info |
|
|
|
echo "DATABASE_URL=postgres://doccano_admin:doccano_pass@localhost:5432/doccano?sslmode=disable" > "${ROOT_DIR}/.env" |
|
|
|
echo "DATABASE_URL=postgres://doccano_admin:doccano_pass@localhost:5432/doccano?sslmode=disable" > "${BACKEND_DIR}/.env" |
|
|
|
echo "Database configuration saved to ${ROOT_DIR}/.env and ${BACKEND_DIR}/.env" |
|
|
|
} |
|
|
|
|
|
|
|
# Ask about database setup |
|
|
|
echo |
|
|
|
echo "=== Database Configuration ===" |
|
|
|
echo "How would you like to set up the PostgreSQL database?" |
|
|
|
echo "1. Set up local PostgreSQL database with psql (requires PostgreSQL server)" |
|
|
|
echo "2. Show Docker command to run PostgreSQL container" |
|
|
|
echo "3. Skip database setup (use existing database or configure manually)" |
|
|
|
|
|
|
|
read -p "Select an option (1-3): " db_option |
|
|
|
case $db_option in |
|
|
|
1) |
|
|
|
setup_postgres |
|
|
|
;; |
|
|
|
2) |
|
|
|
show_docker_command |
|
|
|
;; |
|
|
|
3) |
|
|
|
echo "Skipping database setup" |
|
|
|
;; |
|
|
|
*) |
|
|
|
echo "Invalid option, skipping database setup" |
|
|
|
;; |
|
|
|
esac |
|
|
|
|
|
|
|
echo "=== Setup Complete ===" |
|
|
|
echo "All dependencies have been installed successfully." |
|
|
|
echo "Python version for virtual environment: $($PYTHON_CMD --version)" |
|
|
|