### Install Results Library Source: https://github.com/djrobstep/results/blob/main/README.md Install the 'results' library using pip. This command also installs the 'results' command-line tool. No additional packages are needed for database diffing. ```bash pip install results ``` -------------------------------- ### Results DBDiff CLI Help Source: https://github.com/djrobstep/results/blob/main/README.md Display the help message for the 'results dbdiff' command, showing available options for schema diffing and migration generation. ```bash Options: --schema TEXT Restrict output to single schema --exclude-schema TEXT Restrict output to statements for all schemas except the specified schema --create-extensions-only Only output "create extension..." statements, nothing else --ignore-extension-versions Ignore the versions when comparing extensions --with-privileges Also output privilege differences (ie. grant/revoke statements) --help Show this message and exit. ``` -------------------------------- ### Create Database Object Source: https://github.com/djrobstep/results/blob/main/README.md Instantiate a database object using either a database name or a full connection URL. This is the first step to interacting with the database using the 'results' library. ```python import results db = results.db('example') db = results.db('postgres:///example') ``` -------------------------------- ### Execute Basic Query Source: https://github.com/djrobstep/results/blob/main/README.md Perform a basic query on the database object. Parameterized queries are supported using dictionary-style placeholders. ```python result = db.q('select :x', dict(x=5)) ``` -------------------------------- ### Listen for Database Notifications Source: https://github.com/djrobstep/results/blob/main/README.md Establish a listening connection to receive database notifications on specified channels. The code iterates through incoming notifications. ```python with db.listening_connection(["hello", "hello2"]) as listen_conn: for n in db.notifications( listen_conn ): print(n) ``` -------------------------------- ### Paginate Query Results Source: https://github.com/djrobstep/results/blob/main/README.md Fetch results in pages using the 'paging' parameter. This supports offset-free pagination by using a bookmark for subsequent requests. The loop continues until no more pages are available. ```python bookmark = None while True: page = db.q( "select * from names", paging=dict(order_by="name desc, id", per_page=2, bookmark=bookmark), ) print(page) if not page.paging.has_next: break bookmark = page.paging.next ``` -------------------------------- ### Generate Database Diff using CLI Source: https://github.com/djrobstep/results/blob/main/README.md Use the 'results dbdiff' command to generate a schema diff between two PostgreSQL databases. This command replaces the older 'migra' command. Local database names can be used instead of full connection URLs. ```bash results dbdiff postgresql:///db_a postgresql:///db_b ``` -------------------------------- ### Manage Database Transactions Source: https://github.com/djrobstep/results/blob/main/README.md Execute database operations within a transaction context. The 'with db.t() as t:' syntax ensures proper transaction handling. ```python with db.t() as t: t.q('select 1') ``` -------------------------------- ### Generate Schema Diff SQL Source: https://github.com/djrobstep/results/blob/main/README.md Generate the SQL script required to transform the schema of one database into the schema of another. This function handles structural differences only. ```python from results import db diff = db('a').schemadiff_as_sql(db('b')) print(diff) ``` -------------------------------- ### Insert Data with Upsert Option Source: https://github.com/djrobstep/results/blob/main/README.md Insert a record into a table. The 'upsert_on' parameter allows specifying a column to use for conflict resolution, effectively performing an upsert operation. ```python db.insert("table", dict(a="hi", b=1), upsert_on="a") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.