Skip to content
Draft
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
8 changes: 3 additions & 5 deletions .github/workflows/django_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ jobs:
with:
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
- name: Set up Atlas action
uses: ariga/atlas-action/setup@master
uses: ariga/atlas-action/setup@d1655a01682319057d894ec872d6ca4b82cd4eba
- name: Atlas Schema Lint
uses: ariga/atlas-action/schema/lint@master
uses: ariga/atlas-action/schema/lint@d1655a01682319057d894ec872d6ca4b82cd4eba
with:
working-directory: ./projects/django
env: django
dev-url: 'sqlite://?mode=memory&_fk=1'
url: 'env://url'
env: django
6 changes: 3 additions & 3 deletions projects/django/academie/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models

class University(models.Model):
name = models.CharField(max_length=200, unique=True)
name = models.CharField(max_length=200)
location = models.CharField(max_length=100)
established_date = models.DateField()

Expand All @@ -11,7 +11,7 @@ def __str__(self):
class Department(models.Model):
name = models.CharField(max_length=100, unique=True)
head_of_department = models.CharField(max_length=100, blank=True, null=True)
university = models.ForeignKey(University, on_delete=models.CASCADE, related_name='departments')
university = models.ForeignKey(University, on_delete=models.CASCADE, null=True, related_name='departments')
# A Department belongs to one University

def __str__(self):
Expand All @@ -24,7 +24,7 @@ class Student(models.Model):
date_of_birth = models.DateField()
email = models.EmailField(unique=True)
enrollment_date = models.DateField(auto_now_add=True) # Automatically sets the date when student is created
department = models.ForeignKey(Department, on_delete=models.SET_NULL, null=True, blank=True, related_name='students')
department = models.ForeignKey(Department, on_delete=models.SET_NULL, null=True, blank=True, related_name='students', db_column="department_link")
# A Student belongs to one Department (optional)
# If a Department is deleted, student's department field is set to NULL

Expand Down
8 changes: 6 additions & 2 deletions projects/django/atlas.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ env "django" {
}

lint {
rule "hcl" "name" {
src = [ "atlas.rule.hcl" ]
rule "hcl" "error" {
error = true
src = [ "error.rule.hcl" ]
}
rule "hcl" "warning" {
src = [ "warning.rule.hcl" ]
}
}
32 changes: 0 additions & 32 deletions projects/django/atlas.rule.hcl → projects/django/error.rule.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,4 @@ rule "schema" "foreign-key-not-nullable" {
}
}
}
}

predicate "column" "postfix_id" {
name {
match = ".+_id$"
}
}

predicate "foreign_key" "postfix_id" {
all {
column {
predicate = predicate.column.postfix_id
}
}
}

rule "schema" "foreign-key-postfix-id" {
description = "Foreign keys must have a column name ending with '_id'"
table {
match {
predicate = predicate.table.in
vars = {
names = ["academie_department", "academie_student"]
}
}
foreign_key {
assert {
predicate = predicate.foreign_key.postfix_id
message = "Foreign key ${self.name} must have a column name ending with '_id'"
}
}
}
}
40 changes: 40 additions & 0 deletions projects/django/warning.rule.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
predicate "table" "in" {
variable "names" {
type = list(string)
}
name {
in = var.names
}
}

predicate "column" "postfix_id" {
name {
match = ".+_id$"
}
}

predicate "foreign_key" "postfix_id" {
all {
column {
predicate = predicate.column.postfix_id
}
}
}

rule "schema" "foreign-key-postfix-id" {
description = "Foreign keys should have a column name ending with '_id'"
table {
match {
predicate = predicate.table.in
vars = {
names = ["academie_department", "academie_student"]
}
}
foreign_key {
assert {
predicate = predicate.foreign_key.postfix_id
message = "Foreign key ${self.name} should have a column name ending with '_id'"
}
}
}
}