aboutsummaryrefslogtreecommitdiff
path: root/pyenc/app/model.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyenc/app/model.py')
-rw-r--r--pyenc/app/model.py39
1 files changed, 28 insertions, 11 deletions
diff --git a/pyenc/app/model.py b/pyenc/app/model.py
index c37cec3..f5b66fd 100644
--- a/pyenc/app/model.py
+++ b/pyenc/app/model.py
@@ -10,7 +10,7 @@ def init_app(app):
"""Adds database bindings to a Flask App."""
db.init_app(app)
import logging
- # logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
+ logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
host_classes = db.Table(
@@ -20,7 +20,6 @@ host_classes = db.Table(
)
-# NOTE this is non-final, and might get removed shortly
environment_classes = db.Table(
'environment_classes',
db.Column('environment_id', db.ForeignKey('environment.id'), primary_key=True),
@@ -88,10 +87,14 @@ class PuppetFile(db.Model):
"""
Puppet source code file.
- Keeps track of known puppet files. Each file contains 0 to many
- puppet classes.
+ Keeps track of known puppet files. Each file contains any number of puppet
+ classes.
- Each file is uniquely identified by the pair (path, environment_id).
+ Each file optionally references a PuppetFileContent object, which contains
+ the (parsed) contents of the file. Multiple PuppetFile can reference the
+ same PuppetFileContent.
+
+ (child)
"""
__tablename__ = 'puppet_file'
@@ -106,8 +109,12 @@ class PuppetFile(db.Model):
nullable=False)
environment = db.relationship('Environment', back_populates='files')
- # Checksum of the content, should be usable as a key in PuppetFileContent
- checksum = db.Column(db.Text, nullable=False)
+ checksum = db.Column(db.Text, #db.ForeignKey('puppet_file_content.checksum'),
+ nullable=False)
+
+ content = db.relationship('PuppetFileContent',
+ uselist=False,
+ primaryjoin=lambda: PuppetFile.checksum == db.foreign(PuppetFileContent.checksum))
# When we last read data into json
last_parse = db.Column(db.Float)
@@ -115,7 +122,6 @@ class PuppetFile(db.Model):
classes = db.relationship('PuppetClass',
back_populates='files',
secondary=class_files)
- content = db.relationship('PuppetFileContent', backref='file')
__table_args__ = (
db.UniqueConstraint('path', 'environment_id'),
@@ -128,15 +134,26 @@ class PuppetFileContent(db.Model):
Separate from PuppetFile since many environments can share files,
and I don't want to store reduntand data.
+
+ (parent)
"""
__tablename__ = 'puppet_file_content'
- id = db.Column(db.Integer, primary_key=True)
+ # id = db.Column(db.Integer, primary_key=True)
- file_id = db.Column(db.Integer, db.ForeignKey(f'{PuppetFile.__tablename__}.id'))
+ # file_id = db.Column(db.Integer, db.ForeignKey(f'{PuppetFile.__tablename__}.id'))
# Checksum of the original file
- checksum = db.Column(db.Text, nullable=False)
+ # NOT marked as a foreign key which references
+ # PuppetFile.checksum, since content without a coresponding file
+ # is fine, and will be collected later
+ checksum = db.Column(db.Text, primary_key=True)
+ # files = db.relationship('PuppetFile', back_populates='content',
+ # foreign_keys=[checksum],
+ # primaryjoin=lambda: PuppetFile.checksum == PuppetFileContent.checksum)
+
+ files = db.relationship('PuppetFile',
+ primaryjoin=lambda: PuppetFileContent.checksum == db.foreign(PuppetFile.checksum))
# Output of 'puppet parser dump --format json <filename>'
json = db.Column(db.Text, nullable=False)