Code Snippets
We use this page to share and store code snippets we find useful and reusable.
Ruby & Ruby on Rails
Section titled “Ruby & Ruby on Rails”Example Ruby snippet
s = "Ruby syntax highlighting"print sdef order_lookup_paramsparams .require(:order_lookup) .permit(:tote_id, :order_number) .each_value { |value| value.try(:strip!) }endJavaScript, Nodejs, & JS Frameworks
Section titled “JavaScript, Nodejs, & JS Frameworks”Example JS snippet
const s = "JavaScript syntax highlighting";console.log('s', s);HTML & CSS
Section titled “HTML & CSS”Example CSS snippet
.btn { border: none; /* Remove borders */ color: white; /* Add a text color */ padding: 14px 28px; /* Add some padding */ cursor: pointer; /* Add a pointer cursor on mouse-over */}Database Languages & Queries
Section titled “Database Languages & Queries”Creating a Postgres Cluster Locally
Create a Primary - Follower clusrter setup on Mac using the Postgres.app.
The reason for choosing Postgres.app is because it’s very easy to run several servers in parallel, each on a separate port.
-
Install Postgres.app
-
Create the Primary instance with defaults
-
Configure the Primary for Replication: Edit “/Users/…/Library/Application Support/Postgres/var-16/postgresql.conf” with the following changes:
wal_level = replica
archive_mode = on
synchronous_commit = remote_apply
synchronous_standby_names = '*'
wal_log_hints = on-
Start the Primary instance from the Postgres.app UI
-
Create a physical Replication Slot on the Master named slot1. Connect to the master database using psql and execute the command:
SELECT * FROM pg_create_physical_replication_slot('slot1');- Create an empty folder which will be used by the Follower db as the data folder.
mkdir path/to/follower/folderJust for reference, I’ve used “/Users/sergiu/Library/Application Support/Postgres/var-16-follower”.
- cd into the Postgres.app binaries for the Primary folder:
cd /Applications/Postgres.app/Contents/Versions/16/bin- Use
pg_basebackupto create a copy of the Primary data folder on the follower.
./pg_basebackup --pgdata="path/to/follower/folder" --write-recovery-conf --slot="slot1"This will connect to the Primary (that’s why --port=5432 is not given) and start copying all the data into the new folder.
- Change the rights for the Follower db data folder:
sudo chmod 0700 "/path/to/follower/folder"-
Start the Follower from Postgres.app UI.
-
Create the
kids_passport_developmentdb on the Primary and import the db dump file into it:
psql kids_passport_development < development_application_db_export.sql- Optional: if you’re connecting to these databases from a Docker app, you will have to update the
hostindatabase.ymlto the following:
host: host.docker.internal