LANG=C.UTF-8 FLASK_DEBUG=1 FLASK_APP=motion.py flask run
```
-To install database schema, run in an interactive python shell (`python`):
-```
-import motion
-motion.init_db()
-```
+The database schema is automatically installed when the table "schema_version" does not exist and the application is started.
def init_db():
with app.app_context():
db = get_db()
- with app.open_resource('schema.sql', mode='r') as f:
- db.execute(f.read())
+ try:
+ ver = db.prepare("SELECT version FROM schema_version")()[0][0];
+ print("Database Schema version: ", ver)
+ except postgresql.exceptions.UndefinedTableError:
+ ver = 0
+ if ver < 1:
+ with app.open_resource('sql/schema.sql', mode='r') as f:
+ db.execute(f.read())
+
+init_db()
@app.route("/")
def main():
result vote_type NOT NULL,
entered timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(motion_id, voter_id));
+
+DROP TABLE IF EXISTS schema_version;
+CREATE TABLE schema_version (version INTEGER NOT NULL);
+INSERT INTO schema_version(version) VALUES(1);