Commit 2895bf96 authored by sim's avatar sim

[DB] Initial migration

NOTE: dbmigrate.py is made obsolete by Alembic usage, so get rid of it.
parent 5fe5bcd1
"""Initial migration
Revision ID: 932dbf3e8c43
Revises:
Create Date: 2017-07-05 16:41:23.951422
"""
from alembic import op
import sqlalchemy as sa
import gargantext
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '932dbf3e8c43'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('contacts',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user1_id', sa.Integer(), nullable=True),
sa.Column('user2_id', sa.Integer(), nullable=True),
sa.Column('is_blocked', sa.Boolean(), nullable=True),
sa.Column('date_creation', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['user1_id'], ['auth_user.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['user2_id'], ['auth_user.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('user1_id', 'user2_id')
)
op.create_table('ngrams',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('terms', sa.String(length=255), nullable=True),
sa.Column('n', sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('terms')
)
op.create_index('ngrams_id_n_idx', 'ngrams', ['id', 'n'], unique=False)
op.create_index('ngrams_n_idx', 'ngrams', ['n'], unique=False)
op.create_table('nodes',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('typename', gargantext.models.nodes.NodeType(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('parent_id', sa.Integer(), nullable=True),
sa.Column('name', sa.String(length=255), nullable=True),
sa.Column('date', sa.DateTime(), nullable=True),
sa.Column('hyperdata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.ForeignKeyConstraint(['parent_id'], ['nodes.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['user_id'], ['auth_user.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_nodes_typename'), 'nodes', ['typename'], unique=False)
op.create_index('nodes_hyperdata_idx', 'nodes', ['hyperdata'], unique=False)
op.create_index('nodes_user_id_typename_parent_id_idx', 'nodes', ['user_id', 'typename', 'parent_id'], unique=False)
op.create_table('nodes_hyperdata',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('node_id', sa.Integer(), nullable=True),
sa.Column('key', gargantext.models.hyperdata.HyperdataKey(), nullable=True),
sa.Column('value_int', sa.Integer(), nullable=True),
sa.Column('value_flt', postgresql.DOUBLE_PRECISION(), nullable=True),
sa.Column('value_utc', sa.DateTime(timezone=True), nullable=True),
sa.Column('value_str', sa.String(length=255), nullable=True),
sa.Column('value_txt', sa.Text(), nullable=True),
sa.ForeignKeyConstraint(['node_id'], ['nodes.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_nodes_hyperdata_value_flt'), 'nodes_hyperdata', ['value_flt'], unique=False)
op.create_index(op.f('ix_nodes_hyperdata_value_int'), 'nodes_hyperdata', ['value_int'], unique=False)
op.create_index(op.f('ix_nodes_hyperdata_value_str'), 'nodes_hyperdata', ['value_str'], unique=False)
op.create_index(op.f('ix_nodes_hyperdata_value_utc'), 'nodes_hyperdata', ['value_utc'], unique=False)
op.create_index('nodes_hyperdata_node_id_key_idx', 'nodes_hyperdata', ['node_id', 'key'], unique=False)
op.create_index('nodes_hyperdata_node_id_key_value_flt_idx', 'nodes_hyperdata', ['node_id', 'key', 'value_flt'], unique=False)
op.create_index('nodes_hyperdata_node_id_key_value_int_idx', 'nodes_hyperdata', ['node_id', 'key', 'value_int'], unique=False)
op.create_index('nodes_hyperdata_node_id_key_value_str_idx', 'nodes_hyperdata', ['node_id', 'key', 'value_str'], unique=False)
op.create_index('nodes_hyperdata_node_id_key_value_utc_idx', 'nodes_hyperdata', ['node_id', 'key', 'value_utc'], unique=False)
op.create_index('nodes_hyperdata_node_id_value_utc_idx', 'nodes_hyperdata', ['node_id', 'value_utc'], unique=False)
op.create_table('nodes_ngrams',
sa.Column('node_id', sa.Integer(), nullable=False),
sa.Column('ngram_id', sa.Integer(), nullable=False),
sa.Column('weight', sa.Float(), nullable=True),
sa.ForeignKeyConstraint(['ngram_id'], ['ngrams.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['node_id'], ['nodes.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('node_id', 'ngram_id')
)
op.create_index('nodes_ngrams_ngram_id_idx', 'nodes_ngrams', ['ngram_id'], unique=False)
op.create_index('nodes_ngrams_node_id_idx', 'nodes_ngrams', ['node_id'], unique=False)
op.create_index('nodes_ngrams_node_id_ngram_id_idx', 'nodes_ngrams', ['node_id', 'ngram_id'], unique=False)
op.create_table('nodes_ngrams_ngrams',
sa.Column('node_id', sa.Integer(), nullable=False),
sa.Column('ngram1_id', sa.Integer(), nullable=False),
sa.Column('ngram2_id', sa.Integer(), nullable=False),
sa.Column('weight', sa.Float(precision=24), nullable=True),
sa.ForeignKeyConstraint(['ngram1_id'], ['ngrams.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['ngram2_id'], ['ngrams.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['node_id'], ['nodes.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('node_id', 'ngram1_id', 'ngram2_id')
)
op.create_index('nodes_ngrams_ngrams_ngram1_id_idx', 'nodes_ngrams_ngrams', ['ngram1_id'], unique=False)
op.create_index('nodes_ngrams_ngrams_ngram2_id_idx', 'nodes_ngrams_ngrams', ['ngram2_id'], unique=False)
op.create_index('nodes_ngrams_ngrams_node_id_idx', 'nodes_ngrams_ngrams', ['node_id'], unique=False)
op.create_index('nodes_ngrams_ngrams_node_id_ngram1_id_ngram2_id_idx', 'nodes_ngrams_ngrams', ['node_id', 'ngram1_id', 'ngram2_id'], unique=False)
op.create_table('nodes_nodes',
sa.Column('node1_id', sa.Integer(), nullable=False),
sa.Column('node2_id', sa.Integer(), nullable=False),
sa.Column('score', sa.Float(precision=24), nullable=True),
sa.ForeignKeyConstraint(['node1_id'], ['nodes.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['node2_id'], ['nodes.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('node1_id', 'node2_id')
)
op.create_index('nodes_nodes_node1_id_node2_id_idx', 'nodes_nodes', ['node1_id', 'node2_id'], unique=False)
op.create_table('nodes_nodes_ngrams',
sa.Column('node1_id', sa.Integer(), nullable=False),
sa.Column('node2_id', sa.Integer(), nullable=False),
sa.Column('ngram_id', sa.Integer(), nullable=False),
sa.Column('score', sa.Float(precision=24), nullable=True),
sa.ForeignKeyConstraint(['ngram_id'], ['ngrams.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['node1_id'], ['nodes.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['node2_id'], ['nodes.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('node1_id', 'node2_id', 'ngram_id')
)
op.create_index('nodes_nodes_ngrams_node1_id_idx', 'nodes_nodes_ngrams', ['node1_id'], unique=False)
op.create_index('nodes_nodes_ngrams_node2_id_idx', 'nodes_nodes_ngrams', ['node2_id'], unique=False)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index('nodes_nodes_ngrams_node2_id_idx', table_name='nodes_nodes_ngrams')
op.drop_index('nodes_nodes_ngrams_node1_id_idx', table_name='nodes_nodes_ngrams')
op.drop_table('nodes_nodes_ngrams')
op.drop_index('nodes_nodes_node1_id_node2_id_idx', table_name='nodes_nodes')
op.drop_table('nodes_nodes')
op.drop_index('nodes_ngrams_ngrams_node_id_ngram1_id_ngram2_id_idx', table_name='nodes_ngrams_ngrams')
op.drop_index('nodes_ngrams_ngrams_node_id_idx', table_name='nodes_ngrams_ngrams')
op.drop_index('nodes_ngrams_ngrams_ngram2_id_idx', table_name='nodes_ngrams_ngrams')
op.drop_index('nodes_ngrams_ngrams_ngram1_id_idx', table_name='nodes_ngrams_ngrams')
op.drop_table('nodes_ngrams_ngrams')
op.drop_index('nodes_ngrams_node_id_ngram_id_idx', table_name='nodes_ngrams')
op.drop_index('nodes_ngrams_node_id_idx', table_name='nodes_ngrams')
op.drop_index('nodes_ngrams_ngram_id_idx', table_name='nodes_ngrams')
op.drop_table('nodes_ngrams')
op.drop_index('nodes_hyperdata_node_id_value_utc_idx', table_name='nodes_hyperdata')
op.drop_index('nodes_hyperdata_node_id_key_value_utc_idx', table_name='nodes_hyperdata')
op.drop_index('nodes_hyperdata_node_id_key_value_str_idx', table_name='nodes_hyperdata')
op.drop_index('nodes_hyperdata_node_id_key_value_int_idx', table_name='nodes_hyperdata')
op.drop_index('nodes_hyperdata_node_id_key_value_flt_idx', table_name='nodes_hyperdata')
op.drop_index('nodes_hyperdata_node_id_key_idx', table_name='nodes_hyperdata')
op.drop_index(op.f('ix_nodes_hyperdata_value_utc'), table_name='nodes_hyperdata')
op.drop_index(op.f('ix_nodes_hyperdata_value_str'), table_name='nodes_hyperdata')
op.drop_index(op.f('ix_nodes_hyperdata_value_int'), table_name='nodes_hyperdata')
op.drop_index(op.f('ix_nodes_hyperdata_value_flt'), table_name='nodes_hyperdata')
op.drop_table('nodes_hyperdata')
op.drop_index('nodes_user_id_typename_parent_id_idx', table_name='nodes')
op.drop_index('nodes_hyperdata_idx', table_name='nodes')
op.drop_index(op.f('ix_nodes_typename'), table_name='nodes')
op.drop_table('nodes')
op.drop_index('ngrams_n_idx', table_name='ngrams')
op.drop_index('ngrams_id_n_idx', table_name='ngrams')
op.drop_table('ngrams')
op.drop_table('contacts')
# ### end Alembic commands ###
#!/usr/bin/env python
import sys
import os
if __name__ == "__main__":
# Django settings
dirname = os.path.dirname(os.path.realpath(__file__))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gargantext.settings")
# initialize Django application
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# migrate SQLAlchemy models
from gargantext.models import Base
from gargantext.util.db import engine
sqla_models = (
Base.metadata.tables[model_name]
for model_name in Base.metadata.tables.keys()
)
print()
for model in sqla_models:
try:
model.create(engine)
print('created model: `%s`' % model)
except Exception as e:
print('could not create model: `%s`, %s' % (model, e))
print()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment