Skip to content

Commit

Permalink
Code clean up. Bug related to migrations save location has been resol…
Browse files Browse the repository at this point in the history
…ved. Please update package.
  • Loading branch information
Douglas, Jaylen authored and Douglas, Jaylen committed Nov 1, 2019
1 parent c3caf3f commit a4b53e5
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ docs/_build/**
docs/_templates/**
migrations/**
.DS_Store
data/cache.json
mongrations/data/cache.json
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recursive-include data *.txt
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mongrations
![alt text](https://img.icons8.com/ios/50/000000/database-restore.png "Mongrations Logo")
![alt text](https://img.icons8.com/dusk/64/000000/database.png "Mongrations Logo")
A migrations tool for Python 3.6+. Mongrations started as a MongoDB migrations tool but has introduced MySQL & Postgres
as compatible servers for the Mongrations tool.
as compatible databases for the Mongrations tool.

# Getting Started
1 . Generate a migration file
Expand Down Expand Up @@ -104,6 +104,3 @@ Please report all issues to repo.

# Notes
You can install psycopg2 from source via setup.py develop build or refer to their repo.

# Up Next
Unit Tests.
31 changes: 26 additions & 5 deletions mongrations/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@
from os import remove, makedirs, getcwd
import json
from datetime import datetime
import uuid
import uuid, pkg_resources
from pathlib import Path


def get_filepath():
import sys, getpass
filepath = {
'darwin': Path(f'/Users/{getpass.getuser()}/.mongrations/cache.json'),
'windows': Path('C:/Users/Programs Files/.mongrations/cache.json')
}.get(sys.platform)
if not path.isdir(filepath.parent):
try:
makedirs(filepath.parent)
except FileExistsError:
pass
return filepath


class Cache:
def __init__(self, verbose: bool = False):
self._verbose = verbose
self._file_path = 'data/cache.json'
self._file_path = get_filepath()
self._reference_file = pkg_resources.resource_filename('mongrations', 'data/reference_file.txt')
self.initial = None
if not path.isfile(self._file_path):
self.initial = True
Expand All @@ -26,10 +42,15 @@ def _write_file_obj(self, data, migration_name=''):
new_data = self._collect_meta_data(data, migration_name)
try:
with open(self._file_path, 'w', encoding='utf-8') as writer:
json_obj = json.dumps(new_data, indent=4, sort_keys=True)
json_obj = json.dumps(new_data, indent=2, sort_keys=True)
writer.write(json_obj)
except json.decoder.JSONDecodeError:
remove(self._file_path)
try:
remove(self._file_path)
except OSError:
pass
if self._verbose:
print(f'{self._file_path} could not be saved. Internal error occurred when creating JSON object.')

def _collect_meta_data(self, data, migration_name=''):
new_data = data
Expand Down Expand Up @@ -77,7 +98,7 @@ def new_migration(self, name: str, directory):
pass
name = str(uuid.uuid4())[:16] + '-' + name + '.py'
migration_path = path.join(getcwd(), directory + '/' + name)
reference_file = open('data/reference_file.txt', 'r', encoding='utf-8')
reference_file = open(self._reference_file, 'r', encoding='utf-8')
with open(migration_path, 'w', encoding='utf-8') as migration_file:
migration_file.write(reference_file.read())
reference_file.close()
Expand Down
23 changes: 12 additions & 11 deletions mongrations/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
@click.version_option(version=__version__)
def mongrations(migrate, create, name, directory, undo, down):
main = MongrationsCli()
if migrate:
main.migrate()
sys.exit()
if create:
main.create(directory=directory, name=name)
sys.exit()
if undo:
main.undo()
sys.exit()
if down:
main.down()
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()


Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion mongrations/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.6'
__version__ = '0.0.7'
26 changes: 20 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import requests, zipfile, io, os, subprocess, shlex, shutil, sys
from setuptools import setup
import zipfile, io, os, subprocess, shlex, shutil, sys
import os.path as path
from setuptools import setup, find_packages
from setuptools.command.develop import develop
from mongrations import __version__

try:
import requests
except ImportError:
print('Please install requests library.')
sys.exit()

with open("README.md", "r") as fh:
long_description = fh.read()

Expand All @@ -11,6 +18,8 @@


class PostInstallCommand(develop):
psycopg2_url = 'https://github.com/psycopg/psycopg2/archive/master.zip'

def run(self):
# Install Postgres DB Python Tool
save_dir = os.path.join(str(os.getcwd()) + '/temp/')
Expand All @@ -21,8 +30,8 @@ def run(self):
try:
# Make Directory and Download Driver
os.makedirs(save_dir)
source = requests.get('https://github.com/psycopg/psycopg2/archive/master.zip')
file = zipfile.ZipFile(io.BytesIO(source.content))
content = requests.get(self.psycopg2_url).content
file = zipfile.ZipFile(io.BytesIO(content))
file.extractall(save_dir)
print('Installing psycopg2 from source...')
# Install Driver
Expand Down Expand Up @@ -54,8 +63,13 @@ def run(self):
url="https://github.com/ableinc/mongrations",
keywords=['migrations', 'python3', 'automation', 'database', 'json', 'nosql', 'python', 'database tool',
'automation tool', 'open source', 'mongodb', 'mysql', 'postgres', 'sql'],
packages=['mongrations'],
data_files=[('data', ['data/reference_file.txt'])],
packages=find_packages(),
package_data={
'mongrations': ['mongrations/data/reference_file.txt']
},
data_files=[
('/mongrations/data', [path.join('mongrations/data', 'reference_file.txt')])
],
entry_points='''
[console_scripts]
mongrations=mongrations.cli:mongrations
Expand Down

0 comments on commit a4b53e5

Please sign in to comment.