djangordf.schema¶
Django-migrations-style framework for evolving the RDF schema and
data in the triple store. Define one Migration subclass per change
under the configured DJANGORDF_MIGRATIONS_MODULE, then run
python manage.py migrate_rdf.
Django-style migrations for the RDF schema.
Define a Migration subclass per change, list operations from
djangordf.schema.operations, point at any dependencies by
migration name, and run python manage.py migrate_rdf to apply.
The applied-state recorder lives in the triple store itself
(urn:djangordf:migrations) so the next run knows what is already
in place.
- class djangordf.schema.AddPropertyDeclaration(predicate, *, kind, domain=None, range=None, graph=None)[source]¶
Bases:
OperationDeclare a predicate in the ontology graph.
kindis either"datatype"or"object". Domain and range are optional; when present they are emitted asrdfs:domain/rdfs:rangetriples on the predicate IRI.- Parameters:
- class djangordf.schema.CreateClass(class_iri, *, label=None, comment=None, graph=None)[source]¶
Bases:
OperationAppend an
owl:Classdeclaration to the ontology graph.Optionally writes
rdfs:labelandrdfs:commentannotations alongside.- Parameters:
- class djangordf.schema.DeleteClass(class_iri, *, graph=None)[source]¶
Bases:
OperationStrip every triple whose subject is the class IRI from the ontology graph.
- class djangordf.schema.Executor(backend=None, module_path=None)[source]¶
Bases:
objectDiscover
Migrationsubclasses, sort them topologically, and apply any that the recorder has not seen yet.- Parameters:
module_path (str | None)
- discover()[source]¶
Import every
*.pyunder the configured module, collectMigrationsubclasses, and return one instance per class sorted lexically by name.
- class djangordf.schema.MigrationRecorder(backend)[source]¶
Bases:
objectTiny wrapper around
backendthat tracks applied migrations in a dedicated named graph.
- class djangordf.schema.RenamePredicate(old, new, *, graph=None)[source]¶
Bases:
OperationRewrite every
(?s, old, ?o)triple to(?s, new, ?o).Runs across the data graph by default; an explicit
graph=targets a specific named graph. Useful when the user-facing name of a property changes but the data should remain.
- class djangordf.schema.RunSPARQL(sparql)[source]¶
Bases:
OperationRun an arbitrary SPARQL UPDATE statement against the backend.
The escape hatch for transformations that do not fit any of the higher-level operations.
- Parameters:
sparql (str)
The Migration base class.
Subclass it in a per-change file, list operations, and reference any
prior migrations by name through dependencies:
class Migration(Migration):
name = "0002_rename_pref"
dependencies = ["0001_initial"]
operations = [
RenamePredicate(
old=URIRef("http://example.org/oldPred"),
new=URIRef("http://example.org/newPred"),
),
]
The name field defaults to the module name if not set.
- class djangordf.schema.base.Migration[source]¶
Bases:
objectOne forward-only RDF schema or data change.
Operation types that can appear in a Migration.operations list.
All operations carry their own apply(backend) method. Operations
are forward-only in this release; rollback support is a separate
ticket.
- class djangordf.schema.operations.Operation[source]¶
Bases:
objectMarker base class for migration operations.
- class djangordf.schema.operations.RunSPARQL(sparql)[source]¶
Bases:
OperationRun an arbitrary SPARQL UPDATE statement against the backend.
The escape hatch for transformations that do not fit any of the higher-level operations.
- Parameters:
sparql (str)
- class djangordf.schema.operations.CreateClass(class_iri, *, label=None, comment=None, graph=None)[source]¶
Bases:
OperationAppend an
owl:Classdeclaration to the ontology graph.Optionally writes
rdfs:labelandrdfs:commentannotations alongside.- Parameters:
- class djangordf.schema.operations.DeleteClass(class_iri, *, graph=None)[source]¶
Bases:
OperationStrip every triple whose subject is the class IRI from the ontology graph.
- class djangordf.schema.operations.AddPropertyDeclaration(predicate, *, kind, domain=None, range=None, graph=None)[source]¶
Bases:
OperationDeclare a predicate in the ontology graph.
kindis either"datatype"or"object". Domain and range are optional; when present they are emitted asrdfs:domain/rdfs:rangetriples on the predicate IRI.- Parameters:
- class djangordf.schema.operations.RenamePredicate(old, new, *, graph=None)[source]¶
Bases:
OperationRewrite every
(?s, old, ?o)triple to(?s, new, ?o).Runs across the data graph by default; an explicit
graph=targets a specific named graph. Useful when the user-facing name of a property changes but the data should remain.
Discover, sort and apply RDF migrations.
- class djangordf.schema.executor.Executor(backend=None, module_path=None)[source]¶
Bases:
objectDiscover
Migrationsubclasses, sort them topologically, and apply any that the recorder has not seen yet.- Parameters:
module_path (str | None)
- discover()[source]¶
Import every
*.pyunder the configured module, collectMigrationsubclasses, and return one instance per class sorted lexically by name.
Track applied migrations as triples in the configured backend.
- class djangordf.schema.recorder.MigrationRecorder(backend)[source]¶
Bases:
objectTiny wrapper around
backendthat tracks applied migrations in a dedicated named graph.- record(name, *, when)[source]¶
Persist that
namehas been applied atwhen(an ISO-8601 timestamp).whenis provided by the caller so the executor can stamp every applied migration with the same run-wide timestamp if desired.