summaryrefslogtreecommitdiffstats
path: root/proxy/schema/model/model.py
blob: f27dbb0cf81d5634fe144e94898ec1c0f5d92bc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, Sequence
from sqlalchemy.orm import mapper
from sqlalchemy.orm import sessionmaker
from optparse import OptionParser

import sys

metadata = MetaData()

owner_table = Table('cp_owner', metadata, 
	Column('uuid', String, primary_key=True),
	Column('name', String))

entitlement_table = Table('cp_entitlement', metadata, 
	Column('uuid', String, primary_key=True),
	Column('name', String),
	Column('owner', String, ForeignKey('cp_owner.uuid')),
	Column('parent', String, ForeignKey('cp_entitlement.uuid')))

consumer_table = Table('cp_consumer', metadata, 
	Column('uuid', String, primary_key=True),
	Column('name', String),
	Column('owner', String, ForeignKey('cp_owner.uuid')),
	Column('parent', String, ForeignKey('cp_consumer.uuid')))

consumer_info_to_consumer_table = Table('cp_consumer_info_to_consumer', metadata,
	Column('cid', String, ForeignKey('cp_consumer.uuid')),
	Column('info_id', Integer, ForeignKey('cp_consumer_info.id')))

consumer_type_table = Table('cp_consumer_type', metadata,
	Column('id', Integer, Sequence('consumer_type_id_seq'), primary_key=True),
	Column('label', String))

consumer_info_table = Table('cp_consumer_info', metadata,
	Column('id', Integer, Sequence('consumer_info_id_seq'), primary_key=True),
	Column('name', String),
	Column('value', String),
	Column('type', Integer, ForeignKey('cp_consumer_type.id')))

product_definition_table = Table('cp_product', metadata, 
	Column('uuid', String, primary_key=True),
	Column('name', String))

entitlement_pool_table = Table('cp_entitlement_pool', metadata, 
	Column('uuid', String, primary_key=True),
	Column('name', String),
	Column('owner', String, ForeignKey('cp_owner.uuid')),
	Column('product', String, ForeignKey('cp_product.uuid')))

def get_engine(db):
    if db == "postgresql":
        return create_engine('postgres://candlepin:candlepin@127.0.0.1:5432/candlepin')
    elif db == "hsqldb":
        raise NotImplementedError
    elif db == "sqlite":
        return create_engine('sqlite:///:memory:', echo=True)

def create_db(db):
    engine = get_engine(db)
    metadata.create_all(engine)

def drop_db(db):
    engine = get_engine(db)
    metadata.drop_all(engine)

if __name__ == "__main__":
    usage = "%prog [options]"
    description = "desc"
    parser = OptionParser(usage=usage, description=description)
    parser.add_option("--drop", dest="drop", action="store_true",
        default=False, help="drops all tables")
    parser.add_option("--create", dest="create", action="store_true",
        default=False, help="creates all tables")

    parser.add_option("--postgres", dest="db", action="store_const",
        const="postgresql", default=False, help="use PostgreSQL engine")
    parser.add_option("--hsqldb", dest="db", action="store_const",
        const="hsqldb", default=False, help="use HSQLDB engine")
    parser.add_option("--sqlite", dest="db", action="store_const",
        const="sqlite", default=False, help="use SQLite engine")

    (options, args) = parser.parse_args()

    # validate options
    if not options.db:
        parser.print_help()
        sys.exit(1)

    if not (options.drop or options.create):
        parser.print_help()
        sys.exit(1)

    if options.create:
        create_db(options.db)
    elif options.drop:
        drop_db(options.db)