From a76c64e571549868aa7e8cecbe8442a444833a21 Mon Sep 17 00:00:00 2001 From: Stephen Margheim Date: Tue, 10 Sep 2024 16:23:25 +0200 Subject: [PATCH] Simplify installer (#5) * Slim down installer and have it use a final schema instead of migrations * Update README to detail new installation * Update README.md Co-authored-by: Nick Pezza * Update README.md Co-authored-by: Nick Pezza * Update install_generator.rb --------- Co-authored-by: Nick Pezza --- README.md | 61 +++++++++++++------ ...240103034713_create_solid_cable_message.rb | 14 ----- db/migrate/20240607184711_index_channels.rb | 7 --- .../solid_cable/install/install_generator.rb | 41 +++++++++---- .../install/templates/cable_schema.rb | 13 ++++ 5 files changed, 85 insertions(+), 51 deletions(-) delete mode 100644 db/migrate/20240103034713_create_solid_cable_message.rb delete mode 100644 db/migrate/20240607184711_index_channels.rb create mode 100644 lib/generators/solid_cable/install/templates/cable_schema.rb diff --git a/README.md b/README.md index 5d6ecfb..4b61cee 100644 --- a/README.md +++ b/README.md @@ -20,19 +20,57 @@ Or install it yourself as: $ gem install solid_cable ``` -Now, you need to install the necessary migrations and configure Action Cable's adapter. +Now, you need to run the installer: ```bash $ bin/rails generate solid_cable:install ``` -If you want to install to a different database you can pass an env variable. -```bash -$ DATABASE=cable bin/rails generate solid_cable:install +This will create the `db/cable_schema.rb` file. + +You will then have to add the configuration for the database in `config/database.yml`. If you're using SQLite, it'll look something like this: + +```yaml +production: + primary: + <<: *default + database: storage/production.sqlite3 + cable: + <<: *default + database: storage/production_cable.sqlite3 + migrations_paths: db/cable_migrate +``` + +...or if you're using MySQL/PostgreSQL/Trilogy: + +```yaml +production: + primary: &primary_production + <<: *default + database: app_production + username: app + password: <%= ENV["APP_DATABASE_PASSWORD"] %> + cable: + <<: *primary_production + database: app_production_cable + migrations_paths: db/cable_migrate ``` -Update `config/cable.yml` to use the new adapter. connects_to is can be omitted -if you want to use the primary database. +> [!NOTE] +> Calling `bin/rails generate solid_cable:install` will automatically setup `config/cable.yml`, so no additional configuration is needed there (although you must make sure that you use the `cable` name in `database.yml` for this to match!). But if you want to use Solid Cable in a different environment (like staging or even development), you'll have to manually add that `connects_to` block to the respective environment in the `config/cable.yml` file. And, as always, make sure that the name you're using for the database in `config/cable.yml` matches the name you define in `config/database.yml`. + +Then run `db:prepare` in production to ensure the database is created and the schema is loaded. + +## Usage + +By default messages are kept around forever. SolidCable ships with a job to +prune messages. You can run `SolidCable::PruneJob.perform_later` which removes +Messages that are older than what is specified in `keep_messages_around_for` +setting. + +## Configuration + +All configuration is managed via the `config/cable.yml`file. To use Solid Cable, the `adapter` value *must be* `solid_cable`. When using Solid Cable, the other values you can set are: `connects_to`, `polling_interval`, `silence_polling`, and `keep_messages_around_for`. For example: ```yaml default: &default @@ -56,16 +94,5 @@ production: polling_interval: 0.1.seconds ``` -Finally, you need to run the migrations: - -```bash -$ bin/rails db:migrate -``` - -By default messages are kept around forever. SolidCable ships with a job to -prune messages. You can run `SolidCable::PruneJob.perform_later` which removes -Messages that are older than what is specified in `keep_messages_around_for` -setting. - ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/db/migrate/20240103034713_create_solid_cable_message.rb b/db/migrate/20240103034713_create_solid_cable_message.rb deleted file mode 100644 index 8627866..0000000 --- a/db/migrate/20240103034713_create_solid_cable_message.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class CreateSolidCableMessage < ActiveRecord::Migration[7.1] - def change - create_table :solid_cable_messages, if_not_exists: true do |t| - t.text :channel - t.text :payload - - t.timestamps - - t.index :created_at - end - end -end diff --git a/db/migrate/20240607184711_index_channels.rb b/db/migrate/20240607184711_index_channels.rb deleted file mode 100644 index 6325086..0000000 --- a/db/migrate/20240607184711_index_channels.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -class IndexChannels < ActiveRecord::Migration[7.1] - def change - add_index :solid_cable_messages, :channel, length: 500 - end -end diff --git a/lib/generators/solid_cable/install/install_generator.rb b/lib/generators/solid_cable/install/install_generator.rb index f328adf..3c87eb7 100644 --- a/lib/generators/solid_cable/install/install_generator.rb +++ b/lib/generators/solid_cable/install/install_generator.rb @@ -3,21 +3,36 @@ class SolidCable::InstallGenerator < Rails::Generators::Base source_root File.expand_path("templates", __dir__) - class_option :database, - type: :string, aliases: %i(--db), - desc: "The database for your migration. By default, the " \ - "current environment's primary database is used." - class_option :skip_migrations, type: :boolean, default: nil, - desc: "Skip migrations" + def add_solid_errors_db_schema + template "cable_schema.rb" + end + + def configure_production_cable + gsub_file("config/cable.yml", + old_production_cable_config, + new_production_cable_config) + end - def create_migrations - return if options[:skip_migrations] + private - db_clause = "DATABASE=#{options[:database]}" if options[:database].present? + def old_production_cable_config + <<~YAML + production: + adapter: redis + url: <%%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> + channel_prefix: <%= app_name %>_production + YAML + end - rails_command( - "railties:install:migrations FROM=solid_cable #{db_clause}".strip, - inline: true - ) + def new_production_cable_config + <<~YAML + production: + adapter: solid_cable + connects_to: + database: + writing: cable + polling_interval: 0.1.seconds + keep_messages_around_for: 1.day + YAML end end diff --git a/lib/generators/solid_cable/install/templates/cable_schema.rb b/lib/generators/solid_cable/install/templates/cable_schema.rb new file mode 100644 index 0000000..34f4cb8 --- /dev/null +++ b/lib/generators/solid_cable/install/templates/cable_schema.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +ActiveRecord::Schema[7.1].define(version: 1) do + create_table "solid_cable_messages", force: :cascade do |t| + t.text "channel" + t.text "payload" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["channel"], name: "index_solid_cable_messages_on_channel", + length: 500 + t.index ["created_at"], name: "index_solid_cable_messages_on_created_at" + end +end