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' created!
  3. Create a Data Container from the newly created Data Image.

    $ spawnctl create data-container --image dev
    Data container 'dev-rambbomj' 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 create database or Data Container has a database called mycompany as well as the table customers that where present in our scripts.