More On PostgreSQL and Rails

I had already posted on getting a Rails app to work with PostgreSQL. For that app, I was starting over from SQLite.

But now I am starting to use PostgreSQL from the beginning since Heroku runs on PostgreSQL. I learned more about this from RailsCasts.

First you have to use the –database option when you make the app:

Then you need to edit the config/database.yml file. I have not sent my app to Heroku yet, so I comment out the prod stanza. For the test and development stanzas I change the username to the username that I am logged in as on my Ubuntu laptop.

Then I run

Then you can run

Don’t forget to run this after each migration as well:



Twitter Tools Notes

At the time of writing this post, I am using two widgets to put tweets on this blog. One is the default widget that Twitter provides. Another is Twitter Tools by Alex King. The plug-in page says that it is compatible up to WordPress version 3.05. I am running 3.10. I tried to get it to work, but I could not. I assumed that it no longer worked with the latest version of WordPress.

Then I got the bright idea of checking the Apache logs. There was a line that said something about not being able to find some PHP function.

So I hit the google. It turns out I needed to install another PHP pacakge. I found this page, that told me I had to install php5-curl.

Of course, you will also need curl on your system as well. I checked the docs. There is nothing about curl.

If you are using a shared hosting service, this will probably not be a problem.

Image from Wikimedia, assumed allowed under Fair Use. Image from the Cotton Genesis, a Greek manuscript of the Book of Genesis created in the 4th or 5th Century. It is called the “Cotton Genesis” because at one time it was owned by a man with the surname “Cotton”; it is not made of cotton.


Connecting Rails and Postgres

I am working on a project for Code Academy with a few other students. We decided to use PostgreSQL, since that is what Heroku uses. One of the other students posted some directions on his blog. I could not follow them since I run Linux.

I had already run apt-get to install some libraries, so I will skip that.

I put this into my Gemfile:

I put this in config/database.yml

But when I ran

I got this error:

I had to do a lot of googling. I won’t go over some of the other errors I got, so I will try to list some commands that will help me to get Rails working with Postgres on Linux boxes in the future.

You must create a user that has the same name as the username that will be running the rails app. It must own the database, and have permissions to create and alter databases. For some reason I did not need to enter a password in any Rails file. Even so, things seemed to work.

In order to use Postgres, I have to type this:

I would like to log into postgres without sudo-ing. I can log into MySQL as root from my main user ID.

To create the user:

To see users, run the metacommand \dg

Then create the database:

To list the databases, run the metacommand \l

After this, you should be able to run the commands to create, populate and reset the databases:

Image from Wikimedia, assumed allowed under Fair Use. Image from the Cotton Genesis, a Greek manuscript of the Book of Genesis created in the 4th or 5th Century. It is called the “Cotton Genesis” because at one time it was owned by a man with the surname “Cotton”; it is not made of cotton.

Toshiba Wireless Issue On Ubuntu

I run Ubuntu on a Toshiba laptop. As I have stated in the past, I am pretty much done with desktops and going strictly with laptops (see previous entries here). But sometimes the wireless card does not work. The message is that the wireless card is “disabled by hardware switch”.

I did some searching, and here is a solution that I found that worked.

First as root, I ran the command

Then I pressed the  FN-F8 key combination (function key and the F8 key, or whichever one has the wireless icon).

Image from Wikimedia, assumed allowed under Fair Use. Image from the Ambrosian Iliad, a 5th century manuscript of the Iliad.


Notes On Coupling

I found some notes I took in 2004 at a lecture I attended on coupling. It may have been a lecture Dave Thomas gave at CJUG.

Decoupling your code:
coupling: you can change something, and something else changes too
Methods calling methods: hard to understand, hard to maintain, hard to re-use.

Symptoms: Minor change recompiles the whole system. Unit tests require every jar on the box. Things stop working when you change their context. Hard to partition project between subteams.

Treat code like spy cells: limit interactions, if one is broken all others are safe, enforced structure.

