Migration from SQLite to PostgreSQL for compatibility with Heroku
This commit is contained in:
parent
34c3e5db2a
commit
b62e8ecbb3
5 changed files with 72 additions and 7 deletions
|
@ -1,2 +1,3 @@
|
||||||
flask
|
flask
|
||||||
mock
|
mock
|
||||||
|
psycopg2
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import os
|
import os
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from . import config, database, log, models
|
from . import config, database_postgresql, log, models
|
||||||
|
|
||||||
|
|
||||||
log.configure_logging()
|
log.configure_logging()
|
||||||
|
@ -36,7 +36,7 @@ if 'SECRETKEY' in conf.config:
|
||||||
else:
|
else:
|
||||||
exit("Secret key not set.")
|
exit("Secret key not set.")
|
||||||
|
|
||||||
db = database.Database(conf.config["DBNAME"])
|
db = database_postgresql.PostgreSQL(conf.config["DBNAME"])
|
||||||
models.init_models(db)
|
models.init_models(db)
|
||||||
|
|
||||||
|
|
||||||
|
|
66
smash/database_postgresql.py
Normal file
66
smash/database_postgresql.py
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import os
|
||||||
|
import psycopg2
|
||||||
|
import urlparse
|
||||||
|
|
||||||
|
class DatabasePostgreSQL(object):
|
||||||
|
"""Interface to the heroku PostgreSQL database plugin.
|
||||||
|
"""
|
||||||
|
def __init__(self):
|
||||||
|
urlparse.uses_netloc.append("postgres")
|
||||||
|
self.url = urlparse.urlparse(os.environ["DATABASE_URL"])
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def conn(self):
|
||||||
|
return psycopg2.connect(
|
||||||
|
database=self.url.path[1:],
|
||||||
|
user=self.url.username,
|
||||||
|
password=self.url.password,
|
||||||
|
host=self.url.hostname,
|
||||||
|
port=self.url.port
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cursor(self):
|
||||||
|
return self.conn.cursor()
|
||||||
|
|
||||||
|
|
||||||
|
def select(self, table, fields, condition=None):
|
||||||
|
cur = self.cursor
|
||||||
|
|
||||||
|
if condition is not None:
|
||||||
|
cur.execute("SELECT {} FROM {} WHERE {}".format(fields, table,
|
||||||
|
condition))
|
||||||
|
else:
|
||||||
|
cur.execute("SELECT {} FROM {}".format(fields, table))
|
||||||
|
|
||||||
|
return cur.fetchall()
|
||||||
|
|
||||||
|
|
||||||
|
def create_table(self, table, cols):
|
||||||
|
conn = self.conn
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
query = "CREATE TABLE {}({})".format(table, cols)
|
||||||
|
cur.execute(query)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def insert(self, table, columns, values, params):
|
||||||
|
conn = self.conn
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
query = "INSERT INTO {}({}) VALUES ({})".format(table, columns, values)
|
||||||
|
cur.execute(query, params)
|
||||||
|
conn.commit()
|
||||||
|
return cur
|
||||||
|
|
||||||
|
|
||||||
|
def delete(self, table, condition):
|
||||||
|
conn = self.conn
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
query = "DELETE FROM {} WHERE {}".format(table, condition)
|
||||||
|
cur.execute(query)
|
||||||
|
conn.commit()
|
|
@ -1,5 +1,3 @@
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
class Model(object):
|
class Model(object):
|
||||||
#Name of the table to be created
|
#Name of the table to be created
|
||||||
tablename = "abstractmodel"
|
tablename = "abstractmodel"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import sqlite3
|
import psycopg2
|
||||||
from flask import render_template, Markup, request, abort, session
|
from flask import render_template, Markup, request, abort, session
|
||||||
|
|
||||||
from smash import app, conf, db
|
from smash import app, conf, db
|
||||||
|
@ -155,7 +155,7 @@ def add_new():
|
||||||
(tag,)
|
(tag,)
|
||||||
)
|
)
|
||||||
tid = cur.lastrowid
|
tid = cur.lastrowid
|
||||||
except sqlite3.IntegrityError:
|
except psycopg2.IntegrityError:
|
||||||
logger.warning("Tag {} already exists".format(tag))
|
logger.warning("Tag {} already exists".format(tag))
|
||||||
|
|
||||||
if tid != -1:
|
if tid != -1:
|
||||||
|
@ -166,7 +166,7 @@ def add_new():
|
||||||
"?, ?",
|
"?, ?",
|
||||||
(tid, qid)
|
(tid, qid)
|
||||||
)
|
)
|
||||||
except sqlite3.Error:
|
except psycopg2.Error:
|
||||||
logger.warning("Database error while inserting into tagsToQuotes")
|
logger.warning("Database error while inserting into tagsToQuotes")
|
||||||
return render_template(
|
return render_template(
|
||||||
"message.html",
|
"message.html",
|
||||||
|
|
Loading…
Add table
Reference in a new issue