pg_multitenant_schemas 0.2.0 โ†’ 0.2.3

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.
@@ -0,0 +1,181 @@
1
+ # GitHub Actions Setup Guide
2
+
3
+ This document explains how to set up GitHub Actions for automated testing and releases.
4
+
5
+ ## Overview
6
+
7
+ The repository uses two main workflows:
8
+
9
+ 1. **CI Workflow** (`.github/workflows/main.yml`) - Runs tests on every push and PR
10
+ 2. **Release Workflow** (`.github/workflows/release.yml`) - Automatically releases to RubyGems when version changes
11
+
12
+ ## Setting Up RubyGems API Key
13
+
14
+ To enable automatic releases to RubyGems, you need to set up a GitHub secret with your RubyGems API key.
15
+
16
+ ### Step 1: Get Your RubyGems API Key
17
+
18
+ 1. Go to [RubyGems.org](https://rubygems.org/)
19
+ 2. Sign in to your account
20
+ 3. Go to your profile settings
21
+ 4. Navigate to "API Keys" section
22
+ 5. Create a new API key or use an existing one
23
+ 6. Copy the API key (it should start with `rubygems_`)
24
+
25
+ ### Step 2: Add GitHub Secret
26
+
27
+ 1. Go to your GitHub repository
28
+ 2. Click on "Settings" tab
29
+ 3. In the left sidebar, click "Secrets and variables" โ†’ "Actions"
30
+ 4. Click "New repository secret"
31
+ 5. Name: `RUBYGEMS_API_KEY`
32
+ 6. Value: Paste your RubyGems API key
33
+ 7. Click "Add secret"
34
+
35
+ ### 3. Configure Repository Permissions
36
+
37
+ **Important:** Configure GitHub Actions permissions to allow the release workflow to create tags and releases:
38
+
39
+ 1. Go to your repository Settings
40
+ 2. Navigate to **Actions > General**
41
+ 3. Under "Workflow permissions":
42
+ - Select **"Read and write permissions"**
43
+ - Check **"Allow GitHub Actions to create and approve pull requests"**
44
+ 4. Click **Save**
45
+
46
+ Without these permissions, you'll get errors like:
47
+ ```
48
+ remote: Permission to username/repo.git denied to github-actions[bot].
49
+ fatal: unable to access 'https://github.com/username/repo/': The requested URL returned error: 403
50
+ ```
51
+
52
+ ## How the Workflows Work
53
+
54
+ ### CI Workflow
55
+
56
+ **Triggers:**
57
+ - Every push to `main` branch
58
+ - Every pull request to `main` branch
59
+
60
+ **What it does:**
61
+ - Tests on Ruby 3.2, 3.3, and 3.4
62
+ - Runs RuboCop for code quality
63
+ - Runs unit tests (excluding integration tests)
64
+ - Runs integration tests (with PostgreSQL database)
65
+ - Runs security audit with bundle-audit
66
+
67
+ ### Release Workflow
68
+
69
+ **Triggers:**
70
+ - Push to `main` branch that changes `lib/pg_multitenant_schemas/version.rb`
71
+
72
+ **What it does:**
73
+ - Checks if the version in `version.rb` has changed
74
+ - If version changed:
75
+ 1. Builds the gem
76
+ 2. Creates a Git tag (e.g., `v0.2.1`)
77
+ 3. Creates a GitHub release with changelog notes
78
+ 4. Publishes the gem to RubyGems
79
+
80
+ ## Release Process
81
+
82
+ To release a new version:
83
+
84
+ 1. **Update the version** in `lib/pg_multitenant_schemas/version.rb`:
85
+ ```ruby
86
+ module PgMultitenantSchemas
87
+ VERSION = "0.2.1" # Increment this
88
+ end
89
+ ```
90
+
91
+ 2. **Update the changelog** in `CHANGELOG.md`:
92
+ ```markdown
93
+ ## [0.2.1] - 2025-09-07
94
+
95
+ ### Added
96
+ - New feature description
97
+
98
+ ### Fixed
99
+ - Bug fix description
100
+ ```
101
+
102
+ 3. **Commit and push** to main branch:
103
+ ```bash
104
+ git add lib/pg_multitenant_schemas/version.rb CHANGELOG.md
105
+ git commit -m "Bump version to 0.2.1"
106
+ git push origin main
107
+ ```
108
+
109
+ 4. **Automatic release** will trigger:
110
+ - GitHub Actions will detect the version change
111
+ - Create a Git tag and GitHub release
112
+ - Publish to RubyGems automatically
113
+
114
+ ## Manual Release (Alternative)
115
+
116
+ If you prefer manual releases or need to troubleshoot:
117
+
118
+ ```bash
119
+ # Build the gem
120
+ gem build pg_multitenant_schemas.gemspec
121
+
122
+ # Push to RubyGems (requires authentication)
123
+ gem push pg_multitenant_schemas-0.2.1.gem
124
+
125
+ # Create Git tag
126
+ git tag v0.2.1
127
+ git push origin v0.2.1
128
+ ```
129
+
130
+ ## Workflow Status
131
+
132
+ You can monitor workflow runs:
133
+
134
+ 1. Go to your GitHub repository
135
+ 2. Click the "Actions" tab
136
+ 3. View running and completed workflows
137
+ 4. Click on individual runs to see detailed logs
138
+
139
+ ## Security Considerations
140
+
141
+ - **API Keys**: Never commit API keys to the repository
142
+ - **Secrets**: Use GitHub Secrets for sensitive information
143
+ - **Permissions**: The `GITHUB_TOKEN` has limited permissions for creating releases
144
+ - **Audit**: The security workflow checks for vulnerable dependencies
145
+
146
+ ## Troubleshooting
147
+
148
+ ### Common Issues
149
+
150
+ 1. **RubyGems authentication failed**
151
+ - Check that `RUBYGEMS_API_KEY` secret is set correctly
152
+ - Ensure the API key has publishing permissions
153
+
154
+ 2. **Git tag already exists**
155
+ - The workflow checks for existing tags
156
+ - If tag exists, release is skipped
157
+
158
+ 3. **Tests failing**
159
+ - CI must pass before release workflow runs
160
+ - Check test logs in the Actions tab
161
+
162
+ 4. **PostgreSQL connection issues**
163
+ - Integration tests require PostgreSQL service
164
+ - Check service configuration in workflow
165
+
166
+ ### Debug Steps
167
+
168
+ 1. Check workflow logs in GitHub Actions
169
+ 2. Verify secrets are set correctly
170
+ 3. Test locally with same Ruby versions
171
+ 4. Check RubyGems.org for published gems
172
+
173
+ ## Best Practices
174
+
175
+ 1. **Version Bumping**: Use semantic versioning (MAJOR.MINOR.PATCH)
176
+ 2. **Changelog**: Always update changelog before releasing
177
+ 3. **Testing**: Ensure all tests pass locally before pushing
178
+ 4. **Security**: Regularly update dependencies and run security audits
179
+ 5. **Documentation**: Update documentation for breaking changes
180
+
181
+ This automated setup ensures consistent, reliable releases while maintaining code quality through comprehensive testing.
@@ -0,0 +1,314 @@
1
+ # Testing GitHub Workflows Locally
2
+
3
+ This guide shows you how to test GitHub Actions workflows locally before pushing to GitHub.
4
+
5
+ ## ๐Ÿš€ Method 1: Using `act` (Recommended)
6
+
7
+ `act` is the most popular tool for running GitHub Actions locally using Docker.
8
+
9
+ ### Installation
10
+
11
+ ```bash
12
+ # macOS (using Homebrew)
13
+ brew install act
14
+
15
+ # Linux (using curl)
16
+ curl https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
17
+
18
+ # Windows (using Chocolatey)
19
+ choco install act-cli
20
+ ```
21
+
22
+ ### Basic Usage
23
+
24
+ ```bash
25
+ # List all workflows
26
+ act -l
27
+
28
+ # Run all workflows
29
+ act
30
+
31
+ # Run specific workflow
32
+ act push
33
+
34
+ # Run specific job
35
+ act -j test
36
+
37
+ # Run with specific event
38
+ act pull_request
39
+
40
+ # Dry run (show what would be executed)
41
+ act -n
42
+ ```
43
+
44
+ ### Testing Our Workflows
45
+
46
+ ```bash
47
+ # Test the main CI workflow
48
+ act push
49
+
50
+ # Test the release workflow (simulates a push to main)
51
+ act push --env GITHUB_REF=refs/heads/main
52
+
53
+ # Test pull request workflow
54
+ act pull_request
55
+ ```
56
+
57
+ ### Configuration
58
+
59
+ Create `.actrc` file in project root for default settings:
60
+
61
+ ```bash
62
+ # .actrc
63
+ --container-architecture linux/amd64
64
+ --artifact-server-path /tmp/act-artifacts
65
+ --env-file .env.local
66
+ ```
67
+
68
+ ### Environment Variables
69
+
70
+ Create `.env.local` for local testing:
71
+
72
+ ```bash
73
+ # .env.local
74
+ GITHUB_TOKEN=your_github_token_here
75
+ RUBYGEMS_API_KEY=your_rubygems_key_here
76
+ GITHUB_REPOSITORY=rubenpazch/pg_multitenant_schemas
77
+ GITHUB_ACTOR=your_username
78
+ ```
79
+
80
+ ## ๐Ÿ”ง Method 2: Manual Testing Components
81
+
82
+ ### Test RuboCop Locally
83
+
84
+ ```bash
85
+ # Run RuboCop (same as in CI)
86
+ bundle exec rubocop
87
+
88
+ # Auto-fix issues
89
+ bundle exec rubocop -A
90
+
91
+ # Check specific files
92
+ bundle exec rubocop lib/ spec/
93
+ ```
94
+
95
+ ### Test RSpec Locally
96
+
97
+ ```bash
98
+ # Run all tests
99
+ bundle exec rspec
100
+
101
+ # Run with coverage (if configured)
102
+ COVERAGE=true bundle exec rspec
103
+
104
+ # Run integration tests
105
+ bundle exec rspec --tag integration
106
+
107
+ # Run tests with different Ruby versions (using rbenv/rvm)
108
+ rbenv shell 3.2.0 && bundle exec rspec
109
+ rbenv shell 3.3.0 && bundle exec rspec
110
+ ```
111
+
112
+ ### Test Gem Building
113
+
114
+ ```bash
115
+ # Build gem locally
116
+ gem build pg_multitenant_schemas.gemspec
117
+
118
+ # Install locally built gem
119
+ gem install pg_multitenant_schemas-*.gem
120
+
121
+ # Test gem installation
122
+ gem list | grep pg_multitenant_schemas
123
+ ```
124
+
125
+ ### Test PostgreSQL Setup
126
+
127
+ ```bash
128
+ # Start PostgreSQL (macOS with Homebrew)
129
+ brew services start postgresql@15
130
+
131
+ # Create test database
132
+ createdb pg_multitenant_test
133
+
134
+ # Run integration tests
135
+ PGDATABASE=pg_multitenant_test bundle exec rspec --tag integration
136
+ ```
137
+
138
+ ## ๐Ÿณ Method 3: Docker-based Testing
139
+
140
+ Create a local testing environment that mirrors CI:
141
+
142
+ ```bash
143
+ # Create Dockerfile.test
144
+ cat > Dockerfile.test << 'EOF'
145
+ FROM ruby:3.3
146
+
147
+ # Install PostgreSQL client
148
+ RUN apt-get update && apt-get install -y postgresql-client
149
+
150
+ # Set working directory
151
+ WORKDIR /app
152
+
153
+ # Copy Gemfile
154
+ COPY Gemfile* ./
155
+ RUN bundle install
156
+
157
+ # Copy source code
158
+ COPY . .
159
+
160
+ # Run tests
161
+ CMD ["bundle", "exec", "rspec"]
162
+ EOF
163
+
164
+ # Build and run
165
+ docker build -f Dockerfile.test -t pg_multitenant_test .
166
+ docker run --rm pg_multitenant_test
167
+ ```
168
+
169
+ ## ๐Ÿ” Method 4: GitHub CLI for Remote Testing
170
+
171
+ Use GitHub CLI to trigger workflows manually:
172
+
173
+ ```bash
174
+ # Install GitHub CLI
175
+ brew install gh
176
+
177
+ # Authenticate
178
+ gh auth login
179
+
180
+ # Trigger workflow manually
181
+ gh workflow run main.yml
182
+
183
+ # Check workflow status
184
+ gh run list
185
+
186
+ # View workflow logs
187
+ gh run view --log
188
+ ```
189
+
190
+ ## ๐Ÿ“‹ Pre-Push Checklist
191
+
192
+ Before pushing to GitHub, run these commands locally:
193
+
194
+ ```bash
195
+ #!/bin/bash
196
+ # pre-push-check.sh
197
+
198
+ echo "๐Ÿ” Running pre-push checks..."
199
+
200
+ # 1. RuboCop
201
+ echo "Running RuboCop..."
202
+ bundle exec rubocop || exit 1
203
+
204
+ # 2. RSpec
205
+ echo "Running RSpec..."
206
+ bundle exec rspec || exit 1
207
+
208
+ # 3. Security audit
209
+ echo "Running security audit..."
210
+ bundle audit || exit 1
211
+
212
+ # 4. Gem build test
213
+ echo "Testing gem build..."
214
+ gem build pg_multitenant_schemas.gemspec || exit 1
215
+
216
+ # 5. Integration tests (if PostgreSQL available)
217
+ if command -v psql &> /dev/null; then
218
+ echo "Running integration tests..."
219
+ bundle exec rspec --tag integration || exit 1
220
+ fi
221
+
222
+ echo "โœ… All checks passed! Ready to push."
223
+ ```
224
+
225
+ Make it executable:
226
+
227
+ ```bash
228
+ chmod +x pre-push-check.sh
229
+ ./pre-push-check.sh
230
+ ```
231
+
232
+ ## ๐ŸŽฏ Specific Workflow Testing
233
+
234
+ ### Testing CI Workflow (main.yml)
235
+
236
+ ```bash
237
+ # Using act
238
+ act push -W .github/workflows/main.yml
239
+
240
+ ```bash
241
+ # Manual simulation
242
+ bundle install
243
+ bundle exec rubocop
244
+ bundle exec rspec --exclude-pattern '**/integration/**/*_spec.rb'
245
+ bundle audit
246
+ ```
247
+ ```
248
+
249
+ ### Testing Release Workflow (release.yml)
250
+
251
+ ```bash
252
+ # Simulate version bump
253
+ # 1. Update version in lib/pg_multitenant_schemas/version.rb
254
+ # 2. Test locally
255
+ act push -W .github/workflows/release.yml --env GITHUB_REF=refs/heads/main
256
+
257
+ # Manual simulation (without actual publishing)
258
+ gem build pg_multitenant_schemas.gemspec
259
+ # Note: Don't run 'gem push' in testing
260
+ ```
261
+
262
+ ## ๐Ÿ› ๏ธ Troubleshooting
263
+
264
+ ### Common Issues with `act`
265
+
266
+ 1. **Docker not running**: Start Docker Desktop
267
+ 2. **Permission issues**: Run with `sudo` or fix Docker permissions
268
+ 3. **Platform issues**: Use `--container-architecture linux/amd64`
269
+ 4. **Secret errors**: Provide secrets via `.env.local` file
270
+
271
+ ### PostgreSQL Issues
272
+
273
+ ```bash
274
+ # Check if PostgreSQL is running
275
+ pg_isready
276
+
277
+ # Start PostgreSQL service
278
+ brew services start postgresql@15
279
+
280
+ # Create test database
281
+ createdb pg_multitenant_test
282
+ ```
283
+
284
+ ### Ruby Version Issues
285
+
286
+ ```bash
287
+ # Install required Ruby versions
288
+ rbenv install 3.2.0
289
+ rbenv install 3.3.0
290
+ rbenv install 3.4.0
291
+
292
+ # Test with specific version
293
+ rbenv shell 3.3.0
294
+ bundle install
295
+ bundle exec rspec
296
+ ```
297
+
298
+ ## ๐Ÿ“š Additional Resources
299
+
300
+ - [act Documentation](https://github.com/nektos/act)
301
+ - [GitHub Actions Documentation](https://docs.github.com/en/actions)
302
+ - [Local Development Best Practices](https://docs.github.com/en/actions/using-workflows/about-workflows#best-practices)
303
+
304
+ ## ๐Ÿ”— Integration with Git Hooks
305
+
306
+ Add to `.git/hooks/pre-push`:
307
+
308
+ ```bash
309
+ #!/bin/bash
310
+ ./pre-push-check.sh
311
+ exit $?
312
+ ```
313
+
314
+ This ensures checks run automatically before every push.
@@ -0,0 +1,128 @@
1
+ # ๐Ÿงช Testing Rails Tasks in PG Multitenant Schemas
2
+
3
+ ## ๐Ÿšจ Important: Testing Context
4
+
5
+ The `rails tenants:list` command only works **within a Rails application** that includes this gem, not in the gem directory itself.
6
+
7
+ ## ๐Ÿ”ง Fixed Issues
8
+
9
+ โœ… **Removed circular dependency** in rake task loading
10
+ โœ… **Eliminated duplicate task files** (`pg_multitenant_schemas.rake` was identical to `tenant_tasks.rake`)
11
+ โœ… **Updated railtie** to load all task files properly
12
+ โœ… **Added missing `list_schemas` method** to `SchemaSwitcher` class
13
+ โœ… **Fixed Rails 8 migration_context compatibility** - Updated migration handling for Rails 8
14
+
15
+ ## ๐Ÿš€ How to Test the Rails Tasks
16
+
17
+ ### **Option 1: Create a Test Rails App**
18
+
19
+ ```bash
20
+ # Create a new Rails app for testing
21
+ rails new test_multitenancy --database=postgresql
22
+ cd test_multitenancy
23
+
24
+ # Add the gem to Gemfile
25
+ echo 'gem "pg_multitenant_schemas", path: "/Users/rubenpaz/personal/pg_multitenant_schemas"' >> Gemfile
26
+
27
+ # Install the gem
28
+ bundle install
29
+
30
+ # Now you can test the tasks
31
+ rails tenants:list
32
+ rails tenants:status
33
+ rails tenants:migrate
34
+ ```
35
+
36
+ ### **Option 2: Use an Existing Rails App**
37
+
38
+ ```bash
39
+ # Navigate to your existing Rails app
40
+ cd /Users/rubenpaz/personal/lbyte-security # or wherever your Rails app is
41
+
42
+ # Add gem to Gemfile if not already added
43
+ # gem "pg_multitenant_schemas", path: "/Users/rubenpaz/personal/pg_multitenant_schemas"
44
+
45
+ # Install and test
46
+ bundle install
47
+ rails tenants:list
48
+ ```
49
+
50
+ ### **Option 3: Test in the Example Directory**
51
+
52
+ ```bash
53
+ # If there's a Rails example in the gem
54
+ cd /Users/rubenpaz/personal/pg_multitenant_schemas/examples
55
+ # Follow instructions in that directory
56
+ ```
57
+
58
+ ## ๐Ÿ“‹ Available Tasks
59
+
60
+ After including the gem in a Rails app, you'll have these tasks:
61
+
62
+ ### **Basic Tasks**
63
+ - `rails tenants:list` - List all tenant schemas
64
+ - `rails tenants:status` - Show migration status for all tenants
65
+ - `rails tenants:migrate` - Run migrations for all tenant schemas
66
+
67
+ ### **Advanced Tasks**
68
+ - `rails tenants:migrate_tenant[schema_name]` - Run migrations for specific tenant
69
+ - `rails tenants:create[schema_name]` - Setup new tenant with schema and migrations
70
+ - `rails tenants:setup` - Setup schemas and run migrations for all existing tenants
71
+
72
+ ### **Management Tasks**
73
+ - `rails tenants:new[attributes]` - Create new tenant with attributes (JSON format)
74
+ - `rails tenants:drop[schema_name]` - Drop tenant schema (DANGEROUS)
75
+ - `rails tenants:rollback[schema_name,steps]` - Rollback migrations for a tenant
76
+
77
+ ### **Convenience Aliases**
78
+ - `rails tenants:db:create[schema_name]` - Alias for `tenants:create`
79
+ - `rails tenants:db:migrate` - Alias for `tenants:migrate`
80
+ - `rails tenants:db:status` - Alias for `tenants:status`
81
+
82
+ ### **Legacy Tasks (Deprecated)**
83
+ - `rails pg_multitenant_schemas:list_schemas` - Use `tenants:list` instead
84
+ - `rails pg_multitenant_schemas:migrate_all` - Use `tenants:migrate` instead
85
+
86
+ ## ๐Ÿ› ๏ธ Troubleshooting
87
+
88
+ ### "Command not found" or "stack level too deep"
89
+ - โœ… **Fixed**: Circular dependency in task loading removed
90
+ - Make sure you're in a Rails application directory
91
+ - Run `bundle install` after adding the gem
92
+
93
+ ### "NoMethodError: undefined method 'list_schemas'"
94
+ - โœ… **Fixed**: Added missing `list_schemas` method to `SchemaSwitcher`
95
+ - Update to the latest version of the gem
96
+
97
+ ### "NoMethodError: undefined method 'migration_context'"
98
+ - โœ… **Fixed**: Updated migration handling for Rails 8 compatibility
99
+ - The gem now properly handles migration context access across Rails versions
100
+ - Update to the latest version of the gem
101
+
102
+ ### "No such task"
103
+ - Ensure the gem is properly added to your Rails app's Gemfile
104
+ - Run `bundle install`
105
+ - Check that the gem is loading: `rails runner "puts PgMultitenantSchemas::VERSION"`
106
+
107
+ ### "Environment not loaded"
108
+ - The tasks require `:environment`, so they need a proper Rails environment
109
+ - Make sure your Rails app's database is configured and accessible
110
+
111
+ ## ๐Ÿงช Quick Test
112
+
113
+ To verify everything works, create a minimal test:
114
+
115
+ ```bash
116
+ # In your Rails app directory
117
+ rails runner "puts 'Gem loaded: ' + PgMultitenantSchemas::VERSION"
118
+ rails tenants:list
119
+ ```
120
+
121
+ ## ๐Ÿ“ Task File Structure
122
+
123
+ The gem now has a clean task structure:
124
+ - `basic_tasks.rake` - List, status, migrate tasks
125
+ - `advanced_tasks.rake` - Advanced tenant management
126
+ - `tenant_tasks.rake` - Management tasks + aliases + legacy compatibility
127
+
128
+ All are loaded automatically via the Rails railtie when you include the gem in your Rails application.