card 1.105.6 → 1.106.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/config/initializers/02_patches/active_record.rb +2 -2
  4. data/db/schema.rb +5 -13
  5. data/lib/card/codename.rb +8 -0
  6. data/lib/card/director/store.rb +1 -1
  7. data/lib/card/model/save_helper/save_helper_helper.rb +6 -2
  8. data/lib/card/query/abstract_query/tie.rb +14 -0
  9. data/lib/card/query/card_query/interpretation.rb +26 -3
  10. data/lib/card/query/reference_query.rb +4 -4
  11. data/lib/card/set.rb +27 -0
  12. data/lib/cardio/generators/base.rb +1 -1
  13. data/lib/cardio/migration/assumption.rb +29 -0
  14. data/lib/cardio/migration/schema.rb +12 -0
  15. data/lib/cardio/migration/stamp.rb +31 -0
  16. data/lib/cardio/migration/transform.rb +44 -0
  17. data/lib/cardio/migration.rb +60 -74
  18. data/lib/cardio/mod/eat/edibles.rb +8 -2
  19. data/lib/cardio/mod/eat.rb +2 -1
  20. data/lib/cardio/mod/sow.rb +3 -1
  21. data/lib/cardio/mod.rb +5 -2
  22. data/lib/cardio/railtie.rb +6 -5
  23. data/lib/cardio/seed.rb +3 -6
  24. data/lib/generators/migration/USAGE +10 -11
  25. data/lib/generators/migration/migration_generator.rb +26 -24
  26. data/lib/generators/migration/templates/card_migration.erb +1 -3
  27. data/mod/core/data/fixtures/real/transform_migrations.yml +351 -0
  28. data/mod/core/data/recode.yml +7 -0
  29. data/{db/migrate → mod/core/data/schema}/20110511221913_require_earlier_migrations.rb +3 -3
  30. data/{db/migrate → mod/core/data/schema}/20120105203350_require_1_8_migrations.rb +1 -1
  31. data/{db/migrate → mod/core/data/schema}/20121111025347_require_1_10_migrations.rb +1 -1
  32. data/{db/migrate → mod/core/data/schema}/20121118114000_split_link_type.rb +1 -1
  33. data/{db/migrate → mod/core/data/schema}/20121118115000_update_link_type.rb +1 -1
  34. data/{db/migrate → mod/core/data/schema}/20130106052640_table_cleanup.rb +1 -1
  35. data/{db/migrate → mod/core/data/schema}/20130109015336_trunk_left.rb +1 -1
  36. data/{db/migrate → mod/core/data/schema}/20140822073704_create_new_revision_tables.rb +1 -1
  37. data/mod/core/data/schema/20141001105348_move_revisions_to_actions.rb +87 -0
  38. data/{db/migrate → mod/core/data/schema}/20141216053032_better_index_names.rb +10 -4
  39. data/{db/migrate → mod/core/data/schema}/20150724210803_add_comment_to_actions.rb +1 -1
  40. data/{db/migrate → mod/core/data/schema}/20151105225559_more_space_for_db_content.rb +1 -1
  41. data/{db/migrate → mod/core/data/schema}/20160122153608_new_indices.rb +1 -1
  42. data/{db/migrate → mod/core/data/schema}/20170908114442_create_delayed_jobs.rb +1 -1
  43. data/{db/migrate → mod/core/data/schema}/20170908114452_increase_text_size_for_delayed_jobs.rb +1 -1
  44. data/{db/migrate → mod/core/data/schema}/20180514152037_add_card_virtuals_table.rb +1 -1
  45. data/{db/migrate → mod/core/data/schema}/20190116170824_add_left_key_to_card_virtuals.rb +1 -1
  46. data/{db/migrate → mod/core/data/schema}/20190131223248_add_codename_index.rb +1 -1
  47. data/{db/migrate → mod/core/data/schema}/20190929202325_rename_present_in_reference_table.rb +1 -1
  48. data/{db/migrate → mod/core/data/schema}/20200718051236_compound_names_do_not_store_name_and_key.rb +1 -1
  49. data/{db/migrate → mod/core/data/schema}/20200805200729_add_unique_pair_indices.rb +1 -3
  50. data/{db/migrate → mod/core/data/schema}/20211128040849_virtuals_updated_at.rb +1 -1
  51. data/{db/migrate → mod/core/data/schema}/20221031182227_remove_referee_key_requirement.rb +1 -1
  52. data/mod/core/data/test.yml +4 -4
  53. data/{db/migrate_core_cards → mod/core/data/transform}/20120611203506_rails_inflection_updates.rb +10 -6
  54. data/mod/core/data/transform/20130411191151_renaming_for_menu.rb +17 -0
  55. data/mod/core/data/transform/20130920214038_jsonize_tinymce.rb +28 -0
  56. data/{db/migrate_core_cards → mod/core/data/transform}/20130920291703_update_stylesheets.rb +1 -1
  57. data/mod/core/data/transform/20140307231621_user_data_to_cards.rb +40 -0
  58. data/{db/migrate_core_cards → mod/core/data/transform}/20140317035504_account_requests_to_signups.rb +1 -1
  59. data/{db/migrate_core_cards → mod/core/data/transform}/20141204061304_watchers_to_following.rb +1 -1
  60. data/{db/migrate_core_cards → mod/core/data/transform}/20141208132416_partial_reference_type.rb +1 -1
  61. data/{db/migrate_core_cards → mod/core/data/transform}/20150220134731_following_to_follow_rule.rb +1 -1
  62. data/{db/migrate_core_cards → mod/core/data/transform}/20150429090551_search_card_context.rb +1 -1
  63. data/{db/migrate_core_cards → mod/core/data/transform}/20150510031118_fix_skin_codenames.rb +2 -2
  64. data/{db/migrate_core_cards → mod/core/data/transform}/20150724123438_update_file_and_image_cards.rb +2 -2
  65. data/{db/migrate_core_cards → mod/core/data/transform}/20150807205221_create_references_for_search_cards.rb +1 -1
  66. data/mod/core/data/transform/20150824135418_update_file_history.rb +33 -0
  67. data/{db/migrate_core_cards → mod/core/data/transform}/20160811115836_rename_stats_to_admin.rb +1 -1
  68. data/{db/migrate_core_cards → mod/core/data/transform}/20160914132636_fix_mod_files.rb +1 -1
  69. data/{db/migrate_core_cards → mod/core/data/transform}/20161103154836_update_keys.rb +3 -3
  70. data/{db/migrate_core_cards → mod/core/data/transform}/20170830210517_correct_signup_success_message.rb +1 -1
  71. data/{db/migrate_core_cards → mod/core/data/transform}/20180905061537_migrate_layouts.rb +1 -1
  72. data/{db/migrate_core_cards → mod/core/data/transform}/20190125125150_add_script_rules_card.rb +1 -1
  73. data/{db/migrate_core_cards → mod/core/data/transform}/20190320091257_upgrade_recaptcha_to_v3.rb +1 -1
  74. data/{db/migrate_core_cards → mod/core/data/transform}/20190322095534_remove_toolbar_cards.rb +1 -1
  75. data/{db/migrate_core_cards → mod/core/data/transform}/20190502130029_add_shark_and_help_desk_role.rb +2 -1
  76. data/{db/migrate_core_cards → mod/core/data/transform}/20190528131330_enabled_roles.rb +1 -1
  77. data/{db/migrate_core_cards → mod/core/data/transform}/20190625153616_pointer_overhaul.rb +1 -1
  78. data/{db/migrate_core_cards → mod/core/data/transform}/20190718140126_add_guides.rb +1 -1
  79. data/{db/migrate_core_cards → mod/core/data/transform}/20190822093633_move_help_text_to_code.rb +3 -1
  80. data/{db/migrate_core_cards → mod/core/data/transform}/20190829205148_remove_add_help.rb +1 -1
  81. data/mod/core/data/transform/20190902193208_input_type.rb +10 -0
  82. data/{db/migrate_core_cards → mod/core/data/transform}/20190904174403_token_upgrade.rb +2 -2
  83. data/{db/migrate_core_cards → mod/core/data/transform}/20190909104250_add_cardtype_input_types.rb +2 -2
  84. data/{db/migrate_core_cards → mod/core/data/transform}/20191115160748_history_cleanup.rb +1 -1
  85. data/mod/core/data/transform/20230502094848_repair_all_references.rb +7 -0
  86. data/mod/core/lib/tasks/card/migrate.rake +79 -114
  87. data/mod/core/lib/tasks/card.rake +2 -4
  88. metadata +67 -71
  89. data/db/migrate/20130411210957_update_codenames.rb +0 -15
  90. data/db/migrate/20141001105348_move_revisions_to_actions.rb +0 -70
  91. data/db/migrate/20141121172918_rename_card_migration_table.rb +0 -15
  92. data/db/migrate/20141208132159_remove_present_from_reference_table.rb +0 -9
  93. data/db/migrate_core_cards/20130411191151_renaming_for_menu.rb +0 -47
  94. data/db/migrate_core_cards/20130920214038_jsonize_tinymce.rb +0 -15
  95. data/db/migrate_core_cards/20140307231621_user_data_to_cards.rb +0 -28
  96. data/db/migrate_core_cards/20150824135418_update_file_history.rb +0 -24
  97. data/db/migrate_core_cards/20190902193208_input_type.rb +0 -15
  98. data/db/migrate_core_cards/20230502094848_repair_all_references.rb +0 -7
  99. data/lib/cardio/migration/core.rb +0 -11
  100. data/lib/cardio/migration/deck.rb +0 -0
  101. data/lib/cardio/migration/deck_structure.rb +0 -19
  102. data/lib/cardio/schema.rb +0 -96
  103. data/mod/core/data/fixtures/real/schema_migrations_core_cards.yml +0 -347
  104. data/mod/core/data/fixtures/real/schema_migrations_deck.yml +0 -1
  105. data/mod/core/data/fixtures/real/schema_migrations_deck_cards.yml +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19bf849da74214f9ae14d6bc7310c0015209de781b3b9fa89c509a0665167971
