Skip to content

Commit af3a95c

Browse files
committed
feat: add type checking using ruby rbs
1 parent 1e47cbc commit af3a95c

21 files changed

Lines changed: 334 additions & 0 deletions

.github/workflows/rbs.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: RBS
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
validate:
11+
name: Validate Type Signatures
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up Ruby
18+
uses: ruby/setup-ruby@v1
19+
with:
20+
ruby-version: '3.3'
21+
bundler-cache: true
22+
23+
- name: Install RBS
24+
run: gem install rbs
25+
26+
- name: Install RBS collection
27+
run: rbs collection install
28+
29+
- name: Validate RBS signatures
30+
run: rbs -I sig validate

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ tmp/
3434
*.tmp
3535

3636
Gemfile.lock
37+
38+
# RBS type collection
39+
.gem_rbs_collection/
40+
rbs_collection.lock.yaml

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
[![Gem Version](https://badge.fury.io/rb/staging_table.svg)](https://badge.fury.io/rb/staging_table)
66
[![Test Status](https://github.com/eagerworks/staging_table/actions/workflows/test.yml/badge.svg)](https://github.com/eagerworks/staging_table/actions)
7+
[![RBS](https://github.com/eagerworks/staging_table/actions/workflows/rbs.yml/badge.svg)](https://github.com/eagerworks/staging_table/actions/workflows/rbs.yml)
78

89
Stop shoving data directly into your production tables like a savage. Give it a dressing room first!
910

@@ -237,6 +238,48 @@ end
237238

238239
---
239240

241+
## 🔍 Type Checking (RBS)
242+
243+
This gem ships with [RBS](https://github.com/ruby/rbs) type signatures for static type checking. The signatures are located in the `sig/` directory and are validated in CI.
244+
245+
### Using the Type Signatures
246+
247+
If you want to type-check your own code that uses StagingTable:
248+
249+
```bash
250+
# Install RBS and the collection for dependencies
251+
gem install rbs
252+
rbs collection install
253+
254+
# Validate signatures
255+
rbs -I sig validate
256+
```
257+
258+
### Using with Steep
259+
260+
For full type checking with [Steep](https://github.com/soutaro/steep):
261+
262+
```ruby
263+
# Gemfile
264+
gem 'steep', group: :development
265+
```
266+
267+
```ruby
268+
# Steepfile
269+
target :lib do
270+
signature "sig"
271+
check "lib"
272+
library "activerecord"
273+
library "activesupport"
274+
end
275+
```
276+
277+
```bash
278+
bundle exec steep check
279+
```
280+
281+
---
282+
240283
## 💾 Supported Databases
241284

242285
We speak your language.

rbs_collection.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Run `rbs collection install` to install required type definitions
2+
# See: https://github.com/ruby/rbs/blob/master/docs/collection.md
3+
4+
sources:
5+
- type: git
6+
name: ruby/gem_rbs_collection
7+
remote: https://github.com/ruby/gem_rbs_collection.git
8+
revision: main
9+
repo_dir: gems
10+
11+
path: .gem_rbs_collection
12+
13+
gems:
14+
- name: activerecord
15+
- name: activesupport
16+
# Ignore gems without RBS definitions in the collection
17+
- name: prism
18+
ignore: true

sig/manifest.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# RBS manifest file
2+
# Defines gem dependencies for type checking
3+
dependencies:
4+
- name: activerecord
5+
- name: activesupport

sig/staging_table.rbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Type signatures for staging_table gem
2+
# See https://github.com/ruby/rbs for more information
3+
4+
module StagingTable
5+
def self.configuration: () -> Configuration
6+
def self.configure: () { (Configuration) -> void } -> void
7+
def self.stage: (singleton(ActiveRecord::Base) source_model, **untyped options) -> Session
8+
| [T] (singleton(ActiveRecord::Base) source_model, **untyped options) { (Session) -> T } -> TransferResult
9+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module StagingTable
2+
module Adapters
3+
class Base
4+
attr_reader connection: ActiveRecord::ConnectionAdapters::AbstractAdapter
5+
6+
def initialize: (ActiveRecord::ConnectionAdapters::AbstractAdapter connection) -> void
7+
8+
# Create a staging table based on the source table structure
9+
def create_table: (String temp_table_name, String source_table_name, ?Hash[Symbol, untyped] options) -> void
10+
11+
# Drop the staging table
12+
def drop_table: (String temp_table_name) -> void
13+
14+
# Factory method to get the appropriate adapter for a connection
15+
def self.for: (ActiveRecord::ConnectionAdapters::AbstractAdapter connection) -> Base
16+
end
17+
end
18+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module StagingTable
2+
module Adapters
3+
class Mysql < Base
4+
def create_table: (String temp_table_name, String source_table_name, ?Hash[Symbol, untyped] options) -> void
5+
end
6+
end
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module StagingTable
2+
module Adapters
3+
class Postgresql < Base
4+
def create_table: (String temp_table_name, String source_table_name, ?Hash[Symbol, untyped] options) -> void
5+
end
6+
end
7+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module StagingTable
2+
module Adapters
3+
class Sqlite < Base
4+
def create_table: (String temp_table_name, String source_table_name, ?Hash[Symbol, untyped] options) -> void
5+
6+
private
7+
8+
def copy_indexes: (String temp_table_name, String source_table_name) -> void
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)