class SequelMigrationsToys::MigrationFile

Migration file

Constants

CONTENT
DISABLING_EXT

Attributes

db_connection[RW]
db_migrations_dir[RW]
root_dir[RW]
disabled[R]
name[R]
version[RW]

Public Class Methods

applied() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 56
        def applied
        database_schema_migrations.map { |one| new filename: one }
end
applied_not_existing() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 64
        def applied_not_existing
        existing_names = existing.map(&:basename)

        applied.reject do |one|
                existing_names.include? one.basename
        end
end
database_schema_migrations() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 36
        def database_schema_migrations
        db_connection[:schema_migrations].select_map(:filename)
end
existing() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 60
        def existing
        find_all '*', disabled: false
end
existing_not_applied() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 72
        def existing_not_applied
        existing.reject do |one|
                database_schema_migrations.include? one.basename
        end
end
find_all(query, enabled: true, disabled: true) click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 40
def find_all(query, enabled: true, disabled: true)
        filenames = Dir["#{db_migrations_dir}/*#{query}*"].select { |filename| File.file? filename }
        files = filenames.map { |filename| new filename: filename }.sort!
        files.reject!(&:disabled) unless disabled
        files.select!(&:disabled) unless enabled
        files
end
find_one(query, **options) click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 48
def find_one(query, **options)
        all = find_all query, **options

        return all.first if all.size < 2

        raise 'More than one file mathes the query'
end
new(filename: nil, name: nil, content: nil) click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 82
def initialize(filename: nil, name: nil, content: nil)
        self.filename = filename
        self.name = name if name
        @content = content
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 113
def <=>(other)
        version <=> other.version
end
applied?() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 117
def applied?
        self.class.database_schema_migrations.include?(basename)
end
basename() click to toggle source

Accessors

# File lib/sequel_migrations_toys/template/_migration_file.rb, line 90
def basename
        File.basename(@filename)
end
disable() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 145
def disable
        abort 'Migration already disabled' if disabled

        rename disabled: true

        puts "Migration #{relative_filename} disabled."
end
disabled=(value) click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 103
def disabled=(value)
        @disabled =
                case value
                when String
                        [DISABLING_EXT, DISABLING_EXT[1..-1]].include? value
                else
                        value
                end
end
enable() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 153
def enable
        abort 'Migration already enabled' unless disabled

        rename disabled: false

        puts "Migration #{relative_filename} enabled."
end
filename=(value) click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 94
def filename=(value)
        parse_filename value if value.is_a? String
        @filename = value
end
generate() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 134
def generate
        self.version = new_version
        FileUtils.mkdir_p File.dirname new_filename
        File.write new_filename, CONTENT.call(@content)
        puts "Migration #{relative_filename} created."
end
name=(value) click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 99
def name=(value)
        @name = value.tr(' ', '_').downcase
end
print() click to toggle source

Behavior

reversion() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 141
def reversion
        rename version: new_version
end

Private Instance Methods

fullname() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 163
def fullname
        result = name.tr('_', ' ').capitalize
        disabled ? "- #{result} (disabled)" : result
end
new_filename() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 187
def new_filename
        new_basename = "#{version}_#{name}.rb#{DISABLING_EXT if disabled}"
        "#{self.class.db_migrations_dir}/#{new_basename}"
end
new_version() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 174
def new_version
        Time.now.strftime('%Y%m%d%H%M%S')
end
parse_filename(value = @filename) click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 168
def parse_filename(value = @filename)
        basename = File.basename value
        self.version, parts = basename.split('_', 2)
        self.name, _ext, self.disabled = parts.split('.')
end
relative_filename() click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 192
def relative_filename
        new_filename.gsub("#{self.class.root_dir}/", '')
end
rename(vars = {}) click to toggle source
# File lib/sequel_migrations_toys/template/_migration_file.rb, line 178
def rename(vars = {})
        vars.each { |key, value| send :"#{key}=", value }

        return unless @filename.is_a? String

        File.rename @filename, new_filename
        self.filename = new_filename
end