From 5e070385be72236545ec416bfe39811566c9f97b Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Sat, 17 Aug 2024 18:24:46 -0400 Subject: [PATCH 1/8] Support branch specific databases for dev MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Following instruction from fractaledmind in this post: https://fractaledmind.github.io/2023/09/06/enhancing-rails-sqlite-branch-databases/ I‘m only adding this feature for development of the primary data store. I don‘t anticipate cache, queue, and cable databases needing to change often so leaving them out of branch-specific support for the time being. --- config/database.yml | 2 +- config/environments/development.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/config/database.yml b/config/database.yml index 13ca5c9b..1ff250f5 100644 --- a/config/database.yml +++ b/config/database.yml @@ -16,7 +16,7 @@ default: &default development: primary: <<: *default - database: storage/development/data.sqlite3 + database: storage/development/data-<%= (`git branch --show-current`.chomp || 'default').parameterize %>.sqlite3 pragmas: mmap_size: <%= 128 * 1024 * 1024 %> # 128MB cache: diff --git a/config/environments/development.rb b/config/environments/development.rb index e8cfb393..3c632978 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -92,4 +92,10 @@ config.solid_queue.connects_to = {database: {writing: :queue, reading: :queue}} config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "debug") + + # With branch-specific databases for development defined in config/database.yml + # we need to prepare the databases before starting the server + config.after_initialize do + ActiveRecord::Tasks::DatabaseTasks.prepare_all + end end From 6c92b6e5e89e04553bd00c601d1d91515b7b0d19 Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Sat, 17 Aug 2024 18:34:42 -0400 Subject: [PATCH 2/8] Add comment --- config/database.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/database.yml b/config/database.yml index 1ff250f5..7244516e 100644 --- a/config/database.yml +++ b/config/database.yml @@ -16,6 +16,8 @@ default: &default development: primary: <<: *default + # Why read the branch name? So we can support having a separate database for each branch in development. + # For more info, see: https://fractaledmind.github.io/2023/09/06/enhancing-rails-sqlite-branch-databases/ database: storage/development/data-<%= (`git branch --show-current`.chomp || 'default').parameterize %>.sqlite3 pragmas: mmap_size: <%= 128 * 1024 * 1024 %> # 128MB From 340890246965965690f987fa2c2562864971bee4 Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Sat, 17 Aug 2024 18:35:55 -0400 Subject: [PATCH 3/8] Add task to prune dev data --- lib/tasks/development/prune.rake | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 lib/tasks/development/prune.rake diff --git a/lib/tasks/development/prune.rake b/lib/tasks/development/prune.rake new file mode 100644 index 00000000..8d000e36 --- /dev/null +++ b/lib/tasks/development/prune.rake @@ -0,0 +1,7 @@ +namespace :development do + desc "Prune branch-specific databases" + task :prune_data do + Dir["storage/development/data-*.sqlite3"] + .each { |file| File.delete(file) && puts("Deleted #{file}") } + end +end From 4224b9b3e9024cc21ae437bfd704c5af62d742be Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Sat, 17 Aug 2024 18:47:29 -0400 Subject: [PATCH 4/8] Add fixture file for specs --- spec/fixtures/example_file_for_app_file_test.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 spec/fixtures/example_file_for_app_file_test.rb diff --git a/spec/fixtures/example_file_for_app_file_test.rb b/spec/fixtures/example_file_for_app_file_test.rb new file mode 100644 index 00000000..6087ed47 --- /dev/null +++ b/spec/fixtures/example_file_for_app_file_test.rb @@ -0,0 +1,5 @@ +class ExampleFileForAppFileTest + def hello + puts "Hello World" + end +end From 342f4f7dbf25987c5604a668fb5cf297f6692831 Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Sat, 17 Aug 2024 18:49:25 -0400 Subject: [PATCH 5/8] Update app file spec --- db/cable_schema.rb | 1 + db/cache_schema.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/db/cable_schema.rb b/db/cable_schema.rb index 8927d13d..4622ffde 100644 --- a/db/cable_schema.rb +++ b/db/cable_schema.rb @@ -20,4 +20,5 @@ t.index ["channel_hash"], name: "index_solid_cable_messages_on_channel_hash" t.index ["created_at"], name: "index_solid_cable_messages_on_created_at" end + end diff --git a/db/cache_schema.rb b/db/cache_schema.rb index b72288a4..0ccb5eac 100644 --- a/db/cache_schema.rb +++ b/db/cache_schema.rb @@ -29,4 +29,5 @@ t.index ["key_hash", "byte_size"], name: "index_solid_cache_entries_on_key_hash_and_byte_size" t.index ["key_hash"], name: "index_solid_cache_entries_on_key_hash", unique: true end + end From e381448545b25b451020cc59071587f57a1ee4d5 Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Sat, 17 Aug 2024 22:13:56 -0400 Subject: [PATCH 6/8] Extract seeding of ColorSchemes, add to db seed --- app/models/color_schemes/seed.rb | 37 +++++++++++++++++++++++++++++++ lib/tasks/color_schemes/seed.rake | 14 +----------- 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 app/models/color_schemes/seed.rb diff --git a/app/models/color_schemes/seed.rb b/app/models/color_schemes/seed.rb new file mode 100644 index 00000000..0799fe03 --- /dev/null +++ b/app/models/color_schemes/seed.rb @@ -0,0 +1,37 @@ +class ColorSchemes::Seed + def seed_all + seed_1500 + seed_ui + end + + def seed_ui + hex_json_uicolors = JSON.parse(File.read("./script/colors/data/uicolors-palette-hex.json")) + hex_json_uicolors.each do |name, weights| + custom_name = "Custom #{name.titleize}" + ColorScheme.find_or_create_by!(name: custom_name) do |cs| + weights.each do |weight, css| + cs.set_weight(weight, css) + end + end + end + end + + def seed_1500 + # Generate ColorScheme rows from precalcated JSON + # "hex json" files are expected to represent JSON objects of color scales: { name: { weight: color, ... }, ... + + if !File.exist?("./script/colors/tmp/1500-palette-hex.json") + puts "Run `rake color_schemes:generate_1500` first to generate 1500-palette-hex.json" + exit + end + + hex_json_1500 = JSON.parse(File.read("./script/colors/tmp/1500-palette-hex.json")) + hex_json_1500.each do |name, weights| + ColorScheme.find_or_create_by!(name: name) do |cs| + weights.each do |weight, css| + cs.set_weight(weight, css) + end + end + end + end +end diff --git a/lib/tasks/color_schemes/seed.rake b/lib/tasks/color_schemes/seed.rake index abb5bef4..57c59f90 100644 --- a/lib/tasks/color_schemes/seed.rake +++ b/lib/tasks/color_schemes/seed.rake @@ -1,19 +1,7 @@ namespace :color_schemes do desc "Seed color schemes from JSON files" task seed: :environment do - # Generate ColorScheme rows from precalcated JSON - # "hex json" files are expected to represent JSON objects of color scales: { name: { weight: color, ... }, ... - - if !File.exist?("./script/colors/tmp/1500-palette-hex.json") - puts "Run `rake color_schemes:generate_1500` first to generate 1500-palette-hex.json" - exit - end - - hex_json_1500 = JSON.parse(File.read("./script/colors/tmp/1500-palette-hex.json")) - ColorScheme.bulk_load(hex_json_1500) - - hex_json_uicolors = JSON.parse(File.read("./script/colors/data/uicolors-palette-hex.json")) - ColorSchme.bulk_load(hex_json_uicolors) { |name| "Custom #{name.titleize}" } + ColorSchemes::Seed.new.seed_all end task :generate_1500 do From 13bafa04410ec3691faac1065d29616a506dd10a Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Mon, 2 Sep 2024 12:17:54 -0400 Subject: [PATCH 7/8] Ignore additional db schemas from standard linting --- .standard.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.standard.yml b/.standard.yml index 5c27f38f..e67afac7 100644 --- a/.standard.yml +++ b/.standard.yml @@ -5,3 +5,4 @@ ignore: - '.git/**/*' - 'build/**/*' - 'rubies/**/*' + - 'db/*schema.rb' From 1e8c14d17877e5ab2733193c7778aa52eaa54afd Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Thu, 5 Sep 2024 09:40:43 -0400 Subject: [PATCH 8/8] Add specs for color scheme seed --- spec/models/color_schemes/seed_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 spec/models/color_schemes/seed_spec.rb diff --git a/spec/models/color_schemes/seed_spec.rb b/spec/models/color_schemes/seed_spec.rb new file mode 100644 index 00000000..ed3e6758 --- /dev/null +++ b/spec/models/color_schemes/seed_spec.rb @@ -0,0 +1,17 @@ +require "rails_helper" + +RSpec.describe ColorSchemes::Seed do + describe "#seed_all" do + it "creates color scheme data and is idempotent" do + aggregate_failures do + expect { + described_class.new.seed_all + }.to change(ColorScheme, :count).by_at_least 100 + + expect { + described_class.new.seed_all + }.not_to change(ColorScheme, :count) + end + end + end +end