htm 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/.architecture/decisions/adrs/001-use-postgresql-timescaledb-storage.md +227 -0
  3. data/.architecture/decisions/adrs/002-two-tier-memory-architecture.md +322 -0
  4. data/.architecture/decisions/adrs/003-ollama-default-embedding-provider.md +339 -0
  5. data/.architecture/decisions/adrs/004-multi-robot-shared-memory-hive-mind.md +374 -0
  6. data/.architecture/decisions/adrs/005-rag-based-retrieval-with-hybrid-search.md +443 -0
  7. data/.architecture/decisions/adrs/006-context-assembly-strategies.md +444 -0
  8. data/.architecture/decisions/adrs/007-working-memory-eviction-strategy.md +461 -0
  9. data/.architecture/decisions/adrs/008-robot-identification-system.md +550 -0
  10. data/.architecture/decisions/adrs/009-never-forget-explicit-deletion-only.md +570 -0
  11. data/.architecture/decisions/adrs/010-redis-working-memory-rejected.md +323 -0
  12. data/.architecture/decisions/adrs/011-database-side-embedding-generation-with-pgai.md +585 -0
  13. data/.architecture/decisions/adrs/012-llm-driven-ontology-topic-extraction.md +583 -0
  14. data/.architecture/decisions/adrs/013-activerecord-orm-and-many-to-many-tagging.md +299 -0
  15. data/.architecture/decisions/adrs/014-client-side-embedding-generation-workflow.md +569 -0
  16. data/.architecture/decisions/adrs/015-hierarchical-tag-ontology-and-llm-extraction.md +701 -0
  17. data/.architecture/decisions/adrs/016-async-embedding-and-tag-generation.md +694 -0
  18. data/.architecture/members.yml +144 -0
  19. data/.architecture/reviews/2025-10-29-llm-configuration-and-async-processing-review.md +1137 -0
  20. data/.architecture/reviews/initial-system-analysis.md +330 -0
  21. data/.envrc +32 -0
  22. data/.irbrc +145 -0
  23. data/CHANGELOG.md +150 -0
  24. data/COMMITS.md +196 -0
  25. data/LICENSE +21 -0
  26. data/README.md +1347 -0
  27. data/Rakefile +51 -0
  28. data/SETUP.md +268 -0
  29. data/config/database.yml +67 -0
  30. data/db/migrate/20250101000001_enable_extensions.rb +14 -0
  31. data/db/migrate/20250101000002_create_robots.rb +14 -0
  32. data/db/migrate/20250101000003_create_nodes.rb +42 -0
  33. data/db/migrate/20250101000005_create_tags.rb +38 -0
  34. data/db/migrate/20250101000007_add_node_vector_indexes.rb +30 -0
  35. data/db/schema.sql +473 -0
  36. data/db/seed_data/README.md +100 -0
  37. data/db/seed_data/presidents.md +136 -0
  38. data/db/seed_data/states.md +151 -0
  39. data/db/seeds.rb +208 -0
  40. data/dbdoc/README.md +173 -0
  41. data/dbdoc/public.node_stats.md +48 -0
  42. data/dbdoc/public.node_stats.svg +41 -0
  43. data/dbdoc/public.node_tags.md +40 -0
  44. data/dbdoc/public.node_tags.svg +112 -0
  45. data/dbdoc/public.nodes.md +54 -0
  46. data/dbdoc/public.nodes.svg +118 -0
  47. data/dbdoc/public.nodes_tags.md +39 -0
  48. data/dbdoc/public.nodes_tags.svg +112 -0
  49. data/dbdoc/public.ontology_structure.md +48 -0
  50. data/dbdoc/public.ontology_structure.svg +38 -0
  51. data/dbdoc/public.operations_log.md +42 -0
  52. data/dbdoc/public.operations_log.svg +130 -0
  53. data/dbdoc/public.relationships.md +39 -0
  54. data/dbdoc/public.relationships.svg +41 -0
  55. data/dbdoc/public.robot_activity.md +46 -0
  56. data/dbdoc/public.robot_activity.svg +35 -0
  57. data/dbdoc/public.robots.md +35 -0
  58. data/dbdoc/public.robots.svg +90 -0
  59. data/dbdoc/public.schema_migrations.md +29 -0
  60. data/dbdoc/public.schema_migrations.svg +26 -0
  61. data/dbdoc/public.tags.md +35 -0
  62. data/dbdoc/public.tags.svg +60 -0
  63. data/dbdoc/public.topic_relationships.md +45 -0
  64. data/dbdoc/public.topic_relationships.svg +32 -0
  65. data/dbdoc/schema.json +1437 -0
  66. data/dbdoc/schema.svg +154 -0
  67. data/docs/api/database.md +806 -0
  68. data/docs/api/embedding-service.md +532 -0
  69. data/docs/api/htm.md +797 -0
  70. data/docs/api/index.md +259 -0
  71. data/docs/api/long-term-memory.md +1096 -0
  72. data/docs/api/working-memory.md +665 -0
  73. data/docs/architecture/adrs/001-postgresql-timescaledb.md +314 -0
  74. data/docs/architecture/adrs/002-two-tier-memory.md +411 -0
  75. data/docs/architecture/adrs/003-ollama-embeddings.md +421 -0
  76. data/docs/architecture/adrs/004-hive-mind.md +437 -0
  77. data/docs/architecture/adrs/005-rag-retrieval.md +531 -0
  78. data/docs/architecture/adrs/006-context-assembly.md +496 -0
  79. data/docs/architecture/adrs/007-eviction-strategy.md +645 -0
  80. data/docs/architecture/adrs/008-robot-identification.md +625 -0
  81. data/docs/architecture/adrs/009-never-forget.md +648 -0
  82. data/docs/architecture/adrs/010-redis-working-memory-rejected.md +323 -0
  83. data/docs/architecture/adrs/011-pgai-integration.md +494 -0
  84. data/docs/architecture/adrs/index.md +215 -0
  85. data/docs/architecture/hive-mind.md +736 -0
  86. data/docs/architecture/index.md +351 -0
  87. data/docs/architecture/overview.md +538 -0
  88. data/docs/architecture/two-tier-memory.md +873 -0
  89. data/docs/assets/css/custom.css +83 -0
  90. data/docs/assets/images/htm-core-components.svg +63 -0
  91. data/docs/assets/images/htm-database-schema.svg +93 -0
  92. data/docs/assets/images/htm-hive-mind-architecture.svg +125 -0
  93. data/docs/assets/images/htm-importance-scoring-framework.svg +83 -0
  94. data/docs/assets/images/htm-layered-architecture.svg +71 -0
  95. data/docs/assets/images/htm-long-term-memory-architecture.svg +115 -0
  96. data/docs/assets/images/htm-working-memory-architecture.svg +120 -0
  97. data/docs/assets/images/htm.jpg +0 -0
  98. data/docs/assets/images/htm_demo.gif +0 -0
  99. data/docs/assets/js/mathjax.js +18 -0
  100. data/docs/assets/videos/htm_video.mp4 +0 -0
  101. data/docs/database_rake_tasks.md +322 -0
  102. data/docs/development/contributing.md +787 -0
  103. data/docs/development/index.md +336 -0
  104. data/docs/development/schema.md +596 -0
  105. data/docs/development/setup.md +719 -0
  106. data/docs/development/testing.md +819 -0
  107. data/docs/guides/adding-memories.md +824 -0
  108. data/docs/guides/context-assembly.md +1009 -0
  109. data/docs/guides/getting-started.md +577 -0
  110. data/docs/guides/index.md +118 -0
  111. data/docs/guides/long-term-memory.md +941 -0
  112. data/docs/guides/multi-robot.md +866 -0
  113. data/docs/guides/recalling-memories.md +927 -0
  114. data/docs/guides/search-strategies.md +953 -0
  115. data/docs/guides/working-memory.md +717 -0
  116. data/docs/index.md +214 -0
  117. data/docs/installation.md +477 -0
  118. data/docs/multi_framework_support.md +519 -0
  119. data/docs/quick-start.md +655 -0
  120. data/docs/setup_local_database.md +302 -0
  121. data/docs/using_rake_tasks_in_your_app.md +383 -0
  122. data/examples/basic_usage.rb +93 -0
  123. data/examples/cli_app/README.md +317 -0
  124. data/examples/cli_app/htm_cli.rb +270 -0
  125. data/examples/custom_llm_configuration.rb +183 -0
  126. data/examples/example_app/Rakefile +71 -0
  127. data/examples/example_app/app.rb +206 -0
  128. data/examples/sinatra_app/Gemfile +21 -0
  129. data/examples/sinatra_app/app.rb +335 -0
  130. data/lib/htm/active_record_config.rb +113 -0
  131. data/lib/htm/configuration.rb +342 -0
  132. data/lib/htm/database.rb +594 -0
  133. data/lib/htm/embedding_service.rb +115 -0
  134. data/lib/htm/errors.rb +34 -0
  135. data/lib/htm/job_adapter.rb +154 -0
  136. data/lib/htm/jobs/generate_embedding_job.rb +65 -0
  137. data/lib/htm/jobs/generate_tags_job.rb +82 -0
  138. data/lib/htm/long_term_memory.rb +965 -0
  139. data/lib/htm/models/node.rb +109 -0
  140. data/lib/htm/models/node_tag.rb +33 -0
  141. data/lib/htm/models/robot.rb +52 -0
  142. data/lib/htm/models/tag.rb +76 -0
  143. data/lib/htm/railtie.rb +76 -0
  144. data/lib/htm/sinatra.rb +157 -0
  145. data/lib/htm/tag_service.rb +135 -0
  146. data/lib/htm/tasks.rb +38 -0
  147. data/lib/htm/version.rb +5 -0
  148. data/lib/htm/working_memory.rb +182 -0
  149. data/lib/htm.rb +400 -0
  150. data/lib/tasks/db.rake +19 -0
  151. data/lib/tasks/htm.rake +147 -0
  152. data/lib/tasks/jobs.rake +312 -0
  153. data/mkdocs.yml +190 -0
  154. data/scripts/install_local_database.sh +309 -0
  155. metadata +341 -0
@@ -0,0 +1,302 @@
1
+ # Setting Up Local PostgreSQL Database for HTM
2
+
3
+ This guide walks through setting up a local PostgreSQL database with all required extensions for HTM development.
4
+
5
+ ## Prerequisites
6
+
7
+ - macOS with Homebrew installed
8
+ - PostgreSQL 14+ (PostgreSQL 17.6 recommended)
9
+ - Ollama running locally at `http://localhost:11434`
10
+
11
+ ## Why Local Database?
12
+
13
+ For local development, you'll want a PostgreSQL database on your machine for faster development and testing. HTM generates embeddings client-side using Ollama before inserting into the database.
14
+
15
+ ## Step 1: Install PostgreSQL
16
+
17
+ If you don't have PostgreSQL installed:
18
+
19
+ ```bash
20
+ brew install postgresql@17
21
+ brew services start postgresql@17
22
+ ```
23
+
24
+ Verify installation:
25
+
26
+ ```bash
27
+ psql --version
28
+ # Should show: psql (PostgreSQL) 17.x (Homebrew)
29
+ ```
30
+
31
+ ## Step 2: Install Required Extensions
32
+
33
+ ### 2.1 Install pgvector (Vector Similarity Search)
34
+
35
+ ```bash
36
+ brew install pgvector
37
+ ```
38
+
39
+ ### 2.2 Install TimescaleDB (Time-Series Database)
40
+
41
+ ```bash
42
+ # Add TimescaleDB tap
43
+ brew tap timescale/tap
44
+
45
+ # Install TimescaleDB
46
+ brew install timescaledb
47
+
48
+ # Configure PostgreSQL for TimescaleDB
49
+ # This updates your postgresql.conf with TimescaleDB settings
50
+ timescaledb-tune --quiet --yes
51
+
52
+ # Restart PostgreSQL to load TimescaleDB
53
+ brew services restart postgresql@17
54
+ ```
55
+
56
+ ### 2.3 pg_trgm (Trigram Matching)
57
+
58
+ This extension is included with PostgreSQL, no installation needed.
59
+
60
+ ## Step 3: Configure Environment
61
+
62
+ Update your `.envrc` file (already done):
63
+
64
+ ```bash
65
+ # Database connection - Localhost PostgreSQL
66
+ export HTM_DBHOST=localhost
67
+ export HTM_DBPORT=5432
68
+ export HTM_DBNAME=htm_development
69
+ export HTM_DBUSER=${USER}
70
+ export HTM_DBPASS=
71
+ export HTM_DBURL="postgresql://${HTM_DBUSER}@${HTM_DBHOST}:${HTM_DBPORT}/${HTM_DBNAME}?sslmode=prefer"
72
+
73
+ # Client-side embedding generation
74
+ export HTM_EMBEDDINGS_PROVIDER=ollama
75
+ export HTM_EMBEDDINGS_MODEL=embeddinggemma
76
+ export HTM_EMBEDDINGS_BASE_URL=http://localhost:11434
77
+ export HTM_EMBEDDINGS_DIMENSION=768
78
+ ```
79
+
80
+ Reload environment:
81
+
82
+ ```bash
83
+ cd /path/to/htm
84
+ direnv allow
85
+ ```
86
+
87
+ ## Step 4: Create Database
88
+
89
+ ```bash
90
+ createdb htm_development
91
+ ```
92
+
93
+ ## Step 5: Enable Extensions
94
+
95
+ ```bash
96
+ # Enable pgvector
97
+ psql -d htm_development -c "CREATE EXTENSION IF NOT EXISTS vector;"
98
+
99
+ # Enable TimescaleDB
100
+ psql -d htm_development -c "CREATE EXTENSION IF NOT EXISTS timescaledb;"
101
+
102
+ # Enable pg_trgm (trigram matching)
103
+ psql -d htm_development -c "CREATE EXTENSION IF NOT EXISTS pg_trgm;"
104
+ ```
105
+
106
+ ## Step 6: Run HTM Database Setup
107
+
108
+ ```bash
109
+ be rake htm:db:setup
110
+ ```
111
+
112
+ This will:
113
+ 1. Verify extensions are available
114
+ 2. Create HTM schema (tables, indexes, triggers)
115
+ 3. Set up TimescaleDB hypertables
116
+ 4. Run any pending migrations
117
+
118
+ Expected output:
119
+
120
+ ```
121
+ ✓ TimescaleDB version: X.X.X
122
+ ✓ pgvector version: X.X.X
123
+ ✓ pg_trgm version: X.X.X
124
+ Creating HTM schema...
125
+ ✓ Schema created
126
+ ✓ Created hypertable for operations_log
127
+ ✓ Created hypertable for nodes
128
+ ✓ Enabled compression for nodes older than 30 days
129
+ ✓ HTM database schema created successfully
130
+ ```
131
+
132
+ ## Step 7: Test with Sample Data
133
+
134
+ ```bash
135
+ be rake htm:db:seed
136
+ ```
137
+
138
+ This will:
139
+ 1. Initialize HTM with real EmbeddingService
140
+ 2. Create 6 sample conversation messages
141
+ 3. Generate embeddings client-side using your local Ollama
142
+
143
+ Expected output:
144
+
145
+ ```
146
+ Seeding database with sample data...
147
+ Note: This requires Ollama to be running locally for embedding generation.
148
+
149
+ Creating sample conversation...
150
+ ✓ Database seeded with 6 conversation messages (3 exchanges)
151
+ ```
152
+
153
+ ## Available Rake Tasks
154
+
155
+ ```bash
156
+ rake htm:db:setup # Set up database schema and run migrations
157
+ rake htm:db:migrate # Run pending migrations
158
+ rake htm:db:status # Show migration status
159
+ rake htm:db:drop # Drop all HTM tables (WARNING: destructive!)
160
+ rake htm:db:reset # Drop and recreate database
161
+ rake htm:db:test # Test database connection
162
+ rake htm:db:console # Open PostgreSQL console
163
+ rake htm:db:seed # Seed database with sample data
164
+ rake htm:db:info # Show database info (size, tables, extensions)
165
+ ```
166
+
167
+ ## Troubleshooting
168
+
169
+ ### Error: "type 'vector' does not exist"
170
+
171
+ **Problem:** pgvector extension not installed or not enabled.
172
+
173
+ **Solution:**
174
+ ```bash
175
+ brew install pgvector
176
+ psql -d htm_development -c "CREATE EXTENSION IF NOT EXISTS vector;"
177
+ ```
178
+
179
+ ### Error: "TimescaleDB extension not found"
180
+
181
+ **Problem:** TimescaleDB not installed or not enabled.
182
+
183
+ **Solution:**
184
+ ```bash
185
+ brew tap timescale/tap
186
+ brew install timescaledb
187
+ timescaledb-tune --quiet --yes
188
+ brew services restart postgresql@17
189
+ psql -d htm_development -c "CREATE EXTENSION IF NOT EXISTS timescaledb;"
190
+ ```
191
+
192
+ ### Error: "Connection refused" to Ollama
193
+
194
+ **Problem:** Ollama not running or not accessible.
195
+
196
+ **Solution:**
197
+ ```bash
198
+ # Check Ollama is running
199
+ curl http://localhost:11434/api/tags
200
+
201
+ # Start Ollama if not running
202
+ ollama serve
203
+ ```
204
+
205
+ ### Error: "Database configuration not found"
206
+
207
+ **Problem:** Environment variables not loaded.
208
+
209
+ **Solution:**
210
+ ```bash
211
+ direnv allow
212
+ echo $HTM_DBURL # Verify it's set
213
+ ```
214
+
215
+ ## Switching Back to TimescaleDB Cloud
216
+
217
+ To switch back to TimescaleDB Cloud (production), edit `.envrc`:
218
+
219
+ ```bash
220
+ # Comment out localhost config
221
+ # export HTM_DBHOST=localhost
222
+ # export HTM_DBPORT=5432
223
+ # export HTM_DBNAME=htm_development
224
+ # export HTM_DBUSER=${USER}
225
+ # export HTM_DBPASS=
226
+ # export HTM_DBURL="postgresql://${HTM_DBUSER}@${HTM_DBHOST}:${HTM_DBPORT}/${HTM_DBNAME}?sslmode=prefer"
227
+
228
+ # Uncomment TimescaleDB Cloud config
229
+ export HTM_SERVICE_NAME=$TIGER_SERVICE_NAME
230
+ export HTM_DBURL=$TIGER_DBURL
231
+ export HTM_DBNAME=$TIGER_DBNAME
232
+ export HTM_DBUSER=$TIGER_DBUSER
233
+ export HTM_DBPASS=$TIGER_DBPASS
234
+ export HTM_DBHOST=$TIGER_DBHOST
235
+ export HTM_DBPORT=$TIGER_DBPORT
236
+ ```
237
+
238
+ Then reload:
239
+ ```bash
240
+ direnv allow
241
+ ```
242
+
243
+ ## Verifying Setup
244
+
245
+ Check database info:
246
+
247
+ ```bash
248
+ be rake htm:db:info
249
+ ```
250
+
251
+ Should show:
252
+
253
+ ```
254
+ HTM Database Information
255
+ ================================================================================
256
+
257
+ Connection:
258
+ Host: localhost
259
+ Port: 5432
260
+ Database: htm_development
261
+ User: dewayne
262
+
263
+ PostgreSQL Version:
264
+ PostgreSQL 17.6
265
+
266
+ Extensions:
267
+ pg_trgm (X.X.X)
268
+ plpgsql (X.X.X)
269
+ timescaledb (X.X.X)
270
+ vector (X.X.X)
271
+
272
+ HTM Tables:
273
+ nodes: X rows
274
+ tags: X rows
275
+ robots: X rows
276
+ operations_log: X rows
277
+ schema_migrations: X rows
278
+
279
+ Database Size: XX MB
280
+ ================================================================================
281
+ ```
282
+
283
+ ## Next Steps
284
+
285
+ Once your local database is set up:
286
+
287
+ 1. Run tests: `rake test`
288
+ 2. Start using HTM in your application
289
+ 3. Embeddings will be generated client-side using Ollama
290
+ 4. Check operations_log table to see all HTM operations
291
+
292
+ ## Architecture Notes
293
+
294
+ With this setup:
295
+
296
+ - **PostgreSQL** runs on your localhost
297
+ - **Ollama** runs on your localhost at port 11434
298
+ - **HTM Ruby client** connects to both PostgreSQL and Ollama
299
+ - **Embeddings** are generated client-side before database insertion
300
+ - **Simple, reliable architecture** that works on all platforms
301
+
302
+ This is the ideal development environment for HTM.
@@ -0,0 +1,383 @@
1
+ # Using HTM Rake Tasks in Your Application
2
+
3
+ HTM provides a set of database management rake tasks that can be easily integrated into any application using the gem.
4
+
5
+ ## Quick Setup
6
+
7
+ Add this **single line** to your application's `Rakefile`:
8
+
9
+ ```ruby
10
+ require 'htm/tasks'
11
+ ```
12
+
13
+ That's it! All HTM database tasks are now available in your application.
14
+
15
+ ## Example Rakefile
16
+
17
+ ```ruby
18
+ # Your application's Rakefile
19
+
20
+ require 'bundler/setup'
21
+
22
+ # Load HTM database tasks
23
+ require 'htm/tasks'
24
+
25
+ # Your application's custom tasks
26
+ namespace :app do
27
+ desc "Run your app"
28
+ task :run do
29
+ # your code here
30
+ end
31
+ end
32
+
33
+ task default: :run
34
+ ```
35
+
36
+ ## Available Tasks
37
+
38
+ Once `require 'htm/tasks'` is added, you have access to all HTM database tasks:
39
+
40
+ ```bash
41
+ # List all HTM tasks
42
+ rake -T htm
43
+
44
+ # Database management
45
+ rake htm:db:setup # Set up HTM database schema and run migrations
46
+ rake htm:db:migrate # Run pending database migrations
47
+ rake htm:db:status # Show migration status
48
+ rake htm:db:info # Show database info
49
+ rake htm:db:test # Test database connection
50
+ rake htm:db:console # Open PostgreSQL console
51
+ rake htm:db:seed # Seed database with sample data
52
+ rake htm:db:drop # Drop all HTM tables (destructive!)
53
+ rake htm:db:reset # Drop and recreate database (destructive!)
54
+ ```
55
+
56
+ ## Environment Configuration
57
+
58
+ HTM tasks require database configuration via environment variables. You have several options:
59
+
60
+ ### Option 1: Using direnv (Recommended)
61
+
62
+ Create a `.envrc` file in your application's root:
63
+
64
+ ```bash
65
+ # .envrc
66
+ export HTM_DBURL="postgresql://user:password@host:port/dbname?sslmode=require"
67
+
68
+ # Or use individual parameters
69
+ export HTM_DBHOST="your-host.tsdb.cloud.timescale.com"
70
+ export HTM_DBPORT="37807"
71
+ export HTM_DBNAME="tsdb"
72
+ export HTM_DBUSER="tsdbadmin"
73
+ export HTM_DBPASS="your_password"
74
+
75
+ # Embedding configuration
76
+ export HTM_EMBEDDINGS_PROVIDER=ollama
77
+ export HTM_EMBEDDINGS_MODEL=nomic-embed-text
78
+ export HTM_EMBEDDINGS_BASE_URL=http://localhost:11434
79
+ export HTM_EMBEDDINGS_DIMENSION=768
80
+
81
+ # Topic extraction configuration
82
+ export HTM_TOPIC_PROVIDER=ollama
83
+ export HTM_TOPIC_MODEL=llama3
84
+ export HTM_TOPIC_BASE_URL=http://localhost:11434
85
+ ```
86
+
87
+ Then enable direnv:
88
+
89
+ ```bash
90
+ direnv allow
91
+ ```
92
+
93
+ ### Option 2: Export in Shell
94
+
95
+ ```bash
96
+ export HTM_DBURL="postgresql://user:password@host:port/dbname?sslmode=require"
97
+ rake htm:db:info
98
+ ```
99
+
100
+ ### Option 3: In Your Rakefile
101
+
102
+ ```ruby
103
+ # Rakefile
104
+
105
+ # Set environment variables programmatically
106
+ ENV['HTM_DBURL'] = "postgresql://user:password@host:port/dbname?sslmode=require"
107
+
108
+ # Then load tasks
109
+ require 'htm/tasks'
110
+ ```
111
+
112
+ ### Option 4: Use dotenv gem
113
+
114
+ ```ruby
115
+ # Gemfile
116
+ gem 'dotenv'
117
+
118
+ # Rakefile
119
+ require 'dotenv/load' # Loads .env file
120
+ require 'htm/tasks'
121
+ ```
122
+
123
+ ```bash
124
+ # .env
125
+ HTM_DBURL=postgresql://user:password@host:port/dbname?sslmode=require
126
+ ```
127
+
128
+ ## Real-World Example
129
+
130
+ Here's a complete example for a Rails-like application:
131
+
132
+ ```ruby
133
+ # Rakefile for MyApp
134
+
135
+ require 'bundler/setup'
136
+ require 'dotenv/load' # Load .env file
137
+
138
+ # Load HTM database tasks
139
+ require 'htm/tasks'
140
+
141
+ # Application tasks
142
+ namespace :app do
143
+ desc "Start the application"
144
+ task :start do
145
+ require_relative 'lib/my_app'
146
+ MyApp.start
147
+ end
148
+
149
+ desc "Run database migrations and start app"
150
+ task :bootstrap => ['htm:db:setup', :start]
151
+ end
152
+
153
+ # Default task
154
+ task default: 'app:start'
155
+
156
+ # Development helper
157
+ namespace :dev do
158
+ desc "Reset database and restart (development only!)"
159
+ task :reset => ['htm:db:reset', 'htm:db:seed', 'app:start']
160
+ end
161
+ ```
162
+
163
+ Usage:
164
+
165
+ ```bash
166
+ # First time setup
167
+ rake app:bootstrap # Sets up HTM database + starts app
168
+
169
+ # Development reset
170
+ rake dev:reset # Drops/recreates database + seeds data
171
+
172
+ # Normal start
173
+ rake # Runs default task (app:start)
174
+
175
+ # Database management
176
+ rake htm:db:info # Check database status
177
+ rake htm:db:migrate # Run new migrations
178
+ ```
179
+
180
+ ## Task Composition
181
+
182
+ You can compose HTM tasks with your own:
183
+
184
+ ```ruby
185
+ # Your Rakefile
186
+
187
+ require 'htm/tasks'
188
+
189
+ namespace :deploy do
190
+ desc "Deploy application to production"
191
+ task :production do
192
+ # Run HTM migrations first
193
+ Rake::Task['htm:db:migrate'].invoke
194
+
195
+ # Then deploy your app
196
+ sh "git push production main"
197
+ sh "ssh production 'systemctl restart myapp'"
198
+ end
199
+ end
200
+ ```
201
+
202
+ ## Rails Integration
203
+
204
+ For Rails applications, add to your `Rakefile`:
205
+
206
+ ```ruby
207
+ # Rakefile (Rails)
208
+
209
+ require_relative 'config/application'
210
+
211
+ Rails.application.load_tasks
212
+
213
+ # Load HTM tasks
214
+ require 'htm/tasks'
215
+ ```
216
+
217
+ Now you have both Rails tasks and HTM tasks:
218
+
219
+ ```bash
220
+ rake db:migrate # Rails migrations
221
+ rake htm:db:migrate # HTM migrations
222
+
223
+ rake db:seed # Rails seed
224
+ rake htm:db:seed # HTM seed
225
+ ```
226
+
227
+ ## Sinatra Integration
228
+
229
+ ```ruby
230
+ # Rakefile (Sinatra)
231
+
232
+ require './app' # Your Sinatra app
233
+ require 'htm/tasks'
234
+
235
+ namespace :server do
236
+ desc "Start Sinatra server"
237
+ task :start do
238
+ App.run!
239
+ end
240
+ end
241
+
242
+ task default: 'server:start'
243
+ ```
244
+
245
+ ## Testing Integration
246
+
247
+ Add HTM database setup to your test tasks:
248
+
249
+ ```ruby
250
+ # Rakefile
251
+
252
+ require 'rake/testtask'
253
+ require 'htm/tasks'
254
+
255
+ Rake::TestTask.new(:test) do |t|
256
+ t.libs << "test"
257
+ t.test_files = FileList['test/**/*_test.rb']
258
+ end
259
+
260
+ # Set up test database before running tests
261
+ task :test => 'htm:db:setup'
262
+ ```
263
+
264
+ ## CI/CD Pipeline
265
+
266
+ Example GitHub Actions workflow:
267
+
268
+ ```yaml
269
+ # .github/workflows/test.yml
270
+
271
+ name: Tests
272
+ on: [push]
273
+
274
+ jobs:
275
+ test:
276
+ runs-on: ubuntu-latest
277
+
278
+ services:
279
+ postgres:
280
+ image: timescale/timescaledb-ha:pg17
281
+ env:
282
+ POSTGRES_PASSWORD: postgres
283
+ options: >-
284
+ --health-cmd pg_isready
285
+ --health-interval 10s
286
+ --health-timeout 5s
287
+ --health-retries 5
288
+
289
+ steps:
290
+ - uses: actions/checkout@v2
291
+
292
+ - name: Set up Ruby
293
+ uses: ruby/setup-ruby@v1
294
+ with:
295
+ ruby-version: 3.3
296
+ bundler-cache: true
297
+
298
+ - name: Setup database
299
+ env:
300
+ HTM_DBURL: postgresql://postgres:postgres@localhost:5432/test
301
+ run: |
302
+ bundle exec rake htm:db:setup
303
+
304
+ - name: Run tests
305
+ env:
306
+ HTM_DBURL: postgresql://postgres:postgres@localhost:5432/test
307
+ run: |
308
+ bundle exec rake test
309
+ ```
310
+
311
+ ## Docker Integration
312
+
313
+ In your `docker-compose.yml`:
314
+
315
+ ```yaml
316
+ services:
317
+ app:
318
+ build: .
319
+ environment:
320
+ - HTM_DBURL=postgresql://postgres:postgres@db:5432/myapp
321
+ depends_on:
322
+ - db
323
+ command: bash -c "rake htm:db:setup && rake app:start"
324
+
325
+ db:
326
+ image: timescale/timescaledb-ha:pg17
327
+ environment:
328
+ - POSTGRES_PASSWORD=postgres
329
+ ```
330
+
331
+ ## Troubleshooting
332
+
333
+ ### Tasks not available
334
+
335
+ ```bash
336
+ rake -T
337
+ # If you don't see htm:db tasks, check:
338
+ ```
339
+
340
+ 1. Verify `require 'htm/tasks'` is in your Rakefile
341
+ 2. Check that `htm` gem is in your Gemfile
342
+ 3. Run `bundle install`
343
+
344
+ ### Database not configured
345
+
346
+ ```
347
+ Error: Database configuration not found
348
+ ```
349
+
350
+ Solution: Set `HTM_DBURL` environment variable
351
+
352
+ ```bash
353
+ export HTM_DBURL="postgresql://user:password@host:port/dbname"
354
+ rake htm:db:info
355
+ ```
356
+
357
+ ### Permission errors
358
+
359
+ ```
360
+ Error: permission denied for table nodes
361
+ ```
362
+
363
+ Solution: Ensure your database user has proper permissions
364
+
365
+ ```sql
366
+ GRANT ALL ON ALL TABLES IN SCHEMA public TO your_user;
367
+ GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO your_user;
368
+ ```
369
+
370
+ ## Best Practices
371
+
372
+ 1. **Always use environment variables** for database configuration (never hardcode credentials)
373
+ 2. **Use `htm:db:migrate`** instead of `htm:db:setup` in production (setup drops tables)
374
+ 3. **Run `htm:db:status`** before deploying to check migration state
375
+ 4. **Never use `htm:db:reset`** or `htm:db:drop` in production
376
+ 5. **Compose tasks** rather than duplicating functionality
377
+ 6. **Test migrations** on staging before production
378
+
379
+ ## See Also
380
+
381
+ - [Database Rake Tasks Reference](database_rake_tasks.md) - Complete task documentation
382
+ - [README.md](https://github.com/madbomber/htm/blob/main/README.md) - HTM gem overview
383
+ - [SETUP.md](https://github.com/madbomber/htm/blob/main/SETUP.md) - Initial setup guide