class SoftDeletesIntegrationTestCase(OratorTestCase): databases = { 'test': { 'driver': 'sqlite', 'database': ':memory:' } } def setUp(self): self.db = DatabaseManager(self.databases) Model.set_connection_resolver(self.db) self.create_schema() def create_schema(self): with self.schema().create('users') as table: table.increments('id') table.string('email').unique() table.timestamps() table.soft_deletes() with self.schema().create('posts') as table: table.increments('id') table.string('title') table.integer('user_id') table.timestamps() table.soft_deletes() with self.schema().create('comments') as table: table.increments('id') table.string('body') table.integer('post_id') table.timestamps() table.soft_deletes() def tearDown(self): self.schema().drop('users') self.schema().drop('posts') self.schema().drop('comments') Model.unset_connection_resolver() def test_soft_deletes_are_not_retrieved(self): self.create_users() users = SoftDeletesTestUser.all() self.assertEqual(1, len(users)) self.assertEqual(2, users.first().id) self.assertIsNone(SoftDeletesTestUser.find(1)) def test_soft_deletes_are_not_retrieved_from_base_query(self): self.create_users() query = SoftDeletesTestUser.query().to_base() self.assertIsInstance(query, QueryBuilder) self.assertEqual(1, len(query.get())) def test_soft_deletes_are_not_retrieved_from_builder_helpers(self): self.create_users() count = 0 query = SoftDeletesTestUser.query() for users in query.chunk(2): count += len(users) self.assertEqual(1, count) query = SoftDeletesTestUser.query() self.assertEqual(1, len(query.lists('email'))) Paginator.current_page_resolver(lambda: 1) query = SoftDeletesTestUser.query() self.assertEqual(1, len(query.paginate(2).items)) Paginator.current_page_resolver(lambda: 1) query = SoftDeletesTestUser.query() self.assertEqual(1, len(query.simple_paginate(2).items)) self.assertEqual(0, SoftDeletesTestUser.where('email', '*****@*****.**').increment('id')) self.assertEqual(0, SoftDeletesTestUser.where('email', '*****@*****.**').decrement('id')) def test_with_trashed_returns_all_records(self): self.create_users() self.assertEqual(2, SoftDeletesTestUser.with_trashed().get().count()) self.assertIsInstance(SoftDeletesTestUser.with_trashed().find(1), SoftDeletesTestUser) def test_delete_sets_deleted_column(self): self.create_users() self.assertIsNotNone(SoftDeletesTestUser.with_trashed().find(1).deleted_at) self.assertIsNone(SoftDeletesTestUser.find(2).deleted_at) def test_force_delete_actually_deletes_records(self): self.create_users() SoftDeletesTestUser.find(2).force_delete() users = SoftDeletesTestUser.with_trashed().get() self.assertEqual(1, len(users)) self.assertEqual(1, users.first().id) def test_restore_restores_records(self): self.create_users() john = SoftDeletesTestUser.with_trashed().find(1) self.assertTrue(john.trashed()) john.restore() users = SoftDeletesTestUser.all() self.assertEqual(2, len(users)) self.assertIsNone(SoftDeletesTestUser.find(1).deleted_at) self.assertIsNone(SoftDeletesTestUser.find(2).deleted_at) def test_only_trashed_only_returns_trashed_records(self): self.create_users() users = SoftDeletesTestUser.only_trashed().get() self.assertEqual(1, len(users)) self.assertEqual(1, users.first().id) def test_first_or_new_ignores_soft_deletes(self): self.create_users() john = SoftDeletesTestUser.first_or_new(id=1) self.assertEqual('*****@*****.**', john.email) def test_where_has_with_deleted_relationship(self): self.create_users() jane = SoftDeletesTestUser.where('email', '*****@*****.**').first() post = jane.posts().create(title='First Title') users = SoftDeletesTestUser.where('email', '*****@*****.**').has('posts').get() self.assertEqual(0, len(users)) users = SoftDeletesTestUser.where('email', '*****@*****.**').has('posts').get() self.assertEqual(1, len(users)) users = SoftDeletesTestUser.where('email', '*****@*****.**').or_has('posts').get() self.assertEqual(1, len(users)) users = SoftDeletesTestUser.where_has('posts', lambda q: q.where('title', 'First Title')).get() self.assertEqual(1, len(users)) users = SoftDeletesTestUser.where_has('posts', lambda q: q.where('title', 'Another Title')).get() self.assertEqual(0, len(users)) users = SoftDeletesTestUser.where('email', '*****@*****.**')\ .or_where_has('posts', lambda q: q.where('title', 'First Title'))\ .get() self.assertEqual(1, len(users)) # With post delete post.delete() users = SoftDeletesTestUser.has('posts').get() self.assertEqual(0, len(users)) def test_where_has_with_nested_deleted_relationship(self): self.create_users() jane = SoftDeletesTestUser.where('email', '*****@*****.**').first() post = jane.posts().create(title='First Title') comment = post.comments().create(body='Comment Body') comment.delete() users = SoftDeletesTestUser.has('posts.comments').get() self.assertEqual(0, len(users)) users = SoftDeletesTestUser.doesnt_have('posts.comments').get() self.assertEqual(1, len(users)) def test_or_where_with_soft_deletes_constraint(self): self.create_users() users = SoftDeletesTestUser.where('email', '*****@*****.**').or_where('email', '*****@*****.**') self.assertEqual(1, len(users.get())) self.assertEqual(['*****@*****.**'], users.order_by('id').lists('email')) def create_users(self): john = SoftDeletesTestUser.create(email='*****@*****.**') jane = SoftDeletesTestUser.create(email='*****@*****.**') john.delete() def connection(self): return self.db.connection() def schema(self): return self.connection().get_schema_builder()
from models.subreddit import Subreddit from orator import DatabaseManager from orator import Model from sys import exit from time import sleep from warnings import filterwarnings config = { 'sqlite': { 'driver': 'sqlite', 'database': 'catebot.db', 'prefix': '' } } db = DatabaseManager(config) Model.set_connection_resolver(db) # Ignores ResourceWarnings when using pickle files. May need to look into this later, but it seems to work fine. filterwarnings("ignore", category=ResourceWarning) # Ignores DeprecationWarnings caused by PRAW filterwarnings("ignore", category=DeprecationWarning) # Configure the logging system parser = argparse.ArgumentParser(description='Catebot service') parser.add_argument('-l','--log', help='Log level', required=False) parser.add_argument('-s','--sandbox', action='store_true', help='Use sandbox subreddit(s) only', required=False) args = vars(parser.parse_args()) if args['log']: logLevel = args['log'] else:
def __init__(self, host, port, user, password, database, trace=0): self._client = 'MariaDB' self._host = host self._port = port self._user = user self._password = password self._database = database self._api = None self._last_query = None # Debug messages self._trace = trace # This is to avoid problems that mysql has with localhost if host == 'localhost': self._host = '127.0.0.1' # Create connection with pony orm python API config = { 'mysql': { 'driver': 'mysql', 'host': host, 'database': database, 'user': user, 'password': password, 'port': port, 'prefix': '' } } self._api = DatabaseManager(config) result = self._api.select('SHOW TABLES') if not list(result): raise DBConnectionFailed( f'Connection to MariaDB failed. Check connection parameters')
def __init__(self): self.configuration = {'mysql':{'driver':'mysql','host': 'localhost','database': 'atm3',\ 'user': '******','password': '', 'prefix': '' } } self.db = DatabaseManager(self.configuration) self.customer = self.db.table('customers') self.account = self.db.table('accounts') self.transaction = self.db.table('transactions')
def _connect_to_sqlite(self, db): sqlite_db_path = db.sqlite_file if not os.path.exists(sqlite_db_path): raise Exception( "Sqlite database not found at '%s', indicating that either the SingleM database was built with an out-dated SingleM version, or that the database is corrupt. Please generate a new database with the current version of SingleM." ) logging.debug("Connecting to %s" % sqlite_db_path) dbm = DatabaseManager( {'sqlite3': { 'driver': 'sqlite', 'database': sqlite_db_path }}) Model.set_connection_resolver(dbm) try: len(dbm.table('otus').limit(1).get()) except Exception as e: logging.error( "Failure to extract any data from the otus table of the SQ Lite DB indicates this SingleM DB is either too old or is corrupt." ) raise (e) try: len(dbm.table('clusters').limit(1).get()) except QueryException: logging.error( "Failure to extract any data from the 'clusters' table indicates this SingleM DB is out-dated, and cannot be used with query implemented in this version of SingleM" ) sys.exit(1) return dbm
class WriterWorker(Thread): def __init__(self, task, worker_control): Thread.__init__(self) self.task = task self.worker_control = worker_control self.db = DatabaseManager(DATABASES) def update_task_status(self, params): print("- Updating processing_status for task id %s with processing_status of %s into the database" % (params['id'], params['processing_status'])) self.db.table("tasks").where("id", params['id']).update({"ipfs_hash": params["ipfs_hash"], "processing_status": params['processing_status']}) print("-- Done updating the task-id %s into the database" % params['id']) def run(self): print("Writer worker online for task-id: %s" % self.task["id"]) try: self.update_task_status(self.task) except Exception as err: print(err) self.worker_control.release()
def dump(db_path): """Dump the DB contents to STDOUT, requiring only that the DB is a version that has an otus table in sqlite3 form (i.e. version 2 and 3 at least). """ sqlite_db = os.path.join(db_path, SequenceDatabase.SQLITE_DB_NAME) logging.debug("Connecting to DB {}".format(sqlite_db)) if not os.path.exists(sqlite_db): raise Exception( "SQLite3 database does not appear to exist in the SingleM database - perhaps it is the wrong version?" ) db = DatabaseManager( {'sqlite3': { 'driver': 'sqlite', 'database': sqlite_db }}) Model.set_connection_resolver(db) print "\t".join(OtuTable.DEFAULT_OUTPUT_FIELDS) for chunk in db.table('otus').chunk(1000): for entry in chunk: otu = OtuTableEntry() otu.marker = entry.marker otu.sample_name = entry.sample_name otu.sequence = entry.sequence otu.count = entry.num_hits otu.coverage = entry.coverage otu.taxonomy = entry.taxonomy print str(otu)
def dump(db_path): """Dump the DB contents to STDOUT, requiring only that the DB is a version that has an otus table in sqlite3 form (i.e. version 2 and 3 at least). """ sqlite_db = os.path.join(db_path, SequenceDatabase.SQLITE_DB_NAME) logging.debug("Connecting to DB {}".format(sqlite_db)) if not os.path.exists(sqlite_db): raise Exception("SQLite3 database does not appear to exist in the SingleM database - perhaps it is the wrong version?") db = DatabaseManager({ 'sqlite3': { 'driver': 'sqlite', 'database': sqlite_db }}) Model.set_connection_resolver(db) print "\t".join(OtuTable.DEFAULT_OUTPUT_FIELDS) for chunk in db.table('otus').chunk(1000): for entry in chunk: otu = OtuTableEntry() otu.marker = entry.marker otu.sample_name = entry.sample_name otu.sequence = entry.sequence otu.count = entry.num_hits otu.coverage = entry.coverage otu.taxonomy = entry.taxonomy print str(otu)
def load_data(): try: db = DatabaseManager(app.config.get('DB_CONFIG')).connection() if not db.table('greetings').get(['message']).first(): db.table('greetings').insert({'message': 'Hello world!'}) except Exception as e: app.logger.error('Unable to connect to db: %s' % e)
def index(): ts = time() st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') db = DatabaseManager(app.config.get('DB_CONFIG')).connection() res = db.table('greetings').get(['message']).first() return render_template('index.html', message=res.get('message'), time=st, image_to_show=app.config.get('IMAGE_DEST_PATH'))
def connect_to_db(self): config = { 'default': { 'driver': 'sqlite', 'database': self.db_path, 'log_queries': ENABLE_QUERY_LOGGING } } self.db = DatabaseManager(config) Model.set_connection_resolver(self.db)
def get_connection_resolver(cls): # Adding another connection to test connection switching config = cls.get_manager_config() config['test'] = {'driver': 'sqlite', 'database': ':memory:'} db = DatabaseManager(config) db.connection().enable_query_log() return db
def get_connection_resolver(cls): # Adding another connection to test connection switching config = cls.get_manager_config() config["test"] = {"driver": "sqlite", "database": ":memory:"} db = DatabaseManager(config) db.connection().enable_query_log() return db
def get_or_create_file(self, db: orator.DatabaseManager, filepath: Union[str, pathlib.Path], metadata: Union[str, dict, None] = None) -> dict: # enforce types checks.check_types(db, orator.DatabaseManager) checks.check_types(filepath, [str, pathlib.Path]) checks.check_types(metadata, [str, dict, type(None)]) # convert types filepath = pathlib.Path(filepath) if isinstance(metadata, dict): metadata = str(dict) # check file exists checks.check_file_exists(filepath) # check exists md5 = tools.get_file_hash(filepath) sha256 = tools.get_file_hash(filepath, hashlib.sha256) file_info = self.get_file(db=db, md5=md5) # return if found if file_info is not None: return file_info name = "fms_" + md5 # create if not with tools.suppress_prints(): self._build_file_as_package(filepath, name) # import string read_pkg = importlib.import_module(name="quilt.data." + self.storage_user + "." + name) # file info dict file_info = { "FileId": str(uuid.uuid4()), "OriginalFilepath": str(filepath), "FileType": filepath.suffix[1:], "ReadPath": read_pkg.load(), "MD5": md5, "SHA256": sha256, "Metadata": metadata, "Created": datetime.utcnow() } # insert db.table("File").insert(file_info) return file_info
def Alquilar(self): self.__log.info("Alquilar un libro") estadolibro = EstadoLibro() print(f"\t ID\t Descripcion") for obj in estadolibro.all(): print(f"\t {obj.id}\t {obj.descripcion}") if obj.descripcion == 1: libroporalquilar = Prestamo() cliente = User() print(f"\t ID\t Nombre\t CORREO\t DOCUMENTO\t ESTADO") for obj in cliente.all(): print(f"\t {obj.id}\t {obj.nombre}\t {obj.correo}\t {obj.documento}\t {obj.estado_user_id}") print("Escriba el id del cliente") cliente = input() libro = Libro() print(f"\t ID\t Nombre\t ISBN\t Autor\t Editorial\t Estado") for obj in libro.all(): print(f"\t {obj.id}\t {obj.nombre}\t {obj.isbn}\t {obj.autors_id}\t {obj.editorial_id}\t {obj.estado_libro_id}") print("Escriba el id del libro") libro = input() DatabaseManager.table('libro').where('nombre', f'{obj.nombre}').update({'obj.estado_libro_id': 3}) biblioteca = Biblioteca() print(f"\t ID\t Nombre\t DIRECCION") for obj in biblioteca.all(): print(f"\t {obj.id}\t {obj.nombre}\t {obj.direccion}") print("Escriba el id de la biblioteca") biblioteca = input() libroporalquilar.users_id = cliente libroporalquilar.libros_id = libro libroporalquilar.bibliotecas_id = biblioteca libroporalquilar.save() objMenus = init.Inicio() objMenus.MenuInicioMenu() elif obj.descripcion == 2: print("Lo sentimos el libro que busca esta reservado") objMenus = init.Inicio() objMenus.MenuInicioMenu() elif obj.descripcion == 2: print("Lo sentimos el libro que busca no se encuentra disponible") objMenus = init.Inicio() objMenus.MenuInicioMenu()
def on_get(self, req, resp): resp.set_header('Access-Control-Allow-Origin', '*') resp.set_header('Access-Control-Allow-Methods', 'GET') resp.set_header('Access-Control-Allow-Headers', 'Content-Type') # reading config config = configparser.ConfigParser() config.readfp(open('config.ini')) dbhost = config.get('database', 'host') dbdatabase = config.get('database', 'database') dbuser = config.get('database', 'user') dbpassword = config.get('database', 'password') prefix = config.get('database', 'prefix') wkhtmltopdf_path = config.get('wkhtmltopdf', 'path') baseurl = config.get('frontend', 'baseurl') #Setup DB config = { 'mysql': { 'driver': 'mysql', 'host': dbhost, 'database': dbdatabase, 'user': dbuser, 'password': dbpassword, 'prefix': '' } } db = DatabaseManager(config) Model.set_connection_resolver(db) try: key = req.params['key'] data = {"status": "success", "data": {}} except: resp.status = falcon.HTTP_404 return try: val = db.table(prefix + 'files').where('file_key', key).first() except: esp.status = falcon.HTTP_503 return try: resp.set_header("Content-Disposition", "attachment; filename=" + key + ".pdf") resp.content_type = "application/pdf" resp.data = val['file'] resp.status = falcon.HTTP_200 except: resp.status = falcon.HTTP_404
def RegistroEditorial(self): menuRegisEdit = True __log = log("Ingresando al Registro de las editoriales") self.__log.info("Ingresando al Registro de las editoriales") opcionesRegisEditorial = { "Registrar Editorial": 1, "Listar Editoriales": 2, "Eliminar Editoriales": 3 } MenuRegisEditorial = Menu("Menu Modulo Libros", opcionesRegisEditorial) regisEditoriales = MenuRegisEditorial.mostrarMenu() menuRegisEdit = True while menuRegisEdit: if (regisEditoriales == 1): nuevaEditorial = Editorial() nombreEditorial = input("escriba el nombre de la editorial \n") nuevaEditorial.nombre = nombreEditorial nuevaEditorial.save() menuRegisEdit = False elif (regisEditoriales == 2): editorial = Editorial() print(f"\t ID\t NOMBRE") for obj in editorial.all(): print(f"\t {obj.id}\t {obj.nombre}") input("Regresar???") program.utils.Menu("Menu Modulo Editoriales", regisEditoriales) elif (regisEditoriales == 3): editorial = Editorial() print(f"\t ID\t NOMBRE") for obj in editorial.all(): print(f"\t {obj.id}\t {obj.nombre}") print( "Escriba el id de la editorial que se desea eliminar: \n") id = input() DatabaseManager.table('editorial').get() DatabaseManager.table('editorial').where('id', '=', f'{id}').delete() elif (regisEditoriales == 9): __log = log("Saliendo") break
class Loader(Process): def __init__(self): super(Loader, self).__init__() self.db = DatabaseManager(DATABASES) def run(self): print("*** Loader online") images = get_all_images() ##get all images in list and put that images in the database and set to new PENDING if images: for image in images: print("Inserting image file-path %s into the database" %image) self.db.table("tasks").insert({'image_path': image})
def test_get_indices_description_from_object(): database = "test.db" create_database(database) config = {'sqlite3': {'driver': 'sqlite', 'database': database}} db = DatabaseManager(config) result = _get_indices_description_from_oject(db.get_schema_manager(), 'tasks') assert result == { 'primary': { 'is_unique?': True, 'is_primary?': True, 'columns': ['id'] } } drop_database(database)
def getTweets(self): config = { 'mysql': { 'driver': 'mysql', 'host': self.config['database']['host'], 'database': self.config['database']['database'], 'user': self.config['database']['user'], 'password': self.config['database']['password'], 'prefix': '' } } self.db = DatabaseManager(config) tweets = self.db.table(self.config['database']['table']).where(self.config['database']['retweetfield'], '=', '0').lists(self.config['database']['textfield']) return tweets
class MainOperate: def __init__(self): self.db = DatabaseManager(config=CONFIG).connection() def one_mysql_link(self): """ 根据offset limit 查询数据 :return: """ result = self.db.table("corpus").offset(2).limit(5).get() # 对于多条件查询 db_data = self.db.table("corpus").where('type', 'all').where_between( 'age', [15, 25]).get() for num, data in enumerate(result): # 根据字段名获取每一条信息 logging.info(data.get("url")) return result def two_add_column(self): """ 给已存在的数据表增加一列数据 :return: """ new_data = { "title": "测试标题", "url": "https://www.baidu.com", "content": "测试正文", "published": "2019-11-02 12:16:01", } result = self.db.table("corpus").insert(new_data) logging.info(result) def three_update_data(self): """ 更新数据库操作 :return: """ corpus = self.db.table("corpus").where('id', '1').update( {'published': '2019-12-03 09:25:01'}) logging.info(corpus) def four_delete_data(self): """ 根据id来删除一行数据 :return: """ delete_data = self.db.table("corpus").delete('1899') logging.info(delete_data)
def __init__(self): """ Initializes a database instance and binds the models to it. Arguement(s) - self """ # Bind Models to local variables self.City = city self.Country = country self.CountryInfo = countryinfo self.CountryLanguage = countrylanguage # Set config self.config = { 'mysql': { 'driver': 'mysql', 'host': os.getenv('DB_HOST'), 'database': os.getenv('DB_NAME'), 'user': os.getenv('DB_USER'), 'password': os.getenv('DB_PASSWORD'), 'prefix': '' } } # Create database from config self.db = DatabaseManager(self.config) # Auto-resolve connection Model.set_connection_resolver(self.db)
def setup_database(): """ enable database """ from orator import DatabaseManager, Model db = DatabaseManager(config.DATABASES) Model.set_connection_resolver(db)
def connect_db_orator(env): config = { 'default': 'read', 'read': { 'host': env("POSTGRE_HOST_READ"), 'driver': 'pgsql', 'database': env("POSTGRE_DATABASE"), 'user': env("POSTGRE_USERNAME"), 'password': env("POSTGRE_PASSWORD"), 'prefix': '', 'port': env("POSTGRE_PORT") }, 'write': { 'host': env("POSTGRE_HOST_READ"), 'driver': 'pgsql', 'database': env("POSTGRE_DATABASE"), 'user': env("POSTGRE_USERNAME"), 'password': env("POSTGRE_PASSWORD"), 'prefix': '', 'port': env("POSTGRE_PORT") } } db = DatabaseManager(config) Model.set_connection_resolver(db) return db
def Devolver(self): self.__log.info("Devolver un libro") libro = Libro() print(f"\t ID\t Nombre\t ISBN\t Autor\t Editorial\t Estado") for obj in libro.all(): print(f"\t {obj.id}\t {obj.nombre}\t {obj.isbn}\t {obj.autors_id}\t {obj.editorial_id}\t {obj.estado_libro_id}") print("Escriba el id del libro") idlibro = input() DatabaseManager.table('libro').where('nombre', f'{obj.nombre}').update({'obj.estado_libro_id': 1}) libro.libros_id = idlibro libro.save() objMenus = init.Inicio() objMenus.MenuInicioMenu()
class Helper: host = os.environ.get('ET_EDM_MYSQL_HOST', '127.0.0.1') user = os.environ.get('DATABASE_USER', 'root') password = os.environ.get('DATABASE_PASSWORD', '') database = os.environ.get('DATABASE_NAME', 'mysql') port = int(os.environ.get('MYSQL_3306_TCP', 3306)) config = { 'mysql': { 'driver': 'mysql', 'prefix': '', 'host': host, 'database': database, 'user': user, 'password': password, 'port': port } } db = DatabaseManager(config) schema = Schema(db) @staticmethod def to_blob(model) -> str: return json.dumps(model) @staticmethod def from_blob(blob) -> dict: return dict(json.loads(blob))
def __init__(self): """ Initializes a database instance and binds the models to it. Arguement(s) - self """ # Set config self.config = { 'mysql': { 'driver': 'mysql', 'host': os.getenv('DB_HOST'), 'database': os.getenv('DB_NAME'), 'user': os.getenv('DB_USER'), 'password': os.getenv('DB_PASSWORD'), 'prefix': '' } } # Bind Models to local variables self.Posts = posts self.Drafts = drafts self.Users = users self.Notes = notes # Create database from config self.db = DatabaseManager(self.config) # Auto-resolve connection Model.set_connection_resolver(self.db)
def reconstruct(db: orator.DatabaseManager, ds_info: "DatasetInfo", fms: FMSInterface) -> pd.DataFrame: # get all iota that match data = [ dict(r) for r in db.table("Iota"). join("IotaGroup", "IotaGroup.IotaId", "=", "Iota.IotaId").join( "GroupDataset", "GroupDataset.GroupId", "=", "IotaGroup.GroupId" ).join("Dataset", "GroupDataset.DatasetId", "=", "Dataset.DatasetId" ).where("Dataset.DatasetId", "=", ds_info.id).get() ] # create dictionary of iota with key being their group label groups = {} for iota in data: label = int(iota["Label"]) if label not in groups: groups[label] = {} groups[label][iota["Key"]] = pickle.loads(iota["Value"]) # we know that dataframe labels are actually just their index value # so we can append these rows in order by simply looping through a range of their length and getting each one rows = [] for i in range(len(groups)): rows.append(groups[i]) # return frame return pd.DataFrame(rows)
def __init__(self, task, worker_control, queue): Thread.__init__(self) self.task = task self.worker_control = worker_control self.db = DatabaseManager(DATABASES) self.ipfs_s3_auth = (USERNAME, PASSWORD) self.result_queue = queue
def db_health(): try: DatabaseManager( app.config.get('DB_CONFIG')).connection().get_connection() return 'Ok' except Exception as e: return 'Failure %s' % e
def test_rollback_migration_can_be_pretended(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock(Connection(None)) connection.should_receive('pretend').replace_with( lambda callback: callback(None)) resolver.should_receive('connection').with_args(None).and_return( connection) migrator = flexmock( Migrator( flexmock(DatabaseMigrationRepository(resolver, 'migrations')), resolver)) foo_migration = MigrationStub('foo') bar_migration = MigrationStub('bar') migrator.get_repository().should_receive('get_last').once().and_return( [foo_migration, bar_migration]) bar_mock = flexmock(MigrationStub()) bar_mock.should_receive('down').once() foo_mock = flexmock(MigrationStub()) foo_mock.should_receive('down').once() migrator.should_receive('_resolve').with_args( os.getcwd(), 'bar').once().and_return(bar_mock) migrator.should_receive('_resolve').with_args( os.getcwd(), 'foo').once().and_return(foo_mock) migrator.rollback(os.getcwd(), True)
def test_last_batch_of_migrations_can_be_rolled_back(self): resolver_mock = flexmock(DatabaseManager) resolver_mock.should_receive('connection').and_return({}) resolver = flexmock(DatabaseManager({})) connection = flexmock() connection.should_receive('transaction').twice().and_return(connection) resolver.should_receive('connection').and_return(connection) migrator = flexmock( Migrator( flexmock(DatabaseMigrationRepository(resolver, 'migrations')), resolver)) foo_migration = MigrationStub('foo') bar_migration = MigrationStub('bar') migrator.get_repository().should_receive('get_last').once().and_return( [foo_migration, bar_migration]) bar_mock = flexmock(MigrationStub()) bar_mock.set_connection(connection) bar_mock.should_receive('down').once() foo_mock = flexmock(MigrationStub()) foo_mock.set_connection(connection) foo_mock.should_receive('down').once() migrator.should_receive('_resolve').with_args( os.getcwd(), 'bar').once().and_return(bar_mock) migrator.should_receive('_resolve').with_args( os.getcwd(), 'foo').once().and_return(foo_mock) migrator.get_repository().should_receive('delete').once().with_args( bar_migration) migrator.get_repository().should_receive('delete').once().with_args( foo_migration) migrator.rollback(os.getcwd())
def setup(self): database_manager = DatabaseManager(TEST_DB_CONFIG) Model.set_connection_resolver(database_manager) migrate_in_memory(database_manager) self.repository = OratorEmployeeRepository()
class Orator(object): def __init__(self, app=None): self.Model = BaseModel self.cli = None self._db = None if app is not None: self.init_app(app) def init_app(self, app): if 'ORATOR_DATABASES' not in app.config: raise RuntimeError('Missing "ORATOR_DATABASES" configuration') # Register request hooks self.register_handlers(app) # Getting config databases self._config = app.config['ORATOR_DATABASES'] # Initializing database manager self._db = DatabaseManager(self._config) self.Model.set_connection_resolver(self._db) # Setting current page resolver def current_page_resolver(): return int(request.args.get('page', 1)) Paginator.current_page_resolver(current_page_resolver) # Setting commands self.init_commands() def init_commands(self): self.cli = Application(orator_application.get_name(), orator_application.get_version()) self.cli.add(InstallCommand(self)) self.cli.add(MigrateCommand(self)) self.cli.add(MigrateMakeCommand(self)) self.cli.add(RollbackCommand(self)) self.cli.add(StatusCommand(self)) self.cli.add(ResetCommand(self)) def register_handlers(self, app): teardown = app.teardown_appcontext @teardown def disconnect(_): return self._db.disconnect() def __getattr__(self, item): return getattr(self._db, item)
def _connect_to_sqlite(self, db): sqlite_db_path = db.sqlite_file if not os.path.exists(sqlite_db_path): raise Exception("Sqlite database not found at '%s', indicating that either the SingleM database was built with an out-dated SingleM version, or that the database is corrupt. Please generate a new database with the current version of SingleM.") logging.debug("Connecting to %s" % sqlite_db_path) dbm = DatabaseManager({ 'sqlite3': { 'driver': 'sqlite', 'database': sqlite_db_path }}) Model.set_connection_resolver(dbm) try: len(dbm.table('otus').limit(1).get()) except Exception as e: logging.error("Failure to extract any data from the otus table of the SQ Lite DB indicates this SingleM DB is either too old or is corrupt.") raise(e) try: len(dbm.table('clusters').limit(1).get()) except QueryException: logging.error("Failure to extract any data from the 'clusters' table indicates this SingleM DB is out-dated, and cannot be used with query implemented in this version of SingleM") sys.exit(1) return dbm
print("Waiting for QR") wppDriver.wait_for_login() print("Bot started") wppDriver.save_firefox_profile() config = { 'postgres': { 'driver': 'postgres', 'host': '35.247.235.153', 'database': 'tbc_wpp', 'user': '******', 'password': '******', } } db = DatabaseManager(config) Model.set_connection_resolver(db) class Driver(Model): __table__ = 'drivers' pass print("Buscando motoristas...") contacts = wppDriver.get_contacts() print(str(len(contacts)) + " Contatos") drivers = Driver.where_raw('options_stage = 0').get() phones_that_received = map(lambda x: x.phone, drivers) new_contacts = [a for a in contacts if a.id not in phones_that_received]
import time from models.comment import Comment from orator import DatabaseManager from orator import Model from sys import exit config = { 'sqlite': { 'driver': 'sqlite', 'database': 'catebot.db', 'prefix': '' } } db = DatabaseManager(config) Model.set_connection_resolver(db) parser = argparse.ArgumentParser(description='Update new database from old') parser.add_argument('database', help='Database file') args = vars(parser.parse_args()) # Connects to a sqlite database used to store comment ids. print('Connecting to database...') try: connection = sqlite3.connect(args['database']) connection.row_factory = sqlite3.Row cursor = connection.cursor() print('Connected to database!') except: print('Connection to database failed.',sys.exc_info()[0])
def setUp(self): self.db = DatabaseManager(self.databases) Model.set_connection_resolver(self.db) self.create_schema()
from orator import Model, SoftDeletes from orator import DatabaseManager from orator.migrations import Migration import logging logging.basicConfig(level=logging.DEBUG) class User(SoftDeletes, Model): __fillable__ = ["name", "email"] class CreateTableUsers(Migration): def up(self): pass def down(self): pass # https://orator-orm.com/docs/0.9/basic_usage.html#query-logging config = {"sqlite3": {"driver": "sqlite", "database": ":memory:", "log_queries": True}} db = DatabaseManager(config) Model.set_connection_resolver(db) with db.transaction(): User.create(name="John")
from orator import DatabaseManager import logging logging.basicConfig(level=logging.DEBUG) # https://orator-orm.com/docs/0.9/query_builder.html # https://orator-orm.com/docs/0.9/basic_usage.html#query-logging config = { "sqlite3": {"driver": "sqlite", "database": "examples.db", "log_queries": True} } db = DatabaseManager(config) with db.transaction(): user = db.table("users").first() print(user) with db.transaction(): user = db.table("users").select("id", "name").first() print(user) print("----------------------------------------") for users in db.table("users").select("id", "name").chunk(100): for user in users: print(user)
from orator import DatabaseManager config = { 'mysql': { 'read': [{ 'host': 'localhost', 'database': 'test1', 'user': '******', 'password': '******' }], 'write': [{ 'host': 'localhost', 'database': 'test', 'user': '******', 'password': '******', }], 'driver': 'mysql', 'prefix': '' } } db = DatabaseManager(config) results = db.select('select * from user') print results
def init_app(self, app): if 'ORATOR_DATABASES' not in app.config: raise RuntimeError('Missing "ORATOR_DATABASES" configuration') # Register request hooks self.register_handlers(app) # Getting config databases self._config = app.config['ORATOR_DATABASES'] # Initializing database manager self._db = DatabaseManager(self._config) self.Model.set_connection_resolver(self._db) # Setting current page resolver def current_page_resolver(): return int(request.args.get('page', 1)) Paginator.current_page_resolver(current_page_resolver) # Setting commands self.init_commands()