How to migrate Flask-Sqlalchemy Models from another folder and multiple files using flask-migrate - flask-sqlalchemy

I'm new in flask. I'm using flask-sqlalchemy ORM, and want to migrate my models from another folder and multiple files using flask-migrate, but not able to migrate. This is my project structure.
Project Structure
Flask Project
├── config.py
├── migrations
├── API
│   ├── models
├── model1.py
├── model2.py
├── model3.py
├── resources
└── app.py
app.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] ="My_Database_Url"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
#app.route('/')
def hello():
return 'Hello, World!'
model1.py
from sqlalchemy.sql import func
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(100), nullable=False)
lastname = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(80), unique=True, nullable=False)
age = db.Column(db.Integer)
created_at = db.Column(db.DateTime(timezone=True),
server_default=func.now())
bio = db.Column(db.Text)
def __repr__(self):
return f'<Student {self.firstname}>'
In CMD : running this command
flask db migrate
output
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.env] No changes in schema detected.

Related

Token from Google Credentials Service Account NULL

I am attempting to retrieve the credentials for my Google service account with the following code:
package function
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.LambdaLogger
import com.amazonaws.services.lambda.runtime.RequestHandler
import pojo.Request
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.services.sqladmin.SQLAdminScopes
import pojo.Response
class GoogleAuth implements RequestHandler<Request, Response> {
private LambdaLogger logger
#Override
Response handleRequest(Request input, Context context) {
logger = context.getLogger()
ClassLoader classLoader = getClass().getClassLoader()
File jsonCredentials = new File(classLoader.getResource("leads-cloud-function-service-account.json").getFile())
FileInputStream fis = new FileInputStream(jsonCredentials)
GoogleCredential credential = GoogleCredential.fromStream(fis).createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))
credential.getClientAuthentication()
println("Thai test" + credential.getExpirationTimeMilliseconds())
print("This is the credential" + credential.getAccessToken())
return (new Response())
}
}
The information about the milliseconds for expiration and the AccessToken are null. Has anyone ever experienced this?
So I have resolved this myself by adding before the getToken the following command:
credential.refreshToken()
This is not properly documented in google's website but after adding it, this worked perfectly and the results are displayed with actual values.
Some other error I faced was the "com.google.common.io.ByteStreams.exhaust(Ljava/io/InputStream;)J", in order to resolve it I had to correct the dependency I was using to this one:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.31.1</version>
</dependency>
so, keep on mind that too!

Why in multiple connections to PricesResource Publisher, only one gets the stream?

It seems that only one http client gets the stream of data, while the others do not.
Is it true that the Publisher is hot data, and that it should broadcast to all subscribers?
Please find more in Can I allow multiple http clients to consume a Flowable stream of data with resteasy-rxjava2 / quarkus?
package org.acme.kafka;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import org.jboss.resteasy.annotations.SseElementType;
import org.reactivestreams.Publisher;
import io.smallrye.reactive.messaging.annotations.Channel;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static io.reactivex.Flowable.fromIterable;
/**
* A simple resource retrieving the "in-memory" "my-data-stream" and sending the items to a server sent event.
*/
#Path("/migrations")
public class StreamingResource {
private volatile Map<String, String> counterBySystemDate = new ConcurrentHashMap<>();
#Inject
#Channel("migrations")
Flowable<String> counters;
#GET
#Path("/stream")
#Produces(MediaType.SERVER_SENT_EVENTS) // denotes that server side events (SSE) will be produced
#SseElementType("text/plain") // denotes that the contained data, within this SSE, is just regular text/plain data
public Publisher<String> stream() {
Flowable<String> mainStream = counters.doOnNext(dateSystemToCount -> {
String key = dateSystemToCount.substring(0, dateSystemToCount.lastIndexOf("_"));
counterBySystemDate.put(key, dateSystemToCount);
});
return fromIterable(counterBySystemDate.values().stream().sorted().collect(Collectors.toList()))
.concatWith(mainStream)
.onBackpressureLatest();
}
}
You may use Replay operator or ConnectableObservable
What I did then was to inject the messages coming on the Flowable into a PublishSubject pipe and apply a backpressure strategy, thus offering a broadcast downstream.

How to expose swagger UI with http4k?