Some coupling is good. Some code must call other code. Make it deliberate.

Types of coupling: Static, dynamic, domain, temporal.

Static coupling:
Classes are staticly coupled if one needs another to compile.
Java has interfaces. Good to couple to something more abstract. Gang of Four says to program to interfaces.
Interfaces reduce coupling. Split intent from implementation, single point of abstraction. Interfaces != list of class’s methods.
Inheritance is also static coupling. What if parent class changes?
Different rules for inheritance. Java: easy to inherit, hard to delegate. Inheritance is okay for “is-a”, bad for “has-a” or “uses-a”. Business world – not many uses for “is-a”, more “has-a” and “uses-a”
How to delegate?

Dynamic coupling:
happens at run-time. method chaining: order.getCustomer().getAddress().getState()
coupling question: what can change? What can break? Those methods can change.
Start by asking what caller is doing. Express intent at point of call.
Large-scale coupling: Compiling is transitive: a -> b, b -> c, then a -> c
can be circular.
use observer pattern: can break circular chains. Linear chain: add a layer. Mediator class. Is that a GoF pattern?
Mediator: construction manager. You deal with manager, manager deals with subcontractors. manager negotiates between subcontractors.

Subsystems: Things that need to be coupled tend to cluster. make each a subsystem. Ex: java package. Put a facade on a package. Security: read facade, read-write facade, returned via factories.

Domain coupling:
Business rules in code. When rules change, code must change. Value can be parameterized. Behavior can be too. Use interpreter. Interpreter pattern. Define action codes (pick out verbs)
Activate: A
Upgrade: U
Ship: S
Referral: R

Add strings of action codes to database.
Action code: idea from cobol. Do a search. Book idea: Cobol Principles for Java
not GoF intprepter pattern. Give users a screen to link product and actions.

Temporal coupling:
Coupling in time: dependence on sequence of events. imposing a sequence where none is needed.
Recommended: first edition of Bertrand Nayer’s book
Invariant: something that must be true. Invariant must be true at all times.
Design for concurrency. Do stuff in parallel. use a state machine. Can use mediator pattern. Threads: hard, okay with hash maps. Processed-based: RMI, SOAP. Message-based: JMS

Static coupling: reduce with interfaces, facade, delegation
Dynamic coupling: reduce with mediator, command pattern, express intent at point of call
Domain coupling: reduce with metadata, interpreter
Temporal coupling: reduce with monitor and maintain class invariants, synchronize correct places, parallelism

Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.

Design Pattern Notes

On 2004-06-24, Alan Shalloway of Net Objectives gave a talk on design patterns at my then-employer. Here are my notes.

2004-06-24 – Patterns
Net Objectives -> Alan Shalloway, author of “Design Patterns Explained”
Boeing: Great internal education
Gang Of Four: Difficult book to understand
GOF: Using terms differently
AS: Looking at patterns differently, will also look at analysis
Adding functionality:
Where is the problem? Writing new code, integrating it into the system
Integrating is more difficult
Must accomodate change (not predict change):
identify special cases, abstract out commonalities, containing no more than one variation per class
Design from context: big picture to small, not small to big

Patterns can be architectural, design, idiomatic (language-specific)
Patterns describe relationships between entities (objects) in your problem domain,
handling variation and new ways of using inheritance come from inheritance

Patterns work on many levels: analysis/motivation, design, implementation
Think of them as “software patterns” – gives some “why” as well as “what”

use pre-existing solutions
some patterns deal with modifiability
design is not synthesis, but differentiation

3 perspctives:
– conceptual: what you want, requirements
– specification: interfaces of classes, how objects “talk” to each other
– implementation: code itself

Mop, broom and sponge: same and different
as specific object: different
as concept: all cleaning objects

abstract classes: concepts, never instantiated, define interfaces

person -> clean -> utensil interface (mop, broom, sponge)

encapsulation: ususally thought of as data hiding
can think of it as hiding classes -> derived classes
implementation hiding, type hiding, data hiding

