Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d548e02859
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 75 lines (61 sloc) 2.208 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
require 'sequel'
require 'sequel/model'
require 'rails'
require 'sequel_rails3/configuration'
require 'sequel_rails3/logger'

module SequelRails3
  class Railtie < Rails::Railtie
    cattr_accessor :db
    cattr_accessor :config

    initializer 'sequel_rails3.init', :before => :load_environment_config do |app|
      self.config = Configuration.new(app)
      self.config.normalize!

      Sequel::Model.raise_on_save_failure = false
      Sequel::Model.raise_on_typecast_failure = false

      Sequel::Model.plugin :active_model

      self.db = config[:url] ? Sequel.connect(config[:url], config) : Sequel.connect(config)
    end

    initializer 'sequel_rails3.logging', :after => :initialize_logger do |app|
      SequelRails3::Logger.logger = Rails.logger
    end

    initializer 'sequel_rails3.log_runtime' do |app|
      require 'sequel_rails3/railties/controller_runtime'
      ActiveSupport.on_load(:action_controller) do
        include SequelRails3::Railties::ControllerRuntime
      end
    end

    def self.migrator
      Sequel::IntegerMigrator.new(db, File.join(Rails.root, 'db', 'migrate'))
    end

    def self.migrate_to(version)
      Sequel::IntegerMigrator.apply(db, File.join(Rails.root, 'db', 'migrate'), version)
    end

    def self.schema(action)
      case action
        when :dump
          File.open(schema_file, "w") do |f|
            f.write("# Database schema version: #{migrator.current}\n")
            f.write(db.dump_schema_migration)
          end
        when :up
          migration, version = read_schema
          eval(migration).apply(db, :up)
          db[:schema_info].insert(:version => version)
        when :down
          migration = db.dump_schema_migration
          eval(migration).apply(db, :down)
        else
          raise ArgumentError, "invalid schema action '#{action}'"
      end
    end

    def self.schema_file
      File.join(Rails.root, 'db', 'schema.rb')
    end

    def self.read_schema
      data = File.read(schema_file)
      version = data.match(/# Database schema version: (\d+)\n/)[1]
      raise RuntimeError, "unable to determine schema version from schema file" unless version
      [data, version.to_i]
    end
  end
end

Something went wrong with that request. Please try again.