DEV: Refactor migrations-tooling

* Updates GitHub Action for migrations
* Rubocop: Always `EnforcedShorthandSyntax` for hashes in the `migrations` directory
* Automatically load all available converter steps
* Enable YJIT at runtime, if available
* Progressbar shows skipped records and other small improvements
This commit is contained in:
Gerhard Schlager
2025-03-30 22:36:50 +02:00
committed by Gerhard Schlager
parent 7b5839ec44
commit 71a90dcba2
27 changed files with 258 additions and 201 deletions

View File

@ -8,6 +8,7 @@ module Migrations::Database
PREPARED_STATEMENT_CACHE_SIZE = 5
def self.open_database(path:)
path = File.expand_path(path, ::Migrations.root_path)
FileUtils.mkdir_p(File.dirname(path))
db = ::Extralite::Database.new(path)
@ -25,7 +26,7 @@ module Migrations::Database
attr_reader :db, :path
def initialize(path:, transaction_batch_size: TRANSACTION_BATCH_SIZE)
@path = path
@path = File.expand_path(path, ::Migrations.root_path)
@transaction_batch_size = transaction_batch_size
@db = self.class.open_database(path:)
@statement_counter = 0
@ -54,24 +55,34 @@ module Migrations::Database
if (@statement_counter += 1) >= @transaction_batch_size
commit_transaction
end
end
def query(sql, *parameters, &block)
@db.query(sql, *parameters, &block)
end
def count(sql, *parameters)
@db.query_single_splat(sql, *parameters)
end
def execute(sql, *parameters)
@db.execute(sql, *parameters)
end
def begin_transaction
@db.execute("BEGIN DEFERRED TRANSACTION") unless @db.transaction_active?
end
def commit_transaction
if @db.transaction_active?
@db.execute("COMMIT")
@statement_counter = 0
end
end
private
def begin_transaction
return if @db.transaction_active?
@db.execute("BEGIN DEFERRED TRANSACTION")
end
def commit_transaction
return unless @db.transaction_active?
@db.execute("COMMIT")
end
def close_connection(keep_path:)
return if @db.nil?

View File

@ -11,7 +11,7 @@ module Migrations::Database::IntermediateDB
VALUES (?, ?, ?, ?, ?)
SQL
def self.create!(created_at: Time.now, type:, message:, exception: nil, details: nil)
def self.create(created_at: Time.now, type:, message:, exception: nil, details: nil)
::Migrations::Database::IntermediateDB.insert(
SQL,
::Migrations::Database.format_datetime(created_at),

View File

@ -17,76 +17,79 @@ module Migrations::Database::IntermediateDB
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
SQL
class << self
def create_for_file!(
def self.create_for_file(
path:,
filename: nil,
type: nil,
description: nil,
origin: nil,
user_id: nil
)
create(
id: ::Migrations::ID.hash(path),
filename: filename || File.basename(path),
path:,
filename: nil,
type: nil,
description: nil,
origin: nil,
user_id: nil
type:,
description:,
origin:,
user_id:,
)
create!(
id: ::Migrations::ID.hash(path),
filename: filename || File.basename(path),
path:,
type:,
description:,
origin:,
user_id:,
)
end
end
def create_for_url!(url:, filename:, type: nil, description: nil, origin: nil, user_id: nil)
create!(
id: ::Migrations::ID.hash(url),
filename:,
url:,
type:,
description:,
origin:,
user_id:,
)
end
def create_for_data!(data:, filename:, type: nil, description: nil, origin: nil, user_id: nil)
create!(
id: ::Migrations::ID.hash(data),
filename:,
data: ::Migrations::Database.to_blob(data),
type:,
description:,
origin:,
user_id:,
)
end
private
def create!(
id:,
def self.create_for_url(url:, filename:, type: nil, description: nil, origin: nil, user_id: nil)
create(
id: ::Migrations::ID.hash(url),
filename:,
path: nil,
data: nil,
url: nil,
type: nil,
description: nil,
origin: nil,
user_id: nil
url:,
type:,
description:,
origin:,
user_id:,
)
end
def self.create_for_data(
data:,
filename:,
type: nil,
description: nil,
origin: nil,
user_id: nil
)
create(
id: ::Migrations::ID.hash(data),
filename:,
data: ::Migrations::Database.to_blob(data),
type:,
description:,
origin:,
user_id:,
)
end
def self.create(
id:,
filename:,
path: nil,
data: nil,
url: nil,
type: nil,
description: nil,
origin: nil,
user_id: nil
)
::Migrations::Database::IntermediateDB.insert(
SQL,
id,
filename,
path,
data,
url,
type,
description,
origin,
user_id,
)
::Migrations::Database::IntermediateDB.insert(
SQL,
id,
filename,
path,
data,
url,
type,
description,
origin,
user_id,
)
end
end
end
end

View File

@ -3,7 +3,7 @@
module Migrations::Database
class Migrator
def initialize(db_path)
@db_path = db_path
@db_path = File.expand_path(db_path, ::Migrations.root_path)
@db = nil
end
@ -72,7 +72,7 @@ module Migrations::Database
@db.transaction do
@db.execute(sql)
@db.execute(<<~SQL, path: relative_path, sql_hash: sql_hash)
@db.execute(<<~SQL, path: relative_path, sql_hash:)
INSERT INTO schema_migrations (path, created_at, sql_hash)
VALUES (:path, datetime('now'), :sql_hash)
SQL