From 07ae0fa76cc04b6bc1c192e79d30a28652a80a02 Mon Sep 17 00:00:00 2001 From: gitstart-twenty <140154534+gitstart-twenty@users.noreply.github.com> Date: Mon, 16 Oct 2023 23:02:37 +0300 Subject: [PATCH] Chore(server): Enable local database installation on MacOS (#2057) * Enable local database installation on MacOS Co-authored-by: v1b3m * Fix script --------- Co-authored-by: v1b3m Co-authored-by: Charles Bochet --- .../contributor/local-setup/yarn-setup.mdx | 16 ++- ...up-database.sh => setup-postgres-linux.sh} | 0 infra/dev/scripts/setup-postgres-macos.sh | 108 ++++++++++++++++++ 3 files changed, 121 insertions(+), 3 deletions(-) rename infra/dev/scripts/{setup-database.sh => setup-postgres-linux.sh} (100%) create mode 100755 infra/dev/scripts/setup-postgres-macos.sh diff --git a/docs/docs/contributor/local-setup/yarn-setup.mdx b/docs/docs/contributor/local-setup/yarn-setup.mdx index c5e54e7e4..a2f3c2ff3 100644 --- a/docs/docs/contributor/local-setup/yarn-setup.mdx +++ b/docs/docs/contributor/local-setup/yarn-setup.mdx @@ -60,7 +60,7 @@ This database needs to be provisionned with the a `twenty` user (password: `twen ```bash cd twenty -./infra/dev/scripts/setup-database.sh +./infra/dev/scripts/setup-postgres-linux.sh ``` Option 2: Alternatively if you have docker installed: @@ -75,7 +75,17 @@ You can access them using `twenty` postgres user (password: `twenty`) -You must provision PostgresSQL using Docker. To do so, run the following command: +Option 1: To provision your database locally: +

+ +```bash +cd twenty +./infra/dev/scripts/setup-postgres-macos.sh +``` + +Option 2: Alternatively if you have docker installed: +

+ ```bash cd twenty make provision-postgres @@ -89,7 +99,7 @@ You can access them using `twenty` postgres user (password: `twenty`) We recommend to provision your database locally: ```bash cd twenty -bash ./infra/dev/scripts/setup-database.sh +bash ./infra/dev/scripts/setup-postgres-linux.sh ``` This will create a Docker container, exposing a PostgresSQL instance at [http://localhost:5432](http://localhost:5432). You can access them using `twenty` postgres user (password: `twenty`) diff --git a/infra/dev/scripts/setup-database.sh b/infra/dev/scripts/setup-postgres-linux.sh similarity index 100% rename from infra/dev/scripts/setup-database.sh rename to infra/dev/scripts/setup-postgres-linux.sh diff --git a/infra/dev/scripts/setup-postgres-macos.sh b/infra/dev/scripts/setup-postgres-macos.sh new file mode 100755 index 000000000..3d15484cf --- /dev/null +++ b/infra/dev/scripts/setup-postgres-macos.sh @@ -0,0 +1,108 @@ +#!/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 +CARGO_PGRX_VERSION=0.9.8 + +current_directory=$(pwd) + +# Install PostgresSQL +echo_header $GREEN "Step [1/4]: Installing PostgreSQL..." +brew install postgresql@$PG_MAIN_VERSION + +# Install pg_graphql extensions +echo_header $GREEN "Step [2/4]: Installing GraphQL for PostgreSQL..." + +# To force a reinstall of cargo-pgrx, pass --force to the command below +curl https://sh.rustup.rs -sSf | sh +source "$HOME/.cargo/env" +cargo install --locked cargo-pgrx@$CARGO_PGRX_VERSION +cargo pgrx init --pg14 download + +# Uninstall existing Rust installation if found +existing_rust_path=$(which rustc) +if [ -n "$existing_rust_path" ]; then + echo "Uninstalling existing Rust installation..." + rm -rf "$existing_rust_path" +fi + +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y + +# Add Cargo's bin directory to PATH +. "$HOME/.cargo/env" + +# Create a temporary directory +temp_dir=$(mktemp -d) +cd "$temp_dir" + +curl -LJO https://github.com/supabase/pg_graphql/archive/refs/tags/v$PG_GRAPHQL_VERSION.zip || handle_error "Failed to download pg_graphql package." + +unzip pg_graphql-$PG_GRAPHQL_VERSION.zip + +cd "pg_graphql-$PG_GRAPHQL_VERSION" +cargo pgrx install --release + +# Clean up the temporary directory +echo "Cleaning up..." +cd "$current_directory" +rm -rf "$temp_dir" + +# Start postgresql service +echo_header $GREEN "Step [3/4]: Starting PostgreSQL service..." +if brew services start postgresql@$PG_MAIN_VERSION; 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..." +cp ./infra/dev/postgres/init.sql /tmp/init.sql +psql -f /tmp/init.sql || handle_error "Failed to execute init.sql script."