forked from importers/git-importer
Add basic postgresql setup
This commit is contained in:
parent
e53b030dcf
commit
74fd937732
21
config.py
Normal file
21
config.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
from configparser import ConfigParser
|
||||||
|
|
||||||
|
|
||||||
|
def config(filename="database.ini", section="postgresql"):
|
||||||
|
# create a parser
|
||||||
|
parser = ConfigParser()
|
||||||
|
# read config file
|
||||||
|
parser.read(filename)
|
||||||
|
|
||||||
|
# get section, default to postgresql
|
||||||
|
db = {}
|
||||||
|
if parser.has_section(section):
|
||||||
|
params = parser.items(section)
|
||||||
|
for param in params:
|
||||||
|
db[param[0]] = param[1]
|
||||||
|
else:
|
||||||
|
raise Exception(
|
||||||
|
"Section {0} not found in the {1} file".format(section, filename)
|
||||||
|
)
|
||||||
|
|
||||||
|
return db
|
4
database.ini
Normal file
4
database.ini
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# For local users you can simply rely on the UNIX permissions.
|
||||||
|
# For remote databases you have to add host, user and password
|
||||||
|
[postgresql]
|
||||||
|
database=imported_git
|
80
db.py
Normal file
80
db.py
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
import psycopg2
|
||||||
|
from config import config
|
||||||
|
|
||||||
|
|
||||||
|
class DB:
|
||||||
|
def __init__(self):
|
||||||
|
self.connect()
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
try:
|
||||||
|
# read the connection parameters
|
||||||
|
params = config()
|
||||||
|
# connect to the PostgreSQL server
|
||||||
|
self.conn = psycopg2.connect(**params)
|
||||||
|
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
print(error)
|
||||||
|
raise error
|
||||||
|
|
||||||
|
def schema_version(self):
|
||||||
|
# create a cursor
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
|
||||||
|
# execute a statement
|
||||||
|
try:
|
||||||
|
cur.execute("SELECT MAX(version) from scheme")
|
||||||
|
except psycopg2.errors.UndefinedTable as error:
|
||||||
|
cur.close()
|
||||||
|
self.close()
|
||||||
|
self.connect()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
db_version = cur.fetchone()
|
||||||
|
|
||||||
|
cur.close()
|
||||||
|
return db_version[0]
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.conn is not None:
|
||||||
|
self.conn.close()
|
||||||
|
self.conn = None
|
||||||
|
|
||||||
|
def create_tables(self):
|
||||||
|
"""Create the tables if not existing - assumes connected"""
|
||||||
|
|
||||||
|
commands = (
|
||||||
|
"""
|
||||||
|
CREATE TABLE scheme (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
version SMALLINT NOT NULL
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
"INSERT INTO scheme (version) VALUES(1)",
|
||||||
|
""" CREATE TABLE revisions (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
package VARCHAR(255) NOT NULL
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.schema_version() > 0:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
# create table one by one
|
||||||
|
for command in commands:
|
||||||
|
cur.execute(command)
|
||||||
|
# close communication with the PostgreSQL database server
|
||||||
|
cur.close()
|
||||||
|
# commit the changes
|
||||||
|
self.conn.commit()
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
print(error)
|
||||||
|
self.close()
|
||||||
|
raise error
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
db = DB()
|
||||||
|
db.create_tables()
|
Loading…
Reference in New Issue
Block a user