Module: Iudex::DA::ORM

Defined in:
lib/iudex-da/orm.rb,
lib/iudex-da/models.rb

Defined Under Namespace

Classes: ProfileMigrator, Url

Constant Summary collapse

AR_TO_SEQUEL_MIGRATIONS =
{
   85 => '21500000000001_add_simhash_index.rb',
  100 => '21500000000101_add_index_next_visit.rb'
}
AR_REQUIRED =
[ 10, 20, 21, 30, 40, 50, 60, 70, 80, 81, 110 ]
ARNotComplete =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.dbObject

The Sequel::Database instance. #setup is called if necessary.



32
33
34
35
# File 'lib/iudex-da/orm.rb', line 32

def db
  setup unless @db
  @db
end

.migrate(opts = {}) ⇒ Object

Migrate the DB given opts, including :target version. For backward compatibility, opts may be a single Integer, interpreted as the :target version. Setup must be called beforehand. See also opts for #migrate_ar_to_sequel



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/iudex-da/orm.rb', line 67

def migrate( opts = {} )
  opts = {} if opts.nil?
  opts = { :target => opts } if opts.is_a?( Integer )
  raise "setup must be run before migrate" unless db
  profiles = Hooker.apply( [ :iudex, :migration_profiles ],
                           opts[ :profiles ] || [] )

  migrate_ar_to_sequel( opts )

  pm = ProfileMigrator.new( db, profiles, opts )
  pm.run
end

.migrate_ar_to_sequel(opts) ⇒ Object

Migrate from a iudex [1.1.0,1.3) database managed by activerecord to a 1.3.x database managed by Sequel. No-op if already Sequel.

Options

:ar_to_sequel_migrations

Hash<Integer,String> AR migration number to sequel filename (timestamp) map for extensions supported externally to iudex-da.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/iudex-da/orm.rb', line 88

def migrate_ar_to_sequel( opts )

  columns = ( db.table_exists?( :schema_migrations ) &&
              db.schema( :schema_migrations ).map { |sr| sr[0] } )

  if columns == [ :version ] # Old format AR schema_migrations
    db.transaction do
      versions = db.from( :schema_migrations ).
        map { |r| r[ :version ].to_i }

      if ( versions & AR_REQUIRED ) != AR_REQUIRED
        missing = AR_REQUIRED - ( versions & AR_REQUIRED )
        raise( ARNotComplete,
               "Missing AR migrations #{missing.inspect}; " +
               "Use 'iudex-migrate _1.2.1_' first" )
      end

      migrations_map = AR_TO_SEQUEL_MIGRATIONS.
        merge( opts[ :ar_to_sequel_migrations ] || {} )

      db.drop_table( :schema_migrations )
      db.create_table( :schema_migrations ) do
        String :filename, :null => false
        primary_key [ :filename ]
      end

      sm = db[:schema_migrations]
      sm.insert( :filename => '20111012173757_base.rb' )

      migrations_map.each do | version, filename |
        sm.insert( :filename => filename ) if versions.include?( version )
      end
    end
  end
end

.params(opts) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/iudex-da/orm.rb', line 124

def params( opts )
  pms = {}

  u = opts[ :username ]
  pms[ :user ] = u if u

  p = opts[ :password ]
  pms[ :password ] = p if p

  pms.sort.map { |*p| p.join( '=' ) }.join( '&' )
end

.setup(opts = {}) ⇒ Object

Setup the ORM (Sequel) connection given CONFIG defaults, any passed opts, and connect_props config hooks.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/iudex-da/orm.rb', line 39

def setup( opts = {} )

  @db.disconnect if @db

  log = RJack::SLF4J[ "iudex.da.sequel" ]
  conf = CONFIG.merge( opts )
  conf = Hooker.merge( [ :iudex, :connect_props ], conf )

  conf[ :loggers ] = [ log ] if conf[ :log ]

  cstr = ( "%s://%s/%s?%s" %
           [ conf[ :adapter ],
             [ conf[ :host ], conf[ :port ] ].compact.join( ':' ),
             conf[ :database ],
             params( conf ) ] )

  log.info { "Connecting: #{cstr}" }
  log.debug { "Full Params: #{ conf.inspect }" }

  @db = Sequel.connect( cstr, conf )

end