No proper documentation on how to migrate the existing DB in SQLDelight - database-migration

In some of the stack overflow answers say
1.create new table and drop old tables. how to do that?
2.what is the use of sqm file and when we need to use?
3.what's is the use of deriveSchemaFromMigrations flag while declaring the sqllight db in build.gradle

Related

Create function in database using django ORM

In my project I want to get people who have birthday between some days, I hope to find a solution which does not force any limitations to queries.
I have found this solution which seems efficient and suite for my problem. But now I have a second problem to create the function in database using django ORM, because this must be portable and works with test database also. I could not find any proper way to able to define the function and the index based on it in django.
In brief I want to create below function in database using django:
CREATE OR REPLACE FUNCTION indexable_month_day(date) RETURNS TEXT as $BODY$
SELECT to_char($1, 'MM-DD');
$BODY$ language 'sql' IMMUTABLE STRICT;
CREATE INDEX person_birthday_idx ON people (indexable_month_day(dob));
To answer your question, using RunSQL you can insert raw SQL into a migration
-- it looks like you should be able to put this raw SQL into a migration file, including the function that would create the custom index. So running the migration would create the custom in
But don't do this -- you should just use Django to index the dob field, i.e.
dob = models.DateField(db_index=True)
and use Django to write your queries as well.

Django: how do i create a model dynamically

How do I create a model dynamically upon uploading a csv file? I have done the part where it can read the csv file.
This doc explains very well how to dynamically create models at runtime in django. It also links to an example of doing so.
However, as you will see after looking at the document, it is quite complex and cumbersome to do this. I would not recommend doing this and believe it is quite likely you can determine a model ahead of time that is flexible enough to handle the CSV. This would be much better practice since dynamically changing the schema of your database as your application is running is a recipe for a ton of bugs in your code.
I understand that you want to create new schema's on the fly based on fields in the those in a CSV. While thats a valid use case and could be the absolute right call. I doubt it though - it lends itself to a data model for a single tenet SaaS application that could have goofy performance and migration issues.
I'd try using Mongo/ some other NoSQL solutions as others have mentioned. But a simpler approach may be a modified Star Schema implemented in SQL. In this case you create a dimensions tables that stores each header, then create an instance of each data element that has a foreign key to dimension and records the value of that dimension.
If you read the csv the psuedo code would look something like this:
for row in DictReader(file):
for k in row.keys():
try:
dim = Dimension.objects.get(name=k)
except:
dim = Dimension(name=k)
dim.save()
DimensionRecord(dimension=dim, value=row[k]
Obviously you could better handle reading the headers and error trapping if dimensions already exist, but this would be an example of how you could dynamically load variable headered CSV's into a SQL db.

Entity Framework 5 - Migrations and creating table from entity

I am using Entity Framework 5 RC and i have some code that require a specific table on the database. The entity is already created using Code-First.
a) Is there a way to tell EF to create the table if its not already created in the database ? If yes.. how ?
b) Also.. if the table already exist, can it handle entity changes such adding properties to the entity.. will it get reflected on the database ? (We still use the code-first approach here)
Thanks!
Use code first migrations (either automatic or code based). If you are adding table to existing database create initial migration first and than add your new entity. All links are for EF 4.3 but the usage is exactly the same in EF 5.
For reference for anyone else having this problem, I had the same problem and my solution is here
Entity Framework: Generating specific table if doesn't exist?

EF 4.3 Database Migrations - Is there a way to ignore errors?

Is there a way to ignore errors when running a manual migration?
We have client databases in different states and I need to get them all updated to the latest version.
The reason I ask about ignoring errors is because I just want to code my migration like this
public override void Up()
{
AddColumn("ClientUser", "LastSyncTime", c => c.Guid());
AddColumn("ClientUser", "FileTransferToken", c => c.Guid());
AddColumn("ClientUser", "DateFileTransferTokenIssued", c => c.DateTime());
}
but naturally and expectedly it will throw an exception where the column already exists.
No. It is not supposed use case for EF Migrations. Migration drives database from one defined state to another defined state. If you have database in different states you need multiple migrations each covering just part of the transition.
If you want to start to use migrations in existing project with multiple databases you should first move all your databases to the same state without migrations and start to use it as initial state after which all changes will be handled only through migrations. Otherwise you will have a lot of problems.
This doesn't answer your specific question, but it may be an answer for your problem.
Use the Database Project in VS 2010 to create a schema of your target database.
You can use this "Gold Standard" schema to compare your other databases that are in different states and produce a delta script to take it from its current schema to the target schema.
Once you are at a known state across your databases, then switch to the Database Migrations for the schema moving forward.
Keith

Subsonic 3 SimpleRepository NON Plural Table names?

Is it possible to use SubSonic 3's Simple Repository with non-plural table names? My DB already exists, the table names a re singular add I cannot change them.
Nope, it is hardcoded in the SubSonic's source. You can pull it down and trace the migration steps to see where the plural happens. I know, cause I wanted the same thing.
I was tinkering with modifying the source to make plurals optional via some parameter/config override or alike. But, I didn't get it completed (yet).
If your tables already exist then this is not the intended use of the Simple Repository model. The simple repository model is designed to generate the table structures for you using migrations.
If you are using a database that already exists then you would be better served using the T4 Templates as they also support the relationships between your tables.
Cheers,
Ed
With Subsonic 3.0.0.4 in the settings.ttinclude I removed the line;
AddSingularRule("s$", String.Empty);
which was down about 260 lines in the Inflector rules class. Didn't need to mess around with the subsonic source code.
HTH