From b8282e6789c70a58614105402a4c32960301c66a Mon Sep 17 00:00:00 2001 From: Gaurav Date: Thu, 5 Oct 2023 19:47:27 +0530 Subject: [PATCH] Added script to setup database locally on Linux/WSL (#1879) * Created script to install and setup PostgreSQL database for Linux/WSL * Updated Docs --- docs/docs/developer/local-setup.mdx | 36 ++------------ infra/dev/postgres/init.sql | 8 +++ infra/dev/scripts/setup-database.sh | 76 +++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 32 deletions(-) create mode 100644 infra/dev/scripts/setup-database.sh diff --git a/docs/docs/developer/local-setup.mdx b/docs/docs/developer/local-setup.mdx index c85605167..afa849d02 100644 --- a/docs/docs/developer/local-setup.mdx +++ b/docs/docs/developer/local-setup.mdx @@ -62,41 +62,13 @@ You can access them using `twenty` postgres user (password: `twenty`) Install PostgresSQL on WSL2: ```bash - sudo apt update - sudo apt install postgresql postgresql-contrib +cd twenty/infra/dev/scripts && sudo ./setup-database.sh ``` -Start postgresql service and connect to the database using default `postgres` user: +This script will install and setup PostgreSQL with dependencies. -```bash -sudo service postgresql start -sudo -u postgres psql -``` - -Create two databases: - -```sql -CREATE DATABASE "default"; -CREATE DATABASE "test"; -``` - -Create a `twenty` user with password `twenty`: - -```sql -CREATE USER twenty PASSWORD 'twenty'; -ALTER USER twenty CREATEDB; -``` - -Create `metadata` schema: -```sql -CREATE SCHEMA IF NOT EXISTS "metadata"; -GRANT ALL ON SCHEMA metadata TO twenty; -``` - -Activate `uuid-ossp` extension: -```sql -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -``` +After successful setup, database will contain two databases: `default` and `test`, +You can access them using `twenty` postgres user (password: `twenty`) diff --git a/infra/dev/postgres/init.sql b/infra/dev/postgres/init.sql index 4478153eb..a0114991b 100644 --- a/infra/dev/postgres/init.sql +++ b/infra/dev/postgres/init.sql @@ -1,3 +1,11 @@ +-- Create table "default" for local setup without docker +SELECT 'CREATE DATABASE "default"' +WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'default')\gexec + +-- Create user "twenty" for local setup without docker +SELECT 'CREATE USER twenty PASSWORD ''twenty''' +WHERE NOT EXISTS (SELECT FROM pg_user WHERE usename = 'twenty')\gexec + -- Inflect names for pg_graphql COMMENT ON SCHEMA "public" IS '@graphql({"inflect_names": true})'; diff --git a/infra/dev/scripts/setup-database.sh b/infra/dev/scripts/setup-database.sh new file mode 100644 index 000000000..69e98e0c8 --- /dev/null +++ b/infra/dev/scripts/setup-database.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +# Colors +RED=31 +GREEN=32 +BLUE=34 + +# Function to display colored output +function echo_header { + COLOR=$1 + MESSAGE=$2 + echo -e "\e[${COLOR}m\n=======================================================\e[0m" + echo -e "\e[${COLOR}m${MESSAGE}\e[0m" + echo -e "\e[${COLOR}m=======================================================\e[0m" +} + +# Function to handle errors +function handle_error { + echo_header $RED "Error: $1" + exit 1 +} + +cat << "EOF" +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@#*+=================@@@@@%*+=========++*%@@@@@@@ +@@@@#- .+@@%=. .+@@@@@ +@@@- .*@@%- .#@@@ +@@= .=+++++++++++*#@@@= -++++++++++- %@@ +@@. %@@@@@@@@@@@@@@@+ =%@@@@@@@@@@@@= +@@ +@@. .@@@@@@@@@@@@@@+. -%@@@@@@@@@@@@@@+ +@@ +@@. .@@@@@@@@@@@@*. -#@@#:=@@@@@@@@@@@= +@@ +@@ @@@@@@@@@@#: :#@@#: -@@@@@@@@@@@= +@@ +@@#====#@@@@@@@@#- .*@@@= -@@@@@@@@@@@= +@@ +@@@@@@@@@@@@@@%- .*@@@@# -@@@@@@@@@@@= +@@ +@@@@@@@@@@@@%= +@@@@@@# -@@@@@@@@@@@= +@@ +@@@@@@@@@@@+ =@@@@@@@@# -@@@@@@@@@@@= +@@ +@@@@@@@@@+. -%@@@@@@@@@# -@@@@@@@@@@@= +@@ +@@@@@@@*. -%@@@@@@@@@@@# -@@@@@@@@@@@= +@@ +@@@@@#: :#@@@@@@@@@@@@@# -@@@@@@@@@@@+ +@@ +@@@#: :#@@@@@@@@@@@@@@@# :@@@@@@@@@@@= +@@ +@@= :+*+++++++++++*%@@@. :+++++++++- %@@ +@@ :@@@%. .#@@@ +@@- :@@@@@+: .+@@@@@ +@@@#+===================+%@@@@@@@%*++=======++*%@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +EOF + +echo_header $BLUE " DATABASE SETUP" + +PG_MAIN_VERSION=14 +PG_GRAPHQL_VERSION=1.3.0 +TARGETARCH=$(dpkg --print-architecture) + +# Install PostgresSQL +echo_header $GREEN "Step [1/4]: Installing PostgreSQL..." +apt update -y || handle_error "Failed to update package list." +apt install -y postgresql-$PG_MAIN_VERSION postgresql-contrib || handle_error "Failed to install PostgreSQL." +apt install -y curl || handle_error "Failed to install curl." + +# Install pg_graphql extensions +echo_header $GREEN "Step [2/4]: Installing GraphQL for PostgreSQL..." +curl -L https://github.com/supabase/pg_graphql/releases/download/v$PG_GRAPHQL_VERSION/pg_graphql-v$PG_GRAPHQL_VERSION-pg$PG_MAIN_VERSION-$TARGETARCH-linux-gnu.deb -o pg_graphql.deb || handle_error "Failed to download pg_graphql package." +dpkg --install pg_graphql.deb || handle_error "Failed to install pg_graphql package." +rm pg_graphql.deb + +# Start postgresql service +echo_header $GREEN "Step [3/4]: Starting PostgreSQL service..." +if sudo service postgresql start; then + echo "PostgreSQL service started successfully." +else + handle_error "Failed to start PostgreSQL service." +fi + +# Run the init.sql to setup database +echo_header $GREEN "Step [4/4]: Setting up database..." +sudo -u postgres psql -f ../postgres/init.sql || handle_error "Failed to execute init.sql script."