Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .ameba.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Naming/BlockParameterName:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: false
Documentation/DocumentationAdmonition:
Enabled: false
3 changes: 3 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ development_dependencies:
spectator:
gitlab: arctic-fox/spectator
version: ~> 0.11.3
ameba:
github: crystal-ameba/ameba
version: ~> 1.6.0
2 changes: 1 addition & 1 deletion spec/migration_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ baz;")
end

def statements(migration, direction)
migration.statements(direction).map { |stmt| stmt.strip }
migration.statements(direction).map(&.strip)
end
28 changes: 13 additions & 15 deletions src/micrate.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ module Micrate
end

def self.dbversion(db)
begin
rows = DB.get_versions_last_first_order(db)
return extract_dbversion(rows)
rescue Exception
DB.create_migrations_table(db)
return 0
end
rows = DB.get_versions_last_first_order(db)
extract_dbversion(rows)
rescue Exception
DB.create_migrations_table(db)
0
end

def self.up(db)
Expand All @@ -32,7 +30,7 @@ module Micrate
end

current = dbversion(db)
target = all_migrations.keys.sort.last
target = all_migrations.keys.sort!.last
migrate(all_migrations, current, target, db)
end

Expand Down Expand Up @@ -85,7 +83,7 @@ module Micrate
Dir.mkdir_p dir
File.write(filename, migration_template)

return filename
filename
end

def self.connection_url=(connection_url)
Expand Down Expand Up @@ -150,7 +148,7 @@ module Micrate
# the given version is (likely) valid but we didn't find
# anything before it.
# return value must reflect that no migrations have been applied.
return 0
0
else
raise "no previous version found"
end
Expand All @@ -161,7 +159,7 @@ module Micrate
.select { |name| File.file? File.join(migrations_dir, name) }
.select { |name| /^\d+.+\.sql$/ =~ name }
.map { |name| Migration.from_file(name) }
.index_by { |migration| migration.version }
.index_by(&.version)
end

def self.migration_plan(status : Hash(Migration, Time?), current : Int, target : Int, direction)
Expand All @@ -177,12 +175,12 @@ module Micrate

if direction == :forward
all_versions.keys
.sort
.sort!
.select { |v| v > current && v <= target }
else
all_versions.keys
.sort
.reverse
.sort!
.reverse!
.select { |v| v <= current && v > target }
end
end
Expand All @@ -204,7 +202,7 @@ module Micrate
end
end

return 0
0
end

class UnorderedMigrationsException < Exception
Expand Down
6 changes: 3 additions & 3 deletions src/micrate/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Micrate
File.delete(path)
Log.info { "Deleted file #{path}" }
else
name = set_database_to_schema url
name = extract_schema_name url
Micrate::DB.connect do |db|
db.exec "DROP DATABASE IF EXISTS #{name};"
end
Expand All @@ -24,15 +24,15 @@ module Micrate
if url.starts_with? "sqlite3:"
Log.info { "For sqlite3, the database will be created during the first migration." }
else
name = set_database_to_schema url
name = extract_schema_name url
Micrate::DB.connect do |db|
db.exec "CREATE DATABASE #{name};"
end
Log.info { "Created database #{name}" }
end
end

def self.set_database_to_schema(url)
def self.extract_schema_name(url)
uri = URI.parse(url)
if path = uri.path
Micrate::DB.connection_url = url.gsub(path, "/#{uri.scheme}")
Expand Down
17 changes: 8 additions & 9 deletions src/micrate/db.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ module Micrate
end

def self.connect
validate_connection_url
::DB.connect(self.connection_url.not_nil!)
::DB.connect(valid_connection_url)
end

def self.connect(&block)
validate_connection_url
::DB.open self.connection_url.not_nil! do |db|
def self.connect(&)
::DB.open valid_connection_url do |db|
yield db
end
end
Expand Down Expand Up @@ -50,14 +48,15 @@ module Micrate
end

private def self.dialect
validate_connection_url
@@dialect ||= Dialect.from_connection_url(self.connection_url.not_nil!)
@@dialect ||= Dialect.from_connection_url(valid_connection_url)
end

private def self.validate_connection_url
if !self.connection_url
private def self.valid_connection_url : String
url = self.connection_url
if !url
raise "No database connection URL is configured. Please set the DATABASE_URL environment variable."
end
url
end
end
end
4 changes: 2 additions & 2 deletions src/micrate/migration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ module Micrate

def self.from_version(version)
file_name = Dir.entries(Micrate.migrations_dir)
.find { |name| name.starts_with? version.to_s }
.not_nil!
.find(&.starts_with?(version.to_s))

self.from_file(file_name)
end
end
Expand Down