Class: Migrate::Migrator

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Migrator

Returns a new instance of Migrator.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/migrate/migrator.rb', line 3

def initialize(config)
  @config = config
  @db = config.get_db
  @lang = config.get_lang

  if @db == nil
    throw "Database connection not found."
    exit
  end

  if @lang == nil
    throw "Language not found."
  end
end

Instance Method Details

#current_versionObject



142
143
144
145
146
# File 'lib/migrate/migrator.rb', line 142

def current_version
  @db.tx do
    return @db.current_version
  end
end

#delete(version) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/migrate/migrator.rb', line 148

def delete(version)
  @db.tx do
    Log.info("Removing migration data...")

    if @db.current_version.to_i == version
      return Log.error("Cannot remove current version.")
    end

    dir = self.migration_dir(@db.get_migration(version))
    @db.delete version

    if Dir.exist? dir
      File.delete "#{dir}/up.sql"
      File.delete "#{dir}/down.sql"
      Dir.rmdir dir
    end

    Log.success("Migration data removed.")
  end
end

#down(to_version = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/migrate/migrator.rb', line 124

def down(to_version=nil)
  @db.tx do
    self.exec_migrations(false) do |current_version|
      if current_version == 0
        raise VersionNotFound
      end

      if to_version == nil
        to_version = current_version
      else
        to_version = to_version.to_i + 1
      end

      @db.migrations_range(to_version, current_version, false)
    end
  end
end

#exec_migration(migration, is_up) ⇒ Object

will execute single migration by running up or down script



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/migrate/migrator.rb', line 79

def exec_migration(migration, is_up)
  migration_dir = self.migration_dir(migration)
  result = @lang.exec_migration(migration_dir, is_up)
  if @lang.ext != "sql"
    puts result
  end

  Log.info("Updating current version number...")
  version = migration["version"]
  is_up ? @db.log_up(version) : @db.log_down(version)
end

#exec_migrations(is_up = true) ⇒ Object

will execute range of migrations



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/migrate/migrator.rb', line 92

def exec_migrations(is_up=true)
  Log.info("Executing migrations...")
  migrations = yield @db.current_version

  if migrations.count == 0
    Log.warn("Migrations not found")
    return
  end

  migrations.each do |migration|
    self.exec_migration(migration, is_up)
  end
  Log.success("Migrations executed. Current version: #{@db.current_version}")
end

#initObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/migrate/migrator.rb', line 18

def init
  @db.tx do
    if @db.tables_exists?
      Log.info("Version tables already exist.")
    else
      @db.create_tables
    end

    self.recover
  end
end

#list(select, limit) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/migrate/migrator.rb', line 169

def list(select, limit)
  @db.tx do
    migrations = @db.list_migrations(select, limit)
    if not migrations.any?
      return Log.info("Migrations not found")
    end

    @db.print(migrations, "Migrations")
  end
end

#migration_dir(migration) ⇒ Object



47
48
49
50
# File 'lib/migrate/migrator.rb', line 47

def migration_dir(migration)
  date = DateTime.parse(migration["created_date"].to_s)
  "#{@config.root}/v#{migration["version"]}-#{migration["description"]}"
end

#new(desc, version = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/migrate/migrator.rb', line 52

def new(desc, version=nil)
  @db.tx do
    Log.info("Creating new migration...")

    if version == nil
      version = @db.highest_version.to_i + 1
    end

    migration = @db.new_migration(version, desc)
    migration_dir = self.migration_dir(migration)

    if Dir.exists? migration_dir
      Log.info("Migration directory '#{migration_dir}' already exists.")
    else
      Dir.mkdir migration_dir
      @lang.create_migration(migration_dir)
    end

    Log.success("Migration for version #{migration["version"]} created.")
    migration_dir
  end
rescue Exception => e
  Log.error("Error while creating new migration.", e)
  exit
end

#recoverObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/migrate/migrator.rb', line 30

def recover
  @db.tx do
    directory = @config.root
    migrations = Dir.entries(directory).select { |file| File.directory? File.join(directory, file)}
    migrations.each do |migration|
      match = migration.match(/v(\d*)-(.*)/i)
      if match != nil
        v, desc = match.captures
        unless @db.version_exists?(v)
          self.new(desc, v)
        end
      end
    end
  end
end

#up(to_version = nil, all = false) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/migrate/migrator.rb', line 107

def up(to_version=nil, all=false)
  @db.tx do
    self.exec_migrations do |last_version|
      if all
        @db.migrations_from(@db.next_version)
      else
        new_version = @db.next_version
        if to_version == nil
          to_version = new_version
        end

        @db.migrations_range(new_version, to_version, true)
      end
    end
  end
end