source: trunk/packages/invirt-database/python/database/models.py @ 1714

Last change on this file since 1714 was 1714, checked in by broder, 15 years ago

Turn on lazy loading for all tables

File size: 5.4 KB
RevLine 
[13]1from sqlalchemy import *
[991]2from sqlalchemy import orm
[946]3from sqlalchemy.orm import create_session, relation
[13]4
[18]5from sqlalchemy.ext.sessioncontext import SessionContext
6from sqlalchemy.ext.assignmapper import assign_mapper
7
[77]8__all__ = ['meta',
[991]9           'session',
[296]10           'clear_cache',
[77]11           'machine_table',
[243]12           'machine_access_table',
[77]13           'nic_table',
14           'disk_table',
15           'types_table',
[108]16           'cdroms_table',
[1361]17           'mirrors_table',
[443]18           'autoinstalls_table',
[77]19           'Machine',
[243]20           'MachineAccess',
[77]21           'NIC',
22           'Disk',
[108]23           'Type',
[443]24           'CDROM',
[1361]25           'Mirror',
[443]26           'Autoinstall',
[874]27           'or_',
[443]28           ]
[77]29
[946]30meta = ThreadLocalMetaData()
[1014]31session = orm.scoped_session(orm.sessionmaker(transactional=False, autoflush=False))
[13]32
33machine_table = Table('machines', meta,
34       Column('machine_id', Integer, primary_key=True, nullable=False),
35       Column('name', String, nullable=False),
[609]36       Column('description', String, nullable=False),
[13]37       Column('memory', Integer, nullable=False),
38       Column('owner', String, nullable=False),
39       Column('contact', String, nullable=False),
40       Column('uuid', String, nullable=False),
[1708]41       Column('administrator', String, nullable=True, default=None),
[13]42       Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
43       Column('autorestart', Boolean, nullable=False, default=False),
[874]44       Column('cpus', Integer, nullable=False, default=1),
45       Column('adminable', Boolean, nullable=False, default=False))
[13]46
47nic_table = Table('nics', meta,
48       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=True),
[106]49       Column('mac_addr', String, nullable=False, primary_key=True),
[107]50       Column('ip', String, nullable=False, unique=True),
[106]51       Column('hostname', String, nullable=True))
[13]52
53disk_table = Table('disks', meta,
54       Column('machine_id', Integer, ForeignKey('machines.machine_id'), nullable=False),
55       Column('guest_device_name', String, nullable=False),
56       Column('size', Integer, nullable=False),
57       PrimaryKeyConstraint('machine_id', 'guest_device_name'))
58
59types_table = Table('types', meta,
60       Column('type_id', String, primary_key=True, nullable=False),
61       Column('description', String, nullable=False),
[64]62       Column('hvm', Boolean, nullable=False),
[13]63       Column('apic', Boolean, nullable=False),
64       Column('acpi', Boolean, nullable=False),
65       Column('pae', Boolean, nullable=False))
66
[1247]67mirrors_table = Table('mirrors', meta,
68       Column('mirror_id', String, primary_key=True, nullable=False),
69       Column('uri_prefix', String, nullable=False))
70
[108]71cdroms_table = Table('cdroms', meta,
72       Column('cdrom_id', String, primary_key=True, nullable=False),
[1247]73       Column('description', String, nullable=False),
[1250]74       Column('mirror_id', String, ForeignKey('mirrors.mirror_id')),
[1247]75       Column('uri_suffix', String))
[18]76
[443]77autoinstalls_table = Table('autoinstalls', meta,
78       Column('autoinstall_id', String, primary_key=True, nullable=False),
79       Column('description', String, nullable=False),
[630]80       Column('type_id', String, ForeignKey('types.type_id'), nullable=False),
81       Column('distribution', String, nullable=False),
[1694]82       Column('mirror', String, nullable=False),
83       Column('arch', String, nullable=False))
[443]84
[238]85machine_access_table = Table('machine_access', meta,
[1174]86       Column('machine_id', Integer, ForeignKey('machines.machine_id', ondelete='CASCADE'), nullable=False, index=True),
[238]87       Column('user', String, nullable=False, index=True),
[1174]88       PrimaryKeyConstraint('machine_id', 'user'))
[108]89
[18]90class Machine(object):
[204]91    def __repr__(self):
92        return "<Machine %s: name='%s' owner='%s'>" % (self.machine_id, self.name, self.owner)
[18]93
[238]94class MachineAccess(object):
95    def __repr__(self):
96        return "<MachineAccess machine='%s' user='%s'>" % (self.machine, self.user)
97
[18]98class NIC(object):
[204]99    def __repr__(self):
100        return "<NIC: mac='%s' machine='%s' ip='%s' hostname='%s'>" % (self.mac_addr, self.machine_id, self.ip, self.hostname)
[18]101
102class Disk(object):
[204]103    def __repr__(self):
104        return "<Disk: machine=%s device=%s size=%s>" % (self.machine_id, self.guest_device_name, self.size)
[18]105
106class Type(object):
[204]107    def __repr__(self):
108        return "<Type %s: %s>" % (self.type_id, self.description)
[18]109
[1247]110class Mirror(object):
111    def __repr__(self):
112        return "<Mirror %s>" % (self.mirror_id)
113
[108]114class CDROM(object):
[204]115    def __repr__(self):
116        return "<CDROM %s: %s>" % (self.cdrom_id, self.description)
[18]117
[443]118class Autoinstall(object):
119    def __repr__(self):
120        return "<Autoinstall %s: %s (%s)>" % (self.autoinstall_id, self.description, self.type.type_id)
121
[991]122session.mapper(Machine, machine_table,
[1714]123              properties={'nics': relation(NIC, backref="machine"),
124                          'disks': relation(Disk, backref="machine"),
125                          'type': relation(Type),
126                          'acl': relation(MachineAccess, backref="machine", passive_deletes=True, cascade="all, delete-orphan")});
[991]127session.mapper(MachineAccess, machine_access_table)
128session.mapper(NIC, nic_table)
129session.mapper(Disk, disk_table)
130session.mapper(Type, types_table)
[1247]131session.mapper(Mirror, mirrors_table)
[1363]132session.mapper(CDROM, cdroms_table,
133               properties={'mirror': relation(Mirror, backref="cdroms")})
[991]134session.mapper(Autoinstall, autoinstalls_table)
[287]135
136def clear_cache():
[1136]137    """Clear sqlalchemy's cache
138    """
[287]139
[1136]140    session.clear()
Note: See TracBrowser for help on using the repository browser.