aboutsummaryrefslogtreecommitdiffstats
path: root/python_moondb/python_moondb/migrate_repo/versions/001_moon.py
blob: 2cc3614089af5f1da1c7bc8aa724616051ecf077 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

import sqlalchemy as sql


def upgrade(migrate_engine):
    meta = sql.MetaData()
    meta.bind = migrate_engine

    table = sql.Table(
        'pdp',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    table.create(migrate_engine, checkfirst=True)

    table = sql.Table(
        'policies',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    table.create(migrate_engine, checkfirst=True)

    table = sql.Table(
        'models',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    table.create(migrate_engine, checkfirst=True)

    subject_categories_table = sql.Table(
        'subject_categories',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('description', sql.String(256), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    subject_categories_table.create(migrate_engine, checkfirst=True)

    object_categories_table = sql.Table(
        'object_categories',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('description', sql.String(256), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    object_categories_table.create(migrate_engine, checkfirst=True)

    action_categories_table = sql.Table(
        'action_categories',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('name', sql.String(256), nullable=False),
        sql.Column('description', sql.String(256), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    action_categories_table.create(migrate_engine, checkfirst=True)

    subjects_table = sql.Table(
        'subjects',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    subjects_table.create(migrate_engine, checkfirst=True)

    objects_table = sql.Table(
        'objects',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    objects_table.create(migrate_engine, checkfirst=True)

    actions_table = sql.Table(
        'actions',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    actions_table.create(migrate_engine, checkfirst=True)

    subject_data_table = sql.Table(
        'subject_data',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        sql.Column('category_id', sql.ForeignKey("subject_categories.id"), nullable=False),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    subject_data_table.create(migrate_engine, checkfirst=True)

    object_data_table = sql.Table(
        'object_data',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        sql.Column('category_id', sql.ForeignKey("object_categories.id"), nullable=False),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    object_data_table.create(migrate_engine, checkfirst=True)

    action_data_table = sql.Table(
        'action_data',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        sql.Column('category_id', sql.ForeignKey("action_categories.id"), nullable=False),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    action_data_table.create(migrate_engine, checkfirst=True)

    subject_assignments_table = sql.Table(
        'subject_assignments',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('assignments', sql.Text(), nullable=True),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.Column('subject_id', sql.ForeignKey("subjects.id"), nullable=False),
        sql.Column('category_id', sql.ForeignKey("subject_categories.id"), nullable=False),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    subject_assignments_table.create(migrate_engine, checkfirst=True)

    object_assignments_table = sql.Table(
        'object_assignments',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('assignments', sql.Text(), nullable=True),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.Column('object_id', sql.ForeignKey("objects.id"), nullable=False),
        sql.Column('category_id', sql.ForeignKey("object_categories.id"), nullable=False),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    object_assignments_table.create(migrate_engine, checkfirst=True)

    action_assignments_table = sql.Table(
        'action_assignments',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('assignments', sql.Text(), nullable=True),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.Column('action_id', sql.ForeignKey("actions.id"), nullable=False),
        sql.Column('category_id', sql.ForeignKey("action_categories.id"), nullable=False),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    action_assignments_table.create(migrate_engine, checkfirst=True)

    meta_rules_table = sql.Table(
        'meta_rules',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('value', sql.Text(), nullable=True),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    meta_rules_table.create(migrate_engine, checkfirst=True)

    rules_table = sql.Table(
        'rules',
        meta,
        sql.Column('id', sql.String(64), primary_key=True),
        sql.Column('rule', sql.Text(), nullable=True),
        sql.Column('policy_id', sql.ForeignKey("policies.id"), nullable=False),
        sql.Column('meta_rule_id', sql.ForeignKey("meta_rules.id"), nullable=False),
        mysql_engine='InnoDB',
        mysql_charset='utf8')
    rules_table.create(migrate_engine, checkfirst=True)


def downgrade(migrate_engine):
    meta = sql.MetaData()
    meta.bind = migrate_engine

    for _table in (
        'rules',
        'meta_rules',
        'action_assignments',
        'object_assignments',
        'subject_assignments',
        'action_data',
        'object_data',
        'subject_data',
        'actions',
        'objects',
        'subjects',
        'action_categories',
        'object_categories',
        'subject_categories',
        'models',
        'policies',
        'pdp'
    ):
        try:
            table = sql.Table(_table, meta, autoload=True)
            table.drop(migrate_engine, checkfirst=True)
        except Exception as e:
            print(e)