symfony migration create table and insert record - symfony1

I want to create a new table and then insert a specific record in a symfony 1.4 project. I can do the data insert manually, but I would like to make use of the migration infrastructure so that when different existing instances are upgraded this row will be created when the migrations are run.
Here is the sequence of migrations:
Create a new table email.
Insert a row into the table email.
(Note that these are two separate consecutive migrations.)
The problem is that in this migration there is no chance to generate the base model classes, so attempting to insert the row using the normal Doctrine commands will fail.
I could use a naked SQL INSERT commands, but that seems like an admission of defeat. Is there another, more Doctrine-friendly way to do the data insert?

There is a way to do migrations. Do the following :
Update your schema.yml to include the new table
Create a new task or fixture file with the new entry
Run symfony doctrine:generate-migrations-diff - this will create the migration file
Run symfony doctrine:build --all-classes --and-migrate
The last line will update the Database - ie create the new table and also create the base model classes. Here are the options (related to classes on the build command)
--all-classes Build all classes
--model Build model classes
--forms Build form classes
--filters Build filter classes
The just run the task to insert the new DB entry or use the new fixtures (using symfony doctrine:data-load --append <filename>) file to create the entry in the DB.

Related

Duplicate postgres schema

I'm working on multi-tenant system in which I need to work on different tenats. in some cases I need to create new schema which contains some tables and default data. for that I just want to duplicate or copy public schema with diff. name Is there any way to duplicate or copy it.
I did work around on this problem but I want a solution to copy schema not to create schema and regenrate table and data
Take a back up of the schema you want along with the data In a plain text SQL file using pgadmin. And then create new schema and execute the SQL file under the new schema.

Rails postgres sql view missing data when creating via migration

I've been speeding up some statistics reports in my rails(4.2.7, postgres 9.5 database) project via creating sql views.
I wrote all code in PGAdmin, checked all data in views. Then I generated migration in my rails project and copied all written sql code to migration.
When I run migration, all views are being created with randomly missing data.
Example:
create or replace view revenue_by_date as
select sum(reward) as reward_coins, count(ot.id) as amount, (sum(reward)::float / substring(value from '---\s''(\d+)''')::bigint) as reward_dollars,
ot.created_at::date as date
from offerwall_transactions ot
inner join settings on var = 'exchange_rate'
where ot.reward > 1
group by ot.created_at::date, settings.value;
I get reward_dollars data missing.
However, when creating via PGA data is ok
Settings table structure:
Offerwall transactions table structure:
I have same problems with other views. Some views are created with no data at all.
Where is a mistake?

Manually Editing an EF7 Migration Class & Snapshot

The EF7 migration add command (to date, beta5) compares the model classes defined in the DbContext against the current model snapshot, creates a new migration class, and updates the model snapshot.
I need to modify the migration to make it generate different DDL SQL. As an example, EF7 uses sequences for SQL Server auto-increment values, and I would like it to use identity. However it could be any other reason. The migration remove command would physically delete the migration files and revert the model snapshot, so it's useless in this case.
There are 3 files that contain related code that look like they need to be edited:
The primary migration class: The Up and Down methods should be modified.
The DbContextModelSnapshot file contains Annotations that need to be modified
The secondary migration partial class: The badly named [migration].Designer.cs file also contains a model snapshot for the migration. I'm assuming this snapshot needs to match the model snapshot in item 2, but am not certain. The only information I have about the purpose of it is from Brice's blog , which says "It’s there in case you or a provider need to inspect the model for additional information during a migration."
Specific questions:
Do the two model snapshots need to be kept in sync in order to correctly perform migrations?
Is modifying 3 separate files the only way to edit the migration? (Although depending on the changes, the model snapshots may not have to be touched in some cases.)
Is there some EF command that would regenerate just the model snapshots, but not the migration methods?
Specific answers:
Do the two model snapshots need to be kept in sync in order to correctly perform migrations?
No, the snapshot in the migration is a last resort for provider writers. For example, SQLite can't rename columns so it could use migration's model snapshot to do a table rebuild for this operation. 99% of the time, it won't ever be used.
Is modifying 3 separate files the only way to edit the migration?
Most of the time, you should only ever edit the main migration file. In rare cases, you may need to edit the model snapshot if you're working in a team environment and you encounter a merge conflict. You can ignore the designer file; it just captures some metadata about the migration.
You may not have to edit anything if you configure your model correctly. For example, to use identity instead of sequences, override DbContext.OnModelCreating() and add modelBuilder.ForSqlServer().UseIdentity().
Is there some EF command that would regenerate just the model snapshots, but not the migration methods?
No, it shouldn't be needed since you almost never edit these files.

Command to create database tables(with fields and relationship) based on model

Is there a way to specify the fields and relationships in the model and run a command to the rails create tables in the database based on the models, and then I can create or remove other fielda, change some relationships and run this command again and rails do update the tables in the database? I thought the migrate did it but after I read about it seems that it only creates the tables once and has no concept of model / db sync that i need.
No you need to create a new migration anytime you need to update the database. Changing some code in the model will not generate a migration.
No, but if you want a simpler way to write your migrations etc, I suggest you read more about the generators :)
For example :
rails generate model user first_name:string last_name:string birth_date:datetime friend:references
will directly generate the migration with the correct fields :)
And if you want to add a field after, you can write it like :
rails generate migration AddPseudoToUsers pseudo:string
And it will be correctly written normally :)
This is the doc
http://guides.rubyonrails.org/migrations.html#creating-a-migration
http://guides.rubyonrails.org/command_line.html#rails-generate

updating the model based on the new database changes

I created a model "user" using the generate scaffold user command line. Next I made some changes to the database using generate migration command line and then updated the database using rake db:migration . Now I want to make my model in sync with the database fields automatically. Is it possible through a command line and if yes, how ? If not, what are the other options.
thanks
Your application find automacaly the change in your database if you don't specify a attr_accessible in your model. It make SQL requests to see all fields in the table. It's slower but simpler.

Resources