Add unique constraint on models path and cleanup duplicates

- Added UNIQUE constraint on path column in models table
- Added cleanup query to remove duplicate models, keeping only one per path
parent 1fb1c1e5
...@@ -540,7 +540,7 @@ def init_db(conn) -> None: ...@@ -540,7 +540,7 @@ def init_db(conn) -> None:
id INT AUTO_INCREMENT PRIMARY KEY, id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL,
type VARCHAR(50) NOT NULL, type VARCHAR(50) NOT NULL,
path TEXT NOT NULL, path TEXT NOT NULL UNIQUE,
vram_estimate INT DEFAULT 0, vram_estimate INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
...@@ -552,13 +552,30 @@ def init_db(conn) -> None: ...@@ -552,13 +552,30 @@ def init_db(conn) -> None:
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
type TEXT NOT NULL, type TEXT NOT NULL,
path TEXT NOT NULL, path TEXT NOT NULL UNIQUE,
vram_estimate INTEGER DEFAULT 0, vram_estimate INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) )
''') ''')
# Clean up duplicate models (keep only the first one for each path)
if config['type'] == 'mysql':
cursor.execute('''
DELETE t1 FROM models t1
INNER JOIN models t2
WHERE t1.id > t2.id AND t1.path = t2.path
''')
else:
cursor.execute('''
DELETE FROM models
WHERE id NOT IN (
SELECT MIN(id)
FROM models
GROUP BY path
)
''')
# Insert default model if not exist # Insert default model if not exist
default_model_name = 'Qwen/Qwen2.5-VL-7B-Instruct' default_model_name = 'Qwen/Qwen2.5-VL-7B-Instruct'
cursor.execute('SELECT id FROM models WHERE path = ?', (default_model_name,)) cursor.execute('SELECT id FROM models WHERE path = ?', (default_model_name,))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment