Skip to content

Commit 605b23f

Browse files
authored
Merge pull request #15 from couchbase-examples/chore/streamline-dependencies
chore: Remove SQLite and streamline dependencies
2 parents bb899a9 + a74befe commit 605b23f

File tree

23 files changed

+342
-132
lines changed

23 files changed

+342
-132
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ jobs:
3838
fi
3939
echo "Couchbase configuration validated successfully"
4040
41+
- name: Setup Couchbase indexes
42+
run: RAILS_ENV=test bundle exec rake couchbase:setup_indexes
43+
4144
- name: Run integration tests
4245
run: bundle exec rspec spec/requests/api/v1
4346

Gemfile

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,9 @@ ruby '3.4.1'
55
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
66
gem 'rails', '~> 7.1.3', '>= 7.1.3.2'
77

8-
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
9-
gem 'sprockets-rails'
10-
11-
# Use sqlite3 as the database for Active Record
12-
gem 'sqlite3', '~> 1.4'
13-
148
# Use the Puma web server [https://github.com/puma/puma]
159
gem 'puma', '>= 5.0'
1610

17-
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
18-
gem 'importmap-rails'
19-
20-
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
21-
gem 'turbo-rails'
22-
23-
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
24-
gem 'stimulus-rails'
25-
2611
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
2712
gem 'jbuilder'
2813

@@ -72,7 +57,5 @@ group :development do
7257
end
7358

7459
group :test do
75-
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
76-
gem 'capybara'
77-
gem 'selenium-webdriver'
60+
# API-only testing with RSpec (no system/browser testing needed)
7861
end

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,91 @@ production:
9191
9292
> Note: The connection string expects the `couchbases://` or `couchbase://` part.
9393

94+
## Couchbase Index Management
95+
96+
This application requires specific N1QL indexes on the `travel-sample` bucket to function correctly. These indexes optimize the SQL++ queries used by the application for filtering and joining documents.
97+
98+
### Automatic Index Setup
99+
100+
Indexes are automatically created when you:
101+
102+
- Run `bin/setup` for local development setup
103+
- Run tests in CI/CD (GitHub Actions automatically creates indexes before running tests)
104+
105+
The application uses idempotent index creation (using `CREATE INDEX IF NOT EXISTS`), so it's safe to run the setup multiple times.
106+
107+
### Required Indexes
108+
109+
The application requires the following indexes on the `travel-sample` bucket:
110+
111+
1. **`idx_type`** - General index on the `type` field for all document queries
112+
2. **`idx_type_country`** - Index for airline queries filtered by country (`Airline.list_by_country_or_all`)
113+
3. **`idx_type_destinationairport`** - Index for route queries by destination airport (`Airline.to_airport`)
114+
4. **`idx_type_sourceairport_stops`** - Index for route queries by source airport and stops (`Route.direct_connections`)
115+
5. **`idx_type_airlineid`** - Index for airline queries by airline ID (used in joins with routes)
116+
117+
### Manual Index Management
118+
119+
You can manually manage indexes using the following Rake tasks:
120+
121+
#### Create All Required Indexes
122+
123+
```sh
124+
bundle exec rake couchbase:setup_indexes
125+
```
126+
127+
This command creates all required indexes on the `travel-sample` bucket. It's idempotent and safe to run multiple times.
128+
129+
#### List All Indexes
130+
131+
```sh
132+
bundle exec rake couchbase:list_indexes
133+
```
134+
135+
This command lists all indexes currently present in the `travel-sample` bucket, including their state, type, and indexed fields.
136+
137+
#### Drop Application Indexes
138+
139+
```sh
140+
bundle exec rake couchbase:drop_indexes
141+
```
142+
143+
This command drops all application-managed indexes. It requires confirmation before executing. For automated scripts, you can force the operation:
144+
145+
```sh
146+
FORCE_DROP=true bundle exec rake couchbase:drop_indexes
147+
```
148+
149+
> **Warning**: Use with caution! Dropping indexes will cause queries to fail until indexes are recreated.
150+
151+
### Troubleshooting Index Issues
152+
153+
If you encounter index-related errors:
154+
155+
1. **Verify indexes exist**:
156+
```sh
157+
bundle exec rake couchbase:list_indexes
158+
```
159+
160+
2. **Check index state**: Indexes should be in "online" state. If they're "building" or "pending", wait for them to complete.
161+
162+
3. **Recreate indexes**:
163+
```sh
164+
bundle exec rake couchbase:drop_indexes
165+
bundle exec rake couchbase:setup_indexes
166+
```
167+
168+
4. **Check permissions**: Ensure your Couchbase user has "Query Manage Index" permission to create and drop indexes.
169+
170+
### Index Creation in CI/CD
171+
172+
The GitHub Actions workflow automatically creates indexes before running tests. If index creation fails in CI:
173+
174+
1. Check the "Setup Couchbase indexes" step in the GitHub Actions log
175+
2. Verify that `DB_CONN_STR`, `DB_USERNAME`, and `DB_PASSWORD` secrets/variables are correctly set
176+
3. Ensure the Couchbase user has "Query Manage Index" permission
177+
4. Check that the Couchbase cluster is accessible from GitHub Actions runners
178+
94179
## Running The Application
95180

96181
### Directly on machine

app/channels/application_cable/channel.rb

Lines changed: 0 additions & 4 deletions
This file was deleted.

app/channels/application_cable/connection.rb

Lines changed: 0 additions & 4 deletions
This file was deleted.

app/javascript/application.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

app/javascript/controllers/application.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/javascript/controllers/hello_controller.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

app/javascript/controllers/index.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

app/models/airline.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ class Airline < CouchbaseOrm::Base
2929
}
3030

3131
n1ql :to_airport, query_fn: proc { |bucket, values, options|
32-
cluster.query("SELECT raw META(air).id FROM (SELECT DISTINCT META(airline).id AS airlineId FROM `#{bucket.name}` AS route JOIN `#{bucket.name}` AS airline ON route.airlineid = META(airline).id WHERE route.destinationairport = #{quote(values[0])}) AS subquery JOIN `#{bucket.name}` AS air ON META(air).id = subquery.airlineId LIMIT #{values[1]} OFFSET #{values[2]}", options)
32+
cluster.query("SELECT raw META(air).id FROM (SELECT DISTINCT META(airline).id AS airlineId FROM `#{bucket.name}` AS route JOIN `#{bucket.name}` AS airline ON route.airlineid = META(airline).id AND airline.type = 'airline' WHERE route.type = 'route' AND route.destinationairport = #{quote(values[0])} ORDER BY META(airline).id) AS subquery JOIN `#{bucket.name}` AS air ON META(air).id = subquery.airlineId AND air.type = 'airline' LIMIT #{values[1]} OFFSET #{values[2]}", options)
3333
}
3434
end

0 commit comments

Comments
 (0)