psql-script-generator


Namepsql-script-generator JSON
Version 0.0.3 PyPI version JSON
download
home_pageNone
SummaryIt generates a SQL script based on a Jinja2 template, allowing you to automate the creation of a database, roles, and permissions.
upload_time2024-04-25 03:07:47
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) [year] [fullname] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords psql readwrite sql script
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # psql-script-generator

It generates a SQL script based on a Jinja2 template, allowing you to automate the creation of a database, roles, and permissions.
It is particularly useful for setting up a PostgreSQL database with predefined roles and permissions.

## Installation:

```bash
# You can create a virtual env to install the cli

# Creating the venv
$ python -m venv tutorial_env

# Activating
$ source tutorial_env/bin/activate

# Installing the cli
pip install psql-script-generator

```

## Usage

```bash
$ psql-script-generator -d <database_name> -w <readwrite_role> -u <user_role> -p <password> -t <template_file> -o <output_file>
-d, --database: Name of the database.
-w, --readwrite_role: Name of the readwrite role.
-u, --user_role: Name of the users role.
-p, --password: Password for the user roles.
-t, --template: Path to the Jinja2 template file.
-o, --output_file: Output file for the generated SQL script.
```

Let's validate our generated sql script!!!

Start a PostgreSQL container:
```bash
$ docker run --name psql-validating -e POSTGRES_PASSWORD=mysecretpassword -p 5555:5432 -d postgres:13
```

Run generate_sql_script cli:
```bash
$ psql-script-generator -d test -w test_readwrite -u test_user -p 'qweasdzxc' -t readwrite-user-template.sql.j2 -o test_sql_script.sql
```

Run the generated SQL script:
```bash
$ export PGPASSWORD='mysecretpassword'; psql -h localhost -U postgres -d postgres -p 5555 -w -f test_sql_script.sql

# The output should be something like that:

CREATE DATABASE
CREATE ROLE
GRANT
GRANT
GRANT
GRANT
GRANT
GRANT
GRANT
ALTER DEFAULT PRIVILEGES
ALTER DEFAULT PRIVILEGES
ALTER DEFAULT PRIVILEGES
CREATE ROLE
GRANT ROLE
ALTER DATABASE

```

Now, let's test the grants that provided to test_user
```bash

# Download the validating.sql file
$ curl -o validating.sql https://raw.githubusercontent.com/pledo/psql-script-generator/main/validating.sql

# Run the sql script with write, read and delete permissions
export PGPASSWORD='qweasdzxc'; psql -h localhost -U test_user -d postgres -p 5555 -w -f validating.sql

# The output should be something like:

CREATE TABLE
INSERT 0 1
 id |         title
----+------------------------
  1 | Learn basic SQL syntax
(1 row)

ALTER TABLE
UPDATE 1
 id |         title          | completed
----+------------------------+-----------
  1 | Learn basic SQL syntax | t
(1 row)

INSERT 0 1
DELETE 1
 id |         title          | completed
----+------------------------+-----------
  1 | Learn basic SQL syntax | t
(1 row)

DROP TABLE

```

Uninstall cli
```bash
$ pip uninstall psql-script-generator

and deactive the venv

$ deactivate
```

### For a full automated tests

Enter tests folder
```bash
$ cd tests
```

