Skip to content

Instantly share code, notes, and snippets.

@robacarp
Last active June 22, 2018 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robacarp/f6c136b2ff8d6869c4c4ab69735f4628 to your computer and use it in GitHub Desktop.
Save robacarp/f6c136b2ff8d6869c4c4ab69735f4628 to your computer and use it in GitHub Desktop.
require "pg"
require "migrate"
require "../config/application"
command = ARGV[0]?
case command
when "migrate"
MigrationRunner.new.migrate
when "rollback"
MigrationRunner.new.rollback
when "status"
MigrationRunner.new.status
when nil
puts "Commands to manipulate the database: migrate, rollback, status"
else
puts "unknown command '#{command}"
end
class MigrationRunner
def initialize
@migrator = Migrate::Migrator.new(
DB.open(Amber.settings.database_url),
Logger.new(STDOUT, level: Logger::DEBUG),
File.join("db", "migrations"),
"schema_version",
"version"
)
end
getter migrator
def migrate
migrator.to_latest
end
def rollback
migrator.down
end
def pending?
current_version = migrator.current_version
current_version != migrator.next_version
end
def status
current_version = migrator.current_version
puts "Current version: #{migrator.current_version}."
puts "Next version: #{migrator.next_version}."
puts "There are pending migrations." if pending?
end
end
##########################
# CONFIG
##########################
# select and require the appropriate database driver here:
require "pg"
# require "sqlite"
# require "mysql"
TABLE_NAME = "schema_version"
COLUMN_NAME = "version"
migration_path = "db/migrations"
MIGRATION_FILE_REGEX = /(?<version>\d+)(_(?<name>\w+))?\.sql/
MIGRATION_CMD_PREFIX = /-- \+micrate/
NEW_MIGRATION_CMD = "-- +migrate"
##########################
# Rewriting migration files
##########################
require "colorize"
glob = File.join Dir.current, migration_path, "*.sql"
puts "Searching in #{glob}"
migration_versions = [] of String
Dir[glob].each_with_index do |entry, i|
filename = File.basename entry
unless filename_parts = MIGRATION_FILE_REGEX.match(filename)
puts "#{filename} doesnt match, skipping"
next
end
migration_versions << filename_parts["version"]
lines = File.read_lines entry
lines_changed = 0
lines.map! do |line|
if MIGRATION_CMD_PREFIX.match line
lines_changed += 1
line.gsub MIGRATION_CMD_PREFIX, NEW_MIGRATION_CMD
else
line
end
end
if lines_changed > 0
print "rewriting: ".colorize(:green)
puts filename
lines.push "" if lines.last != ""
File.write entry, lines.join("\n")
end
end
last_version = migration_versions.sort.last
###############################
# WRITING NEW MIGRATION HISTORY
###############################
database_connection = DB.open(Amber.settings.database_url)
statements = [] of String
statements << "DROP TABLE IF EXISTS #{TABLE_NAME}"
statements << "CREATE TABLE #{TABLE_NAME} (#{COLUMN_NAME} BIGINT NOT NULL)"
statements << "TRUNCATE TABLE #{TABLE_NAME}"
statements << "INSERT INTO #{TABLE_NAME} (#{COLUMN_NAME}) VALUES (#{last_version})"
statements << "DROP TABLE IF EXISTS micrate_db_version"
statements.each do |statement|
database_connection.exec statement
end
###############################
# CONNECT MIGRATOR AND GET STATUS
###############################
require "../config/application"
require "migrate"
migrator = Migrate::Migrator.new(
database_connection,
Logger.new(STDOUT),
File.join("db", "migrations"), # Path to migrations
TABLE_NAME, # Version table name
COLUMN_NAME # Version column name
)
puts "connected to #{Amber.settings.database_url}"
puts "current version: #{migrator.current_version}"
puts "next version: #{migrator.next_version}"
puts "previous version: #{migrator.previous_version}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment