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

Simplify installer #5

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cable_schema.rb ends up in the app's root folder instead of the db folder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. That's not good.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1.0.5.


You will then have to add the configuration for the errors database in `config/database.yml`. If you're using sqlite, it'll look something like this:
fractaledmind marked this conversation as resolved.
Show resolved Hide resolved

```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`.
fractaledmind marked this conversation as resolved.
Show resolved Hide resolved

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 "db/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