-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run migrations/snippets in Rails console
- Loading branch information
1 parent
b05c8ec
commit 7f39e83
Showing
7 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
# Provides methods for executing schema modification commands directly in the Rails console. | ||
module ConsoleMigrations | ||
extend self | ||
|
||
SCHEMA_METHODS = %i[ | ||
create_table | ||
create_join_table | ||
drop_table | ||
change_table | ||
add_column | ||
remove_column | ||
change_column | ||
change_column_null | ||
change_column_default | ||
rename_column | ||
add_index | ||
remove_index | ||
rename_index | ||
add_timestamps | ||
remove_timestamps | ||
reversible | ||
add_reference | ||
remove_reference | ||
add_foreign_key | ||
remove_foreign_key | ||
].freeze | ||
|
||
SCHEMA_METHODS.each do |method_name| | ||
define_method(method_name) do |*args, **kwargs, &block| | ||
if kwargs.any? | ||
migration_instance.public_send(method_name, *args, **kwargs, &block) | ||
else | ||
migration_instance.public_send(method_name, *args, &block) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def migration_instance | ||
@migration_instance ||= Class.new(ActiveRecord::Migration[ActiveRecord::Migration.current_version]) {}.new | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
# Integrates the ConsoleMigrations module into the Rails console. | ||
class Railtie < ::Rails::Railtie | ||
console do | ||
require_relative "console_migrations" | ||
|
||
if ActualDbSchema.config[:console_migrations_enabled] | ||
TOPLEVEL_BINDING.receiver.extend(ActualDbSchema::ConsoleMigrations) | ||
puts "[ActualDbSchema] ConsoleMigrations enabled. You can now use migration methods directly at the console." | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require_relative "../lib/actual_db_schema/console_migrations" | ||
|
||
describe "console migrations" do | ||
let(:utils) { TestUtils.new } | ||
|
||
before do | ||
extend ActualDbSchema::ConsoleMigrations | ||
|
||
utils.reset_database_yml(TestingState.db_config["primary"]) | ||
ActiveRecord::Base.configurations = { "test" => TestingState.db_config["primary"] } | ||
ActiveRecord::Tasks::DatabaseTasks.database_configuration = { "test" => TestingState.db_config["primary"] } | ||
ActiveRecord::Base.establish_connection(**TestingState.db_config["primary"]) | ||
utils.cleanup | ||
|
||
utils.define_migration_file("20250124084321_create_users.rb", <<~RUBY) | ||
class CreateUsers < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :users do |t| | ||
t.string :name | ||
t.string :middle_name | ||
t.timestamps | ||
end | ||
add_index :users, :name, name: "index_users_on_name", unique: true | ||
end | ||
end | ||
RUBY | ||
utils.run_migrations | ||
end | ||
|
||
after do | ||
utils.define_migration_file("20250124084323_drop_users.rb", <<~RUBY) | ||
class DropUsers < ActiveRecord::Migration[6.0] | ||
def change | ||
drop_table :users | ||
end | ||
end | ||
RUBY | ||
utils.run_migrations | ||
end | ||
|
||
it "adds a column to a table" do | ||
add_column :users, :email, :string | ||
assert ActiveRecord::Base.connection.column_exists?(:users, :email) | ||
end | ||
|
||
it "removes a column from a table" do | ||
remove_column :users, :middle_name | ||
refute ActiveRecord::Base.connection.column_exists?(:users, :middle_name) | ||
end | ||
|
||
it "creates and drops a table" do | ||
refute ActiveRecord::Base.connection.table_exists?(:categories) | ||
create_table :categories do |t| | ||
t.string :title | ||
t.timestamps | ||
end | ||
assert ActiveRecord::Base.connection.table_exists?(:categories) | ||
|
||
drop_table :categories | ||
refute ActiveRecord::Base.connection.table_exists?(:categories) | ||
end | ||
|
||
it "changes column type" do | ||
change_column :users, :middle_name, :text | ||
assert_equal :text, ActiveRecord::Base.connection.columns(:users).find { |c| c.name == "middle_name" }.type | ||
end | ||
|
||
it "renames a column" do | ||
rename_column :users, :name, :full_name | ||
assert ActiveRecord::Base.connection.column_exists?(:users, :full_name) | ||
refute ActiveRecord::Base.connection.column_exists?(:users, :name) | ||
end | ||
|
||
it "adds and removes an index" do | ||
add_index :users, :middle_name, name: "index_users_on_middle_name", unique: true | ||
assert ActiveRecord::Base.connection.index_exists?(:users, :middle_name, name: "index_users_on_middle_name") | ||
|
||
remove_index :users, name: "index_users_on_middle_name" | ||
refute ActiveRecord::Base.connection.index_exists?(:users, :middle_name, name: "index_users_on_middle_name") | ||
end | ||
|
||
it "adds and removes timestamps" do | ||
remove_timestamps :users | ||
refute ActiveRecord::Base.connection.column_exists?(:users, :created_at) | ||
refute ActiveRecord::Base.connection.column_exists?(:users, :updated_at) | ||
|
||
add_timestamps :users | ||
assert ActiveRecord::Base.connection.column_exists?(:users, :created_at) | ||
assert ActiveRecord::Base.connection.column_exists?(:users, :updated_at) | ||
end | ||
end |