Skip to content

Commit 04ffe07

Browse files
committed
Added migrations files
1 parent 5139db9 commit 04ffe07

File tree

15 files changed

+485
-198
lines changed

15 files changed

+485
-198
lines changed

.github/workflows/test-migrations.yml

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -216,95 +216,11 @@ jobs:
216216
echo "Checking if key tables exist..."
217217
sqlite3 test.db ".tables" | grep -E "units|organizations|users|rules"
218218
219-
test-sqlserver:
220-
name: Test SQL Server Migrations
221-
runs-on: ubuntu-latest
222-
223-
services:
224-
sqlserver:
225-
image: mcr.microsoft.com/mssql/server:2022-latest
226-
env:
227-
ACCEPT_EULA: Y
228-
SA_PASSWORD: YourStrong@Passw0rd
229-
MSSQL_PID: Developer
230-
ports:
231-
- 1433:1433
232-
options: >-
233-
--health-cmd "/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P YourStrong@Passw0rd -Q 'SELECT 1'"
234-
--health-interval 10s
235-
--health-timeout 5s
236-
--health-retries 10
237-
238-
steps:
239-
- name: Checkout code
240-
uses: actions/checkout@v4
241-
242-
- name: Setup Go
243-
uses: actions/setup-go@v5
244-
with:
245-
go-version: '1.22'
246-
cache-dependency-path: |
247-
taco/go.sum
248-
go.work.sum
249-
250-
- name: Install Atlas CLI
251-
run: |
252-
curl -sSf https://atlasgo.sh | sh
253-
atlas version
254-
255-
- name: Create test database
256-
run: |
257-
# Wait for SQL Server to be fully ready
258-
sleep 10
259-
docker exec $(docker ps -q --filter ancestor=mcr.microsoft.com/mssql/server:2022-latest) \
260-
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'YourStrong@Passw0rd' \
261-
-Q 'CREATE DATABASE opentaco_test'
262-
263-
- name: Build statesman binary
264-
working-directory: taco
265-
run: |
266-
go build -o statesman ./cmd/statesman
267-
268-
- name: Test migrations with binary
269-
working-directory: taco
270-
env:
271-
OPENTACO_BACKEND: mssql
272-
OPENTACO_MSSQL_HOST: localhost
273-
OPENTACO_MSSQL_PORT: 1433
274-
OPENTACO_MSSQL_USER: sa
275-
OPENTACO_MSSQL_PASSWORD: YourStrong@Passw0rd
276-
OPENTACO_MSSQL_DATABASE: opentaco_test
277-
OPENTACO_AUTH_DISABLE: true
278-
OPENTACO_JWT_SECRET: test-secret-key-for-ci
279-
run: |
280-
# Run binary in background (it will apply migrations on startup)
281-
timeout 30s ./statesman --port 8080 --auth-disable --storage memory || true
282-
283-
# Give it time to apply migrations
284-
sleep 5
285-
286-
- name: Verify migrations applied
287-
run: |
288-
echo "Checking if atlas_schema_revisions table exists..."
289-
docker exec $(docker ps -q --filter ancestor=mcr.microsoft.com/mssql/server:2022-latest) \
290-
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'YourStrong@Passw0rd' -d opentaco_test \
291-
-Q "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'atlas_schema_revisions'"
292-
293-
echo "Checking applied migrations..."
294-
docker exec $(docker ps -q --filter ancestor=mcr.microsoft.com/mssql/server:2022-latest) \
295-
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'YourStrong@Passw0rd' -d opentaco_test \
296-
-Q "SELECT version FROM atlas_schema_revisions ORDER BY version"
297-
298-
echo "Checking if key tables exist..."
299-
docker exec $(docker ps -q --filter ancestor=mcr.microsoft.com/mssql/server:2022-latest) \
300-
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'YourStrong@Passw0rd' -d opentaco_test \
301-
-Q "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME"
302-
303219
# Summary job that requires all database tests to pass
304220
all-migrations-passed:
305221
name: All Migrations Passed
306222
runs-on: ubuntu-latest
307-
needs: [test-postgres, test-mysql, test-sqlite, test-sqlserver]
223+
needs: [test-postgres, test-mysql, test-sqlite]
308224
steps:
309225
- name: All tests passed
310226
run: echo "✅ All database migrations tested successfully!"

go.work.sum

Lines changed: 32 additions & 22 deletions
Large diffs are not rendered by default.

