MySQL Commands You Should Have Committed to Memory Already
Dump the database
mysqldump -h database_host -uUsername -p database_name > dump.sql
Restore from the dump
If you need to create the database first:
mysqladmin -h database_host -uUsername -p create database_name
Then load the SQL file:
mysql -h database_host -uUsername -p database_name < dump.sql
Source
Build Your Own Database Driven Website Using PHP & MySQL, 2nd Edition (Page 133)
Restore MySQL databases from a hard drive backup
Drag everything from /usr/local/mysql/data into the same location on the new installation, overwriting whatever is there. You may have to chown the folder from mysql to your user name, just don’t forget to chown it back or it won’t start up again.
No dumps needed.
I hope this will show up in a Google search for “restore mysql databases "without dump"“
Changing Part of a String in SQL and Updating the Table
Something like this:
update `table_name` set `field_name` = replace(`field_name`, 'text_to_replace', 'new_text')
Sources
has_many :through
Has Many associations can be configured with the :through option to use an explicit join model to retrieve the data. This operates similarly to a has_and_belongs_to_many association. The advantage is that you’re able to add validations, callbacks, and extra attributes on the join model. Consider the following schema:
class Author < ActiveRecord::Base
has_many :authorships
has_many :books, :through => :authorships
end
class Authorship < ActiveRecord::Base
belongs_to :author
belongs_to :book
end
@author = Author.find :first
@author.authorships.collect { |a| a.book } # selects all books that the author's authorships belong to.
@author.books # selects all books by using the Authorship join model
Sources
- Association Join Models (search for “Association Join Models”)
- Through Associations
Optional Date Fields in Django
Adding blank=True on any field in your models.py file will keep the admin interface from requiring that field. That’s all you need to do if it’s a string field. If it’s an integer, boolean, or a date; you need to add null=True to the field as well.
date = models.DateField(blank=True, null=True)
Source
See also
When to use and when not to use NOT NULL in MySQL / Rails Migrations
Pluralization of Database Models in Django
Part of the “magic-removal” was apparently removing automatic pluralization. That’s a shame. That’s one thing I really dig about Rails.
In your models.py file:
class Meta:
verbose_name_plural = 'something'
So for a ‘Person’ model, you could put ‘people’. Now it will look right in the admin interface.
Using SQLite in memory mode for Rails testing
Watching the latest PeepCode at about the 15:00 mark, he mentions using SQLite in memory mode for your testing database. Here’s what you put in your config/database.yml:
test:
adapter: sqlite3
database: ":memory:"
verbosity: silent
Run these commands:
$ script/plugin install memory_test_fix (use `--force` if you have any problems)
$ rake db:migrate
$ rake
Then you’re ready to go.
Sources:
See also how to set up SQLite for Rails
Rails Migration Data Types
Is this all of them?
:integer
:float
:datetime
:date
:timestamp
:time
:text
:string
:binary
:boolean
Found this:
Valid column types are integer, float, datetime, date, timestamp, time, text, string, binary, and boolean. Valid column options are limit, null (i.e. ” :null => false” implies NOT NULL), and default (to specify default values).
Why is this hard to find? Google search for “rails migration data types” isn’t that good.
And what’s the difference between datetime and timestamp? Or binary and boolean? Any database gurus read this?
Sources
Using MySQL for storing Rails sessions
$ rake db:sessions:create
$ rake db:migrate
Possibly use -c on the first command to add the migration file to SVN?
Uncomment this line in your config/environments.rb:
config.action_controller.session_store = :active_record_store
Then restart your app.
Doing this is supposed to be more better than the default of using the file system.
Using SQLite with Rails
After watching the RESTful Rails PeepCode presentation, I had to try using SQLite for development. That’s nice to not have to set up a database for every little thing you want to play with.
SQLite 3 is already installed on OS X. To get it to work with Rails:
$ sudo port install swig
$ sudo gem install sqlite3-ruby
That will bring up a prompt to choose a gem. Choose the highest version number that says (ruby) after it.
Then you can do things like this in your database.yml:
development:
adapter: sqlite3
database: db/development.sqlite3
Source: Rails wiki