Create stored procedure in seeding of ASP.NET Boilerplate - asp.net-boilerplate

How do I run a SQL command during seeding on ASP.NET Boilerplate?
I need to run a complicated query so I think it will be easier to use a stored procedure to offload the computing onto SQL Server.
I've successfully created a custom repository which injects ActiveTransactionProvider and uses ADO.NET from the DbContext to run the query.
Now I want to check if the stored procedure exists and if not create it in the seeding when the backend starts up.
I tried to use ADO.NET from the DbContext passed SeedHelper but I get an exception that I need a transaction even though the transaction scope has been set to suppressed in the helper. I don't see where I can inject the active transaction provider during seeding so I'm stuck.

You need to use code first migrations to alter database schema (including creating a SP). Just use add-migration command then execute SQL inside it (like Code First Migrations and Stored Procedures).
Seed is just for adding/updating existing entities. It just runs on application startups. EF Core does not have native seed mechanism yet (as I know).

Related

Domain classes and database are out of sync in Grails

I've this weird problem. I have a Grails app in which some database changelog files are missing. Therefore, the database has gone out of sync with the domain classes. I've done some changes in the domain classes. When I try to run the database migration plugin, it is creating a diff betweeb the current domain classes and the database and try to execute all the sql commands that has already been run which is causing error in executing the commands that I want to execute.
Is there is a solution for this problem?
If I understand your problem correctly, you can re-create all of the missing changelogs using dbm-generate-changelog. This will create changelogs based on the current data model. Then you can use dbm-changelog-sync to mark those changelogs as EXECUTED (which will populate the DATABASECHANGELOG table). Once the DATABASECHANGELOG table is in sync with the current data model, you can use dbm-gorm-diff to make sure you're not missing any other data model changes.
https://grails-plugins.github.io/grails-database-migration/1.4.0/ref/Maintenance%20Scripts/dbm-changelog-sync.html
NOTE: My answer assumes you're using Grails 2.x and Database Migration plugin 1.4.x, but I believe the process is similar in Grails 3.x with Database Migration Plugin 2.x or 3.x.

How to write golang integration test with MySQL

I want to write an integration test that uses MySQL to test my queries. How to do this in golang?
This contains few questions:
How to setup MySQL (in-memory?) server in golang test?
How to clean/recreate data model before/after each test so that they do not leave garbage behind?
How to tear down mysql after all the tests are done?
If you really want to have an embedded MySQL, you can use golangs C bindings to integrate with: https://dev.mysql.com/doc/refman/5.1/en/libmysqld.html. I haven't seen any project packing up the bindings for this in a nice Go package, that would be an interesting small project.
Otherwise you can use Docker to set up a throwaway MySQL server, this requires some setup/teardown steps before you run go test though. This is what we are doing where I work.
In both cases, you will need to write setup/teardown methods that creates and drops tables as needed for your tests. These are just normal SQL statements, DROP DATABASE, CREATE TABLE etc.
Testify https://github.com/stretchr/testify has tooling for setup/teardown, but just writing a helper function for this works just fine.

What's the appropriate way to test code that uses MySQL-specific queries internally

I am collecting data and store this data in a MySQL database using Java. Additionally, I use Maven for building the project, TestNG as a test framework, and Spring-Jdbc for accessing the database. I've implemented a DAO layer that encapsulates the access to the database. Besides adding data using the DAO classes I want to execute some queries which aggregate the data and store the results in some other tables (like materialized views).
Now, I would like to write some testcases which check whether the DAO classes are working as they should. Therefore, I thought of using an in-memory database which will be populated with some test data. Since I am also using MySQL-specific SQL queries for aggregating data, I went into some trouble:
Firstly, I've thought of simply using the embedded-database functionality provided by Spring-Jdbc to instantiate an embedded database. I've decided to use the H2 implementation. There I ran into trouble because of the aggregation queries, which are using MySQL-specific content (e.g. time-manipulation functions like DATE()). Another disadvantage of this approach is that I need to maintain two ddl files - the actual ddl file defining the tables in MySQL (here I define the encoding and add comments to tables and columns, both features are MySQL-specific); and the test ddl file that defines the same tables but without comments etc. since H2 does not support comments.
I've found a description for using MySQL as an embedded database which I can use within the test cases (http://literatitech.blogspot.de/2011/04/embedded-mysql-server-for-junit-testing.html). That sounded really promising to me. Unfortunately, it didn't worked: A MissingResourceExcpetion occurred "Resource '5-0-21/Linux-amd64/mysqld' not found". It seems that the driver is not able to find the database daemon on my local machine. But I don't know what I have to look for to find a solution for that issue.
Now, I am a little bit stuck and I am wondering if I should have created the architecture differently. Do someone has some tips how I should setup an appropriate system? I have two other options in mind:
Instead of using an embedded database, I'll go with a native MySQL instance and setup a database that is only used for the testcases. This options sounds slow. Actually, I might want to setup a CI server later on and I thought that using an embedded database would be more appropriate since the test run faster.
I erase all the MySQL-specific stuff out of the SQL queries and use H2 as an embedded database for testing. If this option is the right choice, I would need to find another way to test the SQL queries that aggregates the data into materialized views.
Or is there a 3rd option which I don't have in mind?
I would appreciate any hints.
Thanks,
XComp
I've created Maven plugin exactly for this purpose: jcabi-mysql-maven-plugin. It starts a local MySQL server on pre-integration-test phase and shuts it down on post-integration-test.
If it is not possible to get the in-memory MySQL database to work I suggest using the H2 database for the "simple" tests and a dedicated MySQL instance to test MySQL-specific queries.
Additionally, the tests for the real MySQL database can be configured as integration tests in a separate maven profile so that they are not part of the regular maven build. On the CI server you can create an additional job that runs the MySQL tests periodically, e.g. daily or every few hours. With such a setup you can keep and test your product-specific queries while your regular build will not slow down. You can also run a normal build even if the test database is not available.
There is a nice maven plugin for integration tests called maven-failsafe-plugin. It provides pre- and post- integration test steps that can be used to setup the test data before the tests and to cleanup the database after the tests.

