<commit_before>#! /usr/bin/env python

# Copyright 2017 The Cockroach Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License. See the AUTHORS file
# for names of contributors.

from __future__ import print_function

# Import Flask code.
from flask import Flask, Response, request

# Import SQLAlchemy code.
from flask_sqlalchemy import SQLAlchemy
import sqlalchemy.orm

# This import isn't actually referenced directly below but is included to
# clearly indicate whether the CockroachDB dialect is missing.
import cockroachdb

from models import db, Customer, Order, Product

import argparse
from decimal import Decimal
import json
import logging
import os

DEFAULT_URL = "cockroachdb://root@localhost:26257/company_sqlalchemy?sslmode=disable"

# Setup and return Flask app.
def setup_app():
    # Setup Flask app.
    app = Flask(__name__)
    app.config.from_pyfile('server.cfg')
    app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("ADDR", DEFAULT_URL)
    app.debug = False

    # Disable log messages, which are outputted to stderr and treated as errors
    # by the test driver.
    if not app.debug:
        logging.getLogger('werkzeug').setLevel(logging.ERROR)

    # Initialize flask-sqlachemy.
    db.init_app(app)
    with app.test_request_context():
        db.create_all()

    return app

app = setup_app()


@app.route('/ping')
def ping():
    return 'python/sqlalchemy'


# The following functions respond to various HTTP routes, as described in the
# top-level README.md.

@app.route('/customer', methods=['GET'])
def get_customers():
    customers = [c.as_dict() for c in Customer.query.all()]<commit_msg>examples-orms: Run SQLAlchemy table setup in a retry loop<commit_after>#! /usr/bin/env python

# Copyright 2017 The Cockroach Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License. See the AUTHORS file
# for names of contributors.

from __future__ import print_function

# Import Flask code.
from flask import Flask, Response, request

# Import SQLAlchemy code.
from flask_sqlalchemy import SQLAlchemy
import sqlalchemy.orm

# Import CockroachDB utility code.
from cockroachdb.sqlalchemy import run_transaction

from models import db, Customer, Order, Product

import argparse
from decimal import Decimal
import json
import logging
import os

DEFAULT_URL = "cockroachdb://root@localhost:26257/company_sqlalchemy?sslmode=disable"

# Setup and return Flask app.
def setup_app():
    # Setup Flask app.
    app = Flask(__name__)
    app.config.from_pyfile('server.cfg')
    app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("ADDR", DEFAULT_URL)
    app.debug = False

    # Disable log messages, which are outputted to stderr and treated as errors
    # by the test driver.
    if not app.debug:
        logging.getLogger('werkzeug').setLevel(logging.ERROR)

    # Initialize flask-sqlachemy.
    db.init_app(app)
    # We need to create the application tables within a retry loop.
    # CockroachDB doesn't stop the world when running DDL statements,
    # so it's possible that they may fail due to transaction conflicts
    # when modifying the internal database descriptors.
    with app.test_request_context():
        sessionmaker = sqlalchemy.orm.sessionmaker(db.engine)
        run_transaction(sessionmaker, lambda _: db.create_all())

    return app

app = setup_app()


@app.route('/ping')
def ping():
    return 'python/sqlalchemy'


# The following functions respond to various HTTP routes, as described in the
# top-level README.md.

@app.route('/customer', methods=['GET'])
def get_customers():
    customers = [c.as_dict() for c in Customer.query.all()]
{'fraction_non_alphanumeric': 0.08282476024411509, 'fraction_numerical': 0.005666957279860506, 'mean_word_length': 4.115942028985507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7559987', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1296, 'n_words': 505}