4
- data.tar.gz: 83e55e8f3b6b6791f861938f594dd897fb0ac817fd0be4b922dbe71b707fa435
3
+ metadata.gz: 5a8e1bc39a0131940fbec71fd5a92601c334c895d67d4781e8b03c109d7e0f59
4
+ data.tar.gz: 14bbcc9ac8d90d9a4f3a361710510d43f516ebe7a147f028c31b6cf903b3fd67
5
5
  SHA512:
6
- metadata.gz: ed6f5f0ecd5bfcbf75efeb9f3d9c152e33433e0b6581a3b81971932ef819099cd3157fedb2ad752e3509fe5a4298cec4d9bbfed823fbd5a57d29e71ea0252b90
7
- data.tar.gz: b661938ed367e6f58818107288baf4e248c2ce0725a2c777da2ee30b02b33902a441844aeb1ba81cd677c0774473010b2a241d6e320a2b094b50eb48dc147ba3
6
+ metadata.gz: d215a5fa5a0557d60a9a17ee7a6a4ee134206281ea96c7d3c3665be0f81756fc251055ea27a52c7f0ec2652cc2b91b559006f17816d43594c0fbebcee1c7baf9
7
+ data.tar.gz: e2af61cd07919b7759d9af5f66507f06d5320d8b430974ffa296512466e7d61fd4b5bb56fe2af3120ac0928c19420daa53156cce7fee2b1b3244bfcd434da582
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.15.6
1
+ 0.16.0
@@ -121,8 +121,8 @@ module Patches
121
121
  module Migration
122
122
  module ClassMethods
123
123
  def check_pending! connection=::ActiveRecord::Base.connection
124
- %i[structure core_cards deck deck_cards].each do |migration_type|
125
- Cardio::Schema.mode(migration_type) do |paths|
124
+ %i[schema transform].each do |migration_type|
125
+ Cardio::Migration.new_for(migration_type).mode do |paths|
126
126
  ::ActiveRecord::Migrator.migrations_paths = paths
127
127
  super
128
128
  end
data/db/schema.rb CHANGED
@@ -114,19 +114,6 @@ ActiveRecord::Schema.define(version: 2022_10_31_182227) do
114
114
  t.index ["priority", "run_at"], name: "delayed_jobs_priority"
115
115
  end
116
116
 
117
- create_table "schema_migrations_core_cards", id: false, charset: "utf8mb3", force: :cascade do |t|
118
- t.string "version", null: false
119
- t.index ["version"], name: "unique_schema_migrations_cards", unique: true
120
- end
121
-
122
- create_table "schema_migrations_deck", primary_key: "version", id: :string, charset: "utf8mb3", force: :cascade do |t|
123
- end
124
-
125
- create_table "schema_migrations_deck_cards", id: false, charset: "utf8mb3", force: :cascade do |t|
126
- t.string "version", null: false
127
- t.index ["version"], name: "unique_schema_migrations_deck_cards", unique: true
128
- end
129
-
130
117
  create_table "sessions", id: :integer, charset: "utf8mb3", force: :cascade do |t|
131
118
  t.string "session_id"
132
119
  t.text "data"
@@ -134,6 +121,11 @@ ActiveRecord::Schema.define(version: 2022_10_31_182227) do
134
121
  t.index ["session_id"], name: "sessions_session_id_index"
135
122
  end
136
123
 
124
+ create_table "transform_migrations", id: false, charset: "utf8mb3", force: :cascade do |t|
125
+ t.string "version", null: false
126
+ t.index ["version"], name: "unique_schema_migrations_cards", unique: true
127
+ end
128
+
137
129
  create_table "users", id: :integer, charset: "utf8mb3", force: :cascade do |t|
138
130
  t.string "login", limit: 40
139
131
  t.string "email", limit: 100
data/lib/card/codename.rb CHANGED
@@ -95,6 +95,14 @@ class Card
95
95
  end
96
96
  end
97
97
 
98
+ def recode oldcode, newcode
99
+ return unless id(oldcode) && !id(newcode)
100
+
101
+ puts "recode #{oldcode}, #{newcode}"
102
+ Card.where(codename: oldcode).take.update_column :codename, newcode
103
+ reset_cache
104
+ end
105
+
98
106
  private
99
107
 
100
108
  # iterate through every card with a codename
@@ -7,7 +7,7 @@ class Card
7
7
  @after_store << block
8
8
  end
9
9
 
10
- protected
10
+ # protected
11
11
 
12
12
  def after_store?
13
13
  @after_store.present?
@@ -21,8 +21,12 @@ class Card
21
21
  method_name, cardtype_card = extract_cardtype_from_method_name method
22
22
  return super unless method_name
23
23
 
24
- sargs = standardize_args(*args)
25
- send "#{method_name}_card", sargs.merge(type_id: cardtype_card.id)
24
+ sargs = standardize_args(*args).merge(type_id: cardtype_card.id)
25
+ if method_name == "ensure"
26
+ Card.ensure sargs
27
+ else
28
+ send "#{method_name}_card", sargs
29
+ end
26
30
  end
27
31
 
28
32
  def respond_to_missing? method, _include_private=false
@@ -97,6 +97,20 @@ class Card
97
97
  when String, Symbol then val.card_id || -999
98
98
  end
99
99
  end
100
+
101
+ def op_and_id_or_ids_from_val val
102
+ if (single_id = id_from_val val)
103
+ "= #{single_id}"
104
+ elsif list_of_ids? val
105
+ "in (#{val.map { |v| id_from_val v }.join ', '})"
106
+ end
107
+ end
108
+
109
+ def list_of_ids? val
110
+ return unless val.is_a? Array
111
+
112
+ !val.find { |v| !id_from_val v }
113
+ end
100
114
  end
101
115
  end
102
116
  end
@@ -80,6 +80,7 @@ class Card
80
80
  def relate key, val, opts={}
81
81
  multiple = opts[:multiple].nil? ? val.is_a?(Array) : opts[:multiple]
82
82
  method = opts[:method] || :send
83
+
83
84
  if multiple
84
85
  relate_multi_value method, key, val
85
86
  else
@@ -87,15 +88,37 @@ class Card
87
88
  end
88
89
  end
89
90
 
91
+ private
92
+
90
93
  def relate_multi_value method, key, val
91
94
  conj = conjunction(val.first) ? conjunction(val.shift) : :and
92
- if conj == current_conjunction
95
+ if as_list_of_ids?(conj, key, val)
96
+ relate key, val, multiple: false
97
+ elsif conj == current_conjunction
93
98
  # same conjunction as container, no need for subcondition
94
- val.each { |v| send method, key, v }
99
+ relate_multi_value_without_subcondition method, key, val
95
100
  else
96
- send conj, (val.map { |v| { key => v } })
101
+ relate_multi_value_with_subcondition key, conj, val
97
102
  end
98
103
  end
104
+
105
+ def relate_multi_value_with_subcondition key, conj, val
106
+ send conj, (val.map { |v| { key => v } })
107
+ end
108
+
109
+ def relate_multi_value_without_subcondition method, key, val
110
+ val.each { |v| send method, key, v }
111
+ end
112
+
113
+ # the #list_of_ids optimization is intended to avoid unnecessary joins and
114
+ # can probably be applied more broadly, but in the name of caution, we went
115
+ # with an initial implementation that would only apply to reference attributes
116
+ # (because reference_query can handle lists of values)
117
+ def as_list_of_ids? conj, key, val
118
+ (conj == :or) &&
119
+ key.to_s.start_with?(/refer|nest|include|link|member/) &&
120
+ list_of_ids?(val)
121
+ end
99
122
  end
100
123
  end
101
124
  end
@@ -26,8 +26,8 @@ class Card
26
26
  def add_outfield_condition outfield, outcard
27
27
  if outcard == "_none"
28
28
  non_outfield
29
- elsif (id = id_from_val(outcard))
30
- outfield_id outfield, id
29
+ elsif (op_and_id = op_and_id_or_ids_from_val(outcard))
30
+ outfield_id outfield, op_and_id
31
31
  else
32
32
  tie :card, outcard, from: outfield
33
33
  end
@@ -37,8 +37,8 @@ class Card
37
37
  add_condition "#{fld :is_present} = 0"
38
38
  end
39
39
 
40
- def outfield_id outfield, id
41
- add_condition "#{fld(outfield)} = #{id}"
40
+ def outfield_id outfield, op_and_id
41
+ add_condition "#{fld(outfield)} #{op_and_id}"
42
42
  end
43
43
 
44
44
  def add_reftype_condition reftype
data/lib/card/set.rb CHANGED
@@ -90,7 +90,34 @@ class Card
90
90
  # ...then mycard will include the set modules associated with each of those sets in the
91
91
  # above order.
92
92
  #
93
+ # ### Abstract set modules
93
94
  #
95
+ # Suppose you have code that you'd like to reuse in more than one set.
96
+ #
97
+ # Well, set modules are just ruby, so it's possible to just define a standard ruby
98
+ # module (eg `module MySimpleModule...`) in a lib directory and then include that
99
+ # set (`include MySimpleModule`). That will work just fine so long as you only want
100
+ # to add simple ruby code and include it in the base ruby module. But what if you
101
+ # want the reusable code to use the DSL? What if you want to define reusable events,
102
+ # for example? Or if you want to define reusable views on formats?
103
+ #
104
+ # For this purpose, you can use _abstract set modules_. These are modules that use
105
+ # the set DSL but are not defined directly onto a specific set. Instead, they can
106
+ # be included in a set using the `include_set` command.
107
+ #
108
+ # For example, suppose you create a file at `mod/biz/set/abstract/special_views.rb`.
109
+ # And within that file you define a view such as the following:
110
+ #
111
+ # format :html do
112
+ # view :bizzy do
113
+ # "I'm so busy"
114
+ # end
115
+ # end
116
+ #
117
+ # This will create an abstract set that can be included in any other set by invoking
118
+ # `include_set Card::Set::Abstract::SpecialViews`. Or just `include_set
119
+ # Abstract::SpecialViews` for short. And then the including set will have access to the
120
+ # "bizzy" view.
94
121
  module Set
95
122
  include Event::Api
96
123
  include Trait
@@ -3,7 +3,7 @@
3
3
  module Cardio
4
4
  module Generators
5
5
  # adds Cardio::Generators::ClassMethods to standard rails generator base class.
6
- class Base < ::ActiveRecord::Generators::Base
6
+ class Base < ::Rails::Generators::Base
7
7
  extend ClassMethods
8
8
  end
9
9
  end
@@ -0,0 +1,29 @@
1
+ module Cardio
2
+ class Migration
3
+ # methods for assuming migration states
4
+ module Assumption
5
+ def assume_current
6
+ context do |mc|
7
+ versions = mc.migrations.map(&:version)
8
+ migrated = mc.get_all_versions
9
+ to_mark = versions - migrated
10
+ mark_as_migrated to_mark if to_mark.present?
11
+ end
12
+ end
13
+
14
+ def assume_migrated_upto_version version=nil
15
+ mode do |_paths|
16
+ version ||= self.version
17
+ ActiveRecord::Schema.assume_migrated_upto_version version
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def mark_as_migrated versions
24
+ sql = connection.send :insert_versions_sql, versions
25
+ connection.execute sql
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ require "cardio/migration"
2
+
3
+ module Cardio
4
+ class Migration
5
+ # for migrations involving database schema definitions
6
+ class Schema < Migration
7
+ @migration_type = :schema
8
+ @old_tables = []
9
+ @old_deck_table = "schema_migrations_deck"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ module Cardio
2
+ class Migration
3
+ # methods for stamping migration versions to files
4
+ module Stamp
5
+ def stamp
6
+ mode do
7
+ return unless (version = stampable_version) && (file = stamp_file)
8
+ puts ">> writing version: #{version} to #{file.path}"
9
+ file.puts version
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def stamp_file
16
+ ::File.open stamp_path, "w"
17
+ end
18
+
19
+ def stampable_version
20
+ version = ActiveRecord::Migrator.current_version
21
+ version.to_i.positive? && version
22
+ end
23
+
24
+ def stamp_path
25
+ stamp_dir = ENV["SCHEMA_STAMP_PATH"] || File.join(Cardio.root, "db")
26
+
27
+ File.join stamp_dir, "version_#{migration_type}.txt"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ require "cardio/migration"
2
+
3
+ module Cardio
4
+ class Migration
5
+ # for migrations involving data transformations (but not schema changes)
6
+ class Transform < Migration
7
+ include Card::Model::SaveHelper unless ENV["NO_CARD_LOAD"]
8
+
9
+ @migration_type = :transform
10
+ @old_tables = %w[schema_migrations_core_cards schema_migrations_cards]
11
+ @old_deck_table = "schema_migrations_deck_cards"
12
+
13
+ private
14
+
15
+ def with_migration_table
16
+ self.table_name = "transform_migrations"
17
+ yield
18
+ ensure
19
+ self.table_name = "schema_migrations"
20
+ end
21
+
22
+ # Execute this migration in the named direction
23
+ # override ActiveRecord to wrap 'up' in 'contentedly'
24
+ def exec_migration conn, direction
25
+ return super if respond_to? :change
26
+
27
+ @connection = conn
28
+ contentedly { send direction }
29
+ ensure
30
+ @connection = nil
31
+ end
32
+
33
+ def contentedly
34
+ return yield if ENV["NO_CARD_LOAD"]
35
+ Card::Cache.reset_all
36
+ Card::Auth.as_bot do
37
+ yield
38
+ ensure
39
+ ::Card::Cache.reset_all
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -2,113 +2,99 @@
2
2
 
3
3
  module Cardio
4
4
  class Migration < ActiveRecord::Migration[6.1]
5
- include Card::Model::SaveHelper unless ENV["NO_CARD_LOAD"]
6
- @type = :deck_cards
5
+ include Assumption
6
+ include Stamp
7
7
 
8
8
  class << self
9
- # Rake tasks use class methods, migrations use instance methods.
10
- # To avoid repetition a lot of instance methods here just call class
11
- # methods.
12
- # The subclass Card::CoreMigration needs a different @type so we can't use a
13
- # class variable @@type. It has to be a class instance variable.
14
- # Migrations are subclasses of Cardio::Migration or Card::CoreMigration
15
- # but they don't inherit the @type. The method below solves this problem.
16
- def type
17
- @type || ancestors[1]&.type
18
- end
9
+ attr_reader :migration_type, :old_tables, :old_deck_table
19
10
 
20
- def find_unused_name base_name
21
- test_name = base_name
22
- add = 1
23
- while Card.exists?(test_name)
24
- test_name = "#{base_name}#{add}"
25
- add += 1
26
- end
27
- test_name
11
+ def migration_class type
12
+ type == :schema ? Migration::Schema : Migration::Transform
28
13
  end
29
14
 
30
- def migration_paths mig_type=type
31
- Schema.migration_paths mig_type
15
+ def new_for type
16
+ migration_class(type).new
32
17
  end
33
18
 
34
- def schema_suffix mig_type=type
35
- Schema.suffix mig_type
19
+ def port_all
20
+ %i[schema transform].each do |type|
21
+ migration_class(type).port
22
+ end
36
23
  end
37
24
 
38
- def schema_mode mig_type=type, &block
39
- Schema.mode mig_type, &block
25
+ def port
26
+ return unless connection.table_exists? old_deck_table
27
+ rename_old_tables
28
+ connection.execute "INSERT INTO #{table} (SELECT * from #{old_deck_table})"
29
+ connection.drop_table old_deck_table
40
30
  end
41
31
 
42
- def assume_migrated_upto_version
43
- Schema.assume_migrated_upto_version type
44
- end
32
+ private
45
33
 
46
- def assume_current
47
- migration_context do |mc|
48
- versions = mc.migrations.map(&:version)
49
- migrated = mc.get_all_versions
50
- to_mark = versions - migrated
51
- mark_as_migrated to_mark if to_mark.present?
34
+ def rename_old_tables
35
+ old_tables.each do |old_table_name|
36
+ next unless connection.table_exists? old_table_name
37
+ connection.rename_table old_table_name, table
52
38
  end
53
39
  end
54
40
 
55
- def data_path filename=nil
56
- File.join([migration_paths.first, "data", filename].compact)
57
- end
58
-
59
- private
60
-
61
- def mark_as_migrated versions
62
- sql = connection.send :insert_versions_sql, versions
63
- connection.execute sql
41
+ def table
42
+ "#{migration_type}_migrations"
64
43
  end
65
44
 
66
45
  def connection
67
46
  ActiveRecord::Base.connection
68
47
  end
48
+ end
69
49
 
70
- def migration_context &block
71
- Schema.migration_context type, &block
72
- end
50
+ def migration_type
51
+ self.class.migration_type || :schema
73
52
  end
74
53
 
75
- def contentedly
76
- return yield if ENV["NO_CARD_LOAD"]
77
- Card::Cache.reset_all
78
- Schema.mode "" do
79
- Card::Auth.as_bot do
80
- yield
81
- ensure
82
- ::Card::Cache.reset_all
83
- end
54
+ def run version=nil, verbose=true
55
+ context do |mc|
56
+ ActiveRecord::Migration.verbose = verbose
57
+ mc.migrate version
84
58
  end
85
59
  end
86
60
 
87
- def data_path filename=nil
88
- self.class.data_path filename
61
+ def version
62
+ path = stamp_path
63
+ File.exist?(path) ? File.read(path).strip : nil
89
64
  end
90
65
 
91
- # Execute this migration in the named direction
92
- # copied from ActiveRecord to wrap 'up' in 'contentedly'
93
- def exec_migration conn, direction
94
- @connection = conn
95
- if respond_to?(:change)
96
- if direction == :down
97
- revert { change }
98
- else
99
- change
100
- end
101
- else
102
- contentedly { send(direction) }
66
+ def migration_paths
67
+ Cardio.paths["data/#{migration_type}"].existent.to_a
68
+ end
69
+
70
+ def context
71
+ mode do |paths|
72
+ yield ActiveRecord::MigrationContext.new(paths, ActiveRecord::SchemaMigration)
103
73
  end
104
- ensure
105
- @connection = nil
74
+ end
75
+
76
+ def mode
77
+ with_migration_table { yield migration_paths }
106
78
  end
107
79
 
108
80
  def down
109
81
  raise ActiveRecord::IrreversibleMigration
110
82
  end
83
+
84
+ private
85
+
86
+ def connection
87
+ Cardio::Migration.connection
88
+ end
89
+
90
+ def with_migration_table
91
+ yield
92
+ end
93
+
94
+ def table_name= table_name
95
+ ActiveRecord::Base.schema_migrations_table_name = table_name
96
+ ActiveRecord::SchemaMigration.table_name = table_name
97
+ ActiveRecord::SchemaMigration.reset_column_information
98
+ end
111
99
  end
112
100
  end
113
-
114
- require "cardio/migration/core"
@@ -71,14 +71,20 @@ module Cardio
71
71
  end
72
72
 
73
73
  # for processing that needs to happen on all cards, including fields
74
- def each_card_hash items
74
+ def each_card_hash items, &block
75
75
  items.each do |item|
76
+ raise Card::Error, "inedible pod data: #{item}" unless item.is_a? Hash
77
+
76
78
  yield item
77
- item[:fields]&.values&.each { |val| yield val if val.is_a? Hash }
79
+ process_fields item, &block
78
80
  end
79
81
  items
80
82
  end
81
83
 
84
+ def process_fields item
85
+ item[:fields]&.values&.each { |val| yield val if val.is_a? Hash }
86
+ end
87
+
82
88
  def handle_attachments mod, hash
