pt-osc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.gitignore +22 -0
  2. data/Gemfile +4 -0
  3. data/README.md +35 -0
  4. data/Rakefile +20 -0
  5. data/lib/active_record/connection_adapters/pt_osc_adapter.rb +134 -0
  6. data/lib/active_record/pt_osc_migration.rb +150 -0
  7. data/lib/pt-osc.rb +3 -0
  8. data/lib/pt-osc/version.rb +5 -0
  9. data/pt-osc.gemspec +32 -0
  10. data/test/config/database.yml +6 -0
  11. data/test/dummy/.gitignore +1 -0
  12. data/test/dummy/README.rdoc +261 -0
  13. data/test/dummy/Rakefile +7 -0
  14. data/test/dummy/app/assets/javascripts/application.js +15 -0
  15. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  16. data/test/dummy/app/controllers/application_controller.rb +3 -0
  17. data/test/dummy/app/helpers/application_helper.rb +2 -0
  18. data/test/dummy/app/mailers/.gitkeep +0 -0
  19. data/test/dummy/app/models/.gitkeep +0 -0
  20. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  21. data/test/dummy/config.ru +4 -0
  22. data/test/dummy/config/application.rb +59 -0
  23. data/test/dummy/config/boot.rb +10 -0
  24. data/test/dummy/config/database.yml +16 -0
  25. data/test/dummy/config/environment.rb +5 -0
  26. data/test/dummy/config/environments/development.rb +37 -0
  27. data/test/dummy/config/environments/production.rb +67 -0
  28. data/test/dummy/config/environments/test.rb +37 -0
  29. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  30. data/test/dummy/config/initializers/inflections.rb +15 -0
  31. data/test/dummy/config/initializers/mime_types.rb +5 -0
  32. data/test/dummy/config/initializers/secret_token.rb +7 -0
  33. data/test/dummy/config/initializers/session_store.rb +8 -0
  34. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  35. data/test/dummy/config/locales/en.yml +5 -0
  36. data/test/dummy/config/routes.rb +58 -0
  37. data/test/dummy/db/development.sqlite3 +0 -0
  38. data/test/dummy/db/test.sqlite3 +0 -0
  39. data/test/dummy/lib/assets/.gitkeep +0 -0
  40. data/test/dummy/public/404.html +26 -0
  41. data/test/dummy/public/422.html +26 -0
  42. data/test/dummy/public/500.html +25 -0
  43. data/test/dummy/public/favicon.ico +0 -0
  44. data/test/dummy/script/rails +6 -0
  45. data/test/functional/pt_osc_adapter_test.rb +16 -0
  46. data/test/functional/pt_osc_migration_functional_test.rb +99 -0
  47. data/test/test_helper.rb +49 -0
  48. data/test/unit/pt_osc_adapter_test.rb +209 -0
  49. data/test/unit/pt_osc_migration_unit_test.rb +39 -0
  50. metadata +281 -0
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pt-osc.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ ## `pt-online-schema-change` migrations
2
+
3
+ Runs regular Rails/ActiveRecord migrations via the [Percona Toolkit pt-online-schema-change tool](http://www.percona.com/doc/percona-toolkit/2.1/pt-online-schema-change.html).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pt-osc'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pt-osc
18
+
19
+ ## Usage
20
+
21
+ Set your database adapter to be `pt_osc` in your application's database.yml.
22
+ Specify `pt-online-schema-change` flags in a `percona` hash in the config.
23
+ e.g.
24
+ ```yaml
25
+ environment:
26
+ host: localhost
27
+ username: root
28
+ database: rails
29
+ percona:
30
+ defaults-file: /etc/mysql/percona-user.cnf
31
+ recursion-method: "'dsn=D=percona,t=slaves'"
32
+ ```
33
+
34
+ Additional options for the `percona` hash include:
35
+ - `run_mode`: Specify `'execute'` to actually run `pt-online-schema-change` when the migration runs. Specify `'print'` to output the commands to run to STDOUT instead. Default is `'print'`.
@@ -0,0 +1,20 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push 'test'
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
8
+
9
+ desc 'Run tests'
10
+ task :default => :test
11
+
12
+ load 'active_record/railties/databases.rake'
13
+
14
+ namespace :db do
15
+ task :test_create do
16
+ test_spec = YAML.load_file('./test/config/database.yml')['test']
17
+ test_spec['adapter'] = 'mysql2'
18
+ create_database(test_spec)
19
+ end
20
+ end
@@ -0,0 +1,134 @@
1
+ require 'active_record/connection_adapters/mysql2_adapter'
2
+
3
+ module ActiveRecord
4
+ class Base
5
+ # Establishes a connection to the database that's used by all Active Record objects.
6
+ def self.pt_osc_connection(config)
7
+ config[:username] = 'root' if config[:username].nil?
8
+
9
+ if Mysql2::Client.const_defined? :FOUND_ROWS
10
+ config[:flags] = Mysql2::Client::FOUND_ROWS
11
+ end
12
+
13
+ client = Mysql2::Client.new(config.symbolize_keys)
14
+ options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
15
+ ConnectionAdapters::PtOscAdapter.new(client, logger, options, config)
16
+ end
17
+ end
18
+
19
+ module ConnectionAdapters
20
+ class PtOscAdapter < Mysql2Adapter
21
+ ADAPTER_NAME = 'pt-osc'
22
+
23
+ # Renames a table.
24
+ #
25
+ # Example:
26
+ # rename_table('octopuses', 'octopi')
27
+ def rename_table(table_name, new_name)
28
+ add_command(table_name, "RENAME TO #{quote_table_name(new_name)}")
29
+ end
30
+
31
+ def add_column(table_name, column_name, type, options = {})
32
+ add_command(table_name, add_column_sql(table_name, column_name, type, options))
33
+ end
34
+
35
+ def change_column(table_name, column_name, type, options = {}) #:nodoc:
36
+ add_command(table_name, change_column_sql(table_name, column_name, type, options))
37
+ end
38
+
39
+ def rename_column(table_name, column_name, new_column_name) #:nodoc:
40
+ add_command(table_name, rename_column_sql(table_name, column_name, new_column_name))
41
+ end
42
+
43
+ # Removes the column(s) from the table definition.
44
+ # ===== Examples
45
+ # remove_column(:suppliers, :qualification)
46
+ # remove_columns(:suppliers, :qualification, :experience)
47
+ def remove_column(table_name, *column_names)
48
+ if column_names.flatten!
49
+ message = 'Passing array to remove_columns is deprecated, please use ' +
50
+ 'multiple arguments, like: `remove_columns(:posts, :foo, :bar)`'
51
+ ActiveSupport::Deprecation.warn message, caller
52
+ end
53
+
54
+ columns_for_remove(table_name, *column_names).each do |column_name|
55
+ add_command(table_name, "DROP COLUMN #{column_name}")
56
+ end
57
+ end
58
+
59
+ # Adds a new index to the table. +column_name+ can be a single Symbol, or
60
+ # an Array of Symbols.
61
+ #
62
+ # The index will be named after the table and the column name(s), unless
63
+ # you pass <tt>:name</tt> as an option.
64
+ #
65
+ # ===== Examples
66
+ #
67
+ # ====== Creating a simple index
68
+ # add_index(:suppliers, :name)
69
+ # generates
70
+ # CREATE INDEX suppliers_name_index ON suppliers(name)
71
+ #
72
+ # ====== Creating a unique index
73
+ # add_index(:accounts, [:branch_id, :party_id], :unique => true)
74
+ # generates
75
+ # CREATE UNIQUE INDEX accounts_branch_id_party_id_index ON accounts(branch_id, party_id)
76
+ #
77
+ # ====== Creating a named index
78
+ # add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party')
79
+ # generates
80
+ # CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
81
+ #
82
+ # ====== Creating an index with specific key length
83
+ # add_index(:accounts, :name, :name => 'by_name', :length => 10)
84
+ # generates
85
+ # CREATE INDEX by_name ON accounts(name(10))
86
+ #
87
+ # add_index(:accounts, [:name, :surname], :name => 'by_name_surname', :length => {:name => 10, :surname => 15})
88
+ # generates
89
+ # CREATE INDEX by_name_surname ON accounts(name(10), surname(15))
90
+ #
91
+ # ====== Creating an index with a sort order (desc or asc, asc is the default)
92
+ # add_index(:accounts, [:branch_id, :party_id, :surname], :order => {:branch_id => :desc, :part_id => :asc})
93
+ # generates
94
+ # CREATE INDEX by_branch_desc_party ON accounts(branch_id DESC, party_id ASC, surname)
95
+ #
96
+ # Note: mysql doesn't yet support index order (it accepts the syntax but ignores it)
97
+ #
98
+ def add_index(table_name, column_name, options = {})
99
+ index_name, index_type, index_columns = add_index_options(table_name, column_name, options)
100
+ add_command(table_name, "ADD #{index_type} INDEX #{quote_column_name(index_name)} (#{index_columns})")
101
+ end
102
+
103
+ def remove_index!(table_name, index_name) #:nodoc:
104
+ add_command(table_name, "DROP INDEX #{quote_column_name(index_name)}")
105
+ end
106
+
107
+ def clear_commands
108
+ @osc_commands = {}
109
+ end
110
+
111
+ def get_commands_string(table_name)
112
+ @osc_commands ||= {}
113
+ @osc_commands[table_name] ||= []
114
+ @osc_commands[table_name].join(',')
115
+ end
116
+
117
+ def get_commanded_tables
118
+ @osc_commands.keys
119
+ end
120
+
121
+ protected
122
+ def add_command(table_name, command)
123
+ @osc_commands ||= {}
124
+ @osc_commands[table_name] ||= []
125
+ @osc_commands[table_name] << command
126
+ end
127
+
128
+ def get_commands(table_name)
129
+ @osc_commands ||= {}
130
+ @osc_commands[table_name]
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,150 @@
1
+ module ActiveRecord
2
+ class PtOscMigration < Migration
3
+ # @TODO whitelist all valid pt-osc flags
4
+ DEFAULT_FLAGS = {
5
+ 'defaults-file' => nil,
6
+ 'recursion-method' => nil,
7
+ 'execute' => false,
8
+ }.freeze
9
+
10
+ def migrate(direction)
11
+ return unless respond_to?(direction)
12
+
13
+ run_mode = percona_config[:run_mode] || 'print'
14
+ raise ArgumentError.new('Invalid run_mode specified in database config') unless run_mode.in? %w(print execute)
15
+
16
+ case direction
17
+ when :up then announce 'migrating'
18
+ when :down then announce 'reverting'
19
+ end
20
+
21
+ time = nil
22
+ ActiveRecord::Base.connection_pool.with_connection do |conn|
23
+ @connection = conn
24
+ if respond_to?(:change)
25
+ if direction == :down
26
+ recorder = CommandRecorder.new(@connection)
27
+ suppress_messages do
28
+ @connection = recorder
29
+ change
30
+ end
31
+ @connection = conn
32
+ time = Benchmark.measure {
33
+ self.revert {
34
+ recorder.inverse.each do |cmd, args|
35
+ send(cmd, *args)
36
+ end
37
+ }
38
+ }
39
+ else
40
+ time = Benchmark.measure { change }
41
+ end
42
+ else
43
+ time = Benchmark.measure { send(direction) }
44
+ end
45
+
46
+ case run_mode
47
+ when 'execute' then time += Benchmark.measure { execute_pt_osc }
48
+ when 'print' then print_pt_osc
49
+ end
50
+
51
+ @connection = nil
52
+ end
53
+
54
+ case direction
55
+ when :up then announce 'migrated (%.4fs)' % time.real; write
56
+ when :down then announce 'reverted (%.4fs)' % time.real; write
57
+ end
58
+ end
59
+
60
+ protected
61
+ def execute_pt_osc
62
+ return unless @connection.is_a? ActiveRecord::ConnectionAdapters::PtOscAdapter
63
+
64
+ @connection.get_commanded_tables.each do |table_name|
65
+ execute_sql = @connection.get_commands_string(table_name)
66
+
67
+ Rails.logger.tagged('pt-osc') do |logger|
68
+
69
+ database_name = database_config[:database]
70
+
71
+ logger.info "Running on #{database_name}|#{table_name}: #{execute_sql}"
72
+ announce 'running pt-online-schema-change'
73
+
74
+ [true, false].each do |dry_run|
75
+ command = percona_command(execute_sql, database_name, table_name, execute: !dry_run)
76
+ logger.info "Command is #{command}"
77
+ success = Kernel.system command
78
+ if success
79
+ logger.info "Successfully #{dry_run ? 'dry ran' : 'executed'} on #{database_name}|#{table_name}: #{execute_sql}"
80
+ else
81
+ failure_message = "Unable to #{dry_run ? 'dry run' : 'execute'} query on #{database_name}|#{table_name}: #{execute_sql}"
82
+ logger.error failure_message
83
+ raise RuntimeError.new(failure_message)
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ @connection.clear_commands
90
+ end
91
+
92
+ def print_pt_osc
93
+ return unless @connection.is_a? ActiveRecord::ConnectionAdapters::PtOscAdapter
94
+
95
+ database_name = database_config[:database]
96
+
97
+ @connection.get_commanded_tables.each do |table_name|
98
+ execute_sql = @connection.get_commands_string(table_name)
99
+
100
+ announce 'Run the following commands:'
101
+
102
+ [true, false].each do |dry_run|
103
+ write percona_command(execute_sql, database_name, table_name, execute: !dry_run)
104
+ end
105
+
106
+ end
107
+
108
+ @connection.clear_commands
109
+ end
110
+
111
+ def percona_command(execute_sql, database_name, table_name, options = {})
112
+ command = "pt-online-schema-change --alter '#{execute_sql}' D=#{database_name},t=#{table_name}"
113
+
114
+ # Whitelist
115
+ options = HashWithIndifferentAccess.new(options)
116
+ options = options.slice(*DEFAULT_FLAGS.keys)
117
+
118
+ # Merge config
119
+ config = percona_config
120
+ if config
121
+ config.slice(*DEFAULT_FLAGS.keys).each do |key, value|
122
+ options[key] ||= value
123
+ end
124
+ end
125
+
126
+ # Set defaults
127
+ DEFAULT_FLAGS.each do |key, value|
128
+ options[key] ||= value unless value.nil?
129
+ end
130
+
131
+ # Determine run mode
132
+ command += options.delete(:execute) ? ' --execute' : ' --dry-run'
133
+
134
+ options.each do |key, value|
135
+ command += " --#{key} #{value}"
136
+ end
137
+
138
+ command
139
+ end
140
+
141
+ def database_config
142
+ # @TODO better way to config?
143
+ @connection.instance_variable_get(:@config) || ActiveRecord::Base.connection_config
144
+ end
145
+
146
+ def percona_config
147
+ database_config[:percona]
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,3 @@
1
+ require 'pt-osc/version'
2
+ require 'active_record/connection_adapters/pt_osc_adapter'
3
+ require 'active_record/pt_osc_migration'
@@ -0,0 +1,5 @@
1
+ module Pt
2
+ module Osc
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pt-osc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pt-osc'
8
+ spec.version = Pt::Osc::VERSION
9
+ spec.authors = ['Steve Rice']
10
+ spec.email = ['steve@pagerduty.com']
11
+ spec.summary = 'Rails migrations via pt-online-schema-change'
12
+ spec.description = 'Runs regular Rails/ActiveRecord migrations via the Percona Toolkit pt-online-schema-change tool.'
13
+ spec.homepage = 'https://github.com/steverice/pt-osc'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.6'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'shoulda'
23
+ spec.add_development_dependency 'faker'
24
+ spec.add_development_dependency 'mocha'
25
+
26
+ # For testing using dummy Rails app
27
+ spec.add_development_dependency 'rails', '~> 3.2'
28
+ spec.add_development_dependency 'sqlite3'
29
+
30
+ spec.add_runtime_dependency 'activerecord', '~> 3.2'
31
+ spec.add_runtime_dependency 'mysql2'
32
+ end
@@ -0,0 +1,6 @@
1
+ test:
2
+ pool: 5
3
+ host: localhost
4
+ username: root
5
+ database: pt_osc_test
6
+ reconnect: true
@@ -0,0 +1 @@
1
+ log/*
@@ -0,0 +1,261 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb"
7
+ templates that are primarily responsible for inserting pre-built data in between
8
+ HTML tags. The model contains the "smart" domain objects (such as Account,
9
+ Product, Person, Post) that holds all the business logic and knows how to
10
+ persist themselves to a database. The controller handles the incoming requests
11
+ (such as Save New Account, Update Product, Show Post) by manipulating the model
12
+ and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, create a new Rails application:
32
+ <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
+
34
+ 2. Change directory to <tt>myapp</tt> and start the web server:
35
+ <tt>cd myapp; rails server</tt> (run with --help for options)
36
+
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
39
+
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
42
+
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
+
46
+
47
+ == Debugging Rails
48
+
49
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
+ will help you debug it and get it back on the rails.
51
+
52
+ First area to check is the application log files. Have "tail -f" commands
53
+ running on the server.log and development.log. Rails will automatically display
54
+ debugging and runtime information to these files. Debugging info will also be
55
+ shown in the browser on requests from 127.0.0.1.
56
+
57
+ You can also log your own messages directly into the log file from your code
58
+ using the Ruby logger class from inside your controllers. Example:
59
+
60
+ class WeblogController < ActionController::Base
61
+ def destroy
62
+ @weblog = Weblog.find(params[:id])
63
+ @weblog.destroy
64
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
+ end
66
+ end
67
+
68
+ The result will be a message in your log file along the lines of:
69
+
70
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
+
72
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
73
+
74
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
+ several books available online as well:
76
+
77
+ * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
+
80
+ These two books will bring you up to speed on the Ruby language and also on
81
+ programming in general.
82
+
83
+
84
+ == Debugger
85
+
86
+ Debugger support is available through the debugger command when you start your
87
+ Mongrel or WEBrick server with --debugger. This means that you can break out of
88
+ execution at any point in the code, investigate and change the model, and then,
89
+ resume execution! You need to install ruby-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
+
92
+ class WeblogController < ActionController::Base
93
+ def index
94
+ @posts = Post.all
95
+ debugger
96
+ end
97
+ end
98
+
99
+ So the controller will accept the action, run the first line, then present you
100
+ with a IRB prompt in the server window. Here you can do things like:
101
+
102
+ >> @posts.inspect
103
+ => "[#<Post:0x14a6be8
104
+ @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
+ #<Post:0x14a6620
106
+ @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
+ >> @posts.first.title = "hello from a debugger"
108
+ => "hello from a debugger"
109
+
110
+ ...and even better, you can examine how your runtime objects actually work:
111
+
112
+ >> f = @posts.first
113
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
+ >> f.
115
+ Display all 152 possibilities? (y or n)
116
+
117
+ Finally, when you're ready to resume execution, you can enter "cont".
118
+
119
+
120
+ == Console
121
+
122
+ The console is a Ruby shell, which allows you to interact with your
123
+ application's domain model. Here you'll have all parts of the application
124
+ configured, just like it is when the application is running. You can inspect
125
+ domain models, change values, and save to the database. Starting the script
126
+ without arguments will launch it in the development environment.
127
+
128
+ To start the console, run <tt>rails console</tt> from the application
129
+ directory.
130
+
131
+ Options:
132
+
133
+ * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
+ made to the database.
135
+ * Passing an environment name as an argument will load the corresponding
136
+ environment. Example: <tt>rails console production</tt>.
137
+
138
+ To reload your controllers and models after launching the console run
139
+ <tt>reload!</tt>
140
+
141
+ More information about irb can be found at:
142
+ link:http://www.rubycentral.org/pickaxe/irb.html
143
+
144
+
145
+ == dbconsole
146
+
147
+ You can go to the command line of your database directly through <tt>rails
148
+ dbconsole</tt>. You would be connected to the database with the credentials
149
+ defined in database.yml. Starting the script without arguments will connect you
150
+ to the development database. Passing an argument will connect you to a different
151
+ database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
+ PostgreSQL and SQLite 3.
153
+
154
+ == Description of Contents
155
+
156
+ The default directory structure of a generated Ruby on Rails application:
157
+
158
+ |-- app
159
+ | |-- assets
160
+ | | |-- images
161
+ | | |-- javascripts
162
+ | | `-- stylesheets
163
+ | |-- controllers
164
+ | |-- helpers
165
+ | |-- mailers
166
+ | |-- models
167
+ | `-- views
168
+ | `-- layouts
169
+ |-- config
170
+ | |-- environments
171
+ | |-- initializers
172
+ | `-- locales
173
+ |-- db
174
+ |-- doc
175
+ |-- lib
176
+ | |-- assets
177
+ | `-- tasks
178
+ |-- log
179
+ |-- public
180
+ |-- script
181
+ |-- test
182
+ | |-- fixtures
183
+ | |-- functional
184
+ | |-- integration
185
+ | |-- performance
186
+ | `-- unit
187
+ |-- tmp
188
+ | `-- cache
189
+ | `-- assets
190
+ `-- vendor
191
+ |-- assets
192
+ | |-- javascripts
193
+ | `-- stylesheets
194
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.