-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_db_offline.py
139 lines (112 loc) · 4.12 KB
/
manage_db_offline.py
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
"""Offline management of database."""
import os
import peewee as pw
import playhouse.migrate
from recapi.models import DATABASE
import config
# Overwrite with instance config
if os.path.exists(os.path.join("instance", "config.py")):
import instance.config as config
def init_db():
"""Initialise database."""
DATABASE.init(
config.DB_NAME,
user=config.DB_USER,
password=config.DB_PASSWORD,
host=config.DB_HOST,
port=config.DB_PORT)
config.SQLDB = DATABASE
def update_recipes():
"""Update all recipes, e.g. when values of a column are converted."""
init_db()
from recapi.models import recipemodel
from recapi import utils
recipes = recipemodel.Recipe.select()
for recipe in recipes:
# # Example: copy int value in 'portions' column to 'portions_text' and convert to str
# recipe.portions_text = str(recipe.portions)
# recipe.save()
recipe.url = utils.make_url(recipe.title, recipe.id)
print(recipe.title, recipe.url)
recipe.save()
def update_users():
"""Example: convert all roles to admin."""
from recapi.models import usermodel
users = usermodel.User.select()
for user in users:
user.admin = True
user.save()
def migrate_example():
"""Example: migrate some user data."""
init_db()
# Check documentation for more info:
# http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#schema-migrations
migrator = playhouse.migrate.MySQLMigrator(config.SQLDB)
# Some examples for altering data
# from recapi.models import usermodel
playhouse.migrate.migrate(
# Add column with or without foreign key
# migrator.add_column("recipe", "changed_by_id", pw.ForeignKeyField(usermodel.User, null=True, field=usermodel.User.id)),
# migrator.add_column("recipe", "changed", pw.DateTimeField(null=True)),
# Drop column
# migrator.drop_column("recipe", "changed_by")
# Rename column
# migrator.rename_column('recipe', 'suggestor', 'suggester'),
# migrator.add_column("recipe", "stored", pw.BooleanField(default=False)),
# migrator.add_column("recipe", "needs_fix", pw.BooleanField(default=False))
# migrator.drop_column("recipe", "url"),
migrator.add_column("recipe", "url", pw.CharField(unique=True, max_length="120", null=True))
)
def create_categories():
"""Create the recipe categories."""
from recapi.models.tagmodel import TagCategory
newcatnames = [
"Måltid",
"Recepttyp",
"Ingrediens",
"Svårighet",
"Kök",
"Specialkost"
]
for n, newcat in enumerate(newcatnames, start=1):
cat = TagCategory(categoryname=newcat, categoryorder=n)
cat.save()
def create_tags():
"""Create some example tags."""
from recapi.models.tagmodel import Tag, TagCategory
newtagnames = [
("gratäng", "Recepttyp"),
("gryta", "Recepttyp"),
("soppa", "Recepttyp"),
("paj", "Recepttyp"),
("pasta", "Ingrediens"),
("ris", "Ingrediens"),
("vegetariskt", "Specialkost"),
("veganskt", "Specialkost")
]
for newtagname, cat in newtagnames:
tag = Tag(tagname=newtagname, parent=TagCategory.get(TagCategory.categoryname == cat))
tag.save()
def change_max_length():
"""Change max length of CharField column."""
init_db()
migrator = playhouse.migrate.MySQLMigrator(config.SQLDB)
# Create new column
playhouse.migrate.migrate(
migrator.rename_column("recipe", "portions_text", "portions_text_old"),
migrator.add_column("recipe", "portions_text", pw.CharField(max_length="100", default="")),
)
# Copy column data
from recapi.models import recipemodel
recipes = recipemodel.Recipe.select()
for recipe in recipes:
recipe.portions_text = str(recipe.portions_text_old)
recipe.save()
# Drop column
playhouse.migrate.migrate(
migrator.drop_column("recipe", "portions_text_old")
)
if __name__ == '__main__':
# migrate_example()
# update_recipes()
print("Nothing to be done!")