stacosys/dbmigration/migrate_from_1.1_to_2.0.py

41 lines
1.1 KiB
Python
Raw Normal View History

2023-11-26 17:51:51 +01:00
#!/usr/bin/env python
2021-01-18 19:25:45 +01:00
# -*- coding: UTF-8 -*-
import sqlite3
connection = sqlite3.connect("db.sqlite")
cursor = connection.cursor()
2022-04-26 20:51:42 +02:00
# What script performs:
2022-04-27 03:12:01 +02:00
# - first, remove site table: crash here if table doesn't exist
# (compatibility test without effort)
2022-04-27 03:04:55 +02:00
# - remove site_id column from comment table
2021-01-18 19:25:45 +01:00
script = """
PRAGMA foreign_keys = OFF;
BEGIN TRANSACTION;
DROP TABLE site;
ALTER TABLE comment RENAME TO _comment_old;
CREATE TABLE comment (
2022-04-27 03:04:55 +02:00
id INTEGER NOT NULL PRIMARY KEY,
url VARCHAR(255) NOT NULL,
2022-04-26 20:51:42 +02:00
notified DATETIME,
2022-04-27 03:04:55 +02:00
created DATETIME NOT NULL,
published DATETIME,
author_name VARCHAR(255) NOT NULL,
2022-04-26 20:51:42 +02:00
author_site VARCHAR(255) NOT NULL,
author_gravatar varchar(255),
2022-04-27 03:04:55 +02:00
content TEXT NOT NULL
2021-01-18 19:25:45 +01:00
);
2022-04-27 03:04:55 +02:00
INSERT INTO comment (id, url, notified, created, published,
author_name, author_site, author_gravatar, content)
SELECT id, url, notified, created, published,
author_name, author_site, author_gravatar, content
2021-01-18 19:25:45 +01:00
FROM _comment_old;
DROP TABLE _comment_old;
COMMIT;
PRAGMA foreign_keys = ON;
"""
cursor.executescript(script)
2022-04-26 20:51:42 +02:00
connection.close()