Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 0 additions & 136 deletions .github/workflows/release-ir.yml

This file was deleted.

17 changes: 4 additions & 13 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co

The tool is written in Go 1.24+ (toolchain go1.24.7) and uses:
- Cobra for CLI commands
- embedded-postgres v1.29.0 for integration testing (no Docker required)
- embedded-postgres v1.29.0 for plan command (temporary instances) and testing (no Docker required)
- pgx/v5 v5.7.5 for database connections
- pg_query_go/v6 v6.1.0 for SQL parsing
- Supports PostgreSQL versions 14-17

Key differentiators:
Expand Down Expand Up @@ -86,7 +85,7 @@ PGPASSWORD=testpwd1
- `root.go` - Main CLI setup with Cobra

**Core Packages**:
- `ir/` - Intermediate Representation (IR) package - separate Go module
- `ir/` - Intermediate Representation (IR) package
- Schema objects (tables, indexes, functions, procedures, triggers, policies, etc.)
- Database inspector using pgx (queries pg_catalog for schema extraction)
- Schema normalizer
Expand All @@ -113,9 +112,7 @@ PGPASSWORD=testpwd1

**Database Integration**: Uses `pgx/v5` for database connections and `embedded-postgres` (v1.29.0) for both the plan command (temporary instances) and integration testing (no Docker required).

**SQL Parsing**: Uses `pg_query_go/v6` (libpg_query bindings) for limited SQL expression parsing within the inspector (e.g., view definitions, CHECK constraints). The parser module was removed in favor of the embedded-postgres approach.

**Modular Architecture**: The IR package is a separate Go module that can be versioned and used independently.
**Inspector-Only Approach**: Both desired state (from user SQL files) and current state (from target database) are obtained through database inspection. The plan command spins up an embedded PostgreSQL instance, applies user SQL files, then inspects it to get the desired state IR. This eliminates the need for SQL parsing and ensures consistency.

## Common Development Workflows

Expand All @@ -135,12 +132,6 @@ Note: Parser logic is no longer needed - both desired and current states come fr
2. Use **Validate with Database** skill to test queries against live PostgreSQL
3. Compare pg_dump output with pgschema output using workflows in **Validate with Database** skill

### Understanding SQL Syntax

1. Consult **PostgreSQL Syntax Reference** skill to find grammar rules in gram.y
2. Understand how pg_query_go parse tree maps to grammar
3. Test parsing with real SQL using **Validate with Database** skill

### Testing Changes

1. Use **Run Tests** skill for comprehensive testing workflows
Expand Down Expand Up @@ -198,7 +189,7 @@ The tool supports comprehensive PostgreSQL schema objects (see `ir/ir.go` for co
- `main.go` - Entry point, loads .env and calls cmd.Execute()
- `cmd/root.go` - Root CLI with global flags

**IR Package** (separate Go module at `./ir`):
**IR Package** (`./ir`):
- `ir/ir.go` - Core IR data structures for all schema objects
- `ir/inspector.go` - Database introspection using pgx (queries pg_catalog)
- `ir/normalize.go` - Schema normalization (version-specific differences, type mappings)
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ require (
github.com/google/go-cmp v0.7.0
github.com/jackc/pgx/v5 v5.7.5
github.com/joho/godotenv v1.5.1
github.com/pgschema/pgschema/ir v0.0.0
github.com/lib/pq v1.10.9
github.com/spf13/cobra v1.9.1
golang.org/x/sync v0.17.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/text v0.24.0 // indirect
)

replace github.com/pgschema/pgschema/ir => ./ir
73 changes: 11 additions & 62 deletions ir/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,21 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/pgschema/pgschema/ir.svg)](https://pkg.go.dev/github.com/pgschema/pgschema/ir)
[![Go Report Card](https://goreportcard.com/badge/github.com/pgschema/pgschema/ir)](https://goreportcard.com/report/github.com/pgschema/pgschema/ir)

The `ir` package provides an Intermediate Representation for PostgreSQL database schemas. It can be used by external projects to parse SQL files, introspect live databases, and work with normalized schema representations.
The `ir` package provides an Intermediate Representation for PostgreSQL database schemas. It introspects live databases using PostgreSQL system catalogs and provides normalized schema representations.

## Installation

### Latest Version
```bash
go get github.com/pgschema/pgschema/ir
go get github.com/pgschema/pgschema
```

### Specific Version
```bash
go get github.com/pgschema/pgschema/ir@ir/v0.1.0
```

## Usage

### Parsing SQL Files

Then import the ir package:
```go
import "github.com/pgschema/pgschema/ir"

// Parse SQL file into IR
content, err := os.ReadFile("schema.sql")
if err != nil {
log.Fatal(err)
}

// Create a parser with default schema and no ignore config
parser := ir.NewParser("public", nil)
schema, err := parser.ParseSQL(string(content))
if err != nil {
log.Fatal(err)
}

// Access tables, views, functions, etc.
if publicSchema, ok := schema.GetSchema("public"); ok {
for tableName, table := range publicSchema.Tables {
fmt.Printf("Table: %s\n", tableName)
for _, column := range table.Columns {
fmt.Printf(" Column: %s %s\n", column.Name, column.DataType)
}
}
}
```

## Usage

### Introspecting Live Database

```go
Expand Down Expand Up @@ -105,11 +75,11 @@ newSchema := // ... parse or introspect new schema

## Key Features

- **SQL Parsing**: Supports PostgreSQL DDL via libpg_query bindings
- **Database Introspection**: Query live databases using optimized SQL queries
- **Normalization**: Consistent representation regardless of input source
- **Normalization**: Consistent representation from PostgreSQL system catalogs
- **Rich Type System**: Full support for PostgreSQL data types and constraints
- **Concurrent Safe**: Thread-safe access to schema data structures
- **Embedded Testing**: Use embedded PostgreSQL for testing without Docker (see `ParseSQLForTest` in testutil.go)

## Schema Object Types

Expand Down Expand Up @@ -167,35 +137,14 @@ tables, err := q.GetTables(ctx, "public")
## Testing

```bash
# Unit tests only (fast, no Docker required)
go test -short -v ./...

# Integration tests (requires Docker for testcontainers)
# Run all tests (uses embedded PostgreSQL, no Docker required)
go test -v ./...

# Specific integration tests
go test -v ./ -run "TestIRIntegration_Employee"
# Skip integration tests (faster)
go test -short -v ./...
```

## Versioning

This package follows semantic versioning. Releases are tagged with the pattern `ir/v<major>.<minor>.<patch>`.

### Release Process

1. Update the version in `ir/VERSION`
2. Commit the change
3. Create and push a tag: `git tag ir/v0.1.0 && git push origin ir/v0.1.0`
4. Or trigger a manual release from the GitHub Actions workflow

### Changelog

#### v0.1.0
- Initial standalone release of the IR package
- Support for PostgreSQL schema parsing and introspection
- Complete type system for tables, views, functions, procedures, types, and sequences

## Version Compatibility
## Compatibility

- **Go**: 1.24.0+
- **PostgreSQL**: 14, 15, 16, 17
Expand Down
1 change: 0 additions & 1 deletion ir/VERSION

This file was deleted.

21 changes: 0 additions & 21 deletions ir/go.mod

This file was deleted.

47 changes: 0 additions & 47 deletions ir/go.sum

This file was deleted.

Loading