Class: SequelMigrationsToys::MigrationFile

Inherits:
Object
  • Object
show all
Extended by:
Memery
Defined in:
lib/sequel_migrations_toys/template/_migration_file.rb

Overview

Migration file

Constant Summary collapse

CONTENT =

rubocop:disable Metrics/ClassLength

proc do |content|
	content ||= +<<~DEFAULT
		change do
		end
	DEFAULT

	## /^(?!$)/ - searches for a position that starts at the
	## "start of line" position and
	## is not followed by the "end of line" position

	<<~STR
		# frozen_string_literal: true

		Sequel.migration do
			#{content.gsub!(/^(?!$)/, "\t").strip!}
		end
	STR
end
DISABLING_EXT =
'.bak'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename: nil, name: nil, content: nil) ⇒ MigrationFile

Returns a new instance of MigrationFile.



83
84
85
86
87
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 83

def initialize(filename: nil, name: nil, content: nil)
	self.filename = filename
	self.name = name if name
	@content = content
end

Class Attribute Details

.db_connectionObject

Returns the value of attribute db_connection.



35
36
37
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 35

def db_connection
  @db_connection
end

.db_migrations_dirObject

Returns the value of attribute db_migrations_dir.



35
36
37
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 35

def db_migrations_dir
  @db_migrations_dir
end

.root_dirObject

Returns the value of attribute root_dir.



35
36
37
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 35

def root_dir
  @root_dir
end

Instance Attribute Details

#disabledObject

Returns the value of attribute disabled.



81
82
83
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 81

def disabled
  @disabled
end

#nameObject

Returns the value of attribute name.



81
82
83
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 81

def name
  @name
end

#versionObject

Returns the value of attribute version.



80
81
82
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 80

def version
  @version
end

Class Method Details

.find_all(query, enabled: true, disabled: true) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 41

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) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 49

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

Instance Method Details

#<=>(other) ⇒ Object



114
115
116
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 114

def <=>(other)
	version <=> other.version
end

#applied?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 118

def applied?
	self.class.database_schema_migrations.include?(basename)
end

#basenameObject

Accessors



91
92
93
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 91

def basename
	File.basename(@filename)
end

#disableObject



146
147
148
149
150
151
152
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 146

def disable
	abort 'Migration already disabled' if disabled

	rename disabled: true

	puts "Migration #{relative_filename} disabled."
end

#enableObject



154
155
156
157
158
159
160
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 154

def enable
	abort 'Migration already enabled' unless disabled

	rename disabled: false

	puts "Migration #{relative_filename} enabled."
end

#filename=(value) ⇒ Object



95
96
97
98
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 95

def filename=(value)
	parse_filename value if value.is_a? String
	@filename = value
end

#generateObject



135
136
137
138
139
140
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 135

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

Behavior



124
125
126
127
128
129
130
131
132
133
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 124

def print
	datetime = Time.parse(version).strftime('%F %R')

	puts [
		Paint["[#{version}]", :white],
		Paint[datetime, disabled ? :white : :cyan],
		Paint[fullname, disabled ? :white : :default],
		(Paint['(not applied)', :red] unless applied?)
	].join(' ')
end

#reversionObject



142
143
144
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 142

def reversion
	rename version: new_version
end