first commit
This commit is contained in:
142
db/env.py
Normal file
142
db/env.py
Normal file
@@ -0,0 +1,142 @@
|
||||
from __future__ import with_statement
|
||||
import os
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
from logging.config import fileConfig
|
||||
import logging
|
||||
import re
|
||||
|
||||
USE_TWOPHASE = False
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
|
||||
# gather section names referring to different
|
||||
# databases. These are named "engine1", "engine2"
|
||||
# in the sample .ini file.
|
||||
db_names = config.get_main_option('databases')
|
||||
|
||||
# add your model's MetaData objects here
|
||||
# for 'autogenerate' support. These must be set
|
||||
# up to hold just those tables targeting a
|
||||
# particular database. table.tometadata() may be
|
||||
# helpful here in case a "copy" of
|
||||
# a MetaData is needed.
|
||||
# from myapp import mymodel
|
||||
# target_metadata = {
|
||||
# 'engine1':mymodel.metadata1,
|
||||
# 'engine2':mymodel.metadata2
|
||||
#}
|
||||
target_metadata = {}
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
# for the --sql use case, run migrations for each URL into
|
||||
# individual files.
|
||||
|
||||
engines = {}
|
||||
db_url = os.getenv('DATABASE_URL')
|
||||
if db_url:
|
||||
engines['heroku'] = {'url': db_url}
|
||||
else:
|
||||
for name in re.split(r',\s*', db_names):
|
||||
engines[name] = rec = {}
|
||||
rec['url'] = context.config.get_section_option(name,
|
||||
"sqlalchemy.url")
|
||||
|
||||
for name, rec in engines.items():
|
||||
logger.info("Migrating database %s" % name)
|
||||
file_ = "%s.sql" % name
|
||||
logger.info("Writing output to %s" % file_)
|
||||
with open(file_, 'w') as buffer:
|
||||
context.configure(url=rec['url'], output_buffer=buffer)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations(engine_name=name)
|
||||
|
||||
def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
# for the direct-to-DB use case, start a transaction on all
|
||||
# engines, then run all migrations, then commit all transactions.
|
||||
|
||||
engines = {}
|
||||
db_url = os.getenv('DATABASE_URL')
|
||||
if db_url:
|
||||
engines['heroku'] = rec = {}
|
||||
rec['engine'] = engine_from_config(
|
||||
{'sqlalchemy.url': db_url},
|
||||
poolclass=pool.NullPool)
|
||||
else:
|
||||
for name in re.split(r',\s*', db_names):
|
||||
engines[name] = rec = {}
|
||||
rec['engine'] = engine_from_config(
|
||||
context.config.get_section(name),
|
||||
prefix='sqlalchemy.',
|
||||
poolclass=pool.NullPool)
|
||||
|
||||
for name, rec in engines.items():
|
||||
engine = rec['engine']
|
||||
rec['connection'] = conn = engine.connect()
|
||||
|
||||
if USE_TWOPHASE:
|
||||
rec['transaction'] = conn.begin_twophase()
|
||||
else:
|
||||
rec['transaction'] = conn.begin()
|
||||
|
||||
try:
|
||||
for name, rec in engines.items():
|
||||
logger.info("Migrating database %s" % name)
|
||||
context.configure(
|
||||
connection=rec['connection'],
|
||||
upgrade_token="%s_upgrades" % name,
|
||||
downgrade_token="%s_downgrades" % name,
|
||||
target_metadata=target_metadata.get(name)
|
||||
)
|
||||
context.run_migrations()
|
||||
|
||||
if USE_TWOPHASE:
|
||||
for rec in engines.values():
|
||||
rec['transaction'].prepare()
|
||||
|
||||
for rec in engines.values():
|
||||
rec['transaction'].commit()
|
||||
except:
|
||||
for rec in engines.values():
|
||||
rec['transaction'].rollback()
|
||||
raise
|
||||
finally:
|
||||
for rec in engines.values():
|
||||
rec['connection'].close()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
24
db/script.py.mako
Normal file
24
db/script.py.mako
Normal file
@@ -0,0 +1,24 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
||||
64
db/versions/00817cda9d17_add_ebom_tables.py
Normal file
64
db/versions/00817cda9d17_add_ebom_tables.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""Add ebom tables
|
||||
|
||||
Revision ID: 00817cda9d17
|
||||
Revises: a904d0d1e1a7
|
||||
Create Date: 2016-09-01 14:05:49.858683
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
from sqlalchemy.orm import backref
|
||||
from helix.constants.inverter_type import InverterType
|
||||
|
||||
revision = '00817cda9d17'
|
||||
down_revision = 'a904d0d1e1a7'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'power_stations',
|
||||
sa.Column('id', sa.Integer, primary_key=True),
|
||||
sa.Column('site_id', sa.Integer, sa.ForeignKey('sites.id')),
|
||||
sa.Column('quantity', sa.Integer, nullable=False),
|
||||
sa.Column('ac_run_length', sa.Integer, nullable=False),
|
||||
sa.Column('description', sa.Unicode, nullable=False),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'standalone_inverters',
|
||||
sa.Column('id', sa.Integer, primary_key=True),
|
||||
sa.Column('site_id', sa.Integer, sa.ForeignKey('sites.id')),
|
||||
sa.Column('attachment_point_id', sa.Integer, sa.ForeignKey('power_stations.id')),
|
||||
sa.Column('ac_run_length', sa.Integer, nullable=False),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'inverters',
|
||||
sa.Column('id', sa.Integer, primary_key=True),
|
||||
sa.Column('model', sa.Enum(str(InverterType.SMA.MODEL_12KW.value), str(InverterType.SMA.MODEL_15KW.value), str(InverterType.SMA.MODEL_20KW.value), str(InverterType.SMA.MODEL_24KW.value), name='InverterType'), nullable=False),
|
||||
sa.Column('strings_per_inverter', sa.Integer, nullable=False),
|
||||
sa.Column('sunshade', sa.Boolean),
|
||||
sa.Column('dc_switch', sa.Boolean),
|
||||
sa.Column('power_station_id', sa.Integer, sa.ForeignKey('power_stations.id')),
|
||||
sa.Column('standalone_inverter_id', sa.Integer, sa.ForeignKey('standalone_inverters.id')),
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'power_monitors',
|
||||
sa.Column('id', sa.Integer, primary_key=True),
|
||||
sa.Column('site_id', sa.Integer, sa.ForeignKey('sites.id')),
|
||||
sa.Column('power_station_id', sa.Integer, sa.ForeignKey('power_stations.id'))
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('inverters')
|
||||
op.drop_table('power_monitors')
|
||||
op.drop_table('power_stations')
|
||||
op.drop_table('standalone_inverters')
|
||||
sa.Enum(name='InverterType').drop(op.get_bind(), checkfirst=False)
|
||||
@@ -0,0 +1,24 @@
|
||||
"""Add splice box check on inverters
|
||||
|
||||
Revision ID: 1e472a2f3cbb
|
||||
Revises: ed4c4bd22d6a
|
||||
Create Date: 2017-07-10 12:20:44.492597
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '1e472a2f3cbb'
|
||||
down_revision = 'ed4c4bd22d6a'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column('inverters', sa.Column('splice_box', sa.Boolean))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('inverters', 'splice_box')
|
||||
37
db/versions/3cb6ab91fdc2_update_inverter_models_enum.py
Normal file
37
db/versions/3cb6ab91fdc2_update_inverter_models_enum.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""Update Inverter models enum
|
||||
|
||||
Revision ID: 3cb6ab91fdc2
|
||||
Revises: f90d04c490dc
|
||||
Create Date: 2017-06-26 12:41:17.882779
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '3cb6ab91fdc2'
|
||||
down_revision = 'f90d04c490dc'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
from alembic import op
|
||||
from helix.constants.inverter_type import InverterType
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.execute('COMMIT')
|
||||
|
||||
connection = None
|
||||
if not op.get_context().as_sql:
|
||||
connection = op.get_bind()
|
||||
connection.execution_options(isolation_level='AUTOCOMMIT')
|
||||
|
||||
op.execute('''ALTER TYPE "InverterType" ADD VALUE IF NOT EXISTS \'%s\'''' % (str(InverterType.DELTA.MODEL_36KW.value), ))
|
||||
op.execute('''ALTER TYPE "InverterType" ADD VALUE IF NOT EXISTS \'%s\'''' % (str(InverterType.DELTA.MODEL_42KW.value), ))
|
||||
op.execute('''ALTER TYPE "InverterType" ADD VALUE IF NOT EXISTS \'%s\'''' % (str(InverterType.DELTA.MODEL_60KW.value), ))
|
||||
op.execute('''ALTER TYPE "InverterType" ADD VALUE IF NOT EXISTS \'%s\'''' % (str(InverterType.DELTA.MODEL_80KW.value), ))
|
||||
|
||||
if connection is not None:
|
||||
connection.execution_options(isolation_level='READ_COMMITTED')
|
||||
|
||||
|
||||
def downgrade():
|
||||
pass
|
||||
28
db/versions/72342d883290_add_file_names_to_site.py
Normal file
28
db/versions/72342d883290_add_file_names_to_site.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""add_file_names_to_site
|
||||
|
||||
Revision ID: 72342d883290
|
||||
Revises: 00817cda9d17
|
||||
Create Date: 2016-10-06 16:56:41.943103
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '72342d883290'
|
||||
down_revision = '00817cda9d17'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column('sites', sa.Column('cad_file_name', sa.Unicode))
|
||||
op.add_column('sites', sa.Column('dxf_file', sa.Unicode))
|
||||
op.add_column('sites', sa.Column('dxf_file_name', sa.Unicode))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('sites', 'cad_file_name')
|
||||
op.drop_column('sites', 'dxf_file')
|
||||
op.drop_column('sites', 'dxf_file_name')
|
||||
56
db/versions/a904d0d1e1a7_create_initial_tables.py
Normal file
56
db/versions/a904d0d1e1a7_create_initial_tables.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""create initial tables
|
||||
|
||||
Revision ID: a904d0d1e1a7
|
||||
Revises:
|
||||
Create Date: 2016-08-31 11:16:58.286054
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'a904d0d1e1a7'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'users',
|
||||
sa.Column('id', sa.Integer, primary_key=True),
|
||||
sa.Column('username', sa.Unicode, nullable=False),
|
||||
sa.Column('password_hash', sa.Unicode, nullable=False)
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'sites',
|
||||
sa.Column('id', sa.Integer, primary_key=True),
|
||||
sa.Column('user_id', sa.Integer, sa.ForeignKey('users.id')),
|
||||
sa.Column('project_name', sa.Unicode, nullable=False),
|
||||
sa.Column('building_height', sa.Float, nullable=False),
|
||||
sa.Column('building_width', sa.Float, nullable=False),
|
||||
sa.Column('building_length', sa.Float, nullable=False),
|
||||
sa.Column('parapet_height', sa.Float, nullable=False),
|
||||
sa.Column('wind_speed', sa.Integer, nullable=False),
|
||||
sa.Column('exposure_category', sa.Unicode, nullable=False),
|
||||
sa.Column('exposure_transition_distance', sa.Integer),
|
||||
sa.Column('ballast_block_weight', sa.Float, nullable=False),
|
||||
sa.Column('max_psf', sa.Float, nullable=False),
|
||||
sa.Column('system_type', sa.Enum('0', '1', name='SystemType'), nullable=False),
|
||||
sa.Column('module_type', sa.Enum('96 Cell', '128 Cell', 'P-Series', name='ModuleType'), nullable=False),
|
||||
sa.Column('anchor_type', sa.Enum('OMG PowerGrip', 'OMG PowerGrip Plus', 'EcoFasten Eco 65', name='AnchorType'), nullable=False),
|
||||
sa.Column('spectral_response', sa.Float, nullable=False),
|
||||
sa.Column('seismic_importance_factor', sa.Float, nullable=False),
|
||||
sa.Column('cad_file', sa.Unicode),
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('sites')
|
||||
op.drop_table('users')
|
||||
sa.Enum(name='SystemType').drop(op.get_bind(), checkfirst=False)
|
||||
sa.Enum(name='ModuleType').drop(op.get_bind(), checkfirst=False)
|
||||
sa.Enum(name='AnchorType').drop(op.get_bind(), checkfirst=False)
|
||||
|
||||
39
db/versions/ed4c4bd22d6a_add_cascade_delete_for_inverter.py
Normal file
39
db/versions/ed4c4bd22d6a_add_cascade_delete_for_inverter.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""add cascade delete for inverter
|
||||
|
||||
Revision ID: ed4c4bd22d6a
|
||||
Revises: 3cb6ab91fdc2
|
||||
Create Date: 2017-06-30 11:10:24.762958
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'ed4c4bd22d6a'
|
||||
down_revision = '3cb6ab91fdc2'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.drop_constraint('inverters_standalone_inverter_id_fkey', 'inverters', type_='foreignkey')
|
||||
op.create_foreign_key(
|
||||
'inverters_standalone_inverter_id_fkey',
|
||||
'inverters',
|
||||
'standalone_inverters',
|
||||
['standalone_inverter_id'],
|
||||
['id'],
|
||||
ondelete='CASCADE'
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_constraint('inverters_standalone_inverter_id_fkey', 'inverters', type_='foreignkey')
|
||||
op.create_foreign_key(
|
||||
'inverters_standalone_inverter_id_fkey',
|
||||
'inverters',
|
||||
'standalone_inverters',
|
||||
['standalone_inverter_id'],
|
||||
['id']
|
||||
)
|
||||
30
db/versions/f90d04c490dc_add_inverter_brand_table.py
Normal file
30
db/versions/f90d04c490dc_add_inverter_brand_table.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Add Inverter Brand table
|
||||
|
||||
Revision ID: f90d04c490dc
|
||||
Revises: 72342d883290
|
||||
Create Date: 2017-06-22 15:58:21.358210
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'f90d04c490dc'
|
||||
down_revision = '72342d883290'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'inverter_brands',
|
||||
sa.Column('id', sa.Integer, primary_key=True),
|
||||
sa.Column('site_id', sa.Integer, sa.ForeignKey('sites.id'), primary_key=True),
|
||||
)
|
||||
pass
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('inverter_brands')
|
||||
pass
|
||||
Reference in New Issue
Block a user