Skip to content

Commit

Permalink
Refactor migrations to introduce BaseMigration class
Browse files Browse the repository at this point in the history
Switched from using Migration as the base class to a new abstract
BaseMigration class and parametrized Migration class to enforce
version specification at subclass definition. Updated various methods
and specs to utilize BaseMigration, improving type safety and
clarity of the migration process.
  • Loading branch information
eliasjpr committed Aug 23, 2024
1 parent 6800528 commit 5ea0be3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion spec/migration_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ describe Cql::Migration do
it "prints pending migrations" do
migrator.down
migrator.print_pending_migrations
migrator.pending_migrations.should be_a(Array(Cql::Migration.class))
migrator.pending_migrations.should be_a(Array(Cql::BaseMigration.class))
end
end
4 changes: 1 addition & 3 deletions spec/migrations/alter_users_migration.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class AlterUsersMigration < Cql::Migration
self.version = 987654321_i64

class AlterUsersMigration < Cql::Migration(987654321)
def up
schema.alter :users do
add_column :phone, String
Expand Down
4 changes: 1 addition & 3 deletions spec/migrations/create_users_migration.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class CreateUsersMigration < Cql::Migration
self.version = 123456789_i64

class CreateUsersMigration < Cql::Migration(123456789)
def up
schema.users.create!
end
Expand Down
44 changes: 23 additions & 21 deletions src/migrations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,23 @@ module Cql
# migrator.last
# ```
#
abstract class Migration
getter schema : Schema

abstract class BaseMigration
abstract def up
abstract def down
end

abstract class Migration(V) < BaseMigration
macro inherited
class_getter version : Int64 = 0_i64
def self.version=(number : Int64)
@@version = number
end
Cql::Migrator.migrations << {{@type}}
end
getter schema : Cql::Schema

def initialize(@schema : Schema); end
Cql::Migrator.migrations << {{@type}}
def self.version : Int32
V
end

abstract def up
abstract def down
def initialize(@schema : Cql::Schema); end
end
end

# The `Migrator` class is used to manage migrations and provides methods to apply,
Expand Down Expand Up @@ -142,22 +144,22 @@ module Cql

getter id : Int64
getter name : String
getter version : Int64
getter version : Int32
getter created_at : Time
getter updated_at : Time

def initialize(
@id : Int64,
@name : String,
@version : Int64,
@version : Int32,
@created_at = Time.local,
@updated_at = Time.local
)
end
end

getter schema : Schema
class_property migrations : Array(Migration.class) = [] of Migration.class
class_property migrations : Array(BaseMigration.class) = [] of BaseMigration.class
getter repo : Repository(MigrationRecord, Int32)

def initialize(@schema : Schema)
Expand Down Expand Up @@ -224,7 +226,7 @@ module Cql
# migrator.last
# ```
# @return [Migration.class | Nil]
def last : Migration.class | Nil
def last : BaseMigration.class | Nil
Migrator.migrations.find { |m| m.version == repo.last.version }
rescue DB::NoResultsError
nil
Expand Down Expand Up @@ -259,7 +261,7 @@ module Cql
# ```
# migrator.print_rolled_back_migrations
# ```
def print_rolled_back_migrations(m : Array(Migration.class))
def print_rolled_back_migrations(m : Array(BaseMigration.class))
print_table(m.map { |migration| build_migration_record(migration) }, "".colorize.red.to_s)
end

Expand Down Expand Up @@ -287,7 +289,7 @@ module Cql
# ```
# migrator.pending_migrations
# ```
def pending_migrations : Array(Migration.class)
def pending_migrations : Array(BaseMigration.class)
sorted_migrations.reject { |m| migration_applied?(m.version) }
end

Expand Down Expand Up @@ -315,7 +317,7 @@ module Cql
puts table
end

private def build_migration_record(migration : Migration.class) : MigrationRecord
private def build_migration_record(migration : BaseMigration.class) : MigrationRecord
MigrationRecord.new(0, migration.name, migration.version)
end

Expand All @@ -327,7 +329,7 @@ module Cql
schema.table :schema_migrations do
primary :id, Int32
column :name, String
column :version, Int64, index: true, unique: true
column :version, Int32, index: true, unique: true
timestamps
end
schema.schema_migrations.create!
Expand All @@ -339,11 +341,11 @@ module Cql
false
end

private def record_migration(migration : Migration.class)
private def record_migration(migration : BaseMigration.class)
repo.create(name: migration.name, version: migration.version)
end

private def remove_migration_record(migration : Migration.class)
private def remove_migration_record(migration : BaseMigration.class)
repo.delete_by(name: migration.name, version: migration.version)
end
end
Expand Down

0 comments on commit 5ea0be3

Please sign in to comment.