Objects at different levels:
implementation: data with methods
specification: objects are contracts they implement
conceptual: entities with responsibilities

do not use inheritance badly
lots of switches (coupled) or inheritance
switches are coupled: if you must change one you have to change them all
sales tax: different class for each country

design to interfaces (interfaces in general sense)
favor object aggregation over class inheritance

consider what varies in design, encapsultate the concept that varies
– find what varies and encapsulate it in a class of its own
– contain this class in another class

use cases: “writing effective use cases”

cohesion: focusing on one thing
coupling: dependence of different actions

Commonality/variability analysis
From Jim Coplien – Multi-Paradigm Programming for C++
Thesis online
abstract: emphasize what is common, or ignore what is different
find commonalities relevant to problem domain

challenges in finging commonalities: we are data-centric,
objects usually thought of as “intelligent data”, instead of as behaviors
variability: makes sense only in a given commonality
frame of reference
it is a variation of commonality

commonality: gives architecture longevity
variability: makes it fit for use

commonality -> perspective (conceptual), specification -> abstract class, interfaces
variability -> implementation, specification -> concrete classes

patterns are about relationships

Matrix for CVA

case 1 case 2 case 3
behavior 1   implementations for behavior 1
behavior 2   implementations for behavior 2

Look at patterns in terms of their motivations, not just what they do
switches are okay if they are in one place
can use abstract factory to instantiate the implementations – separates construction from use
Joshua Bloch: Effective Java – go to seminars too
Timeless Way of Being: Alexander
ezine: info @ – subscribe

Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.

Data Sizes

I read an article from a few months back in Business Week about cloud computing. There was a chart about the size of data.

  • A binary digit is a bit
  • 8 bits is a byte
  • 1,000 bytes is a kilobyte (10 to the 3rd power)
  • 1,000 kilobytes is a megabyte (10 to the 6th power)
  • 1,000 megabytes is a gigabyte (10 to the 9th power)
  • 1,000 gigabytes is a terabyte (You can start using the monster truck “SUNDAY!! SUNDAY!! SUNDAY!!” voice here) (10 to the 12th power, aka trillions)
  • 1,000 terabytes is a petabyte (10 to the 15th power, aka quadrillion)
  • 1,000 petabytes is an exabyte (10 to the 18th power, aka quintillion)

With regards to terminology, this was as far as I went.

  • 1,000 exabytes is a zettabyte (10 to the 21st power, aka sextillion)
  • 1,000 zettabytes is a yottabyte (10 to the 24th power, aka septillion)


Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.


PostgreSQL Notes

I volunteered to help out with a couple of open source Ruby projects. One of them is using PostgreSQL. I had some trouble getting it working.

One of the things I did not like about it is that it looks like I can only become the postgres user by becoming root first. I suppose I could reset the password, but I think I am going to deal with it for the time being. I don’t know if changing the password will affect everything. I found this command from the Ubuntu site:

sudo -u postgres psql postgres

Unlike other databases, some of the database commands are done with backslashes followed by letters. To quit the database, you do not type “exit”. That would be silly. It’s “\q”. The meta-commands (as they are called) can be found here. The usual SQL commands can be found here.

I tried to create a database using the createdb command after I started psql because that’s how this page tells you to create a database. There are a lot of client commands that I guess you have to run outside of psql. I think that they should tell you in big letters that you should only read those pages if you think that Postgres is like any other database that you have used.

I also had trouble logging in both from the console and in the Rails app. First I had to change the /etc/postgresql/9.1/main/pg_hba.conf file since Debian and Debian-based distributions do some things differently. To log in at the command line, I read this page to change pg_hba.conf. I added this:

I also had to change the port from 5433 to 5432. I am not clear why Ubuntu changed it.

The guy who started the project said that PostgreSQL is the standard database on Heroku. That is a pretty good reason to learn it. I have heard that Postgres can be more difficult to learn and configure. We shall see.

Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.