Class: Migrate::Storage::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/migrate/storage/db.rb

Direct Known Subclasses

Mysql, Postgres

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DB

Returns a new instance of DB.



8
9
10
# File 'lib/migrate/storage/db.rb', line 8

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/migrate/storage/db.rb', line 6

def config
  @config
end

Instance Method Details

#create_tablesObject

Will create database model used by tool



135
136
137
# File 'lib/migrate/storage/db.rb', line 135

def create_tables
  raise "Implementation for creating tables not found"
end

#current_versionObject



69
70
71
72
73
74
# File 'lib/migrate/storage/db.rb', line 69

def current_version
  self.extract_version self.exec_sql "    SELECT * FROM \#{config.version_number}\n    LIMIT 1\n  eos\nend\n"

#delete(version) ⇒ Object



107
108
109
# File 'lib/migrate/storage/db.rb', line 107

def delete(version)
  self.exec_sql "DELETE FROM #{@config.version_info} WHERE version=#{version}"
end

#exec_sql(sql) ⇒ Object

Executes SQL



140
141
142
# File 'lib/migrate/storage/db.rb', line 140

def exec_sql(sql)
  raise "Implementation for executing SQL script not found"
end

#extract_version(results) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/migrate/storage/db.rb', line 44

def extract_version(results)
  if results && results.count > 0
    results[0]["version"]
  else
    raise VersionNotFound
  end
end

#get_migration(version) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/migrate/storage/db.rb', line 98

def get_migration(version)
  res = self.exec_sql "SELECT * FROM #{@config.version_info} WHERE version=#{version}"
  if res && res.count > 0
    res[0]
  else
    raise VersionNotFound
  end
end

#list_migrations(selects, limit) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/migrate/storage/db.rb', line 28

def list_migrations(selects, limit)
  self.exec_sql "    SELECT \#{(selects == nil ? \"*\" : selects)} FROM \#{@config.version_info}\n    ORDER BY last_up, version\n    \#{limit != nil ? \"LIMIT \#{limit}\" : \"\"}\n  eos\nend\n" 

#log_down(version) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/migrate/storage/db.rb', line 90

def log_down(version)
  self.exec_sql "UPDATE #{@config.version_info} SET last_down=now() WHERE version=#{version}"
  
  lowest_version = self.lowest_version
  version_to_save = lowest_version.to_i < version.to_i ? self.prev_version().to_i : 0
  self.exec_sql "UPDATE #{@config.version_number} SET version=#{version_to_save}"
end

#log_up(version) ⇒ Object



85
86
87
88
# File 'lib/migrate/storage/db.rb', line 85

def log_up(version)
  self.exec_sql "UPDATE #{@config.version_info} SET last_up=now() WHERE version=#{version}"
  self.exec_sql "UPDATE #{@config.version_number} SET version=#{version}"
end

#lowest_versionObject



52
53
54
55
56
57
58
# File 'lib/migrate/storage/db.rb', line 52

def lowest_version
  self.extract_version self.exec_sql "    SELECT version FROM \#{@config.version_info}\n      ORDER BY version\n    LIMIT 1\n  eos\nend\n"

#migrations_range(from, to, is_up) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/migrate/storage/db.rb', line 36

def migrations_range(from, to, is_up)
  self.exec_sql "    SELECT * FROM \#{@config.version_info}\n      WHERE version >= \#{from} AND version <= \#{to}\n    ORDER BY version \#{!is_up ? \"DESC\" : \"\"}\n    eos\nend\n"

#new_migration(description = "") ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/migrate/storage/db.rb', line 16

def new_migration(description="")
  self.exec_sql "    INSERT INTO \#{@config.version_info} (description, created_date) \n    VALUES('\#{description}', now())\n  eos\n\n  res = self.exec_sql <<-eos\n    SELECT * FROM \#{@config.version_info} ORDER BY version DESC LIMIT 1\n  eos\n  res[0]\nend\n"

#next_versionObject



60
61
62
63
64
65
66
67
# File 'lib/migrate/storage/db.rb', line 60

def next_version
  self.extract_version self.exec_sql "    SELECT version FROM \#{@config.version_info} \n      WHERE version > (SELECT version FROM \#{@config.version_number} LIMIT 1)\n    ORDER BY version\n    LIMIT 1\n  eos\nend\n"

#prev_versionObject



76
77
78
79
80
81
82
83
# File 'lib/migrate/storage/db.rb', line 76

def prev_version
  self.extract_version self.exec_sql "    SELECT version FROM \#{@config.version_info} \n      WHERE version < (SELECT version FROM \#{@config.version_number} LIMIT 1) \n    ORDER BY version DESC\n    LIMIT 1\n  eos\nend\n"


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/migrate/storage/db.rb', line 111

def print(results, title="")
  rows = []
  headings = results[0].keys

  results.each do |result|
    row = []
    result.each do |column, value|
      if column == "description"
        if value.length > 70
          value = value.scan(/.{1,70}/).join("\n")
        end
      end

      row << value
    end
    rows << row
  end

  table = Terminal::Table.new :headings => headings, :rows => rows
  table.title = title
  puts table
end

#txObject

Creates new transaction. Should accept block.



145
146
147
# File 'lib/migrate/storage/db.rb', line 145

def tx
  raise "Implementation for starting new transaction not found"
end

#typeObject



12
13
14
# File 'lib/migrate/storage/db.rb', line 12

def type
  @config.storage
end