Run the bash script
```bash
bash full_test.sh
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "psql-script-generator",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Paulo Ledo <paulofledo@gmail.com>",
    "keywords": "psql, readwrite, sql script",
    "author": null,
    "author_email": "Paulo Ledo <paulofledo@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/c3/e1/011b46b7437a5b0e0689cf4d90409e785d652082f43382a30bf045f994cb/psql_script_generator-0.0.3.tar.gz",
    "platform": null,
    "description": "# psql-script-generator\n\nIt generates a SQL script based on a Jinja2 template, allowing you to automate the creation of a database, roles, and permissions.\nIt is particularly useful for setting up a PostgreSQL database with predefined roles and permissions.\n\n## Installation:\n\n```bash\n# You can create a virtual env to install the cli\n\n# Creating the venv\n$ python -m venv tutorial_env\n\n# Activating\n$ source tutorial_env/bin/activate\n\n# Installing the cli\npip install psql-script-generator\n\n```\n\n## Usage\n\n```bash\n$ psql-script-generator -d <database_name> -w <readwrite_role> -u <user_role> -p <password> -t <template_file> -o <output_file>\n-d, --database: Name of the database.\n-w, --readwrite_role: Name of the readwrite role.\n-u, --user_role: Name of the users role.\n-p, --password: Password for the user roles.\n-t, --template: Path to the Jinja2 template file.\n-o, --output_file: Output file for the generated SQL script.\n```\n\nLet's validate our generated sql script!!!\n\nStart a PostgreSQL container:\n```bash\n$ docker run --name psql-validating -e POSTGRES_PASSWORD=mysecretpassword -p 5555:5432 -d postgres:13\n```\n\nRun generate_sql_script cli:\n```bash\n$ psql-script-generator -d test -w test_readwrite -u test_user -p 'qweasdzxc' -t readwrite-user-template.sql.j2 -o test_sql_script.sql\n```\n\nRun the generated SQL script:\n```bash\n$ export PGPASSWORD='mysecretpassword'; psql -h localhost -U postgres -d postgres -p 5555 -w -f test_sql_script.sql\n\n# The output should be something like that:\n\nCREATE DATABASE\nCREATE ROLE\nGRANT\nGRANT\nGRANT\nGRANT\nGRANT\nGRANT\nGRANT\nALTER DEFAULT PRIVILEGES\nALTER DEFAULT PRIVILEGES\nALTER DEFAULT PRIVILEGES\nCREATE ROLE\nGRANT ROLE\nALTER DATABASE\n\n```\n\nNow, let's test the grants that provided to test_user\n```bash\n\n# Download the validating.sql file\n$ curl -o validating.sql https://raw.githubusercontent.com/pledo/psql-script-generator/main/validating.sql\n\n# Run the sql script with write, read and delete permissions\nexport PGPASSWORD='qweasdzxc'; psql -h localhost -U test_user -d postgres -p 5555 -w -f validating.sql\n\n# The output should be something like:\n\nCREATE TABLE\nINSERT 0 1\n id |         title\n----+------------------------\n  1 | Learn basic SQL syntax\n(1 row)\n\nALTER TABLE\nUPDATE 1\n id |         title          | completed\n----+------------------------+-----------\n  1 | Learn basic SQL syntax | t\n(1 row)\n\nINSERT 0 1\nDELETE 1\n id |         title          | completed\n----+------------------------+-----------\n  1 | Learn basic SQL syntax | t\n(1 row)\n\nDROP TABLE\n\n```\n\nUninstall cli\n```bash\n$ pip uninstall psql-script-generator\n\nand deactive the venv\n\n$ deactivate\n```\n\n### For a full automated tests\n\nEnter tests folder\n```bash\n$ cd tests\n```\n\nRun the bash script\n```bash\nbash full_test.sh\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) [year] [fullname]  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "It generates a SQL script based on a Jinja2 template, allowing you to automate the creation of a database, roles, and permissions.",
    "version": "0.0.3",
    "project_urls": {
        "Homepage": "https://github.com/pledo/psql-script-generator",
        "Repository": "https://github.com/pledo/psql-script-generator.git"
    },
    "split_keywords": [
        "psql",
        " readwrite",
        " sql script"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "332b50d8e582f638b9f5f366fab5756e72ba32057b3132051b34c4c2f419324b",
                "md5": "c3a336281843713e06655877c75acd4d",
                "sha256": "3b0b3e787cc0583d7ced580cd388fc4744289adfe03c84aad99713def7dcbc23"
            },
            "downloads": -1,
            "filename": "psql_script_generator-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c3a336281843713e06655877c75acd4d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6609,
            "upload_time": "2024-04-25T03:07:45",
            "upload_time_iso_8601": "2024-04-25T03:07:45.727320Z",
            "url": "https://files.pythonhosted.org/packages/33/2b/50d8e582f638b9f5f366fab5756e72ba32057b3132051b34c4c2f419324b/psql_script_generator-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c3e1011b46b7437a5b0e0689cf4d90409e785d652082f43382a30bf045f994cb",
                "md5": "26c6691f11feae776a9f0cf455f2885d",
                "sha256": "c82713a607d526918185d3bc36eee30ecf6d0b92548c34172716bd9cf2dff995"
            },
            "downloads": -1,
            "filename": "psql_script_generator-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "26c6691f11feae776a9f0cf455f2885d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 12676,
            "upload_time": "2024-04-25T03:07:47",
            "upload_time_iso_8601": "2024-04-25T03:07:47.249555Z",
            "url": "https://files.pythonhosted.org/packages/c3/e1/011b46b7437a5b0e0689cf4d90409e785d652082f43382a30bf045f994cb/psql_script_generator-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-25 03:07:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pledo",
    "github_project": "psql-script-generator",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "psql-script-generator"
}
        
Elapsed time: 0.24393s