# `sc_crawler.cli`

> The `sc-crawler` CLI functions.

# `sc_crawler.cli`

Check `sc-crawler --help` for more details.

**Functions:**

- [**create**](#create) – Print the database schema in a SQL dialect.
- [**current**](#current) – Show current database revision.
- [**upgrade**](#upgrade) – Upgrade the database schema to a given (default: most recent) revision.
- [**downgrade**](#downgrade) – Downgrade the database schema to a given (default: previous) revision.
- [**stamp**](#stamp) – Set the migration revision mark in the database to a specified revision. Set to "heads" if the database schema is up-to-date.
- [**autogenerate**](#autogenerate) – Autogenerate a migrations script based on the current state of a database.
- [**metadata_set**](#metadata_set) – Write metadata key/value pairs into the database.
- [**metadata_get**](#metadata_get) – Print all metadata key/value pairs stored in the database.
- [**metadata_delete**](#metadata_delete) – Delete one or more metadata entries by key.
- [**hash_command**](#hash_command) – Print the hash of the content of a database.
- [**copy**](#copy) – Copy the standard Crawler tables of a database into a blank database.
- [**sync**](#sync) – Sync a database to another one.
- [**dump**](#dump) – Export database records to JSON files organized by primary keys.
- [**pull**](#pull) – Pull data from available vendor APIs and store in a database.
- [**archived_by_command**](#archived_by_command) – List the archive rules that archive a record.

## func <code class="doc-name">create</code> [cli.py:131–170](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L131-L170)

```python
create(connection_string=None, dialect=None, scd=False)
```

Print the database schema in a SQL dialect.

Either `connection_string` or `dialect` is to be provided to decide
what SQL dialect to use to generate the CREATE TABLE (and related)
SQL statements.

## func <code class="doc-name">current</code> [cli.py:173–183](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L173-L183)

```python
current(connection_string='sqlite:///sc-data-all.db', scd=False)
```

Show current database revision.

## func <code class="doc-name">upgrade</code> [cli.py:186–198](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L186-L198)

```python
upgrade(connection_string='sqlite:///sc-data-all.db', revision='heads', scd=False, sql=False)
```

Upgrade the database schema to a given (default: most recent) revision.

## func <code class="doc-name">downgrade</code> [cli.py:201–213](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L201-L213)

```python
downgrade(connection_string='sqlite:///sc-data-all.db', revision='-1', scd=False, sql=False)
```

Downgrade the database schema to a given (default: previous) revision.

## func <code class="doc-name">stamp</code> [cli.py:216–228](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L216-L228)

```python
stamp(connection_string='sqlite:///sc-data-all.db', revision='heads', scd=False, sql=False)
```

Set the migration revision mark in the database to a specified revision. Set to "heads" if the database schema is up-to-date.

## func <code class="doc-name">autogenerate</code> [cli.py:231–248](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L231-L248)

```python
autogenerate(connection_string='sqlite:///sc-data-all.db', message='empty message')
```

Autogenerate a migrations script based on the current state of a database.

## func <code class="doc-name">metadata_set</code> [cli.py:251–288](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L251-L288)

```python
metadata_set(entries=None, connection_string='sqlite:///sc-data-all.db')
```

Write metadata key/value pairs into the database.

Always sets `sc_crawler_version` and `published_at`. When running in a GitHub
Actions workflow, also sets `published_by` to the GitHub Actions run URL.
Additional key/value pairs can be passed as positional arguments.

## func <code class="doc-name">metadata_get</code> [cli.py:299–311](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L299-L311)

```python
metadata_get(connection_string='sqlite:///sc-data-all.db')
```

Print all metadata key/value pairs stored in the database.

## func <code class="doc-name">metadata_delete</code> [cli.py:314–328](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L314-L328)

```python
metadata_delete(keys, connection_string='sqlite:///sc-data-all.db')
```

Delete one or more metadata entries by key.

## func <code class="doc-name">hash_command</code> [cli.py:331–336](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L331-L336)

```python
hash_command(connection_string='sqlite:///sc-data-all.db')
```

Print the hash of the content of a database.

## func <code class="doc-name">copy</code> [cli.py:339–416](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L339-L416)

```python
copy(source, target, exclude_status=[])
```

Copy the standard Crawler tables of a database into a blank database.

All rows are copied by default. Use `--exclude-status` to leave out rows,
e.g. the archived records that are kept only for history. Rows referencing
a left out row (e.g. the zones of an excluded region) are also left out.

## func <code class="doc-name">sync</code> [cli.py:419–674](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L419-L674)

```python
sync(source, target, dry_run=False, scd=False, sync_tables=table_names, log_changes_path=None, log_changes_tables=table_names)
```

Sync a database to another one.

Hashing both the `source` and the `target` databases, then
comparing hashes and marking for syncing the following records:

- new (rows with primary keys found in `source`, but not found in `target`)

- update (rows with different values in `source` and in `target`).

- inactive (rows with primary keys found in `target`, but not found in
  `source`). Rows already archived in `target` are left unchanged, and
  rows matching an archive rule are marked as archived instead of inactive.

The archive rules (see `sc_crawler.archive_rules`) are also checked: the
rules of the synced tables referring to records found in neither database
are reported.

The records marked for syncing are written to the `target` database's
standard or SCD tables. When updating the SCD tables, the hashing still
happens on the standard tables/views, which are probably based on the
most recent records of the SCD tables.

## func <code class="doc-name">dump</code> [cli.py:677–833](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L677-L833)

```python
dump(connection_string, output_directory=Path('.'), dump_tables=None, ignored=['observed_at'])
```

Export database records to JSON files organized by primary keys.

Each record is written as a pretty-printed JSON file in a folder
hierarchy based on the primary key values. For example, a server
record with vendor_id='aws' and server_id='t3.small' would be
written to: `output_directory/server/aws/t3.small.json`

## func <code class="doc-name">pull</code> [cli.py:836–1040](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L836-L1040)

```python
pull(connection_string='sqlite:///sc-data-all.db', include_vendor=[v.vendor_id for v in supported_vendors], exclude_vendor=[], include_records=supported_records, exclude_records=[], log_level=LogLevels.INFO, cache=False, cache_ttl=60 * 24)
```

Pull data from available vendor APIs and store in a database.

As the last step, the archive rules (see `sc_crawler.archive_rules`) are
stored in the database, and the records they match are marked as archived.

Vendor API calls are optionally cached as Pickle objects in `~/.cachier`.

## func <code class="doc-name">archived_by_command</code> [cli.py:1043–1104](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/cli.py#L1043-L1104)

```python
archived_by_command(table_name, primary_key, connection_string='sqlite:///sc-data-all.db')
```

List the archive rules that archive a record.

Looks up the rules stored in the `archive_rule` table of the database,
including the removed (inactive) rules and the rules on the parent tables
of the record (e.g. the region of a server price). The first observation
of each rule is read from the `archive_rule_scd` table, if that exists:
compare it with the first archived version of the record to tell when it
was archived.
