diff --git a/README.md b/README.md
index a153a9e..8ffbf84 100644
--- a/README.md
+++ b/README.md
@@ -37,6 +37,17 @@ flask init-db
Database then lives in `database.sqlite`. To reset database, delete this file and run `init-db` again.
+
+## Migrate to v.1.0.0
+
+(see https://github.com/mbarde/sloth-tools/issues/11)
+
+```
+export FLASK_APP=server.py
+flask migrate-to-v1
+```
+
+
## Run in dev mode
```
@@ -81,6 +92,7 @@ Enable:
sudo systemctl enable homecontrol.service
```
+
## API
Once running you can access Sloth Tools via the provided web application. But you can also directly interact with the backend by calling certain endpoints:
@@ -99,6 +111,3 @@ For example you could use following script to blink node 42 everytime a motion i
#!/bin/sh
wget -O/dev/null 192.168.0.13:5000/toggle?id=42 -q
```
-
-
-
diff --git a/db.py b/db.py
index 3fa8d3d..06c4350 100644
--- a/db.py
+++ b/db.py
@@ -30,6 +30,37 @@ def init_db():
db.executescript(f.read().decode('utf8'))
+@click.command('migrate-to-v1')
+@with_appcontext
+def migrate_to_v1():
+ # version 1.0.0 introduces sorting feature for nodes,
+ # which depends on additional field `sort_order`
+ # in `node` table.
+ db = get_db()
+
+ cursor = db.cursor()
+ cursor.execute('PRAGMA table_info(node);')
+ columns_info = cursor.fetchall()
+ columns_names = [c['name'] for c in columns_info]
+
+ if 'sort_order' not in columns_names:
+ click.echo('Adding missing field `sort_order` to table `node`.')
+ cursor.execute('ALTER TABLE `node` ADD `sort_order` INTEGER NOT NULL;')
+
+ cursor.execute('CREATE UNIQUE INDEX `idx_unique_sort_order` ON `node` (`sort_order`);')
+
+ click.echo('Initializing new field `sort_order` with node ID.')
+ query = 'UPDATE node SET "sort_order" = id;'
+ cursor.execute(query)
+ db.commit()
+
+ cursor.close()
+
+ # we have to stop timer manually here
+ # otherwise process will run forever
+ current_app.eventTable.stopTimer()
+
+
@click.command('init-db')
@with_appcontext
def init_db_command():
@@ -44,3 +75,4 @@ def init_db_command():
def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)
+ app.cli.add_command(migrate_to_v1)
diff --git a/nodes.html b/nodes.html
index 41e406d..c8e71a8 100644
--- a/nodes.html
+++ b/nodes.html
@@ -13,6 +13,16 @@
+ {% if loop.index > 1 %}
+
+ {% else %}
+
+ {% endif %}
+ {% if loop.index < nodes | length %}
+
+ {% else %}
+
+ {% endif %}