2015-12-01 22:04:13 +00:00
|
|
|
from ogn.commands.dbutils import engine, session
|
2016-10-31 12:47:27 +00:00
|
|
|
from ogn.model import Base, AddressOrigin, AircraftBeacon, ReceiverBeacon, Device, Receiver
|
2017-06-12 17:25:24 +00:00
|
|
|
from ogn.utils import get_airports, open_file
|
2016-06-21 17:34:05 +00:00
|
|
|
from ogn.collect.database import update_device_infos
|
2015-11-15 08:10:46 +00:00
|
|
|
|
2016-10-31 12:47:27 +00:00
|
|
|
from sqlalchemy import insert, distinct
|
|
|
|
from sqlalchemy.sql import null
|
|
|
|
|
2015-11-15 08:10:46 +00:00
|
|
|
from manager import Manager
|
|
|
|
manager = Manager()
|
|
|
|
|
2016-01-31 01:25:21 +00:00
|
|
|
ALEMBIC_CONFIG_FILE = "alembic.ini"
|
|
|
|
|
2015-11-15 18:31:58 +00:00
|
|
|
|
2015-11-15 08:10:46 +00:00
|
|
|
@manager.command
|
|
|
|
def init():
|
|
|
|
"""Initialize the database."""
|
2015-12-09 02:37:25 +00:00
|
|
|
|
2016-01-29 01:38:55 +00:00
|
|
|
from alembic.config import Config
|
|
|
|
from alembic import command
|
|
|
|
|
2016-06-05 06:33:13 +00:00
|
|
|
session.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
|
|
|
|
session.commit()
|
2015-11-15 08:10:46 +00:00
|
|
|
Base.metadata.create_all(engine)
|
2016-01-31 01:25:21 +00:00
|
|
|
alembic_cfg = Config(ALEMBIC_CONFIG_FILE)
|
2016-01-12 17:35:33 +00:00
|
|
|
command.stamp(alembic_cfg, "head")
|
2015-11-15 08:10:46 +00:00
|
|
|
print("Done.")
|
2015-11-15 08:23:57 +00:00
|
|
|
|
|
|
|
|
2016-01-31 01:25:21 +00:00
|
|
|
@manager.command
|
|
|
|
def upgrade():
|
|
|
|
"""Upgrade database to the latest version."""
|
|
|
|
|
|
|
|
from alembic.config import Config
|
|
|
|
from alembic import command
|
|
|
|
|
|
|
|
alembic_cfg = Config(ALEMBIC_CONFIG_FILE)
|
|
|
|
command.upgrade(alembic_cfg, 'head')
|
|
|
|
|
|
|
|
|
2016-01-12 17:36:08 +00:00
|
|
|
@manager.command
|
|
|
|
def drop(sure='n'):
|
|
|
|
"""Drop all tables."""
|
|
|
|
if sure == 'y':
|
|
|
|
Base.metadata.drop_all(engine)
|
|
|
|
print('Dropped all tables.')
|
|
|
|
else:
|
|
|
|
print("Add argument '--sure y' to drop all tables.")
|
|
|
|
|
|
|
|
|
2015-11-15 08:23:57 +00:00
|
|
|
@manager.command
|
2015-12-09 02:41:58 +00:00
|
|
|
def import_ddb():
|
|
|
|
"""Import registered devices from the DDB."""
|
2015-11-24 07:20:28 +00:00
|
|
|
|
2015-12-09 02:41:58 +00:00
|
|
|
print("Import registered devices fom the DDB...")
|
2016-06-21 17:34:05 +00:00
|
|
|
address_origin = AddressOrigin.ogn_ddb
|
2016-07-17 20:57:54 +00:00
|
|
|
counter = update_device_infos(session,
|
|
|
|
address_origin)
|
2015-11-15 11:10:20 +00:00
|
|
|
print("Imported %i devices." % counter)
|
2015-12-01 22:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@manager.command
|
2015-12-09 02:41:58 +00:00
|
|
|
def import_file(path='tests/custom_ddb.txt'):
|
|
|
|
"""Import registered devices from a local file."""
|
|
|
|
# (flushes previously manually imported entries)
|
2015-12-01 22:04:13 +00:00
|
|
|
|
2015-12-09 02:41:58 +00:00
|
|
|
print("Import registered devices from '{}'...".format(path))
|
2016-06-21 17:34:05 +00:00
|
|
|
address_origin = AddressOrigin.user_defined
|
2016-07-17 20:57:54 +00:00
|
|
|
counter = update_device_infos(session,
|
|
|
|
address_origin,
|
|
|
|
csvfile=path)
|
2015-12-09 02:41:58 +00:00
|
|
|
print("Imported %i devices." % counter)
|
2016-04-22 08:44:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@manager.command
|
2016-05-22 05:27:21 +00:00
|
|
|
def import_airports(path='tests/SeeYou.cup'):
|
2016-04-22 08:44:39 +00:00
|
|
|
"""Import airports from a ".cup" file"""
|
|
|
|
|
|
|
|
print("Import airports from '{}'...".format(path))
|
|
|
|
airports = get_airports(path)
|
|
|
|
session.bulk_save_objects(airports)
|
|
|
|
session.commit()
|
|
|
|
print("Imported {} airports.".format(len(airports)))
|
2016-10-31 12:47:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@manager.command
|
|
|
|
def update_relations():
|
|
|
|
"""Update AircraftBeacon and ReceiverBeacon relations"""
|
|
|
|
|
|
|
|
# Create missing Receiver from ReceiverBeacon
|
|
|
|
available_receivers = session.query(Receiver.name) \
|
|
|
|
.subquery()
|
|
|
|
|
|
|
|
missing_receiver_query = session.query(distinct(ReceiverBeacon.name)) \
|
|
|
|
.filter(ReceiverBeacon.receiver_id == null()) \
|
|
|
|
.filter(~ReceiverBeacon.name.in_(available_receivers))
|
|
|
|
|
|
|
|
ins = insert(Receiver).from_select([Receiver.name], missing_receiver_query)
|
|
|
|
session.execute(ins)
|
|
|
|
|
|
|
|
# Create missing Device from AircraftBeacon
|
|
|
|
available_addresses = session.query(Device.address) \
|
|
|
|
.subquery()
|
|
|
|
|
|
|
|
missing_addresses_query = session.query(distinct(AircraftBeacon.address)) \
|
|
|
|
.filter(AircraftBeacon.device_id == null()) \
|
|
|
|
.filter(~AircraftBeacon.address.in_(available_addresses))
|
|
|
|
|
|
|
|
ins2 = insert(Device).from_select([Device.address], missing_addresses_query)
|
|
|
|
session.execute(ins2)
|
2017-06-03 09:38:55 +00:00
|
|
|
session.commit()
|
|
|
|
print("Inserted {} Receivers and {} Devices".format(ins, ins2))
|
|
|
|
return
|
2016-10-31 12:47:27 +00:00
|
|
|
|
|
|
|
# Update AircraftBeacons
|
|
|
|
upd = session.query(AircraftBeacon) \
|
|
|
|
.filter(AircraftBeacon.device_id == null()) \
|
|
|
|
.filter(AircraftBeacon.receiver_id == null()) \
|
|
|
|
.filter(AircraftBeacon.address == Device.address) \
|
|
|
|
.filter(AircraftBeacon.receiver_name == Receiver.name) \
|
|
|
|
.update({AircraftBeacon.device_id: Device.id,
|
|
|
|
AircraftBeacon.receiver_id: Receiver.id},
|
|
|
|
synchronize_session='fetch')
|
|
|
|
|
|
|
|
upd2 = session.query(ReceiverBeacon) \
|
|
|
|
.filter(ReceiverBeacon.receiver_id == null()) \
|
|
|
|
.filter(ReceiverBeacon.receiver_name == Receiver.name) \
|
|
|
|
.update({Receiver.name: ReceiverBeacon.receiver_name},
|
|
|
|
synchronize_session='fetch')
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
print("Updated {} AircraftBeacons and {} ReceiverBeacons".
|
|
|
|
format(upd, upd2))
|