Convert Ruby ActiveRecord Schema to SQL syntax - sql

I haven't used Ruby much but I am trying to learn. I found a 'schema.db' file that looks like it is generated by Active Record module.
I am trying to figure out how do I convert this file into SQL so I can import it into my database?

Active Record does generate schema.rb and it represents the current state of the database (useful for getting a quick idea of what attributes each model has).
It is used when deploying new instances of an app, rather than replaying the whole migration history (and is also how the test database is created)
As an example, here's how you can deploy the production database:
RAILS_ENV=production rake db:create db:load

Related

There is already an object named AspNetRoles in the database. (entity-framework-core)

I have an asp.net core mvc website using entity framework core.
I don't know how it initially happened, but I can't get past the error:
"There is already an object named AspNetRoles in the database"
I ended up deleted the database, deleting all my migration .cs files and starting from scratch.
I've then tried
add-migration MyInitialMigration
same error
I then deleted the database again, and tried a direct
update-database
(without adding a migration)
still the same error
I tried changing the appsettings config string to point to a new, non-existing database.
Still the same error!
Previously on MVC5/EF6 I've encountered this error and have resolved it using the same method as this answer:
There is already an object named in the database
-Add-Migration Initial -IgnoreChanges
However -IgnoreChanges is unsupported in EFCore
Or previously on EF6 I've resolved it by adding a migration, then deleting everything in the Up/Down and running an empty migration. This has not worked on EFCore
How can I continue? I've checked the migration table and they're always empty even though it's created the whole database each time. Just doesn't seemed to have logged it in the migrations table
Found my issue.
I had followed this tutorial to load appsettings from EntityFramework:
https://docs.asp.net/en/latest/fundamentals/configuration.html#example-entity-framework-settings
In the EntityFrameworkConfigurationProvider there is a public override void Load() which has a dbContext.Database.EnsureCreated(); and a CreateAndSaveDefaultValues
Between the two of those it looks like it was initializing the database without tracking the migration.
I commented those out, ran a migration and it worked fine. Hopefully I don't have to comment them out every time I want to run a migration :/
This error happened after I changed my database (I stopped using MSSQLLocalDB and started using Sql Server from the production table).
Here is what I did to make it work :
Deleting the migrations folder
Commenting the last changes I wanted to apply
Calling the Add-Migration command : Add-Migration InitialDbChange
Commenting all the commands inside the Up method of the migration file created (keeping only the content of the Down method)
Calling the update-database command : update-database
I have a now a clean Migration from my database. Now my futur changes will be from this baseline.
Found the solution from this link : https://cmatskas.com/ef-core-migrations-with-existing-database-schema-and-data/
I've just had the same issue, and seems like Database.EnsureCreated() doesn't use migrations, you can use Database.Migrate() instead which is what I've just tried and seems to work, but time will tell when I try another migration.
If you're like me and you pull a copy of your production database to work on in development, make sure all of the migrations in your Migration folder are listed in the __EFMigrationsHistory table of your database. If you move the schema to the production database after your development is complete, you're probably not moving the migrations from that table over to your production database. If any of the migrations in your project are missing from that table, EF will try to update the database with any of the missing migrations. In my case, none of the migrations were listed in the database table, so it started by trying to add the initial migration. After adding all of the migrations to the table (except the latest one that I wanted to update the database), EF updated the database correctly.

Entity Framework DB Migration Script

Okay, so this is my situation. I have generated POCOs from a db and developed around them. I have also renamed and changed some of these POCOs to be more readable. I have now made changes to my original db scheme and need to migrate the changes back to my models without overriding my changes except where the scheme has changed. It there a way to generate a *.sql script to hand to by DBAs? If so, it there a way to compare two dbs and generate a change script? ie - dev db => prod db.
You can switch a project to code first and generate the scripts you need via migrations. See the link below for a guide on moving from db first to code first, but it sounds like you may be partially there already.
1) enable-migrations for the project with your context if you haven't already.
2) create a baseline migration. EF will use this as a starting point so you won't get a bunch of code to create the objects that already exist. The ignore changes flag tells EF not to create the existing objects. https://msdn.microsoft.com/en-us/data/dn579398.aspx?f=255&MSPPError=-2147217396#option1
create-migration InitialCodeFirst -IgnoreChanges
3) Now modify your schema as you normally would and create a migration:
add-migration SomeNewThing
4) Create a script for a different database (like PROD) by using -Script. This will not update your database it just creates a script, so I usually run it a second time without -Script:
update-database -Script // creates a script (in VS), but does not apply
update-database // updates the database your connect string points to
5) DBA runs script and this will add a record to __MigrationHistory to identify it as being applied.
http://devgush.com/2014/02/24/migrating-a-project-from-database-first-to-code-first/
Here is a useful link on deployment: http://cpratt.co/migrating-production-database-with-entity-framework-code-first/#at_pco=smlwn-1.0&at_si=54ad5c7b61c48943&at_ab=per-12&at_pos=0&at_tot=1

Recover failed sonar upgrade (missing table)

Due a backup issue the migration from sonar 3.5.1 to 3.7 got messed up a bit. Now some tables are missing but the migration is done.
Is there some way I can rerun the db migration to create the missing tables ?
Note I have so far only seen one problem and it shows in the log as:
MySQLSyntaxErrorException: Table 'sonar.issue_filters' doesn't exist
when I show the issues page or the issues drilldown. And I see that table is created in war/sonar-server/WEB-INF/db/migrate/411_create_issue_filters.rb
So based on the info there it seems I could create that one manually directly in sql but is there a better safe way to recover this migration ? (as I suspect the issue_filters is not the only problem)
Using MySQL for the db.

Rails: copy data from production MySQL into development SQLite3

I'm having trouble copying data from a production MySQL server to a development SQLite3 file (so that I have real data to play with on development machine). I've found tons of resources around the 'net on how to convert from MySQL to SQLite3, most of which were bash scripts with elaborate sed filters, but none worked (the most common problem was syntax issues upon import).
Anyway, then I stumbled upon YamlDB, and I thought "Why, of course! Let Rails do the conversion for me!" Well, this doesn't work either because all of the NULL fields (which are represented in the YAML file as !!null) end up being imported into the SQLite3 database exactly as "--- !!null" instead of actual NULLs. I seem to be the only person with this problem, as there were no mentions of it in the GitHub issues queue.
I even tried the workaround for using syck instead of psych (found in this SO question), but it made no difference.
So my question is this: does ANYONE know of a SIMPLE way to export data from one rails database for importing into another, regardless of database kind? And by "simple", I mean a few commands at the console, or whatever.
Look into taps # http://github.com/ricardochimal/taps
It will dump your MySQL db into a local sqlite db and is relatively simple to use.
From the comments: If you get an error stating schema parsing returned no columns, table probably doesn't exist then you need to specify an absolute path to the sqlite3 db instead of a relative one

Flyway - drop db, create db

I have 2 files: db structure, and db data sql files.
(we are not in part of project where we are creating alter files yet).
Is there a way to achieve running these files before build each time if any of them changed using maven?
The only idea I have is to rename the files (bring the version up) after each change to them making sure flyaway will pick it up.
Is there a smarter way, perhaps using API or some other 'trick'?
Thanks
--MB
You can use flyway migrate with the following configuration
validationMode=ALL
validationErrorMode=CLEAN
For each sql migration a CRC32 checksum is calculated when the sql script is executed. The validate mechanism checks if the sql migrations in the classpath still has the same checksum as the sql migration already executed in the database.
validationErrorMode=CLEAN is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM.
This configuration can be used from API, maven plugin or command line.
With newer Flyway, you would use
flyway.setInitOnMigrate(true);
flyway.setCleanOnValidationError(true);
flyway.setValidateOnMigrate(true);
To drop everything if the checksum of the file changes or the version table is missing.