|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +namespace :couchbase do |
| 4 | + desc 'Setup required Couchbase indexes for the application' |
| 5 | + task setup_indexes: :environment do |
| 6 | + puts '=== Setting up Couchbase indexes ===' |
| 7 | + |
| 8 | + # Get cluster connection from any model (they all share the same cluster) |
| 9 | + cluster = Airline.cluster |
| 10 | + bucket_name = Airline.bucket.name |
| 11 | + |
| 12 | + puts "Target bucket: #{bucket_name}" |
| 13 | + |
| 14 | + # Define all required indexes based on N1QL queries in models |
| 15 | + indexes = [ |
| 16 | + { |
| 17 | + name: 'idx_type', |
| 18 | + fields: ['type'], |
| 19 | + description: 'Index on type field for all document queries' |
| 20 | + }, |
| 21 | + { |
| 22 | + name: 'idx_type_country', |
| 23 | + fields: ['type', 'country'], |
| 24 | + where: "type = 'airline'", |
| 25 | + description: 'Index for airline queries by country (Airline.list_by_country_or_all)' |
| 26 | + }, |
| 27 | + { |
| 28 | + name: 'idx_type_destinationairport', |
| 29 | + fields: ['type', 'destinationairport'], |
| 30 | + where: "type = 'route'", |
| 31 | + description: 'Index for route queries by destination airport (Airline.to_airport)' |
| 32 | + }, |
| 33 | + { |
| 34 | + name: 'idx_type_sourceairport_stops', |
| 35 | + fields: ['type', 'sourceairport', 'stops'], |
| 36 | + where: "type = 'route'", |
| 37 | + description: 'Index for route queries by source airport and stops (Route.direct_connections)' |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'idx_type_airlineid', |
| 41 | + fields: ['type', 'airlineid'], |
| 42 | + where: "type = 'airline'", |
| 43 | + description: 'Index for airline queries by airline ID (Airline.to_airport join)' |
| 44 | + } |
| 45 | + ] |
| 46 | + |
| 47 | + created_count = 0 |
| 48 | + skipped_count = 0 |
| 49 | + failed_indexes = [] |
| 50 | + |
| 51 | + indexes.each do |index_def| |
| 52 | + begin |
| 53 | + puts "\nCreating index: #{index_def[:name]}" |
| 54 | + puts " Description: #{index_def[:description]}" |
| 55 | + |
| 56 | + # Build the CREATE INDEX query with IF NOT EXISTS for idempotency |
| 57 | + query = build_create_index_query(bucket_name, index_def) |
| 58 | + puts " Query: #{query}" |
| 59 | + |
| 60 | + # Execute the query |
| 61 | + cluster.query(query) |
| 62 | + |
| 63 | + # If no error, index is ready |
| 64 | + created_count += 1 |
| 65 | + puts " \u2713 Index created or already exists" |
| 66 | + |
| 67 | + rescue Couchbase::Error::IndexExists => e |
| 68 | + # This shouldn't happen with IF NOT EXISTS, but handle it anyway |
| 69 | + puts " \u2299 Index already exists (skipped)" |
| 70 | + skipped_count += 1 |
| 71 | + rescue Couchbase::Error::CouchbaseError => e |
| 72 | + puts " \u2717 Failed: #{e.message}" |
| 73 | + failed_indexes << { name: index_def[:name], error: e.message } |
| 74 | + rescue StandardError => e |
| 75 | + puts " \u2717 Unexpected error: #{e.message}" |
| 76 | + failed_indexes << { name: index_def[:name], error: e.message } |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + # Print summary |
| 81 | + puts "\n=== Index Setup Summary ===" |
| 82 | + puts "Total indexes: #{indexes.count}" |
| 83 | + puts "Successfully processed: #{created_count}" |
| 84 | + puts "Skipped (already existed): #{skipped_count}" |
| 85 | + puts "Failed: #{failed_indexes.count}" |
| 86 | + |
| 87 | + if failed_indexes.any? |
| 88 | + puts "\n=== Failed Indexes ===" |
| 89 | + failed_indexes.each do |failed| |
| 90 | + puts " - #{failed[:name]}: #{failed[:error]}" |
| 91 | + end |
| 92 | + |
| 93 | + # Exit with error code for CI |
| 94 | + exit 1 |
| 95 | + else |
| 96 | + puts "\n\u2713 All indexes are ready!" |
| 97 | + end |
| 98 | + rescue Couchbase::Error::AuthenticationFailure => e |
| 99 | + puts "\n\u2717 Authentication failed: #{e.message}" |
| 100 | + puts "Please verify your Couchbase credentials:" |
| 101 | + puts " - DB_USERNAME environment variable" |
| 102 | + puts " - DB_PASSWORD environment variable" |
| 103 | + exit 1 |
| 104 | + rescue Couchbase::Error::CouchbaseError => e |
| 105 | + puts "\n\u2717 Couchbase connection error: #{e.message}" |
| 106 | + puts "Please check your connection settings:" |
| 107 | + puts " - DB_CONN_STR environment variable" |
| 108 | + puts " - DB_USERNAME environment variable" |
| 109 | + puts " - DB_PASSWORD environment variable" |
| 110 | + puts " - Network connectivity to Couchbase cluster" |
| 111 | + exit 1 |
| 112 | + rescue StandardError => e |
| 113 | + puts "\n\u2717 Unexpected error: #{e.class} - #{e.message}" |
| 114 | + puts e.backtrace.first(5).join("\n") |
| 115 | + exit 1 |
| 116 | + end |
| 117 | + |
| 118 | + desc 'Drop all application indexes (use with caution!)' |
| 119 | + task drop_indexes: :environment do |
| 120 | + puts '=== Dropping Couchbase indexes ===' |
| 121 | + puts 'WARNING: This will drop all application indexes!' |
| 122 | + |
| 123 | + unless ENV['FORCE_DROP'] == 'true' |
| 124 | + print 'Are you sure? (yes/no): ' |
| 125 | + confirmation = $stdin.gets.chomp |
| 126 | + unless confirmation.downcase == 'yes' |
| 127 | + puts 'Aborted.' |
| 128 | + exit 0 |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + cluster = Airline.cluster |
| 133 | + bucket_name = Airline.bucket.name |
| 134 | + |
| 135 | + index_names = [ |
| 136 | + 'idx_type', |
| 137 | + 'idx_type_country', |
| 138 | + 'idx_type_destinationairport', |
| 139 | + 'idx_type_sourceairport_stops', |
| 140 | + 'idx_type_airlineid' |
| 141 | + ] |
| 142 | + |
| 143 | + dropped_count = 0 |
| 144 | + not_found_count = 0 |
| 145 | + |
| 146 | + index_names.each do |index_name| |
| 147 | + begin |
| 148 | + query = "DROP INDEX `#{bucket_name}`.`#{index_name}`" |
| 149 | + cluster.query(query) |
| 150 | + puts " \u2713 Dropped index: #{index_name}" |
| 151 | + dropped_count += 1 |
| 152 | + rescue Couchbase::Error::IndexNotFound |
| 153 | + puts " \u2299 Index not found (already dropped): #{index_name}" |
| 154 | + not_found_count += 1 |
| 155 | + rescue StandardError => e |
| 156 | + puts " \u2717 Failed to drop #{index_name}: #{e.message}" |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + puts "\n=== Drop Summary ===" |
| 161 | + puts "Dropped: #{dropped_count}" |
| 162 | + puts "Not found: #{not_found_count}" |
| 163 | + puts "\n\u2713 Index cleanup complete!" |
| 164 | + end |
| 165 | + |
| 166 | + desc 'List all indexes in the bucket' |
| 167 | + task list_indexes: :environment do |
| 168 | + puts '=== Couchbase Indexes ===' |
| 169 | + |
| 170 | + cluster = Airline.cluster |
| 171 | + bucket_name = Airline.bucket.name |
| 172 | + |
| 173 | + query = "SELECT idx.* FROM system:indexes AS idx WHERE idx.keyspace_id = '#{bucket_name}' ORDER BY idx.name" |
| 174 | + result = cluster.query(query) |
| 175 | + |
| 176 | + if result.rows.empty? |
| 177 | + puts "No indexes found in bucket '#{bucket_name}'" |
| 178 | + else |
| 179 | + puts "Indexes in bucket '#{bucket_name}':\n" |
| 180 | + result.rows.each do |row| |
| 181 | + puts " Name: #{row['name']}" |
| 182 | + puts " State: #{row['state']}" |
| 183 | + puts " Type: #{row['using']}" |
| 184 | + puts " Keys: #{row['index_key']}" |
| 185 | + puts " Condition: #{row['condition']}" if row['condition'] |
| 186 | + puts "" |
| 187 | + end |
| 188 | + puts "Total: #{result.rows.count} indexes" |
| 189 | + end |
| 190 | + rescue StandardError => e |
| 191 | + puts "\u2717 Error listing indexes: #{e.message}" |
| 192 | + exit 1 |
| 193 | + end |
| 194 | + |
| 195 | + # Private helper method to build CREATE INDEX query |
| 196 | + def build_create_index_query(bucket_name, index_def) |
| 197 | + query = "CREATE INDEX IF NOT EXISTS `#{index_def[:name]}` ON `#{bucket_name}`" |
| 198 | + |
| 199 | + # Add fields |
| 200 | + fields = index_def[:fields].map { |f| "`#{f}`" }.join(', ') |
| 201 | + query += "(#{fields})" |
| 202 | + |
| 203 | + # Add WHERE clause if present |
| 204 | + query += " WHERE #{index_def[:where]}" if index_def[:where] |
| 205 | + |
| 206 | + query |
| 207 | + end |
| 208 | +end |
0 commit comments