LINQtoSQL Not saving to database,but changes show in app

Hey all, I have a linq app using C# express2008 and sqlserver express 2005 (mdf file connection)
I followed the regular dml generation and vanilla datacontext. However i created a repository class to manage the Linq stuff.
In using the functions, selecting data works fine, updating data works in the app.But when i check the data in the tables, nothing has changed.Needless to say, when i close the app, also no change.
I used SQL profiler to see what was being sent to sqlserver express, nothing showed up.
What could be my issues?
Are you calling SubmitChanges() on the DataContext?
No primary key, or no column(s) in the L2S model marked as primary key member(s)..?
Ok... found 'a' soln.
1. changed (forced) default connection string from looking at a file to an instance of sql serverĀ (using database instead of file)
2. Attached file to sql server express..and renamed the db.
Ran app again and everything works. Only thing is..the dml still uses the old connection string..so any mods to tables have to be done in sqlserver.
This is just my quicky patch, anyone care to provide a more elaborate view?

Optimizing Stored Procedures so they will be processed properly by Linq2SQL

Where I work it is a requirement for us to go through stored procedures as a mechanism to access our data through code. I am using LINQ2SQL to minimize this pain so that I can work with objects instead of ADO.NET directly. I have a situation Linq2SQL is consuming one of my stored procedures an generating code where the return type from the stored proc call is an int. The stored procedure actually returns a dataset. After doing a little research I have found that this is because the SQLClient library can not properly parse the sproc to generate the expected metadata that Linq2SQL uses to create the object graph. My question is how can sprocs (even complex ones) be structured so that you get an object graph out of linq2sql, or in other words what should you avoid having in your stored procedure that will create confusion for the SQLClient library to not understand how to generate the metadata that linq2sql consumes in order to create an object graph?
This is not actually a limitation of LINQ to SQL but rather of SQL Server which can not always tell a client what the return type would be without actually running it when it contains temporary tables, cursors or dynamic SQL.
As running it with invalid parameters could be potentially catastrophic it doesn't try.
You can either set it by hand using the designer or if it is absolutely okay to run the stored procedure with invalid data (i.e. it is purely passive) then you can add SET FMTOPT OFF to the start of the stored procedure.
DamienG works on the LinqToSql team at Microsoft and I have upvoted his answer as correct.
That said, he likely won't advise you away from LinqToSql and I think it's very important to consider that option.
Trying to guess the return type of a stored procedure is very difficult and LinqToSql does it as well as anyone (for SQL Server). That said, there are very compelling reasons not to use stored procedures:
Stored procedures are bad, m'kay?
If you must protect your tables from developers for "security reasons" (which I'm guessing is the situation you are in), it's best to do that with views instead of stored procedures.
If you are using views, you then have a lot better options in the ORM department than LinqToSql.
The main problem you are going to run into with LinqToSql in this regard is that what works fine for 5 stored procedures in a tiny database doesn't work fine for 50 or 500 stored procedures. You can use the O/R Designer to "override" the return type of a stored procedure, but you will have significant syncing issues when stored procedures or the tables, etc. they operate on change. Changes to stored procedures will not get reflected in the O/R Designer unless you remove the stored procedure from the O/R Designer, re-add it, and then reapply your custom override. If your project is like any normal project, the tables and stored procedures change often and this sync issue soon becomes a nightmare because it's completely manual and if you fail to do it correctly you will get very strange errors at runtime.
I would strongly advise against continuing down the path you are on.