I'm building a microservice with the http4k framework using their Contract APIs. I can easily expose the swagger API description JSON on eg. /swagger.json with
fun app(): HttpHandler = "/" bind contract {
renderer = OpenApi3(ApiInfo("GoOut Locations API", "1.0"), Jackson)
descriptionPath = "/swagger.json"
routes += ...
}
Is there an easy way to expose the swagger UI so that 1) I can specify the path it will be available on. (eg. /swagger-ui) 2) The UI will be preconfigured to fetch the description JSON from the descriptionPath specified above.
An ideal API would look something like
fun app(): HttpHandler = "/" bind contract {
renderer = OpenApi3(ApiInfo("GoOut Locations API", "1.0"), Jackson)
descriptionPath = "/swagger.json"
uiPath = "/swagger-ui"
routes += ...
}
After a bit of searching I achieved this with combination of Web Jars and http4k's static routing.
The potential viewer of the docs must simply visit /docs path where he gets redirected to /docs/index.html?url=<path to Api description> where
index.html is a static Swagger UI entrypoint served from a web jar.
url query param tells the swagger UI where to fetch the OpenApi description from.
From the DX perspective we have a simple http4k application:
// path the OpenApi description will be exposed on
private const val API_DESCRIPTION_PATH = "/swagger.json"
fun app(): HttpHandler {
val api = contract {
renderer = OpenApi3(ApiInfo("Your API summary", "1.0"), Jackson)
descriptionPath = API_DESCRIPTION_PATH
// the actual API routes
routes += ...
}
return routes(
// the docs routes are not considered part of the API so we define them outside of the contract
swaggerUi(API_DESCRIPTION_PATH),
api
)
}
The swaggerUi handler implementation follows
/**
* Exposes Swagger UI with /docs path as its entry point.
* #param descriptionPath absolute path to API description JSON. The UI will be configured to fetch it after load.
*/
fun swaggerUi(descriptionPath: String): RoutingHttpHandler = routes(
"docs" bind Method.GET to {
Response(Status.FOUND).header("Location", "/docs/index.html?url=$descriptionPath")
},
// For some reason the static handler does not work without "/" path prefix.
"/docs" bind static(Classpath("META-INF/resources/webjars/swagger-ui/3.25.2"))
)
We also have to include the swagger-ui webjar as our dependency. Here's a Gradle directive:
implementation 'org.webjars:swagger-ui:3.25.2'
See the webjars website for Maven (and more) directives.
Note that the swaggerUi handler assumes its bound to the / root path of the whole service. However, that can be easily fixed.
As of http4k 4.28.1.0, there is now a way to do this. See the following code taken from this documentation page:
package guide.howto.create_a_swagger_ui
import org.http4k.contract.contract
import org.http4k.contract.meta
import org.http4k.contract.openapi.ApiInfo
import org.http4k.contract.openapi.v3.OpenApi3
import org.http4k.contract.ui.swaggerUi
import org.http4k.core.Body
import org.http4k.core.ContentType
import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.Uri
import org.http4k.core.with
import org.http4k.lens.string
import org.http4k.routing.routes
import org.http4k.server.SunHttp
import org.http4k.server.asServer
fun main() {
val greetingLens = Body.string(ContentType.TEXT_PLAIN).toLens()
// Define a single http route for our contract
val helloHandler = "/v1/hello" meta {
operationId = "v1Hello"
summary = "Say Hello"
returning(OK, greetingLens to "Sample Greeting")
} bindContract GET to { _: Request ->
Response(OK).with(greetingLens of "HI!")
}
// Define a contract, and render an OpenApi 3 spec at "/spec"
val v1Api = contract {
routes += helloHandler
renderer = OpenApi3(
ApiInfo("Hello Server - Developer UI", "99.3.4")
)
descriptionPath = "spec"
}
// Build a Swagger UI based on the OpenApi spec defined at "/spec"
val ui = swaggerUi(
Uri.of("spec"),
title = "Hello Server",
displayOperationId = true
)
// Combine our api, spec, and ui; then start a server
// The Swagger UI is available on the root "/" path
routes(v1Api, ui)
.asServer(SunHttp(8080))
.start()
.block()
}
The solution using the webjar does not work anymore for SwaggerUI version >= 4.1.3 as the url parameter is ignored (see this issue / the release notes). The URL has to be either specified in the HTML or the url parameter needs to be enabled in the HTML.
So for now the solution seems to be to unpack the UI, update index.html, and serve directly rather through the webjar.
http4k doesn't ship with a version of the OpenApi UI. You can easily ship a version of the UI though by:
unpacking the OpenApi UI into the src/main/resources/public folder
Using a static routing block to server the resources. There is an example of that here: https://github.com/http4k/http4k-by-example/blob/22dcc9a83c497253c29830d5bc981afa5fbbe4ff/src/main/kotlin/verysecuresystems/SecuritySystem.kt#L61

Terraform use backend on module

I need to create optimize the structure of terraform.
Have on root path variables which I imported like module
/variables.tf
variable "aws_profile" { default = "default" }
variable "aws_region" { default = "us-east-1" }
After have a module folder
/ec2_instance/main.tf
module "global_vars" {
source = "../"
}
provider "aws" {
region = module.global_vars.aws_region
profile = module.global_vars.aws_profile
}
terraform {
backend "s3" {
encrypt = true
bucket = "some_bucket"
key = "path_to_statefile/terraform.tfstate"
region = "region"
profile = "profile"
}
}
module "instances_cluster" {
some actions
}
It's working, but I need to move backend and provider part to main.tf on root folder and after include like the module.
How I can do this?
I have tried to create /main.tf on root folder with backend part, but they are not working and backed writing state files locally.
You'd have to a bit of refactoring but these are the steps I would take
Run terraform plan in root and ec2_instance modules to verify zero changes so refactoring can begin
Comment out the backend for ec2_instance/main.tf
Place the backend from ec2_instance/main.tf into root main.tf
In the root main.tf, make a reference to ec2_instance module
Run terraform plan in root module and note the creations and deletions
For each creations and deletion pair, create a terraform state mv statement and run each
Verify the terraform plan has zero changes

Run Integration Tests agains a running Server

How to run Grails integration tests against a running server?
I've searched the web but the advices date to 2010 and Grail 1.3.x...
Grails 3 Integration test will run against a running server. You need to define a test environment application.yml and add the #Integration annotation to your class.
import grails.test.mixin.integration.Integration
import spock.lang.Specification
#Integration
class LookupServiceIntegrationSpec extends Specification {
def lookupService
def setup() {
}
def cleanup() {
}
void "test database connection"() {
when:
def row = lookupService.testMethod()
then:
println("Row one = "+row.one)
row.one == 1
}
}
Here is a simple example service:
import grails.transaction.Transactional
import groovy.sql.Sql
#Transactional
class LookupService {
def dataSource
def testMethod() {
Sql sql = new Sql(dataSource)
sql.firstRow("SELECT 1 AS one")
}
}
Maybe your looking for Functional Tests not Integration Tests. Grails 3 added better support for Functional Tests. See Link Below.
Grails 3 Functional Tests