Class: SequelRails::Migrations

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_rails/migrations.rb

Class Method Summary collapse

Class Method Details

.available_migrations?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/sequel_rails/migrations.rb', line 66

def available_migrations?
  File.exist?(migrations_dir) && Dir[File.join(migrations_dir, '*')].any?
end

.current_migrationObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sequel_rails/migrations.rb', line 42

def current_migration
  return unless available_migrations?

  migrator = init_migrator

  if migrator.respond_to?(:applied_migrations)
    migrator.applied_migrations.last
  elsif migrator.respond_to?(:current_version)
    migrator.current_version
  end
end

.dump_schema_information(opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sequel_rails/migrations.rb', line 26

def dump_schema_information(opts = {})
  sql = opts.fetch :sql
  adapter = SequelRails::Storage.adapter_for(Rails.env)
  res = ''

  if available_migrations?
    migrator = init_migrator
    res << adapter.schema_information_dump(migrator, sql)
  end
  res
end

.init_migratorObject



70
71
72
73
74
75
76
77
78
# File 'lib/sequel_rails/migrations.rb', line 70

def init_migrator
  migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)

  migrator_class.new(
    ::Sequel::Model.db,
    migrations_dir,
    allow_missing_migration_files: !!SequelRails.configuration.allow_missing_migration_files
  )
end

.migrate(version = nil) ⇒ Object Also known as: migrate_up!, migrate_down!



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sequel_rails/migrations.rb', line 6

def migrate(version = nil)
  opts = {}
  opts[:target] = version.to_i if version
  opts[:allow_missing_migration_files] = !!SequelRails.configuration.allow_missing_migration_files

  if migrations_dir.directory?
    ::Sequel::Migrator.run(::Sequel::Model.db, migrations_dir, opts)
  else
    relative_path_name = migrations_dir.relative_path_from(Rails.root).to_s
    raise "The #{relative_path_name} directory doesn't exist, you need to create it."
  end
end

.migrations_dirObject



38
39
40
# File 'lib/sequel_rails/migrations.rb', line 38

def migrations_dir
  Rails.root.join('db/migrate')
end

.pending_migrations?Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/sequel_rails/migrations.rb', line 21

def pending_migrations?
  return false unless available_migrations?
  !::Sequel::Migrator.is_current?(::Sequel::Model.db, migrations_dir)
end

.previous_migrationObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sequel_rails/migrations.rb', line 54

def previous_migration
  return unless available_migrations?

  migrator = init_migrator

  if migrator.respond_to?(:applied_migrations)
    migrator.applied_migrations[-2] || '0'
  elsif migrator.respond_to?(:current_version)
    migrator.current_version - 1
  end
end