Metadata-Version: 2.1
Name: alchemy-modelgen
Version: 0.1.3
Summary: UNKNOWN
Home-page: https://github.com/shree14/sqlalchemy-modelgen
Author: Shrinivas Deshmukh
Author-email: shrinivas.deshmukh11@gmail.com
License: UNKNOWN
Platform: UNKNOWN
Description-Content-Type: text/markdown
Requires-Dist: alembic (>=1.5.8)
Requires-Dist: Jinja2 (>=2.11.3)
Requires-Dist: mysql-connector-python (>=8.0.23)
Requires-Dist: psycopg2-binary (>=2.8.6)
Requires-Dist: python-dotenv (>=0.17.0)
Requires-Dist: PyYAML (>=5.4.1)
Requires-Dist: snowflake-sqlalchemy (>=1.2.4)
Requires-Dist: SQLAlchemy (>=1.4.4)
Requires-Dist: sqlalchemy-redshift (>=0.8.2)

# sqlalchemy-modelgen

Create sqlalchemy python model files by defining tables and columns in a yaml file

# Installation

```
pip install alchemy-modelgen
```

# Usage

* ##### Initialize alchemy-modelgen folder

```
modelgen --init FOLDER_NAME
cd FOLDER_NAME
```

* #### Set the database URI

The URI should follow sqlalchemy syntax.

```
export DATABASE_URI=dialect+driver://username:password@host:port/database`
```

Example: For MySQL
```
export DATABASE_URI=mysql+mysqlconnector://root:example@localhost:3306/modelgen
```

* ##### Create schema template

Structure: 

```
tables: # In this section, we define the tables, their name and schema
    userinfo: # This is the table name
        columns: # In this section, we define column names and their data types
            - name: id
              type: integer
              primary_key: true     # Set this value for the primary key column
            - name: firstname       # Column name
              type: varchar         # Column datatype
            - name: lastname
              type: varchar
            - name: dob
              type: date
            - name: contact
              type: numeric
              nullable: false/true      # Allow / disallow null values in the column, default `true`
              unique: true/false        # Apply unique constraint for the column, default `false`
            - name: address
              type: varchar
              length: 200       # specify length of the column
```

Injecting extra parameters:

It is possible to inject database dialect parameters. For example, for [redshift](https://aws.amazon.com/redshift/) we can specify `redshift_diststyle` or `redshift_distkey` or any other feature supported by redshift.

```
tables: 
    userinfo: 
        columns: 
            - name: firstname 
              type: varchar 
            - name: lastname
              type: varchar
            - name: dob
              type: date
            - name: contact
              type: numeric
            - name: address
              type: varchar
        extra_params: # Here we can specify dialect specific extra parameters
            - name: redshift_distkey        # Name of the parameter
              value: userid                 # Value of the parameter
```

* ##### Run model generation code

```
modelgen -c path/to/yaml_schema.yaml -a
```

* ##### Run alembic migrations

```
alembic revision --autogenerate -m "YOUR_COMMIT_MESSAGE"
alembic upgrade head
```

# Alter Table support

* To alter any column's schema, make the relevant changes in the YAML template file.

* Run the model generation code

```
modelgen -c path/to/yaml_schema.yaml -a
```

* Run alembic migrations

```
alembic revision --autogenerate -m "YOUR_COMMIT_MESSAGE"
alembic upgrade head
```


