-
Notifications
You must be signed in to change notification settings - Fork 14
/
migrations.rst1
60 lines (43 loc) · 1.51 KB
/
migrations.rst1
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
# API командной строки
python -m pony.migrate make
python -m pony.migrate apply
python -m pony.migrate getsql
# Содержимое файла миграций
## Начальная миграция
from pony.orm.migrating import *
prev = [] # нет предыдущей миграции
def initial_state(db):
class Person(db.Entity):
name = Required(str)
age = Required(int)
## Последующие миграции
from pony.orm.migrating import *
prev = ['0000_initial']
operations = [
AddEntity('Contact', bases=(), attrs=[
('type', Required(str)),
('value', Required(str))
])
AddRelationship('Person', 'contacts', Set('Contact'), 'Contact', 'person', Required('Person'))
]
## Кастомные и дата-миграции
prev = ['0001_migration_abcd']
def forward(db):
# Работа с сущностями
for c in db.Contact.select(lambda c: c.type == 'tel'):
c.type = 'phone'
# Raw SQL
db.execute("""
UPDATE "Contact" set "type" = 'tel' WHERE "type" = 'phone'
""")
# Операции
+AddEntity(entity_name, base_names, new_attrs)
-RemoveEntity(entity_name)
RenameEntity(old_name, new_name)
+AddAttr(entity_name, attr_name, attr)
+RemoveAttr(entity_name, attr_name)
+RenameAttr(entity_name, old_name, new_name)
-ModifyAttr(entity_name, attr_name, new_attr)
+AddRelation(e1_name, a1_name, attr1, e2_name, a2_name, attr2)
-RemoveRelation(entity_name, attr_name)
-ModifyRelation(entity_name, attr_name, new_name, new_attr, reverse_name, reverse_attr)