From 6efb8e8c2d5b231ea4b1f418669ee1890c3e76bc Mon Sep 17 00:00:00 2001 From: Taizo Simpson Date: Fri, 12 Oct 2018 13:53:56 -0400 Subject: [PATCH] Added some migrations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WARNING: Still not ready for production — schema version needs incrementing, and testing still required --- supysonic/schema/migration/mysql/20181010.py | 24 +++++++++++++++++++ .../schema/migration/postgres/20181010.py | 21 ++++++++++++++++ supysonic/schema/migration/sqlite/20181010.py | 18 ++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 supysonic/schema/migration/mysql/20181010.py create mode 100644 supysonic/schema/migration/postgres/20181010.py create mode 100644 supysonic/schema/migration/sqlite/20181010.py diff --git a/supysonic/schema/migration/mysql/20181010.py b/supysonic/schema/migration/mysql/20181010.py new file mode 100644 index 0000000..cc42071 --- /dev/null +++ b/supysonic/schema/migration/mysql/20181010.py @@ -0,0 +1,24 @@ +import argparse +from supysonic import db +try: + import MySQLdb as provider +except ImportError: + import pymysql as provider + +parser = argparse.ArgumentParser() +parser.add_argument('username') +parser.add_argument('password') +parser.add_argument('database') +parser.add_argument('-H', '--host', default = 'localhost', help = 'default: localhost') +args = parser.parse_args() + +with provider.connect(host = args.host, user = args.username, passwd = args.password, db = args.database) as conn: + c = conn.cursor() + c.execute('ALTER TABLE track ADD COLUMN has_art BOOLEAN NOT NULL DEFAULT false') + + art = dict() + c.execute('SELECT path FROM track') + for row in c: + art[row[0]] = bool(db.Track._extract_cover_art(row[0].encode('utf-8'))) + c.executemany('UPDATE track SET has_art=? WHERE path=?', [ (a, p) for p, a in art.items() ]) + conn.commit() diff --git a/supysonic/schema/migration/postgres/20181010.py b/supysonic/schema/migration/postgres/20181010.py new file mode 100644 index 0000000..8716e26 --- /dev/null +++ b/supysonic/schema/migration/postgres/20181010.py @@ -0,0 +1,21 @@ +import argparse +import psycopg2 +from supysonic import db + +parser = argparse.ArgumentParser() +parser.add_argument('username') +parser.add_argument('password') +parser.add_argument('database') +parser.add_argument('-H', '--host', default = 'localhost', help = 'default: localhost') +args = parser.parse_args() + +with psycopg2.connect(host = args.host, user = args.username, password = args.password, dbname = args.database) as conn: + c = conn.cursor() + c.execute('ALTER TABLE track ADD COLUMN has_art BOOLEAN NOT NULL DEFAULT false') + + art = dict() + c.execute('SELECT path FROM track') + for row in c.fetchall(): + art[row[0]] = bool(db.Track._extract_cover_art(row[0].encode('utf-8'))) + c.executemany('UPDATE track SET has_art=%s WHERE path=%s', [ (a, p) for p, a in art.items() ]) + conn.commit() diff --git a/supysonic/schema/migration/sqlite/20181010.py b/supysonic/schema/migration/sqlite/20181010.py new file mode 100644 index 0000000..c3b7103 --- /dev/null +++ b/supysonic/schema/migration/sqlite/20181010.py @@ -0,0 +1,18 @@ +import argparse +import sqlite3 +from supysonic import db + +parser = argparse.ArgumentParser() +parser.add_argument('dbfile', help = 'Path to the SQLite database file') +args = parser.parse_args() + +with sqlite3.connect(args.dbfile) as conn: + c = conn.cursor() + c.execute('ALTER TABLE track ADD COLUMN has_art BOOLEAN NOT NULL DEFAULT false') + + art = dict() + for row in c.execute('SELECT path FROM track'): + art[row[0]] = bool(db.Track._extract_cover_art(row[0].encode('utf-8'))) + c.executemany('UPDATE track SET has_art=? WHERE path=?', [ (a, p) for p, a in art.items() ]) + conn.commit() + conn.execute('VACUUM')