Class: SequelMigrationsToys::MigrationFile
- Inherits:
-
Object
- Object
- SequelMigrationsToys::MigrationFile
- 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
-
.db_connection ⇒ Object
Returns the value of attribute db_connection.
-
.db_migrations_dir ⇒ Object
Returns the value of attribute db_migrations_dir.
-
.root_dir ⇒ Object
Returns the value of attribute root_dir.
Instance Attribute Summary collapse
-
#disabled ⇒ Object
Returns the value of attribute disabled.
-
#name ⇒ Object
Returns the value of attribute name.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #applied? ⇒ Boolean
-
#basename ⇒ Object
Accessors.
- #disable ⇒ Object
- #enable ⇒ Object
- #filename=(value) ⇒ Object
- #generate ⇒ Object
-
#initialize(filename: nil, name: nil, content: nil) ⇒ MigrationFile
constructor
A new instance of MigrationFile.
-
#print ⇒ Object
Behavior.
- #reversion ⇒ Object
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_connection ⇒ Object
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_dir ⇒ Object
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_dir ⇒ Object
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
#disabled ⇒ Object
Returns the value of attribute disabled.
81 82 83 |
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 81 def disabled @disabled end |
#name ⇒ Object
Returns the value of attribute name.
81 82 83 |
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 81 def name @name end |
#version ⇒ Object
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, **) all = find_all query, ** 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
118 119 120 |
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 118 def applied? self.class.database_schema_migrations.include?(basename) end |
#basename ⇒ Object
Accessors
91 92 93 |
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 91 def basename File.basename(@filename) end |
#disable ⇒ Object
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 |
#enable ⇒ Object
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 |
#generate ⇒ Object
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 |
#print ⇒ Object
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 |
#reversion ⇒ Object
142 143 144 |
# File 'lib/sequel_migrations_toys/template/_migration_file.rb', line 142 def reversion rename version: new_version end |