diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Rakefile | 2 | ||||
m--------- | commonlib | 0 | ||||
-rw-r--r-- | config/database.yml-example | 2 | ||||
-rw-r--r-- | config/initializers/alaveteli.rb | 5 | ||||
-rw-r--r-- | doc/INSTALL.md | 3 | ||||
-rw-r--r-- | doc/THEMES-UPGRADE.md | 101 | ||||
-rw-r--r-- | lib/alaveteli_external_command.rb | 4 | ||||
-rw-r--r-- | lib/mail_handler/mail_handler.rb | 3 | ||||
-rw-r--r-- | lib/no_constraint_disabling.rb | 110 | ||||
-rwxr-xr-x | script/handle-mail-replies.rb | 4 | ||||
-rw-r--r-- | vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb | 8 |
12 files changed, 238 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore index b9adf61d0..75c348ff9 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ config/httpd.conf .sass-cache alaveteli.sublime* webrat.log +/.rbenv-version @@ -3,5 +3,5 @@ require File.expand_path('../config/application', __FILE__) require 'rake' - Alaveteli::Application.load_tasks +Dir[File.join(File.dirname(__FILE__),'commonlib','rblib','tests','*.rake')].each { |file| load(file) } diff --git a/commonlib b/commonlib -Subproject d34573a26f0894c28ac118fa27c6945223fcf85 +Subproject 3f57d96fe765242c3a7082d386b5763b2e1ca73 diff --git a/config/database.yml-example b/config/database.yml-example index b1597e6fe..e48577f23 100644 --- a/config/database.yml-example +++ b/config/database.yml-example @@ -19,6 +19,8 @@ test: password: <password> host: localhost port: 5432 +# Uncomment the following if the user is not a postgres superuser +# constraint_disabling: false production: adapter: postgresql diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb index b75c5f082..a9e9d498d 100644 --- a/config/initializers/alaveteli.rb +++ b/config/initializers/alaveteli.rb @@ -61,3 +61,8 @@ require 'public_body_categories' require 'ability' require 'normalize_string' require 'alaveteli_file_types' + +# Allow tests to be run under a non-superuser database account if required +if Rails.env == 'test' and ActiveRecord::Base.configurations['test']['constraint_disabling'] == false + require 'no_constraint_disabling' +end diff --git a/doc/INSTALL.md b/doc/INSTALL.md index 09bc8fd43..b6e8d2265 100644 --- a/doc/INSTALL.md +++ b/doc/INSTALL.md @@ -130,7 +130,8 @@ username and password of your postgres database. Make sure that the user specified in database.yml exists, and has full permissions on these databases. As they need the ability to turn off constraints whilst running the tests they also need to be a superuser. -(See http://dev.rubyonrails.org/ticket/9981) +If you don't want your database user to be a superuser, you can add a line +`disable_constraints: false` to the test config in database.yml, as seen in database.yml-example You can create a `foi` user from the command line, thus: diff --git a/doc/THEMES-UPGRADE.md b/doc/THEMES-UPGRADE.md new file mode 100644 index 000000000..457274d7a --- /dev/null +++ b/doc/THEMES-UPGRADE.md @@ -0,0 +1,101 @@ +This file contains some notes on changing your Alaveteli theme for the +upgrade to Rails 3, in version 0.11 of Alaveteli. These were written +by Henare Degan, with some additions by Mark Longair. + +# Alaveteli Theme Upgrade Checks + +## RAILS_ROOT/RAILS_ENV + +[Example](https://github.com/henare/adminbootstraptheme/commit/857e33c9b0bc577024b476404aec4f9749f65a0b) + +Check your theme for instances of: + +* `RAILS_ROOT` and replace it with `Rails.root` +* `RAILS_ENV` and replace it with `Rails.env` + +Note that `Rails.root` is a `Pathname`, so you can replace, for +example: + + File.join(RAILS_ROOT, 'public', 'alavetelitheme') + +... with: + + Rails.root.join('public', 'alavetelitheme') + +## Dispatcher + +[Example](https://github.com/henare/adminbootstraptheme/commit/fba2d6b7dfdc26a25fdc1596bfe120270dd4cd0d) + +This... + +```ruby +require 'dispatcher' +Dispatcher.to_prepare do +``` + +should be replaced with this... + +```ruby +Rails.configuration.to_prepare do +```` + +## Routes + +[Example](https://github.com/henare/adminbootstraptheme/commit/87f1991dafb09401f9b17f642a94382d5a47a713) + +You need to upgrade your custom routes to the new Rails syntax. + +## list_public_bodies_default removed + +[Example](https://github.com/openaustralia/alavetelitheme/commit/5927877af996a1afb1a23a950f0d012b52c36f83) + +The list_public_bodies_default helper has been removed from Alaveteli + +## Patching mailer templates has changed + +[Example](https://github.com/openaustralia/alavetelitheme/commit/ffb5242973a0b2acc4981c25659fcb752b92eb97) + +In `lib/patch_mailer_paths.rb` change `ActionMailer::Base.view_paths.unshift File.join(File.dirname(__FILE__), "views")` to `ActionMailer::Base.prepend_view_path File.join(File.dirname(__FILE__), "views")` + +There's also `ActionMailer::Base.append_view_path` for replacing `ActionMailer::Base.view_paths <<`. + +## Rename view templates + +[Example](https://github.com/henare/adminbootstraptheme/commit/b616b636c283ae6cf696a6af1fa481f371baf2b6) + +Rename view templates from `filename.rhtml` to `filename.html.erb`. + +Run this in the root of your theme directory: + + for r in $(find lib/views -name '*.rhtml'); do echo git mv $r ${r%.rhtml}.html.erb; done + +[GOTCHA!](https://github.com/openaustralia/alavetelitheme/commit/65e775488822367d981bb15ab2cbcf1fce842cc2) +One exception is mailer templates, these should be renamed to +`filename.text.erb` as we only use text emails. + +## The Configuration class has been renamed + +[Example](https://github.com/openaustralia/alavetelitheme/commit/db6cca4650216c6f85acffaea380727344f0f740) + +Due to a naming conflict, `Configuration` has been renamed to `AlaveteliConfiguration`. + +You may have this in your theme for things like `Configuration::site_name`, just change it to `AlaveteliConfiguration::site_name` + +## request.request_uri is deprecated + +[Example](https://github.com/openaustralia/alavetelitheme/commit/d670eeebfb049e1dc83fdb36a628f7722d2ad419) + +Replace instances of `request.request_uri` with `request.fullpath` + +## content-inserting <% %> block helpers are deprecated + +[Example](https://github.com/openaustralia/alavetelitheme/commit/a4b13bbd76249b3a28e2a755cede20dd9db30140) + +The Rails 3 releases notes are [irritatingly +imprecise](http://edgeguides.rubyonrails.org/3_0_release_notes.html#helpers-with-blocks) +about which such helpers have changed. You can find some candidates +with this `git grep` command: + + git grep -E '<%[^=].*(_for|_tag|link_to)\b' + +(Ignore `content_for` in those results.) diff --git a/lib/alaveteli_external_command.rb b/lib/alaveteli_external_command.rb index ac91a5867..fbdee8a62 100644 --- a/lib/alaveteli_external_command.rb +++ b/lib/alaveteli_external_command.rb @@ -8,6 +8,7 @@ module AlaveteliExternalCommand # :stdin_string - stdin string to pass to the process # :binary_output - boolean flag for treating the output as binary or text (only significant # ruby 1.9 and above) + # :memory_limit - maximum amount of memory (in bytes) available to the process def run(program_name, *args) # Run an external program, and return its output. # Standard error is suppressed unless the program @@ -38,6 +39,9 @@ module AlaveteliExternalCommand if opts.has_key? :binary_output xc.binary_mode = opts[:binary_output] end + if opts.has_key? :memory_limit + xc.memory_limit = opts[:memory_limit] + end xc.run(opts[:stdin_string] || "", opts[:env] || {}) if xc.status != 0 diff --git a/lib/mail_handler/mail_handler.rb b/lib/mail_handler/mail_handler.rb index e199ae8b6..9c955cccd 100644 --- a/lib/mail_handler/mail_handler.rb +++ b/lib/mail_handler/mail_handler.rb @@ -80,7 +80,8 @@ module MailHandler tempfile.flush default_params = { :append_to => text, :binary_output => false } if content_type == 'application/vnd.ms-word' - AlaveteliExternalCommand.run("wvText", tempfile.path, tempfile.path + ".txt") + AlaveteliExternalCommand.run("wvText", tempfile.path, tempfile.path + ".txt", + { :memory_limit => 536870912 } ) # Try catdoc if we get into trouble (e.g. for InfoRequestEvent 2701) if not File.exists?(tempfile.path + ".txt") AlaveteliExternalCommand.run("catdoc", tempfile.path, default_params) diff --git a/lib/no_constraint_disabling.rb b/lib/no_constraint_disabling.rb new file mode 100644 index 000000000..d515a959a --- /dev/null +++ b/lib/no_constraint_disabling.rb @@ -0,0 +1,110 @@ +# In order to work around the problem of the database use not having +# the permission to disable referential integrity when loading fixtures, +# we redefine disable_referential_integrity so that it doesn't try to +# disable foreign key constraints, and redefine the +# ActiveRecord::Fixtures.create_fixtures method to pay attention to the order +# which fixture tables are passed so that foreign key constraints won't be +# violated. The only lines that are changed from the initial definition +# are those between the "***" comments +require 'active_record/fixtures' +require 'active_record/connection_adapters/postgresql_adapter' +module ActiveRecord + module ConnectionAdapters + class PostgreSQLAdapter < AbstractAdapter + def disable_referential_integrity(&block) + transaction { + yield + } + end + end + end +end + +module ActiveRecord + class Fixtures + + def self.create_fixtures(fixtures_directory, table_names, class_names = {}) + table_names = [table_names].flatten.map { |n| n.to_s } + table_names.each { |n| + class_names[n.tr('/', '_').to_sym] = n.classify if n.include?('/') + } + + # FIXME: Apparently JK uses this. + connection = block_given? ? yield : ActiveRecord::Base.connection + + files_to_read = table_names.reject { |table_name| + fixture_is_cached?(connection, table_name) + } + + unless files_to_read.empty? + connection.disable_referential_integrity do + fixtures_map = {} + + fixture_files = files_to_read.map do |path| + table_name = path.tr '/', '_' + + fixtures_map[path] = ActiveRecord::Fixtures.new( + connection, + table_name, + class_names[table_name.to_sym] || table_name.classify, + File.join(fixtures_directory, path)) + end + + all_loaded_fixtures.update(fixtures_map) + + connection.transaction(:requires_new => true) do + # Patch - replace this... + # *** + # fixture_files.each do |ff| + # conn = ff.model_class.respond_to?(:connection) ? ff.model_class.connection : connection + # table_rows = ff.table_rows + # + # table_rows.keys.each do |table| + # conn.delete "DELETE FROM #{conn.quote_table_name(table)}", 'Fixture Delete' + # end + # + # table_rows.each do |table_name,rows| + # rows.each do |row| + # conn.insert_fixture(row, table_name) + # end + # end + # end + # *** + # ... with this + fixture_files.reverse.each do |ff| + conn = ff.model_class.respond_to?(:connection) ? ff.model_class.connection : connection + table_rows = ff.table_rows + + table_rows.keys.each do |table| + conn.delete "DELETE FROM #{conn.quote_table_name(table)}", 'Fixture Delete' + end + end + + fixture_files.each do |ff| + conn = ff.model_class.respond_to?(:connection) ? ff.model_class.connection : connection + table_rows = ff.table_rows + table_rows.each do |table_name,rows| + rows.each do |row| + conn.insert_fixture(row, table_name) + end + end + end + # *** + + # Cap primary key sequences to max(pk). + if connection.respond_to?(:reset_pk_sequence!) + table_names.each do |table_name| + connection.reset_pk_sequence!(table_name.tr('/', '_')) + end + end + end + + cache_fixtures(connection, fixtures_map) + end + end + cached_fixtures(connection, table_names) + end + + end + +end diff --git a/script/handle-mail-replies.rb b/script/handle-mail-replies.rb index be6bd10da..da0fc8e96 100755 --- a/script/handle-mail-replies.rb +++ b/script/handle-mail-replies.rb @@ -21,6 +21,10 @@ load 'configuration.rb' MySociety::Config.set_file(File.join($alaveteli_dir, 'config', 'general'), true) MySociety::Config.load_default require 'mail_handler' +if RUBY_VERSION.to_f >= 1.9 + # the default encoding for IO is utf-8, and we use utf-8 internally + Encoding.default_external = Encoding.default_internal = Encoding::UTF_8 +end def main(in_test_mode) Dir.chdir($alaveteli_dir) do diff --git a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb index d45308fec..1e5df8de4 100644 --- a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb +++ b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb @@ -711,6 +711,9 @@ module ActsAsXapian # We fork here, so each batch is run in a different process. This is # because otherwise we get a memory "leak" and you can't rebuild very # large databases (however long you have!) + + ActiveRecord::Base.connection.disconnect! + pid = Process.fork # XXX this will only work on Unix, tough if pid Process.waitpid(pid) @@ -718,11 +721,10 @@ module ActsAsXapian raise "batch fork child failed, exiting also" end # database connection doesn't survive a fork, rebuild it - ActiveRecord::Base.connection.reconnect! else - # fully reopen the database each time (with a new object) # (so doc ids and so on aren't preserved across the fork) + ActiveRecord::Base.establish_connection @@db_path = ActsAsXapian.db_path + ".new" ActsAsXapian.writable_init STDOUT.puts("ActsAsXapian.rebuild_index: New batch. #{model_class.to_s} from #{i} to #{i + batch_size} of #{model_class_count} pid #{Process.pid.to_s}") if verbose @@ -738,6 +740,8 @@ module ActsAsXapian Kernel.exit! 0 end + ActiveRecord::Base.establish_connection + end end end |