Doctrine: ignore new columns before mapping is deployed - doctrine-orm

I run into issues when I add a new column to an existing table mapped by Doctrine. I usually update the schema first and then I deploy the code with the mappings. However, between these two steps I get errors like:
Property MyEntity::$newcolumn does not exist" at /app/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php line 80
Ideally Doctrine would ignore a new column before the mapping is deployed and just let there be a default value. Is there a way to do this? I'm aware of Doctrine migrations but it seems to be too much hassle to solve a simple issue like this.
My Doctrine version: 2.5

Related

Flask-Migrate `db upgrade` fails with "relation does not exist"

I'm working in a development environment on a flask-app with a Postgres 10 database that has ~80 tables. There are lots of relationships and ForeignKeyConstraints networking it all together.
It was working fine with Flask-Migrate. I'd bootstrapped and migrated up to this point with ~80 tables. But, I wanted to test out some new scripts to seed the database tables, and thought it would be quickest to just drop the database and bring it back up again using Flask-Migrate.
In this process, the migration folder was deleted, so I just started over fresh with a db init. Then ran db migrate. I manually fixed a few imports in the migrate script. Finally, I ran db upgrade.
However, now with all these 80 create_table commands in my migrate script, when I run db_upgrade, I receive an error:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "items" does not exist
I receive this error for every Child table that has a ForeignKeyConstraint if the Child table is not in an order which is below the Parent table in the migration file.
But, the autogenerated script from db migrate has the tables sorted alphabetically, ordered by table name.
Referring to documentation, I don't see this importance of sort order mentioned.
Bottom line is, it seems I'm either forced to write a script to sort all these tables in an order where the Parent table is above the Child table. Or else, just cut and paste like a jigsaw puzzle until all the tables are in the required order.
What am I missing? Is there an easier way to do this with Flask-Migrate or Alembic?
After researching this, it seems flask-migrate and/or Alembic does not have any built-in methods to resolve this sort order issue. I fixed it by cutting and pasting the tables in an order which ensured the Parent table was above the child tables in the migration file.
I've just encountered this myself, and could not find a better and/or official answer.
My approach was to separate the table creation from the creation of foreign key constraints:
Edit Alembic's auto-generated migration script: In each table create operation, remove all lines creating foreign key constraints
Run Alembic's upgrade command (tables are created, minus the FK constraints, of course)
Run Alembic's migrate command (additional migration script created, that adds all FK constraints)
Run Alembic's upgrade command (FK constraints added to tables)
I've faced some problems when using flask-sqlalchemy and flask-migrate, I solved it using python interactive shell.
>>> from yourapp import db, create_app
>>> db.create_all(app=create_app())
Check this link to get more information.
Happy coding...

Updating old changesets - Liquibase

We use liquibase for source controlling the database. Initially, we started with Postgres and created the changesets with datatype of columns which are specific to Postgres.
For example, we have a changeset which creates the table with fields of type 'JSON'. Now that, we wanted to move to other database. So, when we run the changeset against the other database, it fails to create the table. I tried adding 'failOnError=false'. But, the later changesets failed because the table doesnot exist.
Could you please suggest how to refactor the old changeset to make compatible with other database as well?
You can make your changesets database specific. You could try re-creating the changeset to work in the new DB, and adding the "dbms" attribute equal to the new DB to the new changeset. While adding the same attribute but with the old DB to the old changeset.

Generating MySql scheme from Doctrine (using ZF2)

Are there any third party tools that can generate a Mysql database from a doctrine scheme?
I am using ZF2 with Doctrine Orm 2 with Skipper to generate my entities. However when it comes to generating my MySql database I do this via Workbench. The problem is that this DB does not always follow the changes and updates I make to the Skipper files.
Now I know I can update via Doctrine's vendor folder using the tool provided, however I have never been able to get this to work due to other vendor modules crashing the environment while in console mode. ZfcRbac / Oauth2 and others that just done play nicely when in command line.
So my question is simply, is there an easy third party tool I can use to save time and frustration or at least some other technique I can use?
thanks!
The best way to synchronize your database is to use Doctrine Migrations. Every time when you update your entities, it does not matter how - manually or with Skypper, you should create a migration. My workflow is:
1) I modify some entity, let say adding a new property;
2) Then I use Doctrine orm tool to generate the entity accessors. /If you already have them, generated by Skypper, you can skip this step/
$php public/index.php orm:generate-entities --update-entities="true" --generate-methods="true" module/Application/src
3) Updated: Generate doctrine proxies
$php public/index.php orm:generate:proxies
4) The next step is to generate the migrations:
$php public/index.php migrations:diff
5) And the final step is to execute the migration and get your database synchronized with the new schema:
$php public/index.php migrations:migrate
That's all, now your database is synchronized. I am not sure if there is a third party library for this. If you have problems with the other modules, it is a good idea to check your configurations trying to fix them.

Syncing db with existing tables through django for an existing schema table and also updating few columns for the tables and the rest automatically

I am doing a poc in Django and i was trying to create the admin console module for inserting,updating and deleting records through django admin console through models and it was doing fine
I have 2 questions.
1.I need to have model objects for existing tables which needs to be present in a particular schema.say schema1.table1
Here as of now i was doing poc for public schema.
So can it be done in a fixed defined schema and if yes how.Any reference would be very helpful
2.Also i wanted to update few columns in the table through console and the rest of the columns will be done automatically like currentimestamp and created date etc.Is it possible through default django console and if yes kindly share any reference
Steps for 1
What i have done as of now is created a class in model.py with attributes as author,title,body,timeofpost
Then i used sqlmigrate after makemigrations app to create the table and after migrating have been using the admin console for django to insert and update the records for the table created.But this is for POC only.
Now i need to do the same but for existing tables with whom i can interact and insert or update record for those existing tables through admin console.
Also the tables are getting created in public schema by default.But i am using postgres and the existing tables are present in different schemas and i wanted to insert,update and delete for this existing tables.
I am stuck up here as i dont know how to configure model with existing database schema tables through which we can interact through django console and also for different schemas and not in public schema
Steps for 2:
Also i wanted the user to give input for few columns like suppose in this case time of creation is not required to be given as input by user .Rather it should be taken care when the database is updating or creating
Thanks
In order for Django to "interact" with an existing database you need to create a model for it which can be done automatically as shown here. This assumes that your "external" database isn't going to be changed often because you'll have to keep your models in sync which is tricky - there are other approaches if you need that.
As for working with multiple database schemas - is there a reason you can't put your POC table in the same database as the others? Django supports multiple databases, but it will be harder to setup. See here.
Finally, it sounds like you are interested in setting the Django default field attribute. For an example of current time see here.

Is it possible to include a database view in a JPA/EclipseLink CriteriaQuery?

I am working on a project that needs to be able to create dynamic queries into an H2 database. This also includes a full text search with built-in H2 logic, tables, and triggers.
I have been trying to figure out how to add that full-text search into my CriteriaQuery but keep running into the road block that the tables used aren't entities in my model. I could add them as entities, but I don't want them created automatically by EclipseLink when a new database file is created since there is a function in H2 that creates the tables and does other necessary housekeeping.
I had tried the path of creating a view to query the full text tables to give me the information I need in the format I need. But I still keep running into the same problem that that view is not an Entity.
Has anyone encountered this situation before and/or figured out a way around it?
Thanks!