Change all "conn = engine.connect()" statements to "with engine.connect() as conn:" in ub.py
Changed all the statements in ub.py to use python context managers so that conn.close() is called automatically. (https://docs.sqlalchemy.org/en/13/core/connections.html)
This commit is contained in:
		
							parent
							
								
									b309c1fc91
								
							
						
					
					
						commit
						76b0505bd9
					
				
							
								
								
									
										90
									
								
								cps/ub.py
									
									
									
									
									
								
							
							
						
						
									
										90
									
								
								cps/ub.py
									
									
									
									
									
								
							| 
						 | 
					@ -474,34 +474,34 @@ def migrate_Database(session):
 | 
				
			||||||
        ArchivedBook.__table__.create(bind=engine)
 | 
					        ArchivedBook.__table__.create(bind=engine)
 | 
				
			||||||
    if not engine.dialect.has_table(engine.connect(), "registration"):
 | 
					    if not engine.dialect.has_table(engine.connect(), "registration"):
 | 
				
			||||||
        ReadBook.__table__.create(bind=engine)
 | 
					        ReadBook.__table__.create(bind=engine)
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("insert into registration (domain, allow) values('%.%',1)")
 | 
					            conn.execute("insert into registration (domain, allow) values('%.%',1)")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        session.query(exists().where(Registration.allow)).scalar()
 | 
					        session.query(exists().where(Registration.allow)).scalar()
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
					    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("ALTER TABLE registration ADD column 'allow' INTEGER")
 | 
					            conn.execute("ALTER TABLE registration ADD column 'allow' INTEGER")
 | 
				
			||||||
        conn.execute("update registration set 'allow' = 1")
 | 
					            conn.execute("update registration set 'allow' = 1")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        session.query(exists().where(RemoteAuthToken.token_type)).scalar()
 | 
					        session.query(exists().where(RemoteAuthToken.token_type)).scalar()
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
					    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("ALTER TABLE remote_auth_token ADD column 'token_type' INTEGER DEFAULT 0")
 | 
					            conn.execute("ALTER TABLE remote_auth_token ADD column 'token_type' INTEGER DEFAULT 0")
 | 
				
			||||||
        conn.execute("update remote_auth_token set 'token_type' = 0")
 | 
					            conn.execute("update remote_auth_token set 'token_type' = 0")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        session.query(exists().where(ReadBook.read_status)).scalar()
 | 
					        session.query(exists().where(ReadBook.read_status)).scalar()
 | 
				
			||||||
    except exc.OperationalError:
 | 
					    except exc.OperationalError:
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("ALTER TABLE book_read_link ADD column 'read_status' INTEGER DEFAULT 0")
 | 
					            conn.execute("ALTER TABLE book_read_link ADD column 'read_status' INTEGER DEFAULT 0")
 | 
				
			||||||
        conn.execute("UPDATE book_read_link SET 'read_status' = 1 WHERE is_read")
 | 
					            conn.execute("UPDATE book_read_link SET 'read_status' = 1 WHERE is_read")
 | 
				
			||||||
        conn.execute("ALTER TABLE book_read_link ADD column 'last_modified' DATETIME")
 | 
					            conn.execute("ALTER TABLE book_read_link ADD column 'last_modified' DATETIME")
 | 
				
			||||||
        conn.execute("ALTER TABLE book_read_link ADD column 'last_time_started_reading' DATETIME")
 | 
					            conn.execute("ALTER TABLE book_read_link ADD column 'last_time_started_reading' DATETIME")
 | 
				
			||||||
        conn.execute("ALTER TABLE book_read_link ADD column 'times_started_reading' INTEGER DEFAULT 0")
 | 
					            conn.execute("ALTER TABLE book_read_link ADD column 'times_started_reading' INTEGER DEFAULT 0")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    test = session.query(ReadBook).filter(ReadBook.last_modified == None).all()
 | 
					    test = session.query(ReadBook).filter(ReadBook.last_modified == None).all()
 | 
				
			||||||
    for book in test:
 | 
					    for book in test:
 | 
				
			||||||
| 
						 | 
					@ -510,11 +510,11 @@ def migrate_Database(session):
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        session.query(exists().where(Shelf.uuid)).scalar()
 | 
					        session.query(exists().where(Shelf.uuid)).scalar()
 | 
				
			||||||
    except exc.OperationalError:
 | 
					    except exc.OperationalError:
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("ALTER TABLE shelf ADD column 'uuid' STRING")
 | 
					            conn.execute("ALTER TABLE shelf ADD column 'uuid' STRING")
 | 
				
			||||||
        conn.execute("ALTER TABLE shelf ADD column 'created' DATETIME")
 | 
					            conn.execute("ALTER TABLE shelf ADD column 'created' DATETIME")
 | 
				
			||||||
        conn.execute("ALTER TABLE shelf ADD column 'last_modified' DATETIME")
 | 
					            conn.execute("ALTER TABLE shelf ADD column 'last_modified' DATETIME")
 | 
				
			||||||
        conn.execute("ALTER TABLE book_shelf_link ADD column 'date_added' DATETIME")
 | 
					            conn.execute("ALTER TABLE book_shelf_link ADD column 'date_added' DATETIME")
 | 
				
			||||||
        for shelf in session.query(Shelf).all():
 | 
					        for shelf in session.query(Shelf).all():
 | 
				
			||||||
            shelf.uuid = str(uuid.uuid4())
 | 
					            shelf.uuid = str(uuid.uuid4())
 | 
				
			||||||
            shelf.created = datetime.datetime.now()
 | 
					            shelf.created = datetime.datetime.now()
 | 
				
			||||||
| 
						 | 
					@ -525,31 +525,31 @@ def migrate_Database(session):
 | 
				
			||||||
    # Handle table exists, but no content
 | 
					    # Handle table exists, but no content
 | 
				
			||||||
    cnt = session.query(Registration).count()
 | 
					    cnt = session.query(Registration).count()
 | 
				
			||||||
    if not cnt:
 | 
					    if not cnt:
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("insert into registration (domain, allow) values('%.%',1)")
 | 
					            conn.execute("insert into registration (domain, allow) values('%.%',1)")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        session.query(exists().where(BookShelf.order)).scalar()
 | 
					        session.query(exists().where(BookShelf.order)).scalar()
 | 
				
			||||||
    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
					    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("ALTER TABLE book_shelf_link ADD column 'order' INTEGER DEFAULT 1")
 | 
					            conn.execute("ALTER TABLE book_shelf_link ADD column 'order' INTEGER DEFAULT 1")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        create = False
 | 
					        create = False
 | 
				
			||||||
        session.query(exists().where(User.sidebar_view)).scalar()
 | 
					        session.query(exists().where(User.sidebar_view)).scalar()
 | 
				
			||||||
    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
					    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("ALTER TABLE user ADD column `sidebar_view` Integer DEFAULT 1")
 | 
					            conn.execute("ALTER TABLE user ADD column `sidebar_view` Integer DEFAULT 1")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
        create = True
 | 
					        create = True
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        if create:
 | 
					        if create:
 | 
				
			||||||
            conn = engine.connect()
 | 
					            with engine.connect() as conn:
 | 
				
			||||||
            conn.execute("SELECT language_books FROM user")
 | 
					                conn.execute("SELECT language_books FROM user")
 | 
				
			||||||
            session.commit()
 | 
					            session.commit()
 | 
				
			||||||
    except exc.OperationalError:
 | 
					    except exc.OperationalError:
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("UPDATE user SET 'sidebar_view' = (random_books* :side_random + language_books * :side_lang "
 | 
					            conn.execute("UPDATE user SET 'sidebar_view' = (random_books* :side_random + language_books * :side_lang "
 | 
				
			||||||
                     "+ series_books * :side_series + category_books * :side_category + hot_books * "
 | 
					                     "+ series_books * :side_series + category_books * :side_category + hot_books * "
 | 
				
			||||||
                     ":side_hot + :side_autor + :detail_random)",
 | 
					                     ":side_hot + :side_autor + :detail_random)",
 | 
				
			||||||
                     {'side_random': constants.SIDEBAR_RANDOM, 'side_lang': constants.SIDEBAR_LANGUAGE,
 | 
					                     {'side_random': constants.SIDEBAR_RANDOM, 'side_lang': constants.SIDEBAR_LANGUAGE,
 | 
				
			||||||
| 
						 | 
					@ -560,29 +560,29 @@ def migrate_Database(session):
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        session.query(exists().where(User.denied_tags)).scalar()
 | 
					        session.query(exists().where(User.denied_tags)).scalar()
 | 
				
			||||||
    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
					    except exc.OperationalError:  # Database is not compatible, some columns are missing
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("ALTER TABLE user ADD column `denied_tags` String DEFAULT ''")
 | 
					            conn.execute("ALTER TABLE user ADD column `denied_tags` String DEFAULT ''")
 | 
				
			||||||
        conn.execute("ALTER TABLE user ADD column `allowed_tags` String DEFAULT ''")
 | 
					            conn.execute("ALTER TABLE user ADD column `allowed_tags` String DEFAULT ''")
 | 
				
			||||||
        conn.execute("ALTER TABLE user ADD column `denied_column_value` String DEFAULT ''")
 | 
					            conn.execute("ALTER TABLE user ADD column `denied_column_value` String DEFAULT ''")
 | 
				
			||||||
        conn.execute("ALTER TABLE user ADD column `allowed_column_value` String DEFAULT ''")
 | 
					            conn.execute("ALTER TABLE user ADD column `allowed_column_value` String DEFAULT ''")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        session.query(exists().where(User.series_view)).scalar()
 | 
					        session.query(exists().where(User.series_view)).scalar()
 | 
				
			||||||
    except exc.OperationalError:
 | 
					    except exc.OperationalError:
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("ALTER TABLE user ADD column `series_view` VARCHAR(10) DEFAULT 'list'")
 | 
					            conn.execute("ALTER TABLE user ADD column `series_view` VARCHAR(10) DEFAULT 'list'")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if session.query(User).filter(User.role.op('&')(constants.ROLE_ANONYMOUS) == constants.ROLE_ANONYMOUS).first() \
 | 
					    if session.query(User).filter(User.role.op('&')(constants.ROLE_ANONYMOUS) == constants.ROLE_ANONYMOUS).first() \
 | 
				
			||||||
        is None:
 | 
					        is None:
 | 
				
			||||||
        create_anonymous_user(session)
 | 
					        create_anonymous_user(session)
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        # check if one table with autoincrement is existing (should be user table)
 | 
					        # check if one table with autoincrement is existing (should be user table)
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("SELECT COUNT(*) FROM sqlite_sequence WHERE name='user'")
 | 
					            conn.execute("SELECT COUNT(*) FROM sqlite_sequence WHERE name='user'")
 | 
				
			||||||
    except exc.OperationalError:
 | 
					    except exc.OperationalError:
 | 
				
			||||||
        # Create new table user_id and copy contents of table user into it
 | 
					        # Create new table user_id and copy contents of table user into it
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("CREATE TABLE user_id (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
 | 
					            conn.execute("CREATE TABLE user_id (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
 | 
				
			||||||
                     "nickname VARCHAR(64),"
 | 
					                     "nickname VARCHAR(64),"
 | 
				
			||||||
                     "email VARCHAR(120),"
 | 
					                     "email VARCHAR(120),"
 | 
				
			||||||
                     "role SMALLINT,"
 | 
					                     "role SMALLINT,"
 | 
				
			||||||
| 
						 | 
					@ -594,19 +594,19 @@ def migrate_Database(session):
 | 
				
			||||||
                     "series_view VARCHAR(10),"
 | 
					                     "series_view VARCHAR(10),"
 | 
				
			||||||
                     "UNIQUE (nickname),"
 | 
					                     "UNIQUE (nickname),"
 | 
				
			||||||
                     "UNIQUE (email))")
 | 
					                     "UNIQUE (email))")
 | 
				
			||||||
        conn.execute("INSERT INTO user_id(id, nickname, email, role, password, kindle_mail,locale,"
 | 
					            conn.execute("INSERT INTO user_id(id, nickname, email, role, password, kindle_mail,locale,"
 | 
				
			||||||
                     "sidebar_view, default_language, series_view) "
 | 
					                     "sidebar_view, default_language, series_view) "
 | 
				
			||||||
                     "SELECT id, nickname, email, role, password, kindle_mail, locale,"
 | 
					                     "SELECT id, nickname, email, role, password, kindle_mail, locale,"
 | 
				
			||||||
                     "sidebar_view, default_language FROM user")
 | 
					                     "sidebar_view, default_language FROM user")
 | 
				
			||||||
        # delete old user table and rename new user_id table to user:
 | 
					        # delete old user table and rename new user_id table to user:
 | 
				
			||||||
        conn.execute("DROP TABLE user")
 | 
					            conn.execute("DROP TABLE user")
 | 
				
			||||||
        conn.execute("ALTER TABLE user_id RENAME TO user")
 | 
					            conn.execute("ALTER TABLE user_id RENAME TO user")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Remove login capability of user Guest
 | 
					    # Remove login capability of user Guest
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        conn = engine.connect()
 | 
					        with engine.connect() as conn:
 | 
				
			||||||
        conn.execute("UPDATE user SET password='' where nickname = 'Guest' and password !=''")
 | 
					            conn.execute("UPDATE user SET password='' where nickname = 'Guest' and password !=''")
 | 
				
			||||||
        session.commit()
 | 
					        session.commit()
 | 
				
			||||||
    except exc.OperationalError:
 | 
					    except exc.OperationalError:
 | 
				
			||||||
        print('Settings database is not writeable. Exiting...')
 | 
					        print('Settings database is not writeable. Exiting...')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user