Skip to content

Commit

Permalink
Uninstall node@16, upgrade node@20 and postgresql@14 on mac (#117)
Browse files Browse the repository at this point in the history
## Summary:
The presence of the node@16 homebrew formula has undesirable effects
when installing postgresql@14 due to a mismatch in shared dependencies.
By removing node@16 before installing node@20 and postgresql@14,
homebrew is free to update all dependencies to latest.

Some users already have a functional node@20 brew install but are on an
older non-functional postgresql@14 due to us previously forcing
compatibility with the deprecated node@16. Fully embrace the fact that
the homebrew team only ever tests the latest formula against all other
latest formulas and upgrade node@20 and postgresql@14 if they're out of
date.

Issue: none

Test plan:

Replicate node@16 state:

```sh
brew uninstall node@16
brew uninstall node@20
brew uninstall postgresql@14

brew install node@16
brew link --force --overwrite node@16

wget -O /tmp/icu4c.rb https://raw.githubusercontent.com/Homebrew/homebrew-core/74261226614d00a324f31e2936b88e7b73519942/Formula/i/icu4c.rb
HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 brew reinstall /tmp/icu4c.rb --force --skip-cask-deps

wget -O /tmp/[email protected] https://raw.githubusercontent.com/Homebrew/homebrew-core/521c3b3f579cd4df16e0b85b26a49e47d2daf9c6/Formula/p/[email protected]
brew install /tmp/[email protected]
HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 brew reinstall /tmp/icu4c.rb --force --skip-cask-deps
```

Test node@16 setup:

```sh
brew ls --version postgresql@14
psql -tc "SELECT rolname from pg_catalog.pg_roles" postgres
node -v
```

Test khan-dotfiles upgrade to node@20 and latest postgresql@14:

```sh
make

psql -tc "SELECT rolname from pg_catalog.pg_roles" postgres
node -v
```

In webapp, verify local dev:

```sh
make deps
make serve
```

Author: nathanjd

Reviewers: MrNickBreen, csilvers, nathanjd, aag, somewhatabstract

Required Reviewers:

Approved By: MrNickBreen, csilvers

Checks:

Pull Request URL: #117
  • Loading branch information
nathanjd authored Apr 4, 2024
1 parent dee10c5 commit fe1ab03
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
26 changes: 15 additions & 11 deletions bin/mac-setup-postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# may be useful to use homebrew to install python3 before running python3
# scripts.

# TODO(ericbrown): Why do we support anything other than postgresql@14 ?

import os
import re
import subprocess
Expand All @@ -38,13 +36,6 @@ def get_brewname():
if result.returncode == 0:
return POSTGRES_FORMULA

# TODO(ericbrown): Remove when sure this is no longer needed
# I believe this code is from when postgresql 11 was the current version
result = subprocess.run([BREW, 'ls', 'postgres', '--versions'],
capture_output=True, text=True)
if result.returncode == 0 and re.search(r'\s11\.\d', result.stdout):
return "postgresql"

# There is no postgresql installed
return None

Expand All @@ -71,8 +62,8 @@ def link_postgres_if_needed(brewname, force=False):


def install_postgres() -> None:
subprocess.run(['BREW', 'install', 'postgresql@14'], check=True)
link_postgres_if_needed('postgresql@14', force=True)
subprocess.run(['BREW', 'install', POSTGRES_FORMULA], check=True)
link_postgres_if_needed(POSTGRES_FORMULA, force=True)


def is_postgres_running(brewname: str) -> bool:
Expand Down Expand Up @@ -116,6 +107,19 @@ def setup_postgres() -> None:
brewname = POSTGRES_FORMULA
install_postgres()
else:
# The homebrew team only tests the latest versions of every package
# together so make sure we're on latest.
print(f'{SCRIPT}: {BREW} upgrade {brewname}')
result = subprocess.check_output([BREW, 'upgrade', POSTGRES_FORMULA])

# Initiate and wait for restart if an upgrade happened. Otherwise the
# service won't be ready in time for does_postgres_user_exist().
if (re.search(r'Upgrading \d+ outdated package',
result.decode('utf-8'))):
print(f'{SCRIPT}: {BREW} services restart {brewname}')
subprocess.check_call([BREW, 'services', 'restart',
POSTGRES_FORMULA])

# Sometimes postgresql gets unlinked if dev is tweaking their env
# Force in case user has another version of postgresql installed too
link_postgres_if_needed(brewname, force=True)
Expand Down
52 changes: 42 additions & 10 deletions mac-setup-normal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ trap exit_warning EXIT # from shared-functions.sh


update_path() {
# We need /usr/local/bin to come before /usr/bin on the path, to
# pick up brew files we install. To do this, we just source
# .profile.khan, which does this for us (and the new user).
# (This assumes you're running mac-setup.sh from the khan-dotfiles
# directory.)
# We need /opt/homebrew/bin (/usr/local/bin on x86) to come before /usr/bin
# on the path, to pick up brew files we install. To do this, we just source
# .profile.khan, which does this for us (and the new user). (This assumes
# you're running mac-setup.sh from the khan-dotfiles directory.)
. .profile.khan
}

Expand Down Expand Up @@ -153,22 +152,55 @@ update_git() {
fi
}

# install_or_upgrade_brew_formula ensures the latest version of the passed
# formula is installed as the homebrew team only tests the latest versions of
# every formula together.
install_or_upgrade_brew_formula() {
formulaName=$1

if brew ls --versions "$formulaName" >/dev/null ; then
info "Upgrading brew formula $formulaName\n"
brew upgrade "$formulaName"
else
info "Installing brew formula $formulaName\n"
brew install "$formulaName"
fi
}

install_node() {
# We need to uninstall the deprecated node@16 homebrew formula if it is
# installed so its dependencies don't conflict with the dependencies of the
# latest postgresql@14 homebrew formula.
if brew ls --versions node@16 >/dev/null ; then
brew uninstall node@16
fi

# Upgrade brew-installed node@20 if it is already installed.
if brew ls --versions node@20 >/dev/null ; then
install_or_upgrade_brew_formula node@20
fi

# Install node@20 homebrew formula if no node binary is found in $PATH.
if ! which node >/dev/null 2>&1; then
# Install node 20: It's LTS and the latest version supported on
# appengine standard.
brew install node@20
install_or_upgrade_brew_formula node@20

# We need this because brew doesn't link /usr/local/bin/node
# by default when installing non-latest node.
# We need this because brew doesn't link /opt/homebrew/bin/node
# (/usr/local/bin/node on x86) by default when installing non-latest
# node.
brew link --force --overwrite node@20
fi

# At this point, users should have a node binary, whether it's from homebrew
# (preferred), NVM or standard install.

# We don't want to force usage of node v20, but we want to make clear we
# don't support anything else.
if ! node --version | grep "v20" >/dev/null ; then
notice "Your version of node is $(node --version). We currently only support v20."
if brew ls --versions node@20 >/dev/null ; then
notice "You do however have node 20 installed."
notice "You do however have node 20 installed via brew."
notice "Consider running:"
else
notice "Consider running:"
Expand Down Expand Up @@ -284,7 +316,7 @@ install_python_tools() {
info "Installing python 3.11\n"
brew install [email protected]
fi
# The python3 cask does not install `python` as a symlink, so we do.
# The python3 formula does not install `python` as a symlink, so we do.
if ! [ -e /usr/local/bin/python ]; then
ln -snf python3 /usr/local/bin/python
fi
Expand Down

0 comments on commit fe1ab03

Please sign in to comment.