Monday, March 24, 2014

MySQL commands

To login to mysql without selecting a database
mysql -h hostname -u username -p
(hostname can be an IP addrress)

To create a new database, and user and grant permissions on that database to the new user.
create database moodle;
create user 'moodle'@'localhost' identified by 'password';
grant all privileges on moodle.* to 'moodle'@'localhost';

To run the sql script in the file text_file.sql in the database databasename:
mysql -h hostname -u username databasename -p < text_file.sql

To create a table:
CREATE TABLE categories (
    id        INTEGER PRIMARY KEY AUTO_INCREMENT,
    name    VARCHAR(64) NOT NULL,
    created_on    DATE,
    created_by    INTEGER REFERENCES user(id),
    modified_by    INTEGER REFERENCES user(id),
    modified_on    DATE   
);

How to test creating a new row in the database

A webpage has a form and a 'Create my account' button on it which is used to create a new user.

initial = User.count
click_button "Create my account"
final = User.count
expect(initial).to eq final

Here, User is the resultset, which includes all users.
We didn't fill in any data, and we are clicking the button to submit an empty form. We expect that a new user will not be created.
So the initial count of users should remain unchanged after clicking the "Create my account" button.

To test the case where the User is successfullly created, you fill in all the values, and check that the final count is 1 more than the intial count.

visit signup_path
fill_in "Name",         with: "Example User"
fill_in "Email",        with: "user@example.com"
fill_in "Password",     with: "foobar"
fill_in "Confirmation", with: "foobar"
expect do
  click_button "Create my account"
end.to change(User, :count).by(1)


RSpec allows you to write the first test this way-

expect { click_button "Create my account" }.not_to change(User, :count)

Working with Ruby on Rails

If you make a change to the Gemfile, run bundle install, and remember to restart the server.

bundle install
rails server

rails console
rails console test
rails server --environment=production
rake db:migrate RAILS_ENV=production


When you change a model and add a new column, if you want to use it in your form, you can't just update the view. You have to add the new column name to the whitelist in the controller.

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end

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