Overview
Comment: | Implemented function to update database files to version 0.1.6 . |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | tools |
Files: | files | file ages | folders |
SHA3-256: |
ba851b13f5c57946da4208d752fea5fe |
User & Date: | florian 2024-07-30 17:45:35 |
Context
2024-08-01
| ||
21:06 | Merged changes from development. check-in: be29261b3f user: florian tags: tools | |
2024-07-31
| ||
14:11 | Merged changes from tools. check-in: 5c175eb73a user: florian tags: development | |
2024-07-30
| ||
17:45 | Implemented function to update database files to version 0.1.6 . check-in: ba851b13f5 user: florian tags: tools | |
14:09 | Adding stub tool script to upgrade databases. check-in: ac5144c06b user: florian tags: tools | |
Changes
Changes to tools/upgrade_db.py.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | """Upgrade the Luna LMS database to the current version. """ # Work started on 2024-07-30 import site site.addsitedir("../") import luna_lms from optparse import OptionParser import os.path import sqlite3 UPDATE_REGISTRY = {} def register(f): """Register handler functions for database updates. """ numerical_version = int(f.__name__.split("_update_to_")[1]) UPDATE_REGISTRY[numerical_version] = f return f @register def _update_to_1006(connection): | > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | """Upgrade the Luna LMS database to the current version. """ # Work started on 2024-07-30 import site site.addsitedir("../") import luna_lms from optparse import OptionParser import os.path import sqlite3 UPDATE_REGISTRY = {} def register(f): """Register handler functions for database updates. """ numerical_version = int(f.__name__.split("_update_to_")[1]) UPDATE_REGISTRY[numerical_version] = f return f @register def _update_to_1006(connection): """Update the database to 0.1.6. """ print("Updating database at connection {} to 0.1.6".format(connection)) cursor = connection.cursor() # Check if the database is actually in the claimed format result = None try: result = cursor.execute('SELECT * FROM formats;') result = result.fetchone() except sqlite3.OperationalError: print("Database is in pre 0.1.6 format") if result is not None: # Table exists, so database is not actually pre 0.1.6 connection.commit() connection.close() raise Exception("The database is not in pre 0.1.6 format") # Create new table result = cursor.execute(''' CREATE TABLE "formats" ( "key" INTEGER, "identifier" TEXT UNIQUE, "publisher" TEXT NOT NULL, "media_type" TEXT NOT NULL, "language" TEXT, "sha256" TEXT NOT NULL, PRIMARY KEY("key") ); ''') result.fetchone() # Populate table with bare minimum standard values result = cursor.execute(''' INSERT INTO "formats" ( "key", "identifier", "publisher", "media_type", "language", "sha256" ) VALUES ( 1, 'luna.text.html.en', 'luna', 'text/html', 'en', '82b141764d5ea3957d1969894a15c3904321fcad0a33f1ed75aad78d71e23836' ), ( 2, 'luna.text.html.de', 'luna', 'text/html', 'de', '49b429fc7f07a05e81a66dd215fc72ed9305751baa4ae7d8622eef81769be69b' ); ''') result.fetchone() connection.commit() # Add foreign key constraint referencing the new table # # Adding new foreign keys in place taken from this answer to "How do I add a # foreign key to an existing SQLite table?": # https://stackoverflow.com/a/48016934/1132250 print("Existing SQL for table variants:") result = cursor.execute("select sql from SQLITE_MASTER where name = 'variants' and type = 'table';") result = result.fetchone() print(result) print("Updated SQL for table variants:") result = cursor.execute(''' select replace( sql, 'PRIMARY KEY("key")', 'PRIMARY KEY("key"), FOREIGN KEY("format") REFERENCES "formats"("identifier") ON UPDATE CASCADE ON DELETE SET NULL' ) from SQLITE_MASTER where name = 'variants' and type = 'table'; ''') result = result.fetchone() print(result) print("Writing updated SQL ...") cursor.execute("pragma writable_schema=1;") result = cursor.execute(''' update SQLITE_MASTER set sql = replace( sql, 'PRIMARY KEY("key")', 'PRIMARY KEY("key"), FOREIGN KEY("format") REFERENCES "formats"("identifier") ON UPDATE CASCADE ON DELETE RESTRICT' ) where name = 'variants' and type = 'table'; ''') result = result.fetchone() connection.commit() cursor.execute("pragma writable_schema=0;") print("Done.") # Update previous formats result = cursor.execute(''' UPDATE variants SET format = 'luna.text.html.en' WHERE format = 'text/html'; ''') result = result.fetchone() connection.commit() print('''INFO: The previous format 'text/html' has been unconditionally replaced by 'luna.text.html.en'. If your HTML content is not in English, you should add a respective format in table 'formats', and replace the format value of the variant accordingly. ''') # Finally, update required version number result = cursor.execute("UPDATE course SET requires = 'Luna LMS 0.1.6';") result = result.fetchone() connection.commit() print("Update to 0.1.6 successful.") def main(): parser = OptionParser(usage = "%prog [options] SQLITE_FILE") (options, args) = parser.parse_args() |
︙ | ︙ | |||
95 96 97 98 99 100 101 | UPDATE_REGISTRY[numerical_handler_version](connection) print("Closing connection") connection.commit() connection.close() | | > | 240 241 242 243 244 245 246 247 248 249 250 251 | UPDATE_REGISTRY[numerical_handler_version](connection) print("Closing connection") connection.commit() connection.close() if __name__ == "__main__": main() |