Make a Database Reset Route

Purpose: In later project parts, you will learn to make web forms that change database data. Frequently, you will want to reset the database, either because there was an error or because of too much experimenting. It is often helpful to reset the database. In professional settings, database rollbacks serve a similar purpose and can become complicated depending on the needs, but in this class, our rollback need is simple. In this exercise, you will discuss two options for resetting the database, then create a route in your project to do it.

Practice Exporting and Importing a .sql File

Unlike SQLite databases, most database engines do not store an entire database in a single file. In those cases, restoring a database usually involves importing and exporting SQL into a .sql file. SQL is "exported" by generating an SQL file with SQL queries that would re-create the database. SQL is "imported" by running the commands in the file, in sequence.

In your project, practice exporting company.db to backup.sql from the sqlite console using the .dump command:

.output backup.sql
.dump
.quit

Now practice exporting company.db to backup.sql from the command line:

sqlite3 company.db ".dump" > backup.sql

Now practice importing from a .sql file (into a dummy database called restored.db) using the .read command in the SQLite console:

.read backup.sql

Look through the database and make sure it imported properly. Now, delete restored.db and practice re-importing it from the command line:

sqlite3 database.db < input_file.sql

After running SQLite and making sure the import worked, feel free to delete restored.db.

Practice Backup and Restore from a .db File

In SQLite, where an entire database is stored in a single file, it may be easier to restore a database just by copying the file. Practice doing so by copying company.db to backup.db and copying backup.db back to company.db. Open the databases in SQLite to confirm that it worked.

Use Python to Backup and Restore a SQLite Database

In the Python SQLite library, the .backup method copies a database file but also ensures that it does not interrupt other database operations in case there is an open connection. In model.py in your Project, create model functions called data_backup() and data_restore() that overwrite the database file from a backup database file. The following functions give the basic functionality.

Add try and except directives to determine whether backup or restore is successful.

Add Admin Template and Routes

Create a template in your project called admin.html. It inherits the default layout and has two links -- one to a "data_backup" route and one to a "data_restore" route. The backup and restore links require a confirmation from the user before going to the route (an easy way to do this is to call the Javascript confirm() function and return its return value; returning false cancels the click). It also has the ability to display a message from a Jinja variable called "message." If the message variable is blank, it displays no message. The wording and format are up to you.

In app.py, create routes called "/admin", "/data_backup", and "/data_restore()". The "/admin" route renders the "admin.html" template with no message. The "/data_backup" route calls data_backup() and renders the "admin.html" template with a custom success or failure message. The "/data_restore()" route calls data_restore() and renders the "admin.html" template with a custom success or failure message.

Do not connect these routes to your nav. They are admin routes. For now, being secret is their only protection, although later, in Project Part 6, we will protect them using login and authorization functionality.