Sunday, March 23, 2014

SQLite Commands

sqlite3 development.sqlite3
sqlite> .tables
sqlite> .schema users
sqlite> select * from users;
sqlite> .quit

Ruby on Rails models

We generate a rails model only once, when we want to create a new table in the database. After that we use migrations to make changes to our tables.


rails generate model User name:string email:string
bundle exec rake db:migrate

bundle exec rake db:rollback

rails console                     # Saves to DB
rails console --sandbox    # Will not save to DB

>> User.new
>> user = User.new(name: "Test One", email: "asdf@asdf.com")     #Create in memory
>> user.save          # Write to DB, returns true on success
>> user.name
>> user.updated_at
>> user2 = User.create(name: "Test two", email: "asdf1@asdf.com")  # Create and write to DB
>> user2.destroy

>> User.find(1)
>> User.find_by(email: "asdf@asdf.com")

>> User.first
>> User.all

>> user.email = "test@one.com"
>> user.save
>> user.reload   # If you don't want to save your changes, and read details from DB

>> user.update_attributes(name: "Testing One", email: "qwer@qwer.com") # write to DB
>> user.update_attribute(:name, "Tester One")

rake test:prepare

rails generate migration add_password_digest_to_users password_digest:string

Sunday, February 2, 2014

bash tips

Check if the last command you ran was successful

echo $?

If it returns 0, the command completed successfully. 1 means, it errored out.

Monday, January 6, 2014

Useful Linux Commands (ubuntu)

Find the size of a directory
du -ah 

See how much space is free, and how much is used
df -h

List all the commands that you can use as sudo
sudo -l

If the output of the command is:
    (www-data) NOPASSWD: /usr/bin/php
    (www-data) NOPASSWD: ALL

You can run php as www-data, but also, you can run any other command as www-data.

To run chmod as www-data
sudo -u www-data chmod g+w testdir

Copying files from one machine to another, when you want to copy only the changes the next time around.
rsync -avz from_local_dir machinename:/some/dir/on_other_machine


Sunday, January 5, 2014

Postgres commands

dropdb dbname

createdb -E utf8 -O username databasename

pg_restore -O path_to_dump.pgdump -U username -d databasename


Sunday, December 29, 2013

Commonly used git commands

  • git checkout oldbranch # Checkout an existing branch
  • git checkout -b newbranchname  # Create a new branch 
  • git checkout -b newbranch remote/newbranch # to avoid confusions

  • git status  # See your changes

  • git branch --all | grep pattern      # All branches in all repos you have added
  • git branch                                     # to list local branches

  • git push --dry-run     # See what you are going to push
  • git push                     # Actually push

  • git config branch.branchname.remote    # to see the value
  • git config branch.branchname.remote  privatebranch   # to set the value

  • git config branch.branchname.merge
  • git config branch.branchname.merge branchname # the remote merge branch 

 # Show all commit logs for all commits on branch1 that are not on branch2
  • git log branch1 ^branch2 --no-merges 
  •   git push origin :branchname    # Delete a remote branch
  •  git remote add newrem git+ssh://path.to.git/remote
# Run this as soon as you add a remote repository. To download all branches.
  • git fetch
  • git fetch remote
   

Wednesday, February 27, 2013

Using screen to manage your terminal windows

You can use screen if you need to have more than one terminal window open to at the same time.

The advantage of screen is that even if there is an internet problem, you can reattach to the screen where you were working on once your internet is back up.

screen                             Will open a new screen
exit                                  To quit the screen, and go back to the shell
screen -ls                         Display a list of all screens
screen -d screen_name     Detach from screen_name

In Screen, you type Control-a and then you can type in your command. Below are a few common commands.

Ctrl-A :sessionname dev_server    Names the current screen dev_server

Ctrl-A c         Open a new screen
Ctrl-a n          Switch to next screen
Ctrl-a p          Switch to previous screen
Ctrl-a \           Quit and kill all your windows