Skip to content

Commit

Permalink
Reverting proof of concept TotK database migrations
Browse files Browse the repository at this point in the history
since it does not match new data.
Added helpful gems, migration down/rollback code, and doc, keeping the old files to use as future samples.
No better ActiveRecord code since we don't have models classes for tables..
Thought pry had an auto require feature usable from the gemfile that was largely applicable =/..
  • Loading branch information
Pysis868 committed May 9, 2023
1 parent bd51216 commit c5b30ad
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -71,6 +79,8 @@ PLATFORMS

DEPENDENCIES
mysql2
pry
pry-byebug
standalone_migrations

BUNDLED WITH
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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
63 changes: 55 additions & 8 deletions docs/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,36 +171,36 @@
- `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`
- Manually generate a timestamp:
- `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, '<defaultValue>')`
- Code (Table 'batch' block): `t.change_default :marker_url, from: '/markers/', to: 'markers/'`
- Code (Individual field): `change_column_default(:table_name, :column_name, '<defaultValue>')`
- 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:
Expand All @@ -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:
Expand Down

0 comments on commit c5b30ad

Please sign in to comment.