Skip to main content

Scripts

Overview#

If you define the sourceType property as scripts you will be able to create a data image whose content will be what you defined in your scripts.

sourceType: scripts
name: dev
engine: postgresql
version: 11.0
scriptsFolders:
- ./Databases/Postgres

In this case we want to create a PostgreSQL data image whose source is scripts and is named dev.

The path given to the scriptsFolder property is relative to the location of the image definition .yaml file.

Spawn will execute scripts inside each folder in alphanumeric order.

You can specify multiple scripts folders in the yaml file. Spawn will execute scripts from each folder in the order specified.

Tutorial#

In this tutorial we will create a data image from scripts and then use it to create a data container. We will then inspect the data container.

  1. Create a file development.yaml with your data image specifications.

    sourceType: scripts
    name: dev
    engine: postgresql
    version: 11.0
    scriptsFolders:
    - ./Database

    We are defining the source of our data image to be scripts and the scripts folders to be imported will be the ones present in the ./Database folder.

    If we inspect the Database folder we find two .sql files:

    $ tree
    .
    โ”œโ”€โ”€ 001.sql
    โ”œโ”€โ”€ 002.sql
    0 directories, 2 files

    If we inspect the files we get the following:

    $ cat Database/001.sql
    CREATE DATABASE mycompany;
    $ cat Database/002.sql
    connect mycompany;
    CREATE TABLE customers
    (
    id BIGSERIAL PRIMARY KEY,
    daterecorded TIMESTAMP
    WITH TIME ZONE,
    description TEXT
    );
  2. Run the following command to create a data image.

    $ spawnctl create data-image -f ./development.yaml
    Data image 'dev' (10001) created!
  3. Create a data container from the newly created data image.

    $ spawnctl create data-container --image dev
    Data container 'dev-rambbomj' (10001) created!
    -> Host=instances.spawn.cc;Port=53223;User ID=<some_user_id>;Password=<some_password>;
  4. You should now be able to connect to your database and execute queries.

    In this example we connect to the PostgreSQL data container (database) using psql.

    $ psql -h instances.spawn.cc -p 53223 -U <some_user_id>
    Password for user <some_user_id>:
    psql (10.5, server 11.0 (Debian 11.0-1.pgdg90+2))
    SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
    Type "help" for help.
    <some_user_id>=# \l
    List of databases
    Name | Owner | Encoding | Collate | Ctype | Access privileges
    ------------------+------------------+----------+------------+------------+---------------------------------------
    postgres | <some_user_id> | UTF8 | en_US.utf8 | en_US.utf8 |
    <some_user_id> | <some_user_id> | UTF8 | en_US.utf8 | en_US.utf8 |
    mycompany | <some_user_id> | UTF8 | en_US.utf8 | en_US.utf8 |
    (5 rows)
    <some_user_id>=# \c mycompany
    mycompany=# \dt
    List of relations
    Schema | Name | Type | Owner
    --------+------------------+-------+------------------
    public | customers | table | <some_user_id>

    Notice the created database/data container has a database called mycompany as well as the table customers that where present in our scripts.