83
89
  each_attachment hash do |key, filename|
84
90
  hash[key] = mod_file mod, filename
@@ -25,7 +25,6 @@ module Cardio
25
25
  track edible do
26
26
  current_user edible.delete(:user)
27
27
  time_machine edible.delete(:time) do
28
- # binding.pry if edible[:type] == :link_list
29
28
  Card.ensure edible
30
29
  end
31
30
  end
@@ -47,6 +46,8 @@ module Cardio
47
46
 
48
47
  def track edible
49
48
  rescuing edible do
49
+ # n = edible[:name]
50
+ # binding.pry if (n.is_a? Array) && n.first.to_s == "main_menu"
50
51
  # puts "eating: #{edible}" if @verbose
51
52
  card = yield
52
53
  puts "eaten: #{card.name}".cyan if @verbose
@@ -114,7 +114,9 @@ module Cardio
114
114
  end
115
115
 
116
116
  def old_data
117
- YAML.safe_load File.read(filename), [Symbol] if File.exist? filename
117
+ return unless File.exist? filename
118
+ # YAML.safe_load File.read(filename), [Symbol] if File.exist? filename
119
+ YAML.safe_load File.read(filename), permitted_classes: [Symbol]
118
120
  end
119
121
 
120
122
  # @return Path
data/lib/cardio/mod.rb CHANGED
@@ -59,9 +59,12 @@ module Cardio
59
59
  "mod_#{name}"
60
60
  end
61
61
 
62
- def subpath *parts
62
+ def subpath *parts, force: false
63
63
  path = File.join [@path] + parts
64
- path if File.exist? path
64
+ return path if File.exist? path
65
+ return unless force
66
+
67
+ FileUtils.mkdir_p path
65
68
  end
66
69
 
67
70
  def tmp_dir type
@@ -65,14 +65,11 @@ module Cardio
65
65
 
66
66
  p.add "lib/graph_q_l/types/query.rb"
67
67
  p.add "mod-data"
68
+ p.add "data/schema"
69
+ p.add "data/transform"
68
70
 
69
71
  p.add "db", with: "#{card_root}/db"
70
72
  p.add "db/seeds.rb", with: "#{card_root}/db/seeds.rb"
71
- p.add "db/migrate", with: "#{card_root}/db/migrate"
72
- p.add "db/migrate_core_cards", with: "#{card_root}/db/migrate_core_cards"
73
-
74
- p.add "db/migrate_deck", with: "db/migrate"
75
- p.add "db/migrate_deck_cards", with: "db/migrate_cards"
76
73
 
77
74
  Cardio::Mod.dirs.each do |mod_path|
78
75
  c.autoload_paths += Dir["#{mod_path}/lib"]
@@ -83,7 +80,11 @@ module Cardio
83
80
  p["config/initializers"] << "#{mod_path}/config/early"
84
81
  p["late/initializers"] << "#{mod_path}/config/late"
85
82
  p["lib/tasks"] << "#{mod_path}/lib/tasks"
83
+
86
84
  p["mod-data"] << "#{mod_path}/data"
85
+ p["data/schema"] << "#{mod_path}/data/schema"
86
+ p["data/transform"] << "#{mod_path}/data/transform"
87
+
87
88
  p["config/locales"] << "#{mod_path}/config/locales"
88
89
  end
89
90
 
data/lib/cardio/seed.rb CHANGED
@@ -2,8 +2,7 @@ module Cardio
2
2
  # methods in support of seeding
3
3
  module Seed
4
4
  TABLES = %w[cards card_actions card_acts card_changes card_references
5
- schema_migrations schema_migrations_core_cards
6
- schema_migrations_deck schema_migrations_deck_cards].freeze
5
+ schema_migrations transform_migrations].freeze
7
6
 
8
7
  class << self
9
8
  def default_path
@@ -23,10 +22,8 @@ module Cardio
23
22
 
24
23
  return unless update_seed?
25
24
 
26
- Cardio::Migration.assume_current
27
- Cardio::Migration::Core.assume_current
28
- Cardio::Migration::DeckStructure.assume_current
29
- # Cardio::Migration::Core.assume_current
25
+ Cardio::Migration::Schema.new.assume_current
26
+ Cardio::Migration::Transform.new.assume_current
30
27
  end
31
28
 
32
29
  def dump