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?
Related
I have developed a application with Grails earlier.Now as per new requirement there is a need to modify the existing domain classes as well as adding a new classes and changing / establishing new relationship between the as well.
Now the new requirement has been implemented and I am going to deploy to production environment. however, the DBA want a script change the production database DDL. DBA is not allowing the auto create / update of database schema while bootstrapping the application.
I know how to export DDL of for creating tables. but that script will drop tables which means all data will be lost.
But I don't know how to export DDL for DDL-update (no drop tables/recreate tables). Anybody has good suggestion ?
You can not expect the existing data to get stored according to the new database schema as is.
For example, you have a table Sample with the contactNumber field with the nullable : true constraint in your existing schema and in your new schema this constraint has been changed to nullable : true & unique : true.
In such cases database will fail to keep the existing data intact or adapt to new schema.
To preserve the existing data, you may have to go through a tedious process like -
Take backup of the existing database.
Make a note of the modification you have made to the existing Domain classes.
Find out which modifications may lead to failure / data loss.
Drop the earlier database schema & Deploy the new application and let it create the database schema.
Write a script or utility which will process & store the data from database backup according to new database schema.Make sure the utility you have written has the capalibility to handle the modification(constraint, field added, field removed) done to the database schema.
I have a form which will be uploading a CSV file with table name. Using this table name i have to create a table in Postgresql and then i have move the data in CSV to that table.
As i understood Ruby on Rails works with model(related to a table) to write data. But when i create a table dynamically i should also create a model. But I Have searched on internet for the solution but no success.
So Can i insert the data into the temporary table without model?
Thanks in advance.
I am using the active-record-reputation-system gem, and would like to update it to v 2.0.0.
The gem creates three tables, RS_Evaluations, RS_Reputation_Messages, and RS_Reputations. These three tables have been renamed for v 2.0.0, but they do not get updated in my database by simply bundle installing the v2.0.0 gem.
How can I go about updating these tables? Should I:
1) Update just the table names with the content of the columns intact? (To the best of my knowledge, the column names are unchanged with the update). If so, how can I do this?
2) Drop the older tables and create new correct tables? This is not ideal, but won't kill me, as I don't have a huge amount of data in the existing tables. Can I simply delete the older tables via my database viewer (ie. PGAdminIII)? Or do I need to migrate a file that drops the tables?
Input would be much appreciated!
UPDATE
The migration guide has a line about
Also, you need to update your database data as follow:
UPDATE rs_reputation_messages SET sender_type = 'ReputationSystem::Evaluation' WHERE sender_type = 'RSEvaluation'
How would I go about doing this? I am only familiar with updating the database through migration files. Is this an SQL call made directly to the database?
Hi I have a sqlite3 database full of data from my previous version of the web app which WASN'T written in rails. Im now rewriting the web app in rails from scratch. But Id like to use the data from my previous app in the new rails app. Whats the best way to accomplish this?
This is what Ive tried so far and it didn't work very well:
1) I created a new rails app
2) replaced my sqlite3 database in the new app with the sqlite3 database from the old app
3) Created a model with the same schema as the old DB.
4) changed the databse.yml file with the updated DB file details
5) un my model added the "establish_connection" method
6) I could thus get it to show me all the details of the old database in my browser #"index.html"
7) However, I ran into problems when wanting to insert/edit records into the DB. Since the DB did not have a primary key column for each row, it didnt work.
8) So I tried to do a migration to add a column with a primary key . It didnt work
9) suddenly a new development.sqlite3 database had shown up in the app and it was trying to add a primary key to the NEW DB.
10) So I just deleted the new DB that had popped up and after that the app wasn't working
11) Now I want to start from scratch and so my question:
"Whats the best way to import data from a previous non-rails app (in a sqlite3 DB format) into a new rails app"
You want to be working with 2 databases, the old and the new. So, your database.yml needs to have 2 connections in it. One should be your normal connection called development or production and the other should be called legacy. Fill out their different connection information. By default, your ActiveRecord models will pull from the one not called legacy.
Create a model called LegacyBase. In the model put establish_connection "legacy". Now create a directory at app/models/legacy. Put all of the models represent data from the old DB inside this directory. All of those models should extend from LegacyBase. This will mean they're all reading from the old DB.
Create your new models. Do you want them to have a primary key column? If not, see this answer. Create an ActiveRecord database table with no :id column?. They will by default have an id column, though and I recommend leaving it that way.
For each of the legacy models, write a method called to_model. In this method, write the code that creates a new object and populate it with the old data and save it. Something like this:
class OldUser < LegacyBase
establish_connection "legacy"
def to_model
User.create!(self.attributes)
end
end
You can do any logic that needs to happen to make the old data fit your new app. Call this method on all of the old records like OldUser.all.map(&:to_model).
Do this for all of the tables that you want to move over.
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.