Flyway does not seem to recognize java/scala migrations - scala

I am using Flyway version 5.2.4 in a scala project and have all my migration scripts are under src/main/resources/db/migrations with the following folder structure
main
-resources
--db
---migrations
----V1__example1.sql
----V2__example2.sql
----V3__scala_migration.scala
locations is set to db.migrations (Without any prefix. Flyway documentation says if no prefix is used then sql/java migrations are supported)
V1 and V2 seem to get picked up without issues. But V3 is being ignored. I tried adding a V3__java_migration.java as well and it made no difference. Did anyone have any kind luck adding non-sql migrations?
Here is the scala code in the migration
package db.migration
import org.flywaydb.core.api.migration.{ BaseJavaMigration, Context }
class V3__scala_migration extends BaseJavaMigration {
override def migrate(context: Context): Unit = {
val conn = context.getConnection
conn.createStatement().executeUpdate(
"""
|DROP TABLE IF EXISTS `users`;
|CREATE TABLE IF NOT EXISTS `users`
|`name` varchar(100) NOT NULL,
|`email` varchar(100) NOT NULL,
|PRIMARY KEY (`email`)
|)ENGINE=InnoDB DEFAULT CHARSET=utf8;
|INSERT INTO `users` (`name`, `email`) ('john','john#example.com');
""".stripMargin)
}
}

You must move your Scala or Java migration scripts to the according directories.
For Scala this would be src/main/scala/db/migration
See here the documentation: https://flywaydb.org/documentation/migrations#discovery-1

Related

Diesel Generate Schema Empty

I'm following the Diesel Getting Started Tutorial.
When I run the migrations the schema.rs file generated is empty.
I checked postgres and the database and table is created.
What am I doing wrong? I followed every step of the tutorial.
Edit: in the up.sql
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR NOT NULL,
body TEXT NOT NULL,
published BOOLEAN NOT NULL DEFAULT FALSE
)
Cargo.toml
[package]
name = "diesel_demo"
version = "0.1.0"
edition = "2021"
[dependencies]
diesel = { version = "2.0.0", features = ["postgres"] }
dotenvy = "0.15"
diesel.toml
[print_schema]
file = "src/schema.rs"
[migrations_directory]
dir = "migrations"
.env
DATABASE_URL=postgres://postgres:123#localhost/diesel_demo
Output of diesel migration run and diesel migration redo. I only get an output in the first time I ran diesel migration run.
Edit2: I solved by reeinstaling rust, postgresql and the c++ build tools. Thanks everyone for the help.

How can I fill in a table from a file when using flyway migration scripts

I have scripts
/*The extension is used to generate UUID*/
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- auto-generated definition
create table users
(
id uuid not null DEFAULT uuid_generate_v4 ()
constraint profile_pkey
primary key,
em varchar(255),
user varchar(255)
);
In IDE Intellij Idea (a project with Spring Boot):
src/main/resources/db-migration
src/main/resources/sql_scripts :
copy.sql
user.txt
I'm just trying to run a simple Sql command for now to see that everything works clearly
copy.sql
COPY profile FROM '/sql_scripts/user.txt'
USING DELIMITERS ',' WITH NULL AS '\null';
user.txt
'm#mai.com', 'sara'
's#yandex.ru', 'jacobs'
But when I run the copy command, I get an error
ERROR: could not open file...
Maybe who knows how it should work and what needs to be fixed ?
Strong possibility its a pathing issue; could you try, instead of
COPY profile FROM '/sql_scripts/user.txt'
doing
COPY profile FROM './sql_scripts/user.txt'
(or an absolute path)

Database dialect included in Alembic downgrade script, but not on upgrade script?

