Skip to content

Commit

Permalink
Introduced the official version 1.0.0 of Mongrations! Refer to change…
Browse files Browse the repository at this point in the history
…log in README.md
  • Loading branch information
ableinc committed Aug 24, 2020
1 parent a4b53e5 commit eb9b817
Show file tree
Hide file tree
Showing 19 changed files with 417 additions and 309 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ old.txt
docs/_build/**
docs/_templates/**
migrations/**
.mongrations/**
.DS_Store
mongrations/data/cache.json
51 changes: 33 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ as compatible databases for the Mongrations tool.
# Getting Started
1 . Generate a migration file
```bash
mongrations -C true --name insert-into-members
mongrations create insert-into-members
```
2 . Contents of the generated migration file (*import and class definition are
autogenerated for migration file* - **contents of up() and down() are user defined**.)
```python
from mongrations import Mongrations, Database
from pydotenv import load_env
from pydotenvs import load_env

load_env()
# This is a MongoDB example. Go to /examples directory
Expand Down Expand Up @@ -42,7 +42,7 @@ Mongrations(Mongration, 'sync')
```
3 . Run migrations
```bash
mongrations -M true
mongrations migrate
```

# Install
Expand All @@ -51,7 +51,6 @@ pip install --upgrade mongrations
```
or install locally
```bash
python setup.py build
sudo python setup.py install
```

Expand All @@ -66,24 +65,27 @@ Refer to Mongrations <a href="https://mongrations.readthedocs.io/en/latest/">doc

**CLI**
```bash
Usage: mongrations [OPTIONS]
Usage: mongrations [OPTIONS] COMMAND [ARGS]...

Mongrations; a database migration tool for Python 3.6 and above.

Options:
-M, --migrate BOOLEAN Run migrations
-C, --create BOOLEAN Create new migration
-N, --name TEXT Name for newly created migration
-F, --file_path TEXT File path for newly created migration
-U, --undo BOOLEAN Undo last migration
-D, --down BOOLEAN Revert database
--version Show the version and exit.
--help Show this message and exit.
--version Show the version and exit.
--help Show this message and exit.

Commands:
create
down
inspect
migrate
undo
```
**CLI Examples**
```bash
mongrations -C true --name [migration_name] # create new migration
mongrations -M true # run migrations
mongrations -D true # tear down migrations
mongrations -U true # undo last migration
mongrations create [name] # create new migration (w/ name)
mongrations migrate # run migrations
mongrations down # tear down migrations
mongrations undo # undo last migration
```

**Mongrations Class**
Expand All @@ -103,4 +105,17 @@ Run example migration in examples/ folder
Please report all issues to repo.

# Notes
You can install psycopg2 from source via setup.py develop build or refer to their repo.
You can install psycopg2 from source via setup.py; python setup.py develop. Follow prompts.
You will need root access to development machine to install this tool.

# Changelog
August 2020:
- Introduced the official version 1.0.0 of Mongrations!
- Rewrote command line tool; much easier and intuiative
- Extracted classes into their own files; reducing clutter
- Added a raw sql function that allows for much more flexibility
- File name rewrites (if you encounter an upgrade error run the option: --no-cache, with pip)
- psycopg2 is now installed optionally (refer to Notes)
- Super fast writing to the system
- Setup.py has been cleaned up. An occasional bug occured when installing
- Added/Updated examples (refer to Github)
6 changes: 3 additions & 3 deletions docs/mongrations/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ MYSQL_DB='mongrations_test'
```
## 3. Create a migration file
```bash
mongrations -C true --name create-members-table
mongrations create create-members-table
```

## 4. Edit migrations
```python
from mongrations import Mongrations, Database
from pydotenv import load_env
from pydotenvs import load_env

load_env() # this will automatically grab your environment variables

Expand Down Expand Up @@ -52,6 +52,6 @@ Mongrations(Mongration, 'sync', db_service='mysql')
```
## 5. Run migrations
```bash
mongrations -M true
mongrations migrate
```

2 changes: 1 addition & 1 deletion docs/mongrations/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Simple as simple gets
.. code:: python
from mongrations import Mongrations, Database
from pydotenv import load_env, load_env_object
from pydotenvs import load_env, load_env_object
load_env() # connect via environment variables (default)
# config = load_env_object() # connect via dictionary of environment variables [ i.e Mongrations(config) ]
Expand Down
2 changes: 1 addition & 1 deletion examples/mongodb/mongodb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from mongrations import Mongrations, Database
from pydotenv import load_env, load_env_object
from pydotenvs import load_env, load_env_object

load_env('.env-example') # by default it looks for .env in the current directory
# config = load_env_object() # connect via dictionary of environment variables [ i.e Mongrations(config) ]
Expand Down
2 changes: 1 addition & 1 deletion examples/mongodb/mongodb_async.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from mongrations import Mongrations, Database
from pydotenv import load_env, load_env_object
from pydotenvs import load_env, load_env_object

# load_env() # connect via environment variables (default)
config = load_env_object('.env-example') # by default it looks for .env in the current directory
Expand Down
2 changes: 1 addition & 1 deletion examples/mysql/mysql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from mongrations import Mongrations, Database
from pydotenv import load_env, load_env_object
from pydotenvs import load_env, load_env_object

load_env('.env-example') # by default it looks for .env in the current directory
# config = load_env_object() # connect via dictionary of environment variables [ i.e Mongrations(config) ]
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres/postgres.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from mongrations import Mongrations, Database
from pydotenv import load_env, load_env_object
from pydotenvs import load_env, load_env_object

load_env('.env-example') # by default it looks for .env in the current directory
# config = load_env_object() # connect via dictionary of environment variables [ i.e Mongrations(config) ]
Expand Down
20 changes: 20 additions & 0 deletions examples/raw/raw_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from mongrations import Mongrations, Database
from pydotenvs import load_env, load_env_object

load_env('.env-example') # by default it looks for .env in the current directory
# config = load_env_object() # connect via dictionary of environment variables [ i.e Mongrations(config) ]


class Mongration(Database):
def __init__(self):
super(Database, self).__init__()

def up(self):
raw_sql = "ALTER TABLE users ADD gender NVARCHAR"
self.raw(raw_sql)

def down(self):
self.drop_table('users')


Mongrations(Mongration, db_service='mysql') # raw can be used with all three supported DBs (i.e. MySQL, MongoDB & Postgres)
13 changes: 10 additions & 3 deletions mongrations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from mongrations.main import Mongrations, MongrationsCli, Database
from mongrations.version import __version__
try:
from mongrations.main import Mongrations, MongrationsCli
from mongrations.database import Database
from mongrations.version import __version__
except ImportError:
from .main import Mongrations, MongrationsCli
from .database import Database
from .version import __version__

__all__ = [Mongrations, Mongrations, __version__, Database]

__all__ = [Mongrations, MongrationsCli, Database, __version__]
7 changes: 6 additions & 1 deletion mongrations/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Cache:
def __init__(self, verbose: bool = False):
self._verbose = verbose
self._file_path = get_filepath()
self._reference_file = pkg_resources.resource_filename('mongrations', 'data/reference_file.txt')
self._reference_file = pkg_resources.resource_filename('mongrations', 'data/template.txt')
self.initial = None
if not path.isfile(self._file_path):
self.initial = True
Expand Down Expand Up @@ -115,3 +115,8 @@ def undo_migration(self, remove_migration: bool = False):
def migrations_file_list(self):
cache = self._get_file_object()
return cache['migrations']

def inspect_cache(self):
self._file_system_check()
cache = self._get_file_object()
print(cache)
79 changes: 46 additions & 33 deletions mongrations/cli.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
import click, sys
from mongrations.main import MongrationsCli
from mongrations.version import __version__


@click.command('mongrations')
@click.option('-M', '--migrate', default=False, type=click.BOOL,
help='Run migrations')
@click.option('-C', '--create', default=False, type=click.BOOL,
help='Create new migration')
@click.option('-N', '--name', default='-no-name-migration', type=click.STRING,
help='Name for newly created migration')
@click.option('--directory', default='migrations', type=click.STRING,
help='File path for newly created migration')
@click.option('-U', '--undo', default=False, type=click.BOOL,
help='Undo last migration')
@click.option('-D', '--down', default=False, type=click.BOOL,
help='Revert database')
try:
from mongrations.main import MongrationsCli
from mongrations.version import __version__
except Exception:
from .main import MongrationsCli
from .version import __version__

main = MongrationsCli()

@click.group()
@click.version_option(version=__version__)
def mongrations(migrate, create, name, directory, undo, down):
main = MongrationsCli()
try:
if migrate:
main.migrate()
if create:
main.create(directory=directory, name=name)
if undo:
main.undo()
if down:
main.down()
except Exception as e:
print(e)
finally:
sys.exit()
def cli():
"""Mongrations; a database migration tool for Python 3.6 and above."""
pass


@cli.command()
def migrate():
main.migrate()


@cli.command()
@click.argument('name', nargs=1)
@click.argument('directory', nargs=-1)
def create(name, directory):
if len(directory) == 0:
directory = 'migrations'

if len(name) == 0:
name = 'no-name-migration'
main.create(directory=directory, name=name)


@cli.command()
def undo():
main.undo()


@cli.command()
def down():
main.down()


@cli.command()
def inspect():
main.inspector()


if __name__ == '__main__':
mongrations()
cli()

0 comments on commit eb9b817

Please sign in to comment.