Class: Woods::Db::Migrator
- Inherits:
-
Object
- Object
- Woods::Db::Migrator
- Defined in:
- lib/woods/db/migrator.rb
Overview
Runs schema migrations against a database connection.
Tracks applied migrations via SchemaVersion and only runs pending ones. Migrations are defined as modules in ‘db/migrations/` with a VERSION constant and a `.up(connection)` class method.
Constant Summary collapse
- MIGRATIONS =
[ Migrations::CreateUnits, Migrations::CreateEdges, Migrations::CreateEmbeddings, Migrations::CreateSnapshots, Migrations::CreateSnapshotUnits, Migrations::RenameTables ].freeze
Instance Attribute Summary collapse
-
#schema_version ⇒ Object
readonly
Returns the value of attribute schema_version.
Instance Method Summary collapse
-
#initialize(connection:) ⇒ Migrator
constructor
A new instance of Migrator.
-
#migrate! ⇒ Array<Integer>
Run all pending migrations.
-
#pending_versions ⇒ Array<Integer>
List version numbers of pending (unapplied) migrations.
Constructor Details
#initialize(connection:) ⇒ Migrator
Returns a new instance of Migrator.
37 38 39 40 41 |
# File 'lib/woods/db/migrator.rb', line 37 def initialize(connection:) @connection = connection @schema_version = SchemaVersion.new(connection: connection) @schema_version.ensure_table! end |
Instance Attribute Details
#schema_version ⇒ Object (readonly)
Returns the value of attribute schema_version.
34 35 36 |
# File 'lib/woods/db/migrator.rb', line 34 def schema_version @schema_version end |
Instance Method Details
#migrate! ⇒ Array<Integer>
Run all pending migrations.
46 47 48 49 50 51 52 53 54 |
# File 'lib/woods/db/migrator.rb', line 46 def migrate! applied = [] pending_migrations.each do |migration| migration.up(@connection) @schema_version.record_version(migration::VERSION) applied << migration::VERSION end applied end |
#pending_versions ⇒ Array<Integer>
List version numbers of pending (unapplied) migrations.
59 60 61 62 |
# File 'lib/woods/db/migrator.rb', line 59 def pending_versions applied = @schema_version.applied_versions MIGRATIONS.map { |m| m::VERSION }.reject { |v| applied.include?(v) } end |