-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathauto_migrate.py
More file actions
132 lines (116 loc) · 6.29 KB
/
Copy pathauto_migrate.py
File metadata and controls
132 lines (116 loc) · 6.29 KB
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
"""
Auto-migration module for Shopify Automation app.
This module provides functions to automatically migrate the database schema
to support multi-store functionality.
"""
from sqlalchemy.exc import OperationalError
from sqlalchemy import text
from models import db, Product, Tag, Collection, Store
def run_migrations(app):
"""
Run database migrations to ensure multi-store support.
This function checks if the necessary columns exist in the database tables
and adds them if they don't. It also associates any orphaned items with
the default store.
Args:
app: The Flask application instance
"""
with app.app_context():
try:
print("Running database migrations...")
# Check if store_id column exists in products table
try:
with db.engine.connect() as conn:
conn.execute(text("SELECT store_id FROM products LIMIT 1"))
print("store_id column exists in products table")
except OperationalError:
print("Adding store_id column to products table")
with db.engine.connect() as conn:
conn.execute(text("ALTER TABLE products ADD COLUMN store_id INTEGER"))
# Check if store_id column exists in collections table
try:
with db.engine.connect() as conn:
conn.execute(text("SELECT store_id FROM collections LIMIT 1"))
print("store_id column exists in collections table")
except OperationalError:
print("Adding store_id column to collections table")
with db.engine.connect() as conn:
conn.execute(text("ALTER TABLE collections ADD COLUMN store_id INTEGER"))
# Check if store_id column exists in tags table
try:
with db.engine.connect() as conn:
conn.execute(text("SELECT store_id FROM tags LIMIT 1"))
print("store_id column exists in tags table")
except OperationalError:
print("Adding store_id column to tags table")
with db.engine.connect() as conn:
conn.execute(text("ALTER TABLE tags ADD COLUMN store_id INTEGER"))
# Associate all products, collections, and tags with the default store if they don't have a store_id
default_store = Store.query.first()
if default_store:
with db.engine.connect() as conn:
conn.execute(text(f"UPDATE products SET store_id = {default_store.id} WHERE store_id IS NULL"))
conn.execute(text(f"UPDATE collections SET store_id = {default_store.id} WHERE store_id IS NULL"))
conn.execute(text(f"UPDATE tags SET store_id = {default_store.id} WHERE store_id IS NULL"))
print(f"Associated orphaned items with default store (ID: {default_store.id})")
# Check if slug column exists in collections table
try:
with db.engine.connect() as conn:
conn.execute(text("SELECT slug FROM collections LIMIT 1"))
print("slug column exists in collections table")
except OperationalError:
print("Adding slug column to collections table")
with db.engine.connect() as conn:
conn.execute(text("ALTER TABLE collections ADD COLUMN slug TEXT"))
# Check if meta_description column exists in collections table
try:
with db.engine.connect() as conn:
conn.execute(text("SELECT meta_description FROM collections LIMIT 1"))
print("meta_description column exists in collections table")
except OperationalError:
print("Adding meta_description column to collections table")
with db.engine.connect() as conn:
conn.execute(text("ALTER TABLE collections ADD COLUMN meta_description TEXT"))
# Check if shopify_id column exists in collections table
try:
with db.engine.connect() as conn:
conn.execute(text("SELECT shopify_id FROM collections LIMIT 1"))
print("shopify_id column exists in collections table")
except OperationalError:
print("Adding shopify_id column to collections table")
with db.engine.connect() as conn:
conn.execute(text("ALTER TABLE collections ADD COLUMN shopify_id TEXT"))
# Check if shopify_id column exists in products table
try:
with db.engine.connect() as conn:
conn.execute(text("SELECT shopify_id FROM products LIMIT 1"))
print("shopify_id column exists in products table")
except OperationalError:
print("Adding shopify_id column to products table")
with db.engine.connect() as conn:
conn.execute(text("ALTER TABLE products ADD COLUMN shopify_id TEXT"))
print("Database migrations completed successfully")
return True
except Exception as e:
print(f"Error during migrations: {str(e)}")
return False
def migrate_store(app, store_id):
"""
Migrate a specific store's data.
This function associates any orphaned items with the specified store.
Args:
app: The Flask application instance
store_id: The ID of the store to migrate
"""
with app.app_context():
try:
# Associate any orphaned products, collections, and tags with the specified store
with db.engine.connect() as conn:
conn.execute(text(f"UPDATE products SET store_id = {store_id} WHERE store_id IS NULL"))
conn.execute(text(f"UPDATE collections SET store_id = {store_id} WHERE store_id IS NULL"))
conn.execute(text(f"UPDATE tags SET store_id = {store_id} WHERE store_id IS NULL"))
print(f"Associated orphaned items with store (ID: {store_id})")
return True
except Exception as e:
print(f"Error during store migration: {str(e)}")
return False