I am using alembic to generate database migration scripts for a mysql database. I've noticed that the syntax of the generated upgrade and downgrade scripts differ slightly, whereas I thought they would basically be the same.
models.py- before
class Message_User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), nullable=True)
models.py-after table modification
class Message_User(db.Model):
id = db.Column(db.Integer, primary_key=True)
tag = db.Column(db.String(15), nullable=True)
migration file - original - shows table creation
def upgrade():
op.create_table('message_user',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=20), nullable=True)
sa.PrimaryKeyConstraint('id', name=op.f('pk_message_user'))
)
def downgrade():
op.drop_table('message_user')
migration file - after - shows table modification
def upgrade():
op.add_column('message_user', sa.Column('tag', sa.String(length=15), nullable=True))
op.drop_column('message_user', 'name')
def downgrade():
op.add_column('message_user', sa.Column('name', mysql.VARCHAR(collation='utf8_bin',
length=20), nullable=True))
op.drop_column('message_user', 'tag')
The upgrade scripts describe the changes purely in sqlalchemy terms, whereas the downgrade scripts add mysql dialect specific changes. Specifically, the upgrade script defines the type as sa.String(length=15) whereas the downgrade defines it as mysql.VARCHAR(collation='utf8_bin', length=20). In create table statements in the downgrade scripts, the autogenerated script also includes mysql_collate, mysql_default_charset and mysql_engine whereas these aren't in create table statements for upgrade scripts. I didn't see any mention of this in the alembic documentation. Does anyone know why this differs?

ActiveRecord: "uuid-ossp" extension added, but no uuid functions available

Using rails-5.0.7.1 (according to bundle show rails)
I wrote a migration which adds the "uuid-ossp" extension, and the SQL gets executed, and the extension shows up when I type \dx in the psql console. However, the functions that this extension provides (such as uuid_generate_v4) do NOT show up when I type \df, and so any attempt to use the functions that should be added fails.
When I take the SQL from the ActiveRecord migration and copy+paste it into the psql console directly, everything works as expected - extension is added, and functions are available.
Here is my migration code:
class EnableUuidOssp < ActiveRecord::Migration[5.0]
def up
enable_extension "uuid-ossp"
end
def down
disable_extension "uuid-ossp"
end
end
Here is the output:
$ bundle exec rake db:migrate
== 20190308113821 EnableUuidOssp: migrating ==============================
-- enable_extension("uuid-ossp")
-> 0.0075s
== 20190308113821 EnableUuidOssp: migrated (0.0076s) =====================
^ this all appears to run successfully, but no functions are enabled. Which means future SQL that includes statements such as ... SET uuid = uuid_generate_v4() ... fail with the this error HINT: No function matches the given name and argument types. You might need to add explicit type casts.
What does work
Going directly into psql and typing:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
^ This installs the extension and makes the functions avaiable.
And yet, what doesn't work
Okay, so if I take the above SQL and rewrite my migration this way:
...
def up
execute <<~SQL
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SQL
end
...
^ this migration will run without error, yet it will still not make the functions available.
So, the same copy+paste SQL that works in psql doesn't work via the ActiveRecord execute method, which really puzzles me. I'm not sure what piece I'm missing that's causing this to fail.
I assume that the schema where the extension is installed is not on your search_path.
You can see that schema with
\dx "uuid-ossp"
Try to qualify the functions with the schema, like in public.uuid_generate_v4().

OrientDB 3 sql script error

I'am migrating from ODB 2.2.29 to 3.0.1 and I find an error that I have not been able to understand.
I have a class called 'EdgeAttrib'. The class does not exist yet in the DB.
Now consider this code:
let exist = select from (select expand(classes) from metadata:schema) where name = 'EdgeAttrib';
if ($exist.size()>0) {
delete vertex EdgeAttrib;
drop class EdgeAttrib;
}
work well in 2.2.29 and fail in 3.0.1. Even more, the same code for other class work fine.
When I run it, it throw:
com.orientechnologies.orient.core.exception.OCommandExecutionException: Class not found: EdgeAttrib DB name="Test"
That happend when try to execute the "delete vertex" line, but the class does not exist so it should never run that line.
I run this code in the ODB Studio.
The fix was added in version 3.0.3.