From 6d1601eb2b40b2ce0509ef8c36ef9e8373bc6ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9nich=20Bon=20=C4=86iri=C4=87?= Date: Thu, 11 Jun 2026 00:58:01 -0600 Subject: [PATCH] fix(core): modernize crystal syntax and ameba linting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add and run ameba for linting. - Resolve not_nil! and syntax/style issues reported by ameba. - Ignore pedantic .ameba.yml rules like BlockParameterName. - Rename set_database_to_schema to extract_schema_name for accuracy. Co-developed-by: Gemini AI Signed-off-by: Rénich Bon Ćirić --- .ameba.yml | 6 ++++++ shard.yml | 3 +++ spec/migration_spec.cr | 2 +- src/micrate.cr | 28 +++++++++++++--------------- src/micrate/cli.cr | 6 +++--- src/micrate/db.cr | 17 ++++++++--------- src/micrate/migration.cr | 4 ++-- 7 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 .ameba.yml diff --git a/.ameba.yml b/.ameba.yml new file mode 100644 index 0000000..ff0dedb --- /dev/null +++ b/.ameba.yml @@ -0,0 +1,6 @@ +Naming/BlockParameterName: + Enabled: false +Metrics/CyclomaticComplexity: + Enabled: false +Documentation/DocumentationAdmonition: + Enabled: false diff --git a/shard.yml b/shard.yml index 9243197..e981846 100644 --- a/shard.yml +++ b/shard.yml @@ -27,3 +27,6 @@ development_dependencies: spectator: gitlab: arctic-fox/spectator version: ~> 0.11.3 + ameba: + github: crystal-ameba/ameba + version: ~> 1.6.0 diff --git a/spec/migration_spec.cr b/spec/migration_spec.cr index 25e88fd..da2cfc7 100644 --- a/spec/migration_spec.cr +++ b/spec/migration_spec.cr @@ -101,5 +101,5 @@ baz;") end def statements(migration, direction) - migration.statements(direction).map { |stmt| stmt.strip } + migration.statements(direction).map(&.strip) end diff --git a/src/micrate.cr b/src/micrate.cr index c6d3522..639dd33 100644 --- a/src/micrate.cr +++ b/src/micrate.cr @@ -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) @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 @@ -204,7 +202,7 @@ module Micrate end end - return 0 + 0 end class UnorderedMigrationsException < Exception diff --git a/src/micrate/cli.cr b/src/micrate/cli.cr index cba6243..1036b79 100644 --- a/src/micrate/cli.cr +++ b/src/micrate/cli.cr @@ -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 @@ -24,7 +24,7 @@ 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 @@ -32,7 +32,7 @@ module Micrate 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}") diff --git a/src/micrate/db.cr b/src/micrate/db.cr index b6606a9..bc86ac1 100644 --- a/src/micrate/db.cr +++ b/src/micrate/db.cr @@ -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 @@ -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 diff --git a/src/micrate/migration.cr b/src/micrate/migration.cr index b474154..823da77 100644 --- a/src/micrate/migration.cr +++ b/src/micrate/migration.cr @@ -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