diff --git a/dev/db/migrate/20230509201500_add_tot_k_data.rb b/dev/db/migrate/20230509201500_add_tot_k_data.rb new file mode 100644 index 00000000..1aeda7d4 --- /dev/null +++ b/dev/db/migrate/20230509201500_add_tot_k_data.rb @@ -0,0 +1,81 @@ +class AddTotKData < ActiveRecord::Migration[7.0] + def sqlFileNames + [ + '1-container' , + '2-map' , + '3-submap' , + '4-marker_category' , + '5-marker' + ] + end + + def database + connection.instance_variable_get(:@config)[:database] + end + + def up + filePathPattern = "#{__dir__}/sql/" + + File.basename(__FILE__). + sub(/\.rb$/i, '/%s.sql') + + sqlFileNames.each do |sqlFileName| + sqlFile = sprintf(filePathPattern, sqlFileName); + execute File.open(sqlFile).read + end + end + + def down + execute <<-SQL + DELETE FROM `marker` + WHERE `marker_category_id` BETWEEN 2100 AND 2300 + ; + SQL + execute <<-SQL + ALTER TABLE `marker` + AUTO_INCREMENT=12522 + ; + SQL + + execute <<-SQL + DELETE FROM `marker_category` + WHERE `id` BETWEEN 2100 AND 2300 + AND `parent_id` IS NOT NULL + ; + SQL + execute <<-SQL + DELETE FROM `marker_category` + WHERE `id` BETWEEN 2100 AND 2300 + AND `parent_id` IS NULL + ; + SQL + execute <<-SQL + ALTER TABLE `marker_category` + AUTO_INCREMENT=1970 + ; + SQL + + execute <<-SQL + DELETE FROM `submap` + WHERE `map_id` BETWEEN 2101 AND 2103 + ; + SQL + + execute <<-SQL + DELETE FROM `map` + WHERE `id` BETWEEN 2101 AND 2103 + ; + SQL + + execute <<-SQL + DELETE FROM `container` + WHERE `id` = 21 + ; + SQL + execute <<-SQL + ALTER TABLE `container` + AUTO_INCREMENT=20 + ; + SQL + + end +end diff --git a/docs/Development.md b/docs/Development.md index 0b4091b1..92bc00e6 100644 --- a/docs/Development.md +++ b/docs/Development.md @@ -160,7 +160,7 @@ - Common Commands: - Create a new migration: - - `rake db:new_migration name=(read -P 'Migration Proper Name: ')` + - `rake db:new_migration name=(read -P 'Migration Proper Name: ' | tr -d ' ')` - Change name subshell to read `read | tr ' ' '_'` on Windows to prevent carriage return characters causing problems. - Then edit content with your features' details. - To apply your newest migration: @@ -212,11 +212,33 @@ SQL ``` - External file: - - ``` + - Code: ``` execute File.open( 'dev/db/migrate/sql/20210811040936_users_add_new_placeholders_for_new_marker_contributions.sql' ).read ``` + - External files: + - Code: ``` + def sqlFileNames + [ + '1-container' , + '2-map' , + '3-submap' , + '4-marker_category' , + '5-marker' + ] + end + ``` + ``` + filePathPattern = "#{__dir__}/sql/" + + File.basename(__FILE__). + sub(/\.rb$/i, '/%s.sql') + + sqlFileNames.each do |sqlFileName| + sqlFile = sprintf(filePathPattern, sqlFileName); + execute File.open(sqlFile).read + end + ``` - Multiple Queries: - Add `.lines.each { |line| execute line if line != "\n" }` the string containing the queries separated by newlines. - Specific Goals: @@ -242,7 +264,8 @@ ``` - Source: `dev/db/migrate/disabled/20230406031615_submap_add_tot_k_overworld_submaps.rb` - Reverting the auto increment value: - - Code: ``` + - Note: Be sure to check if the table actually has auto increment column(s). + - Code (Dynamic): ``` def database connection.instance_variable_get(:@config)[:database] end @@ -265,6 +288,13 @@ ; SQL ``` + - Code (Manual): ``` + execute <<-SQL + ALTER TABLE `mapper` + AUTO_INCREMENT=... + ; + SQL + ``` - Source: `dev/db/migrate/disabled/20230406031615_submap_add_tot_k_overworld_submaps.rb` - Names: - Note: The RoR AR Migrations guide may recommend trying to keep migrations focused on a specific table per file.