Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch specific databases #195

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ ignore:
- '.git/**/*'
- 'build/**/*'
- 'rubies/**/*'
- 'db/*schema.rb'
37 changes: 37 additions & 0 deletions app/models/color_schemes/seed.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ default: &default
development:
primary:
<<: *default
database: storage/development/data.sqlite3
# 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
cache:
Expand Down
6 changes: 6 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions db/cable_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions db/cache_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 1 addition & 13 deletions lib/tasks/color_schemes/seed.rake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/tasks/development/prune.rake
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/fixtures/example_file_for_app_file_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ExampleFileForAppFileTest
def hello
puts "Hello World"
end
end
17 changes: 17 additions & 0 deletions spec/models/color_schemes/seed_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading