Spaces:
Sleeping
Sleeping
Upload migrate_guest.py
Browse files- migrate_guest.py +64 -0
migrate_guest.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
|
| 3 |
+
conn = sqlite3.connect('vortex.db')
|
| 4 |
+
cursor = conn.cursor()
|
| 5 |
+
|
| 6 |
+
# Check current constraints
|
| 7 |
+
cursor.execute('PRAGMA table_info(carts)')
|
| 8 |
+
columns = cursor.fetchall()
|
| 9 |
+
print("Current carts columns:")
|
| 10 |
+
for col in columns:
|
| 11 |
+
print(f" {col}")
|
| 12 |
+
|
| 13 |
+
# SQLite doesn't support ALTER COLUMN to change NOT NULL.
|
| 14 |
+
# We need to recreate the table with the new schema.
|
| 15 |
+
# First, back up data
|
| 16 |
+
cursor.execute('SELECT * FROM carts')
|
| 17 |
+
rows = cursor.fetchall()
|
| 18 |
+
print(f"\nExisting carts rows: {len(rows)}")
|
| 19 |
+
|
| 20 |
+
# Drop and recreate - safest approach for SQLite
|
| 21 |
+
cursor.execute('DROP TABLE IF EXISTS cart_items')
|
| 22 |
+
cursor.execute('DROP TABLE IF EXISTS carts')
|
| 23 |
+
|
| 24 |
+
cursor.execute('''
|
| 25 |
+
CREATE TABLE carts (
|
| 26 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 27 |
+
user_id INTEGER,
|
| 28 |
+
session_id VARCHAR(255),
|
| 29 |
+
coupon_id INTEGER,
|
| 30 |
+
created_at DATETIME NOT NULL DEFAULT (datetime('now')),
|
| 31 |
+
updated_at DATETIME NOT NULL DEFAULT (datetime('now')),
|
| 32 |
+
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
| 33 |
+
FOREIGN KEY (coupon_id) REFERENCES coupons(id) ON DELETE SET NULL
|
| 34 |
+
)
|
| 35 |
+
''')
|
| 36 |
+
|
| 37 |
+
cursor.execute('''
|
| 38 |
+
CREATE TABLE cart_items (
|
| 39 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 40 |
+
cart_id INTEGER NOT NULL,
|
| 41 |
+
product_id INTEGER NOT NULL,
|
| 42 |
+
quantity INTEGER NOT NULL DEFAULT 1,
|
| 43 |
+
unit_price FLOAT NOT NULL,
|
| 44 |
+
created_at DATETIME NOT NULL DEFAULT (datetime('now')),
|
| 45 |
+
updated_at DATETIME NOT NULL DEFAULT (datetime('now')),
|
| 46 |
+
FOREIGN KEY (cart_id) REFERENCES carts(id) ON DELETE CASCADE,
|
| 47 |
+
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
|
| 48 |
+
)
|
| 49 |
+
''')
|
| 50 |
+
|
| 51 |
+
# Create index for session_id
|
| 52 |
+
cursor.execute('CREATE UNIQUE INDEX IF NOT EXISTS ix_carts_session_id ON carts(session_id)')
|
| 53 |
+
|
| 54 |
+
conn.commit()
|
| 55 |
+
|
| 56 |
+
# Verify
|
| 57 |
+
cursor.execute('PRAGMA table_info(carts)')
|
| 58 |
+
columns = cursor.fetchall()
|
| 59 |
+
print("\nNew carts columns:")
|
| 60 |
+
for col in columns:
|
| 61 |
+
print(f" {col}")
|
| 62 |
+
|
| 63 |
+
conn.close()
|
| 64 |
+
print("\nMigration complete - carts and cart_items recreated with nullable user_id and session_id")
|