Add callable to get inventory from command line

This commit is contained in:
fedjo 2020-05-22 18:43:01 +02:00
parent b0fa24b9bf
commit c3cffbeaa7
4 changed files with 338 additions and 317 deletions

View File

@ -89,57 +89,59 @@ Testing
Migrations Migrations
********** **********
At this stage, migration files are created manually. To apply the migrations we follow At this stage, migration files are created manually.
a hybrid approach. Set up the database:
* When a schema is to be created in the db we create a revision file holding **all** the
necessary table definitions. For example have a look on the migration file holding the initial
declarations. There you see a full list of tables to be created. You just need to specify
the env variable **dhi**. To create a revision file execute:
.. code:: bash .. code:: bash
$ alembic revision -m "My initial base revision" $ sudo su - postgres
$ bash $PATH_TO_DEVIHUBTEAL/examples/create-db.sh devicehub dhub
Then run Initialize the database:
.. code:: bash .. code:: bash
$ export dhi=dbtest; dh inv add --common --name dbtest $ export dhi=dbtest; dh inv add --common --name dbtest
This command will create the schemas, tables in the specified database and will stamp the This command will create the schemas, tables in the specified database.
migration file you have created as the base schema for future migrations. For more info Then we need to stamp the initial migration.
in migration stamping please see https://alembic.sqlalchemy.org/en/latest/cookbook.html
Whenever you want to create a new schema just create a new revision with:
.. code:: bash .. code:: bash
$ alembic revision -m "My new base revision" $ alembic stamp head
and add there **all** the tables that the new database will have. Next, you can add the
new inventory and stamp the revision as the new base.
.. code:: bash
$ export dhi=dbtest2; dh inv add --name dbtest2
* When you want to alter a table, column or perform another operation on tables, create This command will set the revision **fbb7e2a0cde0_initial** as our initial migration.
a revision file For more info in migration stamping please see https://alembic.sqlalchemy.org/en/latest/cookbook.html
Whenever a change needed eg to create a new schema, alter an existing table, column or perform any
operation on tables, create a new revision file:
.. code:: bash .. code:: bash
$ alembic revision -m "A table change" $ alembic revision -m "A table change"
Then edit the generated file with the necessary operations to perform the migration. This command will create a new revision file with name `<revision_id>_a_table_change`.
Edit the generated file with the necessary operations to perform the migration:
.. code:: bash
$ alembic edit <revision_id>
Apply migrations using: Apply migrations using:
.. code:: bash .. code:: bash
$ alembic upgrade head $ alembic -x inventory=dbtest upgrade head
* Whenever you to see a full list of migrations use Then to go back to previous db version:
.. code:: bash
$ alembic -x inventory=dbtest downgrade <revision_id>
To see a full list of migrations use
.. code:: bash .. code:: bash

View File

@ -124,12 +124,6 @@ class Devicehub(Teal):
else: else:
self.db.create_all() self.db.create_all()
from alembic.config import Config
from alembic import command
alembic_cfg = Config('alembic.ini')
command.stamp(alembic_cfg, "head")
return True return True
@click.confirmation_option(prompt='Are you sure you want to delete the inventory {}?' @click.confirmation_option(prompt='Are you sure you want to delete the inventory {}?'

View File

@ -19,6 +19,12 @@ branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)} depends_on = ${repr(depends_on)}
def get_inv():
INV = context.get_x_argument(as_dictionary=True).get('inventory')
if not INV:
raise ValueError("Inventory value is not specified")
return INV
def upgrade(): def upgrade():
${upgrades if upgrades else "pass"} ${upgrades if upgrades else "pass"}

File diff suppressed because it is too large Load Diff