Class: MigrationWriter

Inherits:
Object
  • Object
show all
Defined in:
app/writers/migration_writer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_table) ⇒ MigrationWriter

Returns a new instance of MigrationWriter.



4
5
6
# File 'app/writers/migration_writer.rb', line 4

def initialize(data_table)
  @data_table = data_table
end

Instance Attribute Details

#data_tableObject (readonly)

Returns the value of attribute data_table.



2
3
4
# File 'app/writers/migration_writer.rb', line 2

def data_table
  @data_table
end

Class Method Details

.schema_migrationObject



52
53
54
# File 'app/writers/migration_writer.rb', line 52

def self.schema_migration
  "db/migrate/#{unique_timestamp}_create_gatherable_schema.rb"
end

.schema_migration_created?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/writers/migration_writer.rb', line 48

def self.schema_migration_created?
  Dir[File.join('db', 'migrate', "*create_gatherable_schema.rb")].present?
end

.unique_timestampObject



42
43
44
45
46
# File 'app/writers/migration_writer.rb', line 42

def self.unique_timestamp
  t = Time.now
  t += 1 while Dir[File.join('db', 'migrate', "#{t.strftime("%Y%m%d%H%M%S")}*.rb")].present?
  t.strftime("%Y%m%d%H%M%S")
end

.write_schema_migrationObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/writers/migration_writer.rb', line 8

def self.write_schema_migration
  FileUtils.mkdir_p('db/migrate')
  return if schema_migration_created?
  File.open(schema_migration, 'w') do |f|
    f.puts <<-schema_migration
class CreateGatherableSchema < ActiveRecord::Migration
def up
  create_schema '#{Gatherable.config.schema_name}'
end

def down
  drop_schema '#{Gatherable.config.schema_name}'
end
end
    schema_migration
  end
  puts "created #{schema_migration}" if File.exists?(schema_migration)
end

Instance Method Details

#writeObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/writers/migration_writer.rb', line 27

def write
  if matching_migrations.present?
    puts already_found_message
    return
  end
  FileUtils.mkdir_p('db/migrate')
  filename = "db/migrate/#{self.class.unique_timestamp}_#{file_suffix}"
  File.open(filename, 'w') do |f|
    f.puts file_template
  end
  puts "created #{filename}" if File.exists?(filename)
end