taco/Makefile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,17 @@ atlas-install: ## Install Atlas CLI and GORM provider
104104
@go install ariga.io/atlas-provider-gorm@latest
105105
@echo "✅ Atlas tools installed successfully"
106106

107-
# Generate migrations for all database dialects
107+
# Generate migrations for all database dialects (Postgres, MySQL, SQLite)
108108
atlas-diff-all: ## Generate migrations for all databases (use: make atlas-diff-all name=my_change)
109109
@echo "Generating migrations for all databases..."
110110
@if [ -z "$(filter-out $(NAME),$(shell date +%Y%m%d%H%M%S))" ]; then \
111111
echo "⚠️ Using auto-generated name: $(NAME)"; \
112112
echo "💡 Tip: Use 'make atlas-diff-all name=descriptive_name' for better naming"; \
113113
fi
114-
@mkdir -p migrations/postgres migrations/mysql migrations/sqlite migrations/sqlserver
114+
@mkdir -p migrations/postgres migrations/mysql migrations/sqlite
115115
@echo "\n📊 PostgreSQL..." && atlas migrate diff $(NAME) --env postgres
116116
@echo "\n📊 MySQL..." && atlas migrate diff $(NAME) --env mysql
117117
@echo "\n📊 SQLite..." && atlas migrate diff $(NAME) --env sqlite
118-
@echo "\n📊 SQL Server..." && atlas migrate diff $(NAME) --env sqlserver
119118
@$(MAKE) atlas-hash-all
120119
@echo "\n✅ All migrations generated successfully!"
121120

@@ -126,12 +125,10 @@ atlas-lint-all: ## Validate and lint all migration files
126125
@echo "PostgreSQL..." && atlas migrate validate --env postgres
127126
@echo "MySQL..." && atlas migrate validate --env mysql
128127
@echo "SQLite..." && atlas migrate validate --env sqlite
129-
@echo "SQL Server..." && atlas migrate validate --env sqlserver
130128
@echo "✅ All migrations validated successfully"
131129

132130
# Update migration directory hashes (called automatically by atlas-diff-all)
133131
atlas-hash-all:
134132
@atlas migrate hash --dir file://migrations/postgres 2>/dev/null || true
135133
@atlas migrate hash --dir file://migrations/mysql 2>/dev/null || true
136-
@atlas migrate hash --dir file://migrations/sqlite 2>/dev/null || true
137-
@atlas migrate hash --dir file://migrations/sqlserver 2>/dev/null || true
134+
@atlas migrate hash --dir file://migrations/sqlite 2>/dev/null || true

taco/atlas.hcl

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
# SQLite configuration
2-
data "external_schema" "gorm_sqlite" {
3-
program = [
4-
"sh",
5-
"-c",
6-
"cd internal && go run ./atlas_loader.go sqlite",
7-
]
1+
variable "DB_URL" {
2+
type = string
3+
default = "postgres://postgres:postgres@localhost:5432/devdb?sslmode=disable"
84
}
95

10-
env "sqlite" {
11-
src = data.external_schema.gorm_sqlite.url
12-
dev = "sqlite://file?mode=memory"
13-
migration {
14-
dir = "file://internal/query/migration/atlas/migrations/sqlite"
15-
}
16-
format {
17-
migrate {
18-
diff = "{{ sql . \" \" }}"
19-
}
20-
}
6+
variable "POSTGRES_MIGRATIONS_DIR" {
7+
type = string
8+
default = "file://migrations/postgres"
9+
}
10+
11+
variable "MYSQL_MIGRATIONS_DIR" {
12+
type = string
13+
default = "file://migrations/mysql"
14+
}
15+
16+
variable "SQLITE_MIGRATIONS_DIR" {
17+
type = string
18+
default = "file://migrations/sqlite"
2119
}
2220

23-
# PostgreSQL configuration
2421
data "external_schema" "gorm_postgres" {
2522
program = [
2623
"sh",
@@ -31,10 +28,23 @@ data "external_schema" "gorm_postgres" {
3128

3229
env "postgres" {
3330
src = data.external_schema.gorm_postgres.url
34-
dev = "docker://postgres/16.1"
31+
url = var.DB_URL
32+
33+
# IMPORTANT: no env=… params here; keep 5m timeout.
34+
# If your build wants fully-qualified, use docker://docker.io/library/postgres/16
35+
dev = "docker://postgres/16?timeout=5m"
36+
37+
schemas = ["public"]
38+
3539
migration {
36-
dir = "file://internal/query/migration/atlas/migrations/postgres"
40+
dir = var.POSTGRES_MIGRATIONS_DIR
41+
format = "atlas"
42+
}
43+
44+
lint {
45+
destructive { error = true }
3746
}
47+
3848
format {
3949
migrate {
4050
diff = "{{ sql . \" \" }}"
@@ -53,32 +63,49 @@ data "external_schema" "gorm_mysql" {
5363

5464
env "mysql" {
5565
src = data.external_schema.gorm_mysql.url
56-
dev = "docker://mysql/8"
66+
67+
dev = "docker://mysql/8/devdb?timeout=5m"
68+
5769
migration {
58-
dir = "file://internal/query/migration/atlas/migrations/mysql"
70+
dir = var.MYSQL_MIGRATIONS_DIR
71+
format = "atlas"
5972
}
73+
74+
lint {
75+
destructive { error = true }
76+
}
77+
6078
format {
6179
migrate {
6280
diff = "{{ sql . \" \" }}"
6381
}
6482
}
6583
}
6684

67-
# SQL Server configuration
68-
data "external_schema" "gorm_sqlserver" {
85+
# SQLite configuration
86+
data "external_schema" "gorm_sqlite" {
6987
program = [
7088
"sh",
7189
"-c",
72-
"cd internal && go run ./atlas_loader.go sqlserver",
90+
"cd internal && go run ./atlas_loader.go sqlite",
7391
]
7492
}
7593

76-
env "sqlserver" {
77-
src = data.external_schema.gorm_sqlserver.url
78-
dev = "docker://sqlserver/2022-latest"
94+
env "sqlite" {
95+
src = data.external_schema.gorm_sqlite.url
96+
97+
# SQLite uses in-memory dev database, no Docker needed
98+
dev = "sqlite://file?mode=memory"
99+
79100
migration {
80-
dir = "file://internal/query/migration/atlas/migrations/sqlserver"
101+
dir = var.SQLITE_MIGRATIONS_DIR
102+
format = "atlas"
103+
}
104+
105+
lint {
106+
destructive { error = true }
81107
}
108+
82109
format {
83110
migrate {
84111
diff = "{{ sql . \" \" }}"

taco/internal/query/common/sql_store.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ type SQLStore struct {
2323
}
2424

2525
// NewSQLStore creates a new SQLStore.
26+
// Note: Schema migrations are now handled by Atlas in queryfactory.NewQueryStore()
2627
func NewSQLStore(db *gorm.DB) (*SQLStore, error) {
2728
store := &SQLStore{db: db}
28-
29-
if err := store.migrate(); err != nil {
30-
return nil, fmt.Errorf("failed to migrate common sql schema: %w", err)
31-
}
3229

30+
// Create org-scoped indexes (not handled by Atlas migrations)
3331
if err := CreateOrgScopedIndexes(db); err != nil {
3432
return nil, fmt.Errorf("failed to create org-scoped indexes: %w", err)
3533
}
3634

35+
// Create database views (not handled by Atlas migrations)
3736
if err := store.createViews(); err != nil {
3837
return nil, fmt.Errorf("failed to create views: %w", err)
3938
}

taco/internal/query/migration/atlas/migrations/postgres/.gitkeep

Lines changed: 0 additions & 2 deletions
This file was deleted.

taco/internal/query/migration/atlas/migrations/sqlite/.gitkeep

Lines changed: 0 additions & 2 deletions
This file was deleted.

taco/internal/query/migration/atlas/migrations/sqlite/atlas.sum

Lines changed: 0 additions & 2 deletions
This file was deleted.

taco/internal/query/types/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type Rule struct {
5050
Effect string `gorm:"size:8;not null;default:allow"`
5151
WildcardAction bool `gorm:"not null;default:false"`
5252
WildcardResource bool `gorm:"not null;default:false"`
53-
ResourcePatterns string `gorm:"type:text;default:''"`
53+
ResourcePatterns string `gorm:"type:text;"`
5454
Actions []RuleAction `gorm:"constraint:OnDelete:CASCADE"`
5555
UnitTargets []RuleUnit `gorm:"constraint:OnDelete:CASCADE"`
5656
TagTargets []RuleUnitTag `gorm:"constraint:OnDelete:CASCADE"`

0 commit comments

Comments
 (0)