diff --git a/Gemfile b/Gemfile index 422257eb..28175736 100644 --- a/Gemfile +++ b/Gemfile @@ -14,3 +14,8 @@ gem 'standalone_migrations' # Still relevant as `mysql` is still out-dated/-moded by Ruby 2/4 # Source: https://stackoverflow.com/a/41521197/1091943 gem 'mysql2' + +group :development do + gem 'pry-byebug' + # gem 'pry-byebug', require: true +end diff --git a/Gemfile.lock b/Gemfile.lock index c26caba0..6e68b484 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -25,6 +25,8 @@ GEM minitest (>= 5.1) tzinfo (~> 2.0) builder (3.2.4) + byebug (11.1.3) + coderay (1.1.3) concurrent-ruby (1.2.2) crass (1.0.6) erubi (1.12.0) @@ -40,6 +42,12 @@ GEM nokogiri (1.14.2) mini_portile2 (~> 2.8.0) racc (~> 1.4) + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) + pry-byebug (3.10.1) + byebug (~> 11.0) + pry (>= 0.13, < 0.15) racc (1.6.2) rack (2.2.6.4) rack-test (2.1.0) @@ -71,6 +79,8 @@ PLATFORMS DEPENDENCIES mysql2 + pry + pry-byebug standalone_migrations BUNDLED WITH diff --git a/dev/db/migrate/disabled/20230405204517_container_add_basic_tot_k_support.rb.disabled b/dev/db/migrate/disabled/20230405204517_container_add_basic_tot_k_support.rb.disabled index f66dddd6..3b7cec2b 100644 --- a/dev/db/migrate/disabled/20230405204517_container_add_basic_tot_k_support.rb.disabled +++ b/dev/db/migrate/disabled/20230405204517_container_add_basic_tot_k_support.rb.disabled @@ -1,7 +1,35 @@ class ContainerAddBasicTotKSupport < ActiveRecord::Migration[7.0] + def database + connection.instance_variable_get(:@config)[:database] + end + def up execute <<-SQL - INSERT INTO `container` (`short_name`, `name`, `icon`) VALUES ('TotK','Tears of the Kingdom', 'Tears-of-the-Kingdom'); + INSERT INTO `container` (`id`, `short_name`, `name`, `icon`) VALUES (20, 'TotK','Tears of the Kingdom', 'Tears-of-the-Kingdom'); + SQL + end + def down + execute <<-SQL + DELETE FROM `container` + WHERE `id` = 20; + ; + SQL + + aiValue = ( + execute <<-SQL + SELECT `AUTO_INCREMENT` + FROM `INFORMATION_SCHEMA`.`TABLES` + WHERE `TABLE_SCHEMA` = '#{database}' + AND `TABLE_NAME` = 'container' + LIMIT 1 + ; + SQL + ).first.first + contentEntryAmount = 1 + execute <<-SQL + ALTER TABLE `container` + AUTO_INCREMENT=#{aiValue - contentEntryAmount} + ; SQL end end diff --git a/dev/db/migrate/disabled/20230405222955_map_add_tot_k_overworld_maps.rb.disabled b/dev/db/migrate/disabled/20230405222955_map_add_tot_k_overworld_maps.rb.disabled index 5b91aac7..5c8fbafd 100644 --- a/dev/db/migrate/disabled/20230405222955_map_add_tot_k_overworld_maps.rb.disabled +++ b/dev/db/migrate/disabled/20230405222955_map_add_tot_k_overworld_maps.rb.disabled @@ -8,4 +8,11 @@ class MapAddTotKOverworldMaps < ActiveRecord::Migration[7.0] ; SQL end + def down + execute <<-SQL + DELETE FROM `map` + WHERE `id` IN (30, 31) + ; + SQL + end end diff --git a/dev/db/migrate/disabled/20230406031615_submap_add_tot_k_overworld_submaps.rb.disabled b/dev/db/migrate/disabled/20230406031615_submap_add_tot_k_overworld_submaps.rb.disabled index 439625ee..be99e023 100644 --- a/dev/db/migrate/disabled/20230406031615_submap_add_tot_k_overworld_submaps.rb.disabled +++ b/dev/db/migrate/disabled/20230406031615_submap_add_tot_k_overworld_submaps.rb.disabled @@ -1,4 +1,8 @@ class SubmapAddTotKOverworldSubmaps < ActiveRecord::Migration[7.0] + def database + connection.instance_variable_get(:@config)[:database] + end + def up execute <<-SQL INSERT INTO `mapper` @@ -13,4 +17,33 @@ class SubmapAddTotKOverworldSubmaps < ActiveRecord::Migration[7.0] ; SQL end + def down + execute <<-SQL + DELETE FROM `submap` + WHERE `id` IN (2010, 2011) + ; + SQL + execute <<-SQL + DELETE FROM `mapper` + WHERE `id` = 3; + ; + SQL + + aiValue = ( + execute <<-SQL + SELECT `AUTO_INCREMENT` + FROM `INFORMATION_SCHEMA`.`TABLES` + WHERE `TABLE_SCHEMA` = '#{database}' + AND `TABLE_NAME` = 'mapper' + LIMIT 1 + ; + SQL + ).first.first + contentEntryAmount = 1 + execute <<-SQL + ALTER TABLE `mapper` + AUTO_INCREMENT=#{aiValue - contentEntryAmount} + ; + SQL + end end diff --git a/docs/Development.md b/docs/Development.md index b7bd8f40..e2ac0650 100644 --- a/docs/Development.md +++ b/docs/Development.md @@ -171,9 +171,9 @@ - `rake db:migrate RAILS_ENV=test` - To execute a specific up/down of one single migration - `rake db:migrate:up VERSION=20081220234130` - - To revert your last migration + - To revert the last migration - `rake db:rollback` - - To revert your last 3 , and migrations + - To revert the last 3 migrations - `rake db:rollback STEP=3` - Check which version of the tool you are currently using - `rake db:version` @@ -181,26 +181,26 @@ - `date -u "+%Y%m%d%H%M%S" | putclip` - Samples: - - ActiveRecord Ruby Code + - ActiveRecord Ruby Code: - Source: http://guides.rubyonrails.org/active_record_migrations.html - Source: https://www.ralfebert.de/snippets/ruby-rails/models-tables-migrations-cheat-sheet/ - Note: Main benefit is hopefully more terse and efficient syntax, but also applies to automatically handling bidirectional migration/rollback support with declarative styling. - Add column: - - `t.column :hidden, :boolean, null: false, default: 0, after: :version_patch` + - Code: `t.column :hidden, :boolean, null: false, default: 0, after: :version_patch` - Source: `dev/db/migrate/20230403193442_changelog_add_hidden_field_and_disable_blank_content.rb` - Changing Columns: - Source: https://guides.rubyonrails.org/active_record_migrations.html#changing-columns - Change table common code: - Source: https://guides.rubyonrails.org/active_record_migrations.html#changing-tables - Source: https://api.rubyonrails.org/v7.0.4.2/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-change_table - - `change_table :table_name do |t|` + - Code: `change_table :table_name do |t|` - Then refer to the specific section goal below with the reduced column variants. - Change column null property: - - `t.change_null :content, false` + - Code: `t.change_null :content, false` - Source: `dev/db/migrate/20230403193442_changelog_add_hidden_field_and_disable_blank_content.rb` - Change column default value: - - `t.change_default :marker_url, from: '/markers/', to: 'markers/'` - - `change_column_default(:table_name, :column_name, '')` + - Code (Table 'batch' block): `t.change_default :marker_url, from: '/markers/', to: 'markers/'` + - Code (Individual field): `change_column_default(:table_name, :column_name, '')` - Source: `dev/db/migrate/20230405195637_container_update_and_add_defaults.rb` - Source: https://api.rubyonrails.org/v7.0.4.2/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-change_column_default - Execute Raw SQL: @@ -219,6 +219,53 @@ ``` - Multiple Queries: - Add `.lines.each { |line| execute line if line != "\n" }` the string containing the queries separated by newlines. + - Specific Goals: + - Investigating/Debugging + - ``` + require 'pry'; binding.pry + exit + ``` + - Removing entries: + - Code (List/Array of Values): ``` + execute <<-SQL + DELETE FROM `submap` + WHERE `id` IN (2010, 2011) + ; + SQL + ``` + - Code (Condition): ``` + execute <<-SQL + DELETE FROM `mapper` + WHERE `id` = 3; + ; + SQL + ``` + - Source: `dev/db/migrate/disabled/20230406031615_submap_add_tot_k_overworld_submaps.rb` + - Reverting the auto increment value: + - Code: ``` + def database + connection.instance_variable_get(:@config)[:database] + end + ``` + ``` + aiValue = ( + execute <<-SQL + SELECT `AUTO_INCREMENT` + FROM `INFORMATION_SCHEMA`.`TABLES` + WHERE `TABLE_SCHEMA` = '#{database}' + AND `TABLE_NAME` = 'mapper' + LIMIT 1 + ; + SQL + ).first.first + contentEntryAmount = 1 + execute <<-SQL + ALTER TABLE `mapper` + AUTO_INCREMENT=#{aiValue - contentEntryAmount} + ; + SQL + ``` + - Source: `dev/db/migrate/disabled/20230406031615_submap_add_tot_k_overworld_submaps.rb` - Names: - Note: Try to keep migration files focused on specific tables. - Format: