|
1 | | -# Copilot Instructions for Declarative SQLite |
| 1 | +# Declarative SQLite Development Instructions |
2 | 2 |
|
3 | | -## Project Overview |
| 3 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
4 | 4 |
|
5 | 5 | This is a Dart package that provides a declarative approach to SQLite schema management and data access. The library implements a fluent builder pattern for defining database schemas and automatically handles migrations and CRUD operations. |
6 | 6 |
|
7 | | -## Architecture |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Prerequisites and Setup |
| 10 | +- Install Dart SDK 3.5.3 or later: |
| 11 | + - Download: `wget -O /tmp/dart-sdk.zip https://storage.googleapis.com/dart-archive/channels/stable/release/latest/sdk/dartsdk-linux-x64-release.zip` |
| 12 | + - Install: `cd /tmp && unzip -q dart-sdk.zip && sudo mv dart-sdk /opt/` |
| 13 | + - Add to PATH: `export PATH="/opt/dart-sdk/bin:$PATH"` |
| 14 | + - Verify: `dart --version` |
| 15 | + |
| 16 | +### Build and Test Commands (FAST EXECUTION) |
| 17 | +- Install dependencies: `dart pub get` -- takes 3-4 seconds. NEVER CANCEL. |
| 18 | +- Run all tests: `dart test` -- takes 6-8 seconds total. NEVER CANCEL. |
| 19 | +- Run specific test: `dart test test/integration_test.dart` -- takes 1-2 seconds. NEVER CANCEL. |
| 20 | +- Run linter: `dart analyze` -- takes 2-3 seconds. NEVER CANCEL. |
| 21 | +- Validate library functionality: `dart scripts/validate.dart` -- takes 1 second. NEVER CANCEL. |
| 22 | + |
| 23 | +**IMPORTANT**: This is a pure Dart library with very fast build and test times. All commands complete in seconds, not minutes. Use short timeouts (60 seconds max) for all operations. |
| 24 | + |
| 25 | +### Validation |
| 26 | +- ALWAYS run `dart pub get && dart test` after making changes to core library code. |
| 27 | +- ALWAYS run the validation scenario below to ensure your changes work correctly. |
| 28 | +- ALWAYS run `dart analyze` before finalizing changes to catch linting issues. |
| 29 | +- The build process is simple - no compilation or complex setup required. |
| 30 | + |
| 31 | +#### Validation Scenario |
| 32 | +After making changes, run this complete scenario to verify functionality: |
| 33 | +```bash |
| 34 | +dart scripts/validate.dart |
| 35 | +``` |
| 36 | +This script: |
| 37 | +1. Creates a schema with tables, columns, constraints, indices |
| 38 | +2. Applies schema to in-memory database using SchemaMigrator |
| 39 | +3. Uses DataAccess for insert/retrieve/update operations |
| 40 | +4. Tests bulk operations and migration planning |
| 41 | +5. Verifies all operations work correctly |
| 42 | + |
| 43 | +Should print success messages and complete in ~1 second. |
| 44 | + |
| 45 | +## Project Structure |
| 46 | + |
| 47 | +### Key Directories |
| 48 | +- `lib/src/`: Core library implementation |
| 49 | + - `schema_builder.dart`: Main entry point for defining schemas |
| 50 | + - `table_builder.dart`: Individual table structure definitions |
| 51 | + - `migrator.dart`: Database migration and validation logic |
| 52 | + - `data_access.dart`: Type-safe CRUD operations and bulk loading |
| 53 | + - `view_builder.dart`: SQL views support |
| 54 | +- `test/`: Comprehensive test suite with integration tests |
| 55 | +- `example/`: Example usage (some API compatibility issues exist) |
| 56 | +- `.github/`: GitHub workflows and documentation |
8 | 57 |
|
9 | 58 | ### Core Components |
10 | | - |
11 | 59 | 1. **SchemaBuilder**: Main entry point for defining database schemas |
12 | 60 | 2. **TableBuilder**: Defines individual table structures with columns and indices |
13 | 61 | 3. **ColumnBuilder**: Specifies column properties, constraints, and data types |
14 | | -4. **IndexBuilder**: Defines table indices (single and composite) |
15 | | -5. **SchemaMigrator**: Handles database migration and validation |
16 | | -6. **DataAccess**: Provides type-safe CRUD operations and bulk data loading |
17 | | - |
18 | | -### Design Patterns |
19 | | - |
20 | | -- **Immutable Builder Pattern**: All builders are immutable and return new instances |
21 | | -- **Fluent Interface**: Chainable method calls for readable schema definitions |
22 | | -- **Declarative Configuration**: Schema is defined, not executed step-by-step |
23 | | - |
24 | | -## Dart Language Guidelines |
25 | | - |
26 | | -### Code Style |
27 | | - |
28 | | -- Use `@immutable` annotations for all builder classes |
29 | | -- Prefer `const` constructors where possible |
30 | | -- Use named parameters for optional configuration |
31 | | -- Follow Dart naming conventions (camelCase for variables/methods, PascalCase for classes) |
32 | | -- Use trailing commas for better formatting and git diffs |
33 | | - |
34 | | -### Error Handling |
35 | | - |
36 | | -- Throw `ArgumentError` for invalid configuration (duplicate names, invalid constraints) |
37 | | -- Use descriptive error messages that help developers understand what went wrong |
38 | | -- Validate input at the builder level before SQL generation |
39 | | - |
40 | | -### Documentation |
| 62 | +4. **SchemaMigrator**: Handles database migration and validation |
| 63 | +5. **DataAccess**: Provides type-safe CRUD operations and bulk data loading |
41 | 64 |
|
42 | | -- Use triple-slash (`///`) comments for public API documentation |
43 | | -- Include code examples in documentation comments |
44 | | -- Document parameter constraints and return values |
45 | | -- Use `@param` and `@returns` tags where helpful |
46 | | - |
47 | | -## Code Patterns |
48 | | - |
49 | | -### Builder Pattern Implementation |
| 65 | +## Common Development Tasks |
50 | 66 |
|
| 67 | +### Creating Schemas |
51 | 68 | ```dart |
52 | | -@immutable |
53 | | -class ExampleBuilder { |
54 | | - const ExampleBuilder._({required this.property}); |
55 | | - |
56 | | - // Public constructor starts with empty/default state |
57 | | - const ExampleBuilder() : property = defaultValue; |
58 | | - |
59 | | - final PropertyType property; |
60 | | - |
61 | | - // Builder methods return new instances |
62 | | - ExampleBuilder withProperty(PropertyType value) { |
63 | | - return ExampleBuilder._(property: value); |
64 | | - } |
65 | | -} |
| 69 | +final schema = SchemaBuilder() |
| 70 | + .table('users', (table) => table |
| 71 | + .autoIncrementPrimaryKey('id') |
| 72 | + .text('name', (col) => col.notNull()) |
| 73 | + .text('email', (col) => col.unique()) |
| 74 | + .integer('age') |
| 75 | + .index('idx_email', ['email'])); |
66 | 76 | ``` |
67 | 77 |
|
68 | | -### Fluent Interface Methods |
| 78 | +### Running Tests |
| 79 | +- Full test suite: `dart test` (6-8 seconds) |
| 80 | +- Integration tests: `dart test test/integration_test.dart` (1-2 seconds) |
| 81 | +- Data access tests: `dart test test/data_access_test.dart` (1-2 seconds) |
| 82 | +- View builder tests: `dart test test/view_builder_test.dart` (1-2 seconds) |
69 | 83 |
|
70 | | -- Methods should return the builder instance for chaining |
71 | | -- Use descriptive method names that read like natural language |
72 | | -- Group related configuration methods together |
73 | | -- Provide both simple and advanced overloads |
| 84 | +### Adding New Features |
| 85 | +1. Write tests first in appropriate test file (`test/*_test.dart`) |
| 86 | +2. Implement feature in `lib/src/` following immutable builder pattern |
| 87 | +3. Run `dart test` to verify all tests pass |
| 88 | +4. Run `dart analyze` to check code quality |
| 89 | +5. Test with `dart test/validate_example.dart` for integration validation |
74 | 90 |
|
75 | | -### SQL Generation |
| 91 | +## Known Issues and Workarounds |
76 | 92 |
|
77 | | -- Generate standards-compliant SQLite DDL statements |
78 | | -- Handle SQL injection prevention in data access layer |
79 | | -- Use parameterized queries for all data operations |
80 | | -- Validate SQL identifiers (table names, column names) |
| 93 | +### Current Issues |
| 94 | +- Some example files (`example/`) have API compatibility issues - avoid using as reference |
| 95 | +- Relationship features (`test/relationship_test.dart`) have parameter naming issues in progress |
| 96 | +- View API has some deprecated methods - use `ViewBuilder.create()` pattern |
81 | 97 |
|
82 | | -## Testing Guidelines |
| 98 | +### Working Areas |
| 99 | +- Core schema definition (SchemaBuilder, TableBuilder) - fully functional |
| 100 | +- Database migration (SchemaMigrator) - fully functional |
| 101 | +- Data access operations (DataAccess) - fully functional |
| 102 | +- Basic view support (ViewBuilder) - core functionality works |
83 | 103 |
|
84 | | -### Test Structure |
| 104 | +## Build Troubleshooting |
85 | 105 |
|
86 | | -- Use `group()` to organize related tests |
87 | | -- Use descriptive test names that explain the behavior being tested |
88 | | -- Test both positive and negative cases (valid/invalid inputs) |
89 | | -- Include integration tests for complete workflows |
| 106 | +If you encounter issues: |
| 107 | +1. Ensure Dart SDK 3.5.3+ is installed: `dart --version` |
| 108 | +2. Clean and reinstall dependencies: `dart pub get` |
| 109 | +3. Run tests to check current state: `dart test` |
| 110 | +4. Check for linting issues: `dart analyze` |
90 | 111 |
|
91 | | -### Test Patterns |
| 112 | +The library has no complex build dependencies - if basic Dart commands work, the library should work. |
92 | 113 |
|
| 114 | +## API Reference Quick Start |
| 115 | + |
| 116 | +### Basic Usage Pattern |
93 | 117 | ```dart |
94 | | -group('ComponentName', () { |
95 | | - test('can perform basic operation', () { |
96 | | - // Arrange |
97 | | - final builder = ComponentBuilder(); |
98 | | - |
99 | | - // Act |
100 | | - final result = builder.operation(); |
101 | | - |
102 | | - // Assert |
103 | | - expect(result.property, equals(expectedValue)); |
104 | | - }); |
105 | | - |
106 | | - test('throws error for invalid input', () { |
107 | | - expect(() => ComponentBuilder().invalidOperation(), |
108 | | - throwsA(isA<ArgumentError>())); |
109 | | - }); |
110 | | -}); |
| 118 | +import 'package:declarative_sqlite/declarative_sqlite.dart'; |
| 119 | +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; |
| 120 | +
|
| 121 | +// 1. Initialize for testing |
| 122 | +sqfliteFfiInit(); |
| 123 | +databaseFactory = databaseFactoryFfi; |
| 124 | +
|
| 125 | +// 2. Define schema |
| 126 | +final schema = SchemaBuilder() |
| 127 | + .table('tablename', (table) => table |
| 128 | + .autoIncrementPrimaryKey('id') |
| 129 | + .text('column1', (col) => col.notNull()) |
| 130 | + .integer('column2')); |
| 131 | +
|
| 132 | +// 3. Apply to database |
| 133 | +final database = await openDatabase(':memory:'); |
| 134 | +final migrator = SchemaMigrator(); |
| 135 | +await migrator.migrate(database, schema); |
| 136 | +
|
| 137 | +// 4. Use data access |
| 138 | +final dataAccess = DataAccess(database: database, schema: schema); |
| 139 | +final id = await dataAccess.insert('tablename', {'column1': 'value'}); |
| 140 | +final row = await dataAccess.getByPrimaryKey('tablename', id); |
111 | 141 | ``` |
112 | 142 |
|
113 | | -### Database Testing |
114 | | - |
115 | | -- Use in-memory SQLite databases for tests (`':memory:'`) |
116 | | -- Test schema migration scenarios (empty DB, existing schema) |
117 | | -- Verify generated SQL statements match expectations |
118 | | -- Test data access operations with actual database interactions |
119 | | - |
120 | | -## Library-Specific Guidelines |
121 | | - |
122 | | -### Schema Definition |
123 | | - |
124 | | -- Always validate schema consistency (no duplicate table/column names) |
125 | | -- Support both simple and composite primary keys |
126 | | -- Handle system columns (systemId, systemVersion) automatically |
127 | | -- Provide meaningful validation errors for schema conflicts |
128 | | - |
129 | | -### Data Types |
| 143 | +This pattern works for all core functionality and is the foundation for any development work. |
130 | 144 |
|
131 | | -- Map Dart types to SQLite affinities correctly: |
132 | | - - `int` → INTEGER |
133 | | - - `double` → REAL |
134 | | - - `String` → TEXT |
135 | | - - `Uint8List` → BLOB |
136 | | -- Support nullable and non-nullable columns appropriately |
137 | | -- Handle default values and constraints properly |
| 145 | +## Common Files and Commands Reference |
138 | 146 |
|
139 | | -### Migration Safety |
140 | | - |
141 | | -- Only support additive migrations (no destructive changes) |
142 | | -- Validate existing schema before applying changes |
143 | | -- Provide migration preview functionality |
144 | | -- Handle edge cases like existing data conflicts |
145 | | - |
146 | | -### Data Access Layer |
147 | | - |
148 | | -- Provide type-safe methods that use schema metadata |
149 | | -- Support bulk operations with proper error handling |
150 | | -- Handle missing/extra columns gracefully in bulk loading |
151 | | -- Use transactions for batch operations |
152 | | - |
153 | | -## API Design Principles |
154 | | - |
155 | | -### Consistency |
156 | | - |
157 | | -- Use consistent naming patterns across all builders |
158 | | -- Provide similar method signatures for related operations |
159 | | -- Handle optional parameters uniformly |
160 | | -- Use consistent error types and messages |
161 | | - |
162 | | -### Discoverability |
163 | | - |
164 | | -- Use method names that clearly indicate their purpose |
165 | | -- Provide multiple ways to achieve common tasks |
166 | | -- Include comprehensive examples in documentation |
167 | | -- Design APIs that guide users toward correct usage |
168 | | - |
169 | | -### Performance |
170 | | - |
171 | | -- Minimize object allocation in hot paths |
172 | | -- Use efficient SQL generation (avoid string concatenation) |
173 | | -- Batch database operations where possible |
174 | | -- Provide configuration options for performance tuning |
175 | | - |
176 | | -## Common Pitfalls to Avoid |
177 | | - |
178 | | -1. **Mutating Builder State**: All builders must be immutable |
179 | | -2. **SQL Injection**: Always use parameterized queries for user data |
180 | | -3. **Schema Validation**: Don't skip validation of table/column names and constraints |
181 | | -4. **Transaction Management**: Ensure proper transaction handling in bulk operations |
182 | | -5. **Resource Cleanup**: Close database connections and statements properly |
183 | | - |
184 | | -## Development Workflow |
185 | | - |
186 | | -### Adding New Features |
| 147 | +### Repository Root Structure |
| 148 | +``` |
| 149 | +declarative_sqlite/ |
| 150 | +├── .github/ # GitHub workflows and documentation |
| 151 | +├── .gitignore |
| 152 | +├── CHANGELOG.md |
| 153 | +├── LICENSE |
| 154 | +├── README.md # Main project documentation |
| 155 | +├── analysis_options.yaml # Dart linter configuration |
| 156 | +├── pubspec.yaml # Project dependencies and metadata |
| 157 | +├── lib/ |
| 158 | +│ ├── declarative_sqlite.dart # Main library export |
| 159 | +│ └── src/ # Core implementation |
| 160 | +│ ├── schema_builder.dart |
| 161 | +│ ├── table_builder.dart |
| 162 | +│ ├── migrator.dart |
| 163 | +│ ├── data_access.dart |
| 164 | +│ └── view_builder.dart |
| 165 | +├── test/ # Test files |
| 166 | +│ ├── integration_test.dart |
| 167 | +│ ├── data_access_test.dart |
| 168 | +│ └── view_builder_test.dart |
| 169 | +├── example/ # Usage examples (some API issues) |
| 170 | +└── scripts/ # Utility scripts |
| 171 | + └── validate.dart # Library validation script |
| 172 | +``` |
187 | 173 |
|
188 | | -1. Define the public API first with documentation |
189 | | -2. Implement builder classes with immutable pattern |
190 | | -3. Add SQL generation and validation logic |
191 | | -4. Write comprehensive tests (unit and integration) |
192 | | -5. Update library exports and documentation |
193 | | -6. Install dart sdk then run all tests |
| 174 | +### Dependencies from pubspec.yaml |
| 175 | +```yaml |
| 176 | +dependencies: |
| 177 | + sqflite_common: ^2.5.4+3 |
| 178 | + path: ^1.8.0 |
| 179 | + meta: ^1.11.0 |
| 180 | + |
| 181 | +dev_dependencies: |
| 182 | + lints: ^4.0.0 |
| 183 | + test: ^1.24.0 |
| 184 | + sqflite_common_ffi: ^2.3.4+4 # For testing |
| 185 | +``` |
194 | 186 |
|
195 | | -### Code Review Checklist |
| 187 | +### Frequently Used Commands Output |
| 188 | +```bash |
| 189 | +# dart --version |
| 190 | +Dart SDK version: 3.9.3 (stable) |
196 | 191 |
|
197 | | -- [ ] All public APIs are documented |
198 | | -- [ ] Builder classes are immutable |
199 | | -- [ ] Error cases are handled with clear messages |
200 | | -- [ ] Tests cover both success and failure scenarios |
201 | | -- [ ] SQL generation is safe and standards-compliant |
202 | | -- [ ] Performance considerations are addressed |
| 192 | +# dart pub get (fast - 3-4 seconds) |
| 193 | +Resolving dependencies... |
| 194 | ++ 54 dependencies installed |
203 | 195 |
|
204 | | -## Examples and References |
| 196 | +# dart test (fast - 6-8 seconds total) |
| 197 | +✅ 90 tests passed, 1 failed (relationship test has issues) |
205 | 198 |
|
206 | | -Refer to existing code in `lib/src/` for established patterns and `test/` directory for testing approaches. The library follows standard Dart package conventions and SQLite best practices. |
| 199 | +# dart analyze (fast - 2-3 seconds) |
| 200 | +Shows linting issues but core functionality works |
| 201 | +``` |
0 commit comments