Skip to content
Chris Jones edited this page Jul 12, 2018 · 30 revisions

👉 click me 👈

Ruby Notes

Preamble - Text Editor

⚠️ Try and remember to set your text editor to use 2 spaces instead of tabs, others will appreciate ❤️

Something I regret is not properly setting my text editor to use spaces for the various rails projects I worked on (╯°□°)╯︵ ┻━┻

Syntax Checking Ruby and erb files

To syntax a ruby file for syntax errors, ie. kind of known as linting a ruby file

ruby -c mr-fancy-42-file.rb

To syntax an embedded Ruby file, ie. an erb file commonly used with the rails templating engine

  1. Install rails-erb-check
gem install rails-erb-check

To syntax a mr-fancy-42-template-file.html.erb

rails-erb-check mr-fancy-42-template-file.html.erb

Working with RubyGems

gem is the app for interacting with bundled ruby apps on a system.

To list locally installed gems on the system

gem list

Yet another way to show a list of locally installed gems

gem query --local

To install a gem

gem install rails

To update gems

gem update --system

To check a Gemfile for syntax errors

ruby -c Gemfile

To read documenation for a particular gem, ie. rails or sinatra

ri rails

To view documentation for installed RubyGems installed for paritcular version of Ruby installed on the sytem in a web browser

gem server

Rails Notes

rails defaults to the system encoding which is UTF-8 the majority of the time.

Make sure all databases have been created before trying to deploy a rails app.

rake db:create:all

To create a project without a test suite

rails new _project_name_ -T

To delete a model

rails destroy model _model_name_

To run all the migrations for the DB

rake db:migrate

To show the status of the migrations, ie. whether they have been applied or not

rake db:migrate:status

To rollback one migration

rake db:rollback

To clear the contents of the DB

rake db:reset

Working with PostgreSQL

Installation

To install postgres suite of tools on Debian

apt-get install postgresql-client postgresql postgresql-contrib
sudo -u postgres psql
ALTER USER postgres PASSWORD 'mrFancyPantsPassword';

Then proceed to create a user role who can create, edit, and drop databases in psql.

CREATE USER [mrFancyPantsUser] WITH PASSWORD 'mrFancyPantsPassword';
ALTER USER [mr_fancy_pants_user] CREATEDB; 

Definitions

  • foreign key - a table column whose values reference rows in another table
  • index - a data structure on a table to increase lookup speed
  • schema - the structure definition of a database, i.e. tables columns and indexes of DB

psql

To create a role within the database

CREATE USER [mrFancyPantsUser] WITH PASSWORD 'mrFancyPantsPassword';

To list all the postgres roles on the system

\du

To create a database

create database [mr_fancy_pants_database];

To allow an existing postgres user be able to create databases

ALTER USER [mr_fancy_pants_user] CREATEDB;

To allow an existing postgres user be able to drop databases

ALTER USER [mr_fancy_pants_user] SUPERUSER;

To create a Postgres database

createdb <database_name>

To create a Postgres database using the psql monitor

create database mr_fancy_pants;

To create a new DB user for postgres

sudo -u postgres createuser <user_name>
createuser --interactive

To set a database password for a user in Postgres database

su - postgres
psql
alter user <user_name> with password ‘my_secret_password’;

🚨 The preferred way to change a password for a database user

\password

To create a database w/ a user having all privileges on the DB.

su - postgres
psql
grant all privileges on database <database_name> to <userName>;
\q

To dump a postgres database to a .sql file

pg_dump <dbname> > <fileName.sql>

Need to be logged in as postgres user to make this happen.

To import a .sql file into a postgres database, see 🙈

To create a postgres DB user

sudo -u postgres createuser <userName>

Postgres troubleshooting

psql: FATAL: Peer authentication failed for user see 🙈

MySQL Notes

To show databases

mysql> show databases;

To create a database

mysql> create database <db_name>;

To delete a database

mysql> drop database <db_name>;

To create a username for the newly created DB

mysql> grant all privileges on <db_name>.* to ‘username’@ ‘localhost’ identified by ‘password’;

To show permissions for a MySQL user

mysql> show grants for ‘username’@ ‘localhost’;

To show the structure of a table

mysql> show fields from users;

MySQL socket location on a Debian system

/var/run/mysqld/mysqld.sock

Useful Links

Documentation

marketplace platforms

Deploy

Clone this wiki locally