Skip to content

Commit

Permalink
Simplify installer (#5)
Browse files Browse the repository at this point in the history
* 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 <pezza@hey.com>

* Update README.md

Co-authored-by: Nick Pezza <pezza@hey.com>

* Update install_generator.rb

---------

Co-authored-by: Nick Pezza <pezza@hey.com>
  • Loading branch information
fractaledmind and npezza93 authored Sep 10, 2024
1 parent 5faf524 commit a76c64e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 51 deletions.
61 changes: 44 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
14 changes: 0 additions & 14 deletions db/migrate/20240103034713_create_solid_cable_message.rb

This file was deleted.

7 changes: 0 additions & 7 deletions db/migrate/20240607184711_index_channels.rb

This file was deleted.

41 changes: 28 additions & 13 deletions lib/generators/solid_cable/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions lib/generators/solid_cable/install/templates/cable_schema.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a76c64e

Please sign in to comment.