From a70b4f2eaecfb7c02ccf9657a602b2f49d015cb7 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Wed, 6 Nov 2019 11:12:11 +0100 Subject: [PATCH] Schema and version number changes --- CHANGELOG.md | 5 +++++ app/__init__.py | 2 +- app/models.py | 9 ++++++++- migrate_0_7_to_0_8.py | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 migrate_0_7_to_0_8.py diff --git a/CHANGELOG.md b/CHANGELOG.md index c022145..6759900 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.8-dev + +* Schema changes: moved `full_name` field from table `userprofile` to table `user` for search improvement reasons. +* Adding `messages_count`, `followers_count` and `following_count` to `profile_info` API endpoint (what I've done to 0.7.1 too). + ## 0.7.1-dev * Adding `messages_count`, `followers_count` and `following_count` to `profile_info` API endpoint (forgot to release). diff --git a/app/__init__.py b/app/__init__.py index fdab8d1..cbcdd40 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -20,7 +20,7 @@ import datetime, time, re, os, sys, string, json, html from functools import wraps from flask_login import LoginManager -__version__ = '0.7.0' +__version__ = '0.8-dev' # we want to support Python 3 only. # Python 2 has too many caveats. diff --git a/app/models.py b/app/models.py index 9626890..5c35108 100644 --- a/app/models.py +++ b/app/models.py @@ -26,6 +26,8 @@ class BaseModel(Model): class User(BaseModel): # The unique username. username = CharField(unique=True) + # The user's full name (here for better search since 0.8) + full_name = TextField() # The password hash. password = CharField() # An email address. @@ -107,7 +109,6 @@ class UserAdminship(BaseModel): # New in 0.6 class UserProfile(BaseModel): user = ForeignKeyField(User, primary_key=True) - full_name = TextField() biography = TextField(default='') location = IntegerField(null=True) year = IntegerField(null=True) @@ -115,6 +116,12 @@ class UserProfile(BaseModel): instagram = TextField(null=True) facebook = TextField(null=True) telegram = TextField(null=True) + @property + def full_name(self): + ''' + Moved to User in 0.8 for search improvement reasons. + ''' + return self.user.full_name # The message privacy values. MSGPRV_PUBLIC = 0 # everyone diff --git a/migrate_0_7_to_0_8.py b/migrate_0_7_to_0_8.py new file mode 100644 index 0000000..ab1390b --- /dev/null +++ b/migrate_0_7_to_0_8.py @@ -0,0 +1,20 @@ +import sqlite3 + +import sqlite3 + +conn = sqlite3.connect('coriplus.sqlite') + +if __name__ == '__main__': + conn.executescript(''' +BEGIN TRANSACTION; + CREATE TABLE "new_userprofile" ("user_id" INTEGER NOT NULL PRIMARY KEY, "biography" TEXT NOT NULL, "location" INTEGER, "year" INTEGER, "website" TEXT, "instagram" TEXT, "facebook" TEXT, telegram TEXT, FOREIGN KEY ("user_id") REFERENCES "user" ("id")); + CREATE TABLE "new_user" ("id" INTEGER NOT NULL PRIMARY KEY, "username" VARCHAR(30) NOT NULL, "full_name" VARCHAR(30), "password" VARCHAR(255) NOT NULL, "email" VARCHAR(255) NOT NULL, "birthday" DATE NOT NULL, "join_date" DATETIME NOT NULL, "is_disabled" INTEGER NOT NULL); + INSERT INTO new_user (id, username, full_name, password, email, birthday, join_date, is_disabled) SELECT t1.id, t1.username, t2.full_name, t1.password, t1.email, t1.birthday, t1.join_date, t1.is_disabled FROM user AS t1 LEFT JOIN userprofile AS t2 ON t1.id = t2.user_id; + INSERT INTO new_userprofile (user_id, biography, location, year, website, instagram, facebook, telegram) SELECT user_id, biography, location, year, website, instagram, facebook, telegram FROM userprofile; + UPDATE new_user SET full_name = username WHERE username IS NULL; + DROP TABLE user; + DROP TABLE userprofile; + ALTER TABLE new_user RENAME TO user; + ALTER TABLE new_userprofile RENAME TO userprofile; +COMMIT; +''')