Compare commits
4 Commits
e61204cb3c
...
v1.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 9f29bc112e | |||
| b55737ab4c | |||
| 2a271b9859 | |||
| beb5b4fac8 |
36
.github/workflows/ci.yml
vendored
36
.github/workflows/ci.yml
vendored
@@ -34,8 +34,8 @@ jobs:
|
|||||||
- name: Download dependencies
|
- name: Download dependencies
|
||||||
run: go mod download
|
run: go mod download
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run unit tests
|
||||||
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
|
run: make test
|
||||||
|
|
||||||
- name: Upload coverage to Codecov
|
- name: Upload coverage to Codecov
|
||||||
uses: codecov/codecov-action@v4
|
uses: codecov/codecov-action@v4
|
||||||
@@ -55,13 +55,15 @@ jobs:
|
|||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
|
|
||||||
- name: golangci-lint
|
- name: Install golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v9
|
run: |
|
||||||
with:
|
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin latest
|
||||||
version: latest
|
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
|
||||||
args: --config=.golangci.json
|
|
||||||
|
- name: Run linter
|
||||||
|
run: make lint
|
||||||
|
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build
|
||||||
@@ -74,10 +76,22 @@ jobs:
|
|||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.25'
|
||||||
|
|
||||||
- name: Build
|
- name: Download dependencies
|
||||||
run: go build -v ./cmd/relspec
|
run: go mod download
|
||||||
|
|
||||||
|
- name: Build binary
|
||||||
|
run: make build
|
||||||
|
|
||||||
|
- name: Verify binary exists
|
||||||
|
run: |
|
||||||
|
if [ ! -f build/relspec ]; then
|
||||||
|
echo "Error: Binary not found at build/relspec"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Build successful: build/relspec"
|
||||||
|
ls -lh build/relspec
|
||||||
|
|
||||||
- name: Check mod tidiness
|
- name: Check mod tidiness
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
91
.github/workflows/integration-tests.yml
vendored
Normal file
91
.github/workflows/integration-tests.yml
vendored
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
name: Integration Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ master ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
integration-tests:
|
||||||
|
name: Integration Tests
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: '1.25'
|
||||||
|
|
||||||
|
- name: Cache Go modules
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: ~/go/pkg/mod
|
||||||
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-
|
||||||
|
|
||||||
|
- name: Download dependencies
|
||||||
|
run: go mod download
|
||||||
|
|
||||||
|
- name: Start PostgreSQL container
|
||||||
|
run: |
|
||||||
|
docker run -d \
|
||||||
|
--name relspec-test-postgres \
|
||||||
|
--network host \
|
||||||
|
-e POSTGRES_USER=relspec \
|
||||||
|
-e POSTGRES_PASSWORD=relspec_test_password \
|
||||||
|
-e POSTGRES_DB=relspec_test \
|
||||||
|
postgres:16-alpine
|
||||||
|
|
||||||
|
- name: Wait for PostgreSQL to be ready
|
||||||
|
run: |
|
||||||
|
echo "Waiting for PostgreSQL to start..."
|
||||||
|
for i in {1..30}; do
|
||||||
|
if docker exec relspec-test-postgres pg_isready -U relspec -d relspec_test > /dev/null 2>&1; then
|
||||||
|
echo "PostgreSQL is ready!"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
echo "Waiting... ($i/30)"
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
- name: Copy init script into container
|
||||||
|
run: |
|
||||||
|
docker cp tests/postgres/init.sql relspec-test-postgres:/tmp/init.sql
|
||||||
|
|
||||||
|
- name: Initialize test database
|
||||||
|
run: |
|
||||||
|
docker exec relspec-test-postgres psql -U relspec -d relspec_test -f /tmp/init.sql
|
||||||
|
|
||||||
|
- name: Verify database setup
|
||||||
|
run: |
|
||||||
|
echo "Verifying database initialization..."
|
||||||
|
docker exec relspec-test-postgres psql -U relspec -d relspec_test -c "
|
||||||
|
SELECT
|
||||||
|
(SELECT COUNT(*) FROM pg_namespace WHERE nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast') AND nspname NOT LIKE 'pg_%') as schemas,
|
||||||
|
(SELECT COUNT(*) FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema')) as tables,
|
||||||
|
(SELECT COUNT(*) FROM pg_views WHERE schemaname NOT IN ('pg_catalog', 'information_schema')) as views,
|
||||||
|
(SELECT COUNT(*) FROM pg_sequences WHERE schemaname NOT IN ('pg_catalog', 'information_schema')) as sequences;
|
||||||
|
"
|
||||||
|
|
||||||
|
- name: Run integration tests
|
||||||
|
env:
|
||||||
|
RELSPEC_TEST_PG_CONN: postgres://relspec:relspec_test_password@localhost:5432/relspec_test
|
||||||
|
run: make test-integration
|
||||||
|
|
||||||
|
- name: Stop PostgreSQL container
|
||||||
|
if: always()
|
||||||
|
run: |
|
||||||
|
docker stop relspec-test-postgres || true
|
||||||
|
docker rm relspec-test-postgres || true
|
||||||
|
|
||||||
|
- name: Summary
|
||||||
|
if: always()
|
||||||
|
run: |
|
||||||
|
echo "Integration tests completed."
|
||||||
|
echo "PostgreSQL container has been cleaned up."
|
||||||
116
.github/workflows/release.yml
vendored
Normal file
116
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*.*.*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-and-release:
|
||||||
|
name: Build and Release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: '1.25'
|
||||||
|
|
||||||
|
- name: Get version from tag
|
||||||
|
id: get_version
|
||||||
|
run: |
|
||||||
|
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
|
||||||
|
echo "Version: ${GITHUB_REF#refs/tags/}"
|
||||||
|
|
||||||
|
- name: Build binaries for multiple platforms
|
||||||
|
run: |
|
||||||
|
mkdir -p dist
|
||||||
|
|
||||||
|
# Linux AMD64
|
||||||
|
GOOS=linux GOARCH=amd64 go build -o dist/relspec-linux-amd64 -ldflags "-X main.version=${{ steps.get_version.outputs.VERSION }}" ./cmd/relspec
|
||||||
|
|
||||||
|
# Linux ARM64
|
||||||
|
GOOS=linux GOARCH=arm64 go build -o dist/relspec-linux-arm64 -ldflags "-X main.version=${{ steps.get_version.outputs.VERSION }}" ./cmd/relspec
|
||||||
|
|
||||||
|
# macOS AMD64
|
||||||
|
GOOS=darwin GOARCH=amd64 go build -o dist/relspec-darwin-amd64 -ldflags "-X main.version=${{ steps.get_version.outputs.VERSION }}" ./cmd/relspec
|
||||||
|
|
||||||
|
# macOS ARM64 (Apple Silicon)
|
||||||
|
GOOS=darwin GOARCH=arm64 go build -o dist/relspec-darwin-arm64 -ldflags "-X main.version=${{ steps.get_version.outputs.VERSION }}" ./cmd/relspec
|
||||||
|
|
||||||
|
# Windows AMD64
|
||||||
|
GOOS=windows GOARCH=amd64 go build -o dist/relspec-windows-amd64.exe -ldflags "-X main.version=${{ steps.get_version.outputs.VERSION }}" ./cmd/relspec
|
||||||
|
|
||||||
|
# Create checksums
|
||||||
|
cd dist
|
||||||
|
sha256sum * > checksums.txt
|
||||||
|
cd ..
|
||||||
|
|
||||||
|
- name: Generate release notes
|
||||||
|
id: release_notes
|
||||||
|
run: |
|
||||||
|
# Get the previous tag
|
||||||
|
previous_tag=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
|
||||||
|
|
||||||
|
if [ -z "$previous_tag" ]; then
|
||||||
|
# No previous tag, get all commits
|
||||||
|
commits=$(git log --pretty=format:"- %s (%h)" --no-merges)
|
||||||
|
else
|
||||||
|
# Get commits since the previous tag
|
||||||
|
commits=$(git log "${previous_tag}..HEAD" --pretty=format:"- %s (%h)" --no-merges)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create release notes
|
||||||
|
cat > release_notes.md << EOF
|
||||||
|
# Release ${{ steps.get_version.outputs.VERSION }}
|
||||||
|
|
||||||
|
## Changes
|
||||||
|
|
||||||
|
${commits}
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Download the appropriate binary for your platform:
|
||||||
|
|
||||||
|
- **Linux (AMD64)**: \`relspec-linux-amd64\`
|
||||||
|
- **Linux (ARM64)**: \`relspec-linux-arm64\`
|
||||||
|
- **macOS (Intel)**: \`relspec-darwin-amd64\`
|
||||||
|
- **macOS (Apple Silicon)**: \`relspec-darwin-arm64\`
|
||||||
|
- **Windows (AMD64)**: \`relspec-windows-amd64.exe\`
|
||||||
|
|
||||||
|
Make the binary executable (Linux/macOS):
|
||||||
|
\`\`\`bash
|
||||||
|
chmod +x relspec-*
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
Verify the download with the provided checksums.
|
||||||
|
EOF
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
uses: softprops/action-gh-release@v1
|
||||||
|
with:
|
||||||
|
body_path: release_notes.md
|
||||||
|
files: |
|
||||||
|
dist/relspec-linux-amd64
|
||||||
|
dist/relspec-linux-arm64
|
||||||
|
dist/relspec-darwin-amd64
|
||||||
|
dist/relspec-darwin-arm64
|
||||||
|
dist/relspec-windows-amd64.exe
|
||||||
|
dist/checksums.txt
|
||||||
|
draft: false
|
||||||
|
prerelease: false
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Summary
|
||||||
|
run: |
|
||||||
|
echo "Release ${{ steps.get_version.outputs.VERSION }} created successfully!"
|
||||||
|
echo "Binaries built for:"
|
||||||
|
echo " - Linux (amd64, arm64)"
|
||||||
|
echo " - macOS (amd64, arm64)"
|
||||||
|
echo " - Windows (amd64)"
|
||||||
72
Makefile
72
Makefile
@@ -1,4 +1,4 @@
|
|||||||
.PHONY: all build test lint coverage clean install help docker-up docker-down docker-test docker-test-integration
|
.PHONY: all build test test-unit test-integration lint coverage clean install help docker-up docker-down docker-test docker-test-integration release release-version
|
||||||
|
|
||||||
# Binary name
|
# Binary name
|
||||||
BINARY_NAME=relspec
|
BINARY_NAME=relspec
|
||||||
@@ -22,9 +22,23 @@ build: ## Build the binary
|
|||||||
$(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/relspec
|
$(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/relspec
|
||||||
@echo "Build complete: $(BUILD_DIR)/$(BINARY_NAME)"
|
@echo "Build complete: $(BUILD_DIR)/$(BINARY_NAME)"
|
||||||
|
|
||||||
test: ## Run tests
|
test: test-unit ## Run all unit tests (alias for test-unit)
|
||||||
@echo "Running tests..."
|
|
||||||
$(GOTEST) -v -race -coverprofile=coverage.out ./...
|
test-unit: ## Run unit tests (excludes integration tests)
|
||||||
|
@echo "Running unit tests..."
|
||||||
|
$(GOTEST) -v -race -coverprofile=coverage.out -covermode=atomic $$(go list ./... | grep -v '/tests/integration' | grep -v '/tests/assets' | grep -v '/pkg/readers/pgsql')
|
||||||
|
|
||||||
|
test-integration: ## Run integration tests (requires RELSPEC_TEST_PG_CONN environment variable)
|
||||||
|
@echo "Running integration tests..."
|
||||||
|
@if [ -z "$$RELSPEC_TEST_PG_CONN" ]; then \
|
||||||
|
echo "Error: RELSPEC_TEST_PG_CONN environment variable is not set"; \
|
||||||
|
echo "Example: export RELSPEC_TEST_PG_CONN='postgres://relspec:relspec_test_password@localhost:5432/relspec_test'"; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
@echo "Running PostgreSQL reader tests..."
|
||||||
|
$(GOTEST) -v -count=1 ./pkg/readers/pgsql/
|
||||||
|
@echo "Running general integration tests..."
|
||||||
|
$(GOTEST) -v -count=1 ./tests/integration/
|
||||||
|
|
||||||
coverage: test ## Run tests with coverage report
|
coverage: test ## Run tests with coverage report
|
||||||
@echo "Generating coverage report..."
|
@echo "Generating coverage report..."
|
||||||
@@ -98,5 +112,55 @@ docker-test-integration: docker-up ## Start DB and run integration tests
|
|||||||
$(GOTEST) -v ./pkg/readers/pgsql/ -count=1 || (make docker-down && exit 1)
|
$(GOTEST) -v ./pkg/readers/pgsql/ -count=1 || (make docker-down && exit 1)
|
||||||
@make docker-down
|
@make docker-down
|
||||||
|
|
||||||
|
release: ## Create and push a new release tag (auto-increments patch version)
|
||||||
|
@echo "Creating new release..."
|
||||||
|
@latest_tag=$$(git describe --tags --abbrev=0 2>/dev/null || echo ""); \
|
||||||
|
if [ -z "$$latest_tag" ]; then \
|
||||||
|
version="v1.0.0"; \
|
||||||
|
echo "No existing tags found. Creating first release: $$version"; \
|
||||||
|
commit_logs=$$(git log --pretty=format:"- %s" --no-merges); \
|
||||||
|
else \
|
||||||
|
echo "Latest tag: $$latest_tag"; \
|
||||||
|
version_number=$${latest_tag#v}; \
|
||||||
|
IFS='.' read -r major minor patch <<< "$$version_number"; \
|
||||||
|
patch=$$((patch + 1)); \
|
||||||
|
version="v$$major.$$minor.$$patch"; \
|
||||||
|
echo "Creating new release: $$version"; \
|
||||||
|
commit_logs=$$(git log "$${latest_tag}..HEAD" --pretty=format:"- %s" --no-merges); \
|
||||||
|
fi; \
|
||||||
|
if [ -z "$$commit_logs" ]; then \
|
||||||
|
tag_message="Release $$version"; \
|
||||||
|
else \
|
||||||
|
tag_message="Release $$version\n\n$$commit_logs"; \
|
||||||
|
fi; \
|
||||||
|
git tag -a "$$version" -m "$$tag_message"; \
|
||||||
|
git push origin "$$version"; \
|
||||||
|
echo "Tag $$version created and pushed to remote repository."
|
||||||
|
|
||||||
|
release-version: ## Create and push a release with specific version (use: make release-version VERSION=v1.2.3)
|
||||||
|
@if [ -z "$(VERSION)" ]; then \
|
||||||
|
echo "Error: VERSION is required. Usage: make release-version VERSION=v1.2.3"; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
@version="$(VERSION)"; \
|
||||||
|
if ! echo "$$version" | grep -q "^v"; then \
|
||||||
|
version="v$$version"; \
|
||||||
|
fi; \
|
||||||
|
echo "Creating release: $$version"; \
|
||||||
|
latest_tag=$$(git describe --tags --abbrev=0 2>/dev/null || echo ""); \
|
||||||
|
if [ -z "$$latest_tag" ]; then \
|
||||||
|
commit_logs=$$(git log --pretty=format:"- %s" --no-merges); \
|
||||||
|
else \
|
||||||
|
commit_logs=$$(git log "$${latest_tag}..HEAD" --pretty=format:"- %s" --no-merges); \
|
||||||
|
fi; \
|
||||||
|
if [ -z "$$commit_logs" ]; then \
|
||||||
|
tag_message="Release $$version"; \
|
||||||
|
else \
|
||||||
|
tag_message="Release $$version\n\n$$commit_logs"; \
|
||||||
|
fi; \
|
||||||
|
git tag -a "$$version" -m "$$tag_message"; \
|
||||||
|
git push origin "$$version"; \
|
||||||
|
echo "Tag $$version created and pushed to remote repository."
|
||||||
|
|
||||||
help: ## Display this help screen
|
help: ## Display this help screen
|
||||||
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
|
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
|
||||||
|
|||||||
@@ -1,71 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Ask if the user wants to make a release version
|
|
||||||
read -p "Do you want to make a release version? (y/n): " make_release
|
|
||||||
|
|
||||||
if [[ $make_release =~ ^[Yy]$ ]]; then
|
|
||||||
# Get the latest tag from git
|
|
||||||
latest_tag=$(git describe --tags --abbrev=0 2>/dev/null)
|
|
||||||
|
|
||||||
if [ -z "$latest_tag" ]; then
|
|
||||||
# No tags exist yet, start with v1.0.0
|
|
||||||
suggested_version="v1.0.0"
|
|
||||||
echo "No existing tags found. Starting with $suggested_version"
|
|
||||||
else
|
|
||||||
echo "Latest tag: $latest_tag"
|
|
||||||
|
|
||||||
# Remove 'v' prefix if present
|
|
||||||
version_number="${latest_tag#v}"
|
|
||||||
|
|
||||||
# Split version into major.minor.patch
|
|
||||||
IFS='.' read -r major minor patch <<< "$version_number"
|
|
||||||
|
|
||||||
# Increment patch version
|
|
||||||
patch=$((patch + 1))
|
|
||||||
|
|
||||||
# Construct new version
|
|
||||||
suggested_version="v${major}.${minor}.${patch}"
|
|
||||||
echo "Suggested next version: $suggested_version"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Ask the user for the version number with the suggested version as default
|
|
||||||
read -p "Enter the version number (press Enter for $suggested_version): " version
|
|
||||||
|
|
||||||
# Use suggested version if user pressed Enter without input
|
|
||||||
if [ -z "$version" ]; then
|
|
||||||
version="$suggested_version"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Prepend 'v' to the version if it doesn't start with it
|
|
||||||
if ! [[ $version =~ ^v ]]; then
|
|
||||||
version="v$version"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Get commit logs since the last tag
|
|
||||||
if [ -z "$latest_tag" ]; then
|
|
||||||
# No previous tag, get all commits
|
|
||||||
commit_logs=$(git log --pretty=format:"- %s" --no-merges)
|
|
||||||
else
|
|
||||||
# Get commits since the last tag
|
|
||||||
commit_logs=$(git log "${latest_tag}..HEAD" --pretty=format:"- %s" --no-merges)
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Create the tag message
|
|
||||||
if [ -z "$commit_logs" ]; then
|
|
||||||
tag_message="Release $version"
|
|
||||||
else
|
|
||||||
tag_message="Release $version
|
|
||||||
|
|
||||||
${commit_logs}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Create an annotated tag with the commit logs
|
|
||||||
git tag -a "$version" -m "$tag_message"
|
|
||||||
|
|
||||||
# Push the tag to the remote repository
|
|
||||||
git push origin "$version"
|
|
||||||
|
|
||||||
echo "Tag $version created and pushed to the remote repository."
|
|
||||||
else
|
|
||||||
echo "No release version created."
|
|
||||||
fi
|
|
||||||
@@ -382,6 +382,23 @@ func (r *Reader) isRelationship(tag string) bool {
|
|||||||
return strings.Contains(tag, "bun:\"rel:") || strings.Contains(tag, ",rel:")
|
return strings.Contains(tag, "bun:\"rel:") || strings.Contains(tag, ",rel:")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getRelationType extracts the relationship type from a bun tag
|
||||||
|
func (r *Reader) getRelationType(bunTag string) string {
|
||||||
|
if strings.Contains(bunTag, "rel:has-many") {
|
||||||
|
return "has-many"
|
||||||
|
}
|
||||||
|
if strings.Contains(bunTag, "rel:belongs-to") {
|
||||||
|
return "belongs-to"
|
||||||
|
}
|
||||||
|
if strings.Contains(bunTag, "rel:has-one") {
|
||||||
|
return "has-one"
|
||||||
|
}
|
||||||
|
if strings.Contains(bunTag, "rel:many-to-many") {
|
||||||
|
return "many-to-many"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// parseRelationshipConstraints parses relationship fields to extract foreign key constraints
|
// parseRelationshipConstraints parses relationship fields to extract foreign key constraints
|
||||||
func (r *Reader) parseRelationshipConstraints(table *models.Table, structType *ast.StructType, structMap map[string]*models.Table) {
|
func (r *Reader) parseRelationshipConstraints(table *models.Table, structType *ast.StructType, structMap map[string]*models.Table) {
|
||||||
for _, field := range structType.Fields.List {
|
for _, field := range structType.Fields.List {
|
||||||
@@ -409,27 +426,51 @@ func (r *Reader) parseRelationshipConstraints(table *models.Table, structType *a
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse the join information: join:user_id=id
|
// Parse the join information: join:user_id=id
|
||||||
// This means: referencedTable.user_id = thisTable.id
|
// This means: thisTable.user_id = referencedTable.id
|
||||||
joinInfo := r.parseJoinInfo(bunTag)
|
joinInfo := r.parseJoinInfo(bunTag)
|
||||||
if joinInfo == nil {
|
if joinInfo == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// The FK is on the referenced table
|
// Determine which table gets the FK based on relationship type
|
||||||
|
relType := r.getRelationType(bunTag)
|
||||||
|
|
||||||
|
var fkTable *models.Table
|
||||||
|
var fkColumn, refTable, refColumn string
|
||||||
|
|
||||||
|
switch strings.ToLower(relType) {
|
||||||
|
case "belongs-to":
|
||||||
|
// For belongs-to: FK is on the current table
|
||||||
|
// join:user_id=id means table.user_id references referencedTable.id
|
||||||
|
fkTable = table
|
||||||
|
fkColumn = joinInfo.ForeignKey
|
||||||
|
refTable = referencedTable.Name
|
||||||
|
refColumn = joinInfo.ReferencedKey
|
||||||
|
case "has-many":
|
||||||
|
// For has-many: FK is on the referenced table
|
||||||
|
// join:id=user_id means referencedTable.user_id references table.id
|
||||||
|
fkTable = referencedTable
|
||||||
|
fkColumn = joinInfo.ReferencedKey
|
||||||
|
refTable = table.Name
|
||||||
|
refColumn = joinInfo.ForeignKey
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
constraint := &models.Constraint{
|
constraint := &models.Constraint{
|
||||||
Name: fmt.Sprintf("fk_%s_%s", referencedTable.Name, table.Name),
|
Name: fmt.Sprintf("fk_%s_%s", fkTable.Name, refTable),
|
||||||
Type: models.ForeignKeyConstraint,
|
Type: models.ForeignKeyConstraint,
|
||||||
Table: referencedTable.Name,
|
Table: fkTable.Name,
|
||||||
Schema: referencedTable.Schema,
|
Schema: fkTable.Schema,
|
||||||
Columns: []string{joinInfo.ForeignKey},
|
Columns: []string{fkColumn},
|
||||||
ReferencedTable: table.Name,
|
ReferencedTable: refTable,
|
||||||
ReferencedSchema: table.Schema,
|
ReferencedSchema: fkTable.Schema,
|
||||||
ReferencedColumns: []string{joinInfo.ReferencedKey},
|
ReferencedColumns: []string{refColumn},
|
||||||
OnDelete: "NO ACTION", // Bun doesn't specify this in tags
|
OnDelete: "NO ACTION", // Bun doesn't specify this in tags
|
||||||
OnUpdate: "NO ACTION",
|
OnUpdate: "NO ACTION",
|
||||||
}
|
}
|
||||||
|
|
||||||
referencedTable.Constraints[constraint.Name] = constraint
|
fkTable.Constraints[constraint.Name] = constraint
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user