diff --git a/.gitignore b/.gitignore index 61378cc..0e115a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,18 @@ +# Vagrant stuff +acceptance_config.yml +boxes/* +/.vagrant +/website/.vagrant +/website/build +/vagrant-spec.config.rb +test/vagrant-spec/.vagrant/ -#Build result - +# Logs *.log -_pycache_/ \ No newline at end of file + +# IDE +_pycache_/ +.idea/* + +# Python +*.pyc diff --git a/Vagrantfile b/Vagrantfile index ca7721a..2041ef5 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,4 +1,20 @@ +db_file_exists = "test -f /vagrant/edison/db.sql" +restore_db = "sudo -u postgres psql edison < /vagrant/edison/db.sql" +db_restored_msg = "echo \"Database restored.\"" +db_not_exists_msg = "echo \"db.sql not exists.\"" +try_restore_db = "bash -c '#{db_file_exists} && #{restore_db} && #{db_restored_msg} || #{db_not_exists_msg}'" +save_db_data = "sudo -u postgres pg_dump edison > /vagrant/edison/db.sql" + Vagrant.configure("2") do |config| + config.trigger.before :destroy do |trigger| + trigger.info = "Saving database data inside synced folder..." + trigger.run_remote = {inline: "#{save_db_data}"} + end + + config.trigger.after :up do |trigger| + trigger.info = "Trying to restore database from /vagrant/edison/db.sql..." + trigger.run_remote = {inline: "#{try_restore_db}"} + end config.vm.box = "ubuntu/bionic64" config.vm.provision :shell, path: "setup.sh" diff --git a/edison/__init__.py b/edison/__init__.py index befb02a..d4ff709 100644 --- a/edison/__init__.py +++ b/edison/__init__.py @@ -1,6 +1,7 @@ import os from flask import Flask +from flask_sqlalchemy import SQLAlchemy from edison.config import get_config_object @@ -8,3 +9,4 @@ app = Flask(__name__) app.config.from_object(get_config_object(app.config["ENV"])) basedir = os.path.abspath(os.path.dirname(__file__)) +db = SQLAlchemy(app) diff --git a/edison/app.py b/edison/app.py index 7aec790..14c6e6a 100644 --- a/edison/app.py +++ b/edison/app.py @@ -1,9 +1,16 @@ import edison +import edison.models from flask import render_template app = edison.app +db = edison.db + +# Creates all tables defined in the database models and the only ones that are not created yet. +# If there's any change in the database models you should perform a migration to apply this change in the database itself. +# More about database migrations can be found in /edison/migrations/README. +db.create_all() @app.route("/policy") def policy(): diff --git a/edison/config.py b/edison/config.py index 97f665d..284caae 100644 --- a/edison/config.py +++ b/edison/config.py @@ -25,8 +25,11 @@ def get_config_object(env_keyword: str): class Config: ENV_KEYWORD = "" DEBUG = False + # Turns off the Flask-SQLAlchemy event system + SQLALCHEMY_TRACK_MODIFICATIONS = False + SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:edison@127.0.0.1/edison' - +# PostgreSQL connection string should be updated once an actual production environment is established. class ProductionConfig(Config): ENV_KEYWORD = "production" diff --git a/edison/db.sql b/edison/db.sql new file mode 100644 index 0000000..e69de29 diff --git a/edison/models/__init__.py b/edison/models/__init__.py new file mode 100644 index 0000000..ee4c00b --- /dev/null +++ b/edison/models/__init__.py @@ -0,0 +1 @@ +from .user import User diff --git a/edison/models/user.py b/edison/models/user.py new file mode 100644 index 0000000..7349cd0 --- /dev/null +++ b/edison/models/user.py @@ -0,0 +1,13 @@ +from edison import db + + +class User(db.Model): + __tablename__ = 'users' + + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(50), nullable=False, unique=True) + + def to_json(self): + return { + "username": self.username + } diff --git a/requirements.txt b/requirements.txt index 32e8968..75b8dba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ Flask==1.1.1 +flask-sqlalchemy==2.4.1 +psycopg2-binary==2.8.5 diff --git a/setup.sh b/setup.sh index 1e6346c..147887d 100644 --- a/setup.sh +++ b/setup.sh @@ -17,6 +17,10 @@ sudo apt-get install -y postgresql postgresql-contrib echo "install requirements" pip3 install -r /vagrant/requirements.txt +echo "configuring database" +sudo -u postgres createdb edison +sudo -u postgres psql -c "ALTER ROLE postgres WITH PASSWORD 'edison';" + export FLASK_ENV=development echo "running app.py"