Any easy ways to automate creation of Yii DB migration code - sql

I have started using Yii (1.1) in order to keep syncronized db schemas between different environments.
My questions is: are there any easy ways to automate the writing of the Up() and Down() methods with SQL code?
At the moment, if I add a new table or alter an existing table I need to write manually these methods.
The command yiic migrate create table_name only creates an empty class.
Alternatively I could create the new migration file and use the $this->exexute('sql code here') property. But I was just wondering if there is any way for Yii to detect the db changes and create the migration code on it's own

Related

What's the recommended way to do database migrations with Ktor + Exposed (Kotlin)?

The Ktor or Exposed frameworks do not have any built-in support for database migrations. What's the recommended way to do this?
If you are using Ktor with Gradle I would recommend using Flyway programatically inside the entry point of your Application. This way it could easily be a part of your Continuous Delivery pipeline. You can see the Flyway docs using API here: https://flywaydb.org/documentation/api/
What I essentially do though is add the dependency (using Kotlin DSL):
implementation("org.flywaydb:flyway-core:6.5.2")
And then all you need to do is create an instance of Flyway and call migrate when you load your module:
fun Application.module() {
Flyway.configure().dataSource(/*config to your DB*/).load().migrate()
//the rest of your application
routing {
}
}
You could of course extract the creation of Flyway to your DI tool (e.g. Koin) and add some logging to show progress.
This way your DB will be migrated (if necessary) every time just before your app is started.
As for writing up the actual migration the official docs are very helpful. What you essentially need to do is:
Make sure you have the required directory for migration files - src/main/resources/db/migration by default ).
Write plain SQL in separate migration files in the above directory. The filenames need to stick to the convention too (by default: start with capital V then a number, which you will increment for each new migration, double underscore - this tricked me at the beginning ;) - and a snake_case description, e.g. V1__Create_person_table.sql)
Run the app and observe magic :D
Database tables are migrated at deployment via Flyway and scripts can be added in db/migrations folder to add new tables or execute queries like inserting data etc on server startup.
(https://github.com/arun0009/kotlin-ktor-exposed-sample-api)
Here's how Flyway works: https://flywaydb.org/getstarted/how
Download, install, and configure Flyway (See https://flywaydb.org/getstarted/firststeps/commandline)
Point it to a database, which will be the location of flyway_schema_history
Write your migration files for create table or insert data following their naming convention: V<version number>__<migration description> and run with flyway migrate
Write your repeatable migration files for create view following their naming convention: R__<migration description> and run with flyway migrate
Write your undo migration files for drop table or delete data following their naming convention: V<version number>__<migration description> and run with flyway migrate
Check your migration status with flyway info and commit your files if you're happy
Make any necessary modifications are rerun migration. Repeat and commit.
In case if this is a one time activity, you can try using a Off-the shelf utility like SQL Data Compare.
To make this happen, you would need to ensure that both the databases are accessible locally from your machine so that you can create 2 DB connections and run a comparison against them.
At the end of comparison, you can get a Auto-Generated SQL Script out of it to run against your new schema and make it sychronized.
In case if you wish to compare Schema Objects as well, again Red-Gate provides a similar Schema Compare tool, which they have now started to call as SQL Compare (God knows why !!). This utility would also provide similar auto-generated script to help you.
But, again Red Gate is good for one-time migration and you can use this with their Trial version for a period of 30 days. For similar activity in regular basis, you would need to buy the Licensed version of same software.
For Data Migrantion, I use Navicat Premium which i find very easy to use, but it is not an open source. If you are looking for an Open Source Tool, then You can use SQLines Data which is an open source (Apache License 2.0), scalable, parallel high performance data transfer and schema conversion tool that you can use for database migrations and ETL processes.
SQLines
It is available for Linux, Windows, both 64-bit and 32-bit platforms.
You can also use SQLines Data for cross-platform database migration. The tool migrates table definitions, constraints, indexes and transfers data.
This how you can start with SQLines:
Download and unzip the file, no installation is required
Run sqldataw.exe on Windows to launch the GUI version
Run ./sqldata on Linux to launch the command line tool.
And There are Migration guidelines available for specific databases.Guidelines

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

Run SQL Scripts and then update Entity Model in Code First set up

I have run into a situation. We are still in the development cycle and we have implemented Code First Model using EF6. Now, we have just received SQL scripts to add new tables as well as few more function scripts. Is there a way I can run those scripts with Entity Migrations? I am open for any suggestions.

create symfony1.2 existing project new module?

If i create new module in existing project the table is not create and display in command line Application frontend not exist message display
not sure i understand properly your problem; when you create a module, it doesn't create a table (i.e. a database table); it only creates a directory within the modules/ folder of your app (for example frontend/modules); within this directory, it creates an actions folder containing the actions.class.php file, as well as a templates module.
When you want to alter your database, it depends whether you're working with propel or doctrine.
For example, using Propel, what you do is :
you update your tables and columns definitions in config/schema.yml;
you run the following scripts :
symfony propel:build-model (creates the classes);
symfony propel:build-forms (creates the forms related);
symfony propel:build-filters (creates the filters);
symfony propel:build-sql (creates the new complete SQL file, with the new tables);
symfony propel:insert-sql (process the SQL file in your DB — Be aware that it overrides your actual database; if you already have data you wish to save, i recommend that you manually add the tables in your database).

Best practice to insert 20 tables from sql file to YII project to create models

I just try yii last version.
In doctrine the models generated easly by shell comand, I want to do the same with yii.
Edit:
maybe its something like?:
yiic migrate create *
Thanks in advance
Start gii, go to the Model Generator and type '*' in the table name.
Yii provides a graphical too called Gii to auto generate models, CRUD functionality, controllers and many other things.
You don't need to care about SQL code, just import all your tables in database using PHPMyAdmin or whatever tool you use. then configure the database settings in /protected/config/main.php. sample code is provided in same file.
Then enable Gii tool with these guidlines Automated code generation
then start with creating models. type in name of your table click preview and